blob: 390017647177b95f4a5de924052ee5c18393ff3a [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendling6de55a02009-12-21 19:34:59 +000013
Chris Lattner600d3082003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng00fd0b62010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-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 Carruthed0881b2012-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 Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-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 Wendlingbd092622008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-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"
Eric Christopherd9134482014-08-04 21:25:23 +000049#include "llvm/Target/TargetSubtargetInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000050#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000051#include <cmath>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000052
Chris Lattner560b5e42004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000054
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000065
Jim Laskeyd66e6162005-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 Johannesenb6d2bec2007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000076}
77
Owen Anderson53aa7a92009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Dale Johannesend246b2c2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000089}
90
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000094
Evan Chengc70e33c2006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000098 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +000099 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Evan Chengc70e33c2006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000105
Chris Lattnerc2d28112006-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 Michelcf0da6c2009-02-17 22:15:04 +0000109
Chris Lattnerc2d28112006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000112
Chris Lattnerc2d28112006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-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 Gohman2ce6f2a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000125 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000128 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000129 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Chris Lattnerc2d28112006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-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 Lattnerc2d28112006-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 Chenga6789912006-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 Lattner7e7ad592006-04-15 23:38:00 +0000146 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +0000147 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chenga6789912006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Kevin Qin93d45ec2014-06-24 05:37:27 +0000152 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 Michelcf0da6c2009-02-17 22:15:04 +0000176
Evan Chengc70e33c2006-03-27 06:58:47 +0000177 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000178 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000179 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000180 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000181}
182
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000183/// \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
Simon Pilgrim09f3ff92015-03-25 22:30:31 +0000199/// \brief Return true if the specified node is a BUILD_VECTOR node of
200/// all ConstantFPSDNode or undef.
201bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
202 if (N->getOpcode() != ISD::BUILD_VECTOR)
203 return false;
204
205 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
206 SDValue Op = N->getOperand(i);
207 if (Op.getOpcode() == ISD::UNDEF)
208 continue;
209 if (!isa<ConstantFPSDNode>(Op))
210 return false;
211 }
212 return true;
213}
214
Evan Cheng6200c222008-02-18 23:04:32 +0000215/// isScalarToVector - Return true if the specified node is a
216/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
217/// element is not an undef.
218bool ISD::isScalarToVector(const SDNode *N) {
219 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
220 return true;
221
222 if (N->getOpcode() != ISD::BUILD_VECTOR)
223 return false;
224 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
225 return false;
226 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000227 if (NumElems == 1)
228 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000229 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000230 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000231 if (V.getOpcode() != ISD::UNDEF)
232 return false;
233 }
234 return true;
235}
236
Nadav Rotema62368c2012-07-15 08:38:23 +0000237/// allOperandsUndef - Return true if the node has at least one operand
238/// and all operands of the specified node are ISD::UNDEF.
239bool ISD::allOperandsUndef(const SDNode *N) {
240 // Return false if the node has no operands.
241 // This is "logically inconsistent" with the definition of "all" but
242 // is probably the desired behavior.
243 if (N->getNumOperands() == 0)
244 return false;
245
246 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
247 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
248 return false;
249
250 return true;
251}
252
Matt Arsenaultbd223422015-01-14 01:35:17 +0000253ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000254 switch (ExtType) {
255 case ISD::EXTLOAD:
Matt Arsenaultbd223422015-01-14 01:35:17 +0000256 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000257 case ISD::SEXTLOAD:
258 return ISD::SIGN_EXTEND;
259 case ISD::ZEXTLOAD:
260 return ISD::ZERO_EXTEND;
261 default:
262 break;
263 }
264
265 llvm_unreachable("Invalid LoadExtType");
266}
267
Chris Lattner061a1ea2005-01-07 07:46:32 +0000268/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
269/// when given the operation for (X op Y).
270ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
271 // To perform this operation, we just need to swap the L and G bits of the
272 // operation.
273 unsigned OldL = (Operation >> 2) & 1;
274 unsigned OldG = (Operation >> 1) & 1;
275 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
276 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000277 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000278}
279
280/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
281/// 'op' is a valid SetCC operation.
282ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
283 unsigned Operation = Op;
284 if (isInteger)
285 Operation ^= 7; // Flip L, G, E bits, but not U.
286 else
287 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000288
Chris Lattner061a1ea2005-01-07 07:46:32 +0000289 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000290 Operation &= ~8; // Don't let N and U bits get set.
291
Chris Lattner061a1ea2005-01-07 07:46:32 +0000292 return ISD::CondCode(Operation);
293}
294
295
296/// isSignedOp - For an integer comparison, return 1 if the comparison is a
297/// signed operation and 2 if the result is an unsigned comparison. Return zero
298/// if the operation does not depend on the sign of the input (setne and seteq).
299static int isSignedOp(ISD::CondCode Opcode) {
300 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000301 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000302 case ISD::SETEQ:
303 case ISD::SETNE: return 0;
304 case ISD::SETLT:
305 case ISD::SETLE:
306 case ISD::SETGT:
307 case ISD::SETGE: return 1;
308 case ISD::SETULT:
309 case ISD::SETULE:
310 case ISD::SETUGT:
311 case ISD::SETUGE: return 2;
312 }
313}
314
315/// getSetCCOrOperation - Return the result of a logical OR between different
316/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
317/// returns SETCC_INVALID if it is not possible to represent the resultant
318/// comparison.
319ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
320 bool isInteger) {
321 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
322 // Cannot fold a signed integer setcc with an unsigned integer setcc.
323 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000324
Chris Lattner061a1ea2005-01-07 07:46:32 +0000325 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000326
Chris Lattner061a1ea2005-01-07 07:46:32 +0000327 // If the N and U bits get set then the resultant comparison DOES suddenly
328 // care about orderedness, and is true when ordered.
329 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000330 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000331
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000332 // Canonicalize illegal integer setcc's.
333 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
334 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Chris Lattner061a1ea2005-01-07 07:46:32 +0000336 return ISD::CondCode(Op);
337}
338
339/// getSetCCAndOperation - Return the result of a logical AND between different
340/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
341/// function returns zero if it is not possible to represent the resultant
342/// comparison.
343ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
344 bool isInteger) {
345 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
346 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000347 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000348
349 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000350 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000351
Chris Lattner393d96a2006-04-27 05:01:07 +0000352 // Canonicalize illegal integer setcc's.
353 if (isInteger) {
354 switch (Result) {
355 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000356 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000357 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000358 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
359 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
360 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000361 }
362 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000363
Chris Lattner393d96a2006-04-27 05:01:07 +0000364 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000365}
366
Jim Laskeyd66e6162005-08-17 20:08:02 +0000367//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000368// SDNode Profile Support
369//===----------------------------------------------------------------------===//
370
Jim Laskeybd0f0882006-10-27 23:52:51 +0000371/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
372///
Jim Laskeyf576b422006-10-27 23:46:08 +0000373static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
374 ID.AddInteger(OpC);
375}
376
377/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
378/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000379static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000380 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000381}
382
Jim Laskeybd0f0882006-10-27 23:52:51 +0000383/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
384///
Jim Laskeyf576b422006-10-27 23:46:08 +0000385static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000386 ArrayRef<SDValue> Ops) {
387 for (auto& Op : Ops) {
388 ID.AddPointer(Op.getNode());
389 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000390 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000391}
392
Dan Gohman768f2c92008-07-07 18:26:29 +0000393/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
394///
395static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000396 ArrayRef<SDUse> Ops) {
397 for (auto& Op : Ops) {
398 ID.AddPointer(Op.getNode());
399 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000400 }
401}
402
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000403static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
404 bool exact) {
405 ID.AddBoolean(nuw);
406 ID.AddBoolean(nsw);
407 ID.AddBoolean(exact);
408}
409
410/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
411static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
412 bool nuw, bool nsw, bool exact) {
413 if (isBinOpWithFlags(Opcode))
414 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
415}
416
Craig Topper633d99b2014-04-27 23:22:43 +0000417static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
418 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000419 AddNodeIDOpcode(ID, OpC);
420 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000421 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000422}
423
Duncan Sands835bdca2008-10-27 15:30:53 +0000424/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
425/// the NodeID data.
426static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000427 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000428 case ISD::TargetExternalSymbol:
429 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000430 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000431 default: break; // Normal nodes don't need extra info.
432 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000433 case ISD::Constant: {
434 const ConstantSDNode *C = cast<ConstantSDNode>(N);
435 ID.AddPointer(C->getConstantIntValue());
436 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000438 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000439 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000440 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000441 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000442 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000443 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000444 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000445 case ISD::GlobalAddress:
446 case ISD::TargetGlobalTLSAddress:
447 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000448 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000449 ID.AddPointer(GA->getGlobal());
450 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000451 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000452 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000453 break;
454 }
455 case ISD::BasicBlock:
456 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
457 break;
458 case ISD::Register:
459 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
460 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000461 case ISD::RegisterMask:
462 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
463 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000464 case ISD::SRCVALUE:
465 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
466 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000467 case ISD::FrameIndex:
468 case ISD::TargetFrameIndex:
469 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
470 break;
471 case ISD::JumpTable:
472 case ISD::TargetJumpTable:
473 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000474 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000475 break;
476 case ISD::ConstantPool:
477 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000478 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000479 ID.AddInteger(CP->getAlignment());
480 ID.AddInteger(CP->getOffset());
481 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000482 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000483 else
484 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000485 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000486 break;
487 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000488 case ISD::TargetIndex: {
489 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
490 ID.AddInteger(TI->getIndex());
491 ID.AddInteger(TI->getOffset());
492 ID.AddInteger(TI->getTargetFlags());
493 break;
494 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000495 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000496 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000497 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000498 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000499 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000500 break;
501 }
502 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000503 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000504 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000505 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000506 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000507 break;
508 }
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000509 case ISD::SDIV:
510 case ISD::UDIV:
511 case ISD::SRA:
512 case ISD::SRL:
513 case ISD::MUL:
514 case ISD::ADD:
515 case ISD::SUB:
516 case ISD::SHL: {
517 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
518 AddBinaryNodeIDCustom(ID, N->getOpcode(), BinNode->hasNoUnsignedWrap(),
519 BinNode->hasNoSignedWrap(), BinNode->isExact());
520 break;
521 }
Dan Gohman12f24902008-12-23 21:37:04 +0000522 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000523 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000524 case ISD::ATOMIC_SWAP:
525 case ISD::ATOMIC_LOAD_ADD:
526 case ISD::ATOMIC_LOAD_SUB:
527 case ISD::ATOMIC_LOAD_AND:
528 case ISD::ATOMIC_LOAD_OR:
529 case ISD::ATOMIC_LOAD_XOR:
530 case ISD::ATOMIC_LOAD_NAND:
531 case ISD::ATOMIC_LOAD_MIN:
532 case ISD::ATOMIC_LOAD_MAX:
533 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000534 case ISD::ATOMIC_LOAD_UMAX:
535 case ISD::ATOMIC_LOAD:
536 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000537 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000538 ID.AddInteger(AT->getMemoryVT().getRawBits());
539 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000540 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
541 break;
542 }
543 case ISD::PREFETCH: {
544 const MemSDNode *PF = cast<MemSDNode>(N);
545 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000546 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000547 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000548 case ISD::VECTOR_SHUFFLE: {
549 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000550 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000551 i != e; ++i)
552 ID.AddInteger(SVN->getMaskElt(i));
553 break;
554 }
Dan Gohman6c938802009-10-30 01:27:03 +0000555 case ISD::TargetBlockAddress:
556 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000557 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
558 ID.AddPointer(BA->getBlockAddress());
559 ID.AddInteger(BA->getOffset());
560 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000561 break;
562 }
Mon P Wang6a490372008-06-25 08:15:39 +0000563 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000564
565 // Target specific memory nodes could also have address spaces to check.
566 if (N->isTargetMemoryOpcode())
567 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000568}
569
Duncan Sands835bdca2008-10-27 15:30:53 +0000570/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
571/// data.
572static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
573 AddNodeIDOpcode(ID, N->getOpcode());
574 // Add the return value info.
575 AddNodeIDValueTypes(ID, N->getVTList());
576 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000577 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000578
579 // Handle SDNode leafs with special info.
580 AddNodeIDCustom(ID, N);
581}
582
Dan Gohman2da2bed2008-08-20 15:58:01 +0000583/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000584/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000585/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000586///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000587static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000588encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000589 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000590 assert((ConvType & 3) == ConvType &&
591 "ConvType may not require more than 2 bits!");
592 assert((AM & 7) == AM &&
593 "AM may not require more than 3 bits!");
594 return ConvType |
595 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000596 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000597 (isNonTemporal << 6) |
598 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000599}
600
Jim Laskeyf576b422006-10-27 23:46:08 +0000601//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000602// SelectionDAG Class
603//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000604
Duncan Sands835bdca2008-10-27 15:30:53 +0000605/// doNotCSE - Return true if CSE should not be performed for this node.
606static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000607 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000608 return true; // Never CSE anything that produces a flag.
609
610 switch (N->getOpcode()) {
611 default: break;
612 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000613 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000614 return true; // Never CSE these nodes.
615 }
616
617 // Check that remaining values produced are not flags.
618 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000619 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000620 return true; // Never CSE anything that produces a flag.
621
622 return false;
623}
624
Chris Lattner9c667932005-01-07 21:09:16 +0000625/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000626/// SelectionDAG.
627void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000628 // Create a dummy node (which is not added to allnodes), that adds a reference
629 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000630 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000631
Chris Lattner8927c872006-08-04 17:45:20 +0000632 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000633
Chris Lattner8927c872006-08-04 17:45:20 +0000634 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000635 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000636 if (I->use_empty())
637 DeadNodes.push_back(I);
638
Dan Gohman91697632008-07-07 20:57:48 +0000639 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000640
Dan Gohman91697632008-07-07 20:57:48 +0000641 // If the root changed (e.g. it was a dead load, update the root).
642 setRoot(Dummy.getValue());
643}
644
645/// RemoveDeadNodes - This method deletes the unreachable nodes in the
646/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000647void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000648
Chris Lattner8927c872006-08-04 17:45:20 +0000649 // Process the worklist, deleting the nodes and adding their uses to the
650 // worklist.
651 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000652 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000653
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000654 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000655 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000656
Chris Lattner8927c872006-08-04 17:45:20 +0000657 // Take the node out of the appropriate CSE map.
658 RemoveNodeFromCSEMaps(N);
659
660 // Next, brutally remove the operand list. This is safe to do, as there are
661 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000662 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
663 SDUse &Use = *I++;
664 SDNode *Operand = Use.getNode();
665 Use.set(SDValue());
666
Chris Lattner8927c872006-08-04 17:45:20 +0000667 // Now that we removed this operand, see if there are no uses of it left.
668 if (Operand->use_empty())
669 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000670 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000671
Dan Gohman534c8a22009-01-19 22:39:36 +0000672 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000673 }
Chris Lattner9c667932005-01-07 21:09:16 +0000674}
675
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000676void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000677 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000678
679 // Create a dummy node that adds a reference to the root node, preventing
680 // it from being deleted. (This matters if the root is an operand of the
681 // dead node.)
682 HandleSDNode Dummy(getRoot());
683
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000684 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000685}
686
Chris Lattnerc738d002005-08-29 21:59:31 +0000687void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000688 // First take this out of the appropriate CSE map.
689 RemoveNodeFromCSEMaps(N);
690
Scott Michelcf0da6c2009-02-17 22:15:04 +0000691 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000692 // AllNodes list, and delete the node.
693 DeleteNodeNotInCSEMaps(N);
694}
695
696void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000697 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
698 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000699
Dan Gohman86aa16a2008-09-30 18:30:35 +0000700 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000701 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000702
Dan Gohman534c8a22009-01-19 22:39:36 +0000703 DeallocateNode(N);
704}
705
Frederic Riss8ad4f492014-11-11 21:21:08 +0000706void SDDbgInfo::erase(const SDNode *Node) {
707 DbgValMapType::iterator I = DbgValMap.find(Node);
708 if (I == DbgValMap.end())
709 return;
710 for (auto &Val: I->second)
711 Val->setIsInvalidated();
712 DbgValMap.erase(I);
713}
714
Dan Gohman534c8a22009-01-19 22:39:36 +0000715void SelectionDAG::DeallocateNode(SDNode *N) {
716 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000717 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000718
Dan Gohman534c8a22009-01-19 22:39:36 +0000719 // Set the opcode to DELETED_NODE to help catch bugs when node
720 // memory is reallocated.
721 N->NodeType = ISD::DELETED_NODE;
722
Dan Gohman2e834902008-08-26 01:44:34 +0000723 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000724
Frederic Riss8ad4f492014-11-11 21:21:08 +0000725 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
726 // them and forget about that node.
727 DbgInfo->erase(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000728}
729
Chandler Carruth41b20e72014-07-22 04:07:55 +0000730#ifndef NDEBUG
731/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
732static void VerifySDNode(SDNode *N) {
733 switch (N->getOpcode()) {
734 default:
735 break;
736 case ISD::BUILD_PAIR: {
737 EVT VT = N->getValueType(0);
738 assert(N->getNumValues() == 1 && "Too many results!");
739 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
740 "Wrong return type!");
741 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
742 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
743 "Mismatched operand types!");
744 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
745 "Wrong operand type!");
746 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
747 "Wrong return type size");
748 break;
749 }
750 case ISD::BUILD_VECTOR: {
751 assert(N->getNumValues() == 1 && "Too many results!");
752 assert(N->getValueType(0).isVector() && "Wrong return type!");
753 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
754 "Wrong number of operands!");
755 EVT EltVT = N->getValueType(0).getVectorElementType();
756 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
757 assert((I->getValueType() == EltVT ||
758 (EltVT.isInteger() && I->getValueType().isInteger() &&
759 EltVT.bitsLE(I->getValueType()))) &&
760 "Wrong operand type!");
761 assert(I->getValueType() == N->getOperand(0).getValueType() &&
762 "Operands must all have the same type");
763 }
764 break;
765 }
766 }
767}
768#endif // NDEBUG
769
770/// \brief Insert a newly allocated node into the DAG.
771///
772/// Handles insertion into the all nodes list and CSE map, as well as
773/// verification and other common operations when a new node is allocated.
774void SelectionDAG::InsertNode(SDNode *N) {
775 AllNodes.push_back(N);
776#ifndef NDEBUG
777 VerifySDNode(N);
778#endif
779}
780
Chris Lattner19732782005-08-16 18:17:10 +0000781/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
782/// correspond to it. This is useful when we're about to delete or repurpose
783/// the node. We don't want future request for structurally identical nodes
784/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000785bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000786 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000787 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000788 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000789 case ISD::CONDCODE:
790 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
791 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000792 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
793 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000794 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000795 case ISD::ExternalSymbol:
796 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000797 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000798 case ISD::TargetExternalSymbol: {
799 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
800 Erased = TargetExternalSymbols.erase(
801 std::pair<std::string,unsigned char>(ESN->getSymbol(),
802 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000803 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000804 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000805 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000806 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000807 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000808 Erased = ExtendedValueTypeNodes.erase(VT);
809 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000810 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
811 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000812 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000813 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000814 }
Chris Lattner9c667932005-01-07 21:09:16 +0000815 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000816 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000817 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
818 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000819 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000820 break;
821 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000822#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000823 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000824 // flag result (which cannot be CSE'd) or is one of the special cases that are
825 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000826 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000827 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000828 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000829 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000830 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000831 }
832#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000833 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000834}
835
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000836/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
837/// maps and modified in place. Add it back to the CSE maps, unless an identical
838/// node already exists, in which case transfer all its users to the existing
839/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000840///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000841void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000842SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000843 // For node types that aren't CSE'd, just act as if no identical node
844 // already exists.
845 if (!doNotCSE(N)) {
846 SDNode *Existing = CSEMap.GetOrInsertNode(N);
847 if (Existing != N) {
848 // If there was already an existing matching node, use ReplaceAllUsesWith
849 // to replace the dead one with the existing one. This can cause
850 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000851 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000852
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000853 // N is now dead. Inform the listeners and delete it.
854 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
855 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000856 DeleteNodeNotInCSEMaps(N);
857 return;
858 }
859 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000860
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000861 // If the node doesn't already exist, we updated it. Inform listeners.
862 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
863 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000864}
865
Chris Lattnerf34156e2006-01-28 09:32:45 +0000866/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000867/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000868/// return null, otherwise return a pointer to the slot it would take. If a
869/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000870SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000871 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000872 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000873 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000874
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000875 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000876 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000877 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000878 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000879 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000880 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000881}
882
883/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000884/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000885/// return null, otherwise return a pointer to the slot it would take. If a
886/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000887SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000888 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000889 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000890 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000891 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000892
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000893 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000894 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000895 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000896 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000897 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000898 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000899}
900
901
902/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000903/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000904/// return null, otherwise return a pointer to the slot it would take. If a
905/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000906SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000907 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000908 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000909 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000910
Jim Laskeyf576b422006-10-27 23:46:08 +0000911 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000912 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000913 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000914 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000915 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000916}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000917
Owen Anderson53aa7a92009-08-10 22:56:29 +0000918/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000919/// given type.
920///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000921unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000922 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000923 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000924 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000925
Eric Christopher1e845f22014-10-08 21:08:32 +0000926 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000927}
Chris Lattner9c667932005-01-07 21:09:16 +0000928
Dale Johannesen8ba713212009-02-07 02:15:05 +0000929// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000930SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000931 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000932 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
933 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
934 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000935 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000936 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000937}
938
Eric Christopher8d07f442014-10-08 01:57:58 +0000939void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000940 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000941 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000942 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000943 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000944}
945
Chris Lattner600d3082003-08-11 14:57:33 +0000946SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000947 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000948 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000949 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000950}
951
952void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000953 assert(&*AllNodes.begin() == &EntryNode);
954 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000955 while (!AllNodes.empty())
956 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000957}
958
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000959BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
960 SDVTList VTs, SDValue N1,
961 SDValue N2, bool nuw, bool nsw,
962 bool exact) {
963 if (isBinOpWithFlags(Opcode)) {
964 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
965 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
966 FN->setHasNoUnsignedWrap(nuw);
967 FN->setHasNoSignedWrap(nsw);
968 FN->setIsExact(exact);
969
970 return FN;
971 }
972
973 BinarySDNode *N = new (NodeAllocator)
974 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
975 return N;
976}
977
Dan Gohmane1a9a782008-08-27 23:52:12 +0000978void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000979 allnodes_clear();
980 OperandAllocator.Reset();
981 CSEMap.clear();
982
983 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000984 ExternalSymbols.clear();
985 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000986 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000987 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000988 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000989 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000990
Craig Topperc0196b12014-04-14 00:51:57 +0000991 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000992 AllNodes.push_back(&EntryNode);
993 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000994 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000995}
996
Andrew Trickef9de2a2013-05-25 02:42:55 +0000997SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000998 return VT.bitsGT(Op.getValueType()) ?
999 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1000 getNode(ISD::TRUNCATE, DL, VT, Op);
1001}
1002
Andrew Trickef9de2a2013-05-25 02:42:55 +00001003SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001004 return VT.bitsGT(Op.getValueType()) ?
1005 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1006 getNode(ISD::TRUNCATE, DL, VT, Op);
1007}
1008
Andrew Trickef9de2a2013-05-25 02:42:55 +00001009SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001010 return VT.bitsGT(Op.getValueType()) ?
1011 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1012 getNode(ISD::TRUNCATE, DL, VT, Op);
1013}
1014
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001015SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1016 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001017 if (VT.bitsLE(Op.getValueType()))
1018 return getNode(ISD::TRUNCATE, SL, VT, Op);
1019
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001020 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001021 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1022}
1023
Andrew Trickef9de2a2013-05-25 02:42:55 +00001024SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001025 assert(!VT.isVector() &&
1026 "getZeroExtendInReg should use the vector element type instead of "
1027 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001028 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001029 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1030 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001031 VT.getSizeInBits());
1032 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1033 getConstant(Imm, Op.getValueType()));
1034}
1035
Chandler Carruth0b666e02014-07-10 12:32:32 +00001036SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1037 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1038 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1039 "The sizes of the input and result must match in order to perform the "
1040 "extend in-register.");
1041 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1042 "The destination vector type must have fewer lanes than the input.");
1043 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1044}
1045
1046SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1047 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1048 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1049 "The sizes of the input and result must match in order to perform the "
1050 "extend in-register.");
1051 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1052 "The destination vector type must have fewer lanes than the input.");
1053 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1054}
1055
Chandler Carruthafe4b252014-07-09 10:58:18 +00001056SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1057 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001058 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1059 "The sizes of the input and result must match in order to perform the "
1060 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001061 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1062 "The destination vector type must have fewer lanes than the input.");
1063 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1064}
1065
Bob Wilsonc5890052009-01-22 17:39:32 +00001066/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1067///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001068SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001069 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001070 SDValue NegOne =
1071 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001072 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1073}
1074
Pete Cooper7fd1d722014-05-12 23:26:58 +00001075SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1076 EVT EltVT = VT.getScalarType();
1077 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001078 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001079 case TargetLowering::ZeroOrOneBooleanContent:
1080 case TargetLowering::UndefinedBooleanContent:
1081 TrueValue = getConstant(1, VT);
1082 break;
1083 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1084 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1085 VT);
1086 break;
1087 }
1088 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1089}
1090
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001091SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001092 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001093 assert((EltVT.getSizeInBits() >= 64 ||
1094 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1095 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001096 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001097}
1098
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001099SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1100{
1101 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001102}
1103
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001104SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1105 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001106 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001107
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001108 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001109 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001110
Duncan Sandsf2641e12011-09-06 19:07:46 +00001111 // In some cases the vector type is legal but the element type is illegal and
1112 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1113 // inserted value (the type does not need to match the vector element type).
1114 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001115 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001116 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001117 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001118 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1119 Elt = ConstantInt::get(*getContext(), NewVal);
1120 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001121 // In other cases the element type is illegal and needs to be expanded, for
1122 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1123 // the value into n parts and use a vector type with n-times the elements.
1124 // Then bitcast to the type requested.
1125 // Legalizing constants too early makes the DAGCombiner's job harder so we
1126 // only legalize if the DAG tells us we must produce legal types.
1127 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1128 TLI->getTypeAction(*getContext(), EltVT) ==
1129 TargetLowering::TypeExpandInteger) {
1130 APInt NewVal = Elt->getValue();
1131 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1132 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1133 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1134 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1135
1136 // Check the temporary vector is the correct size. If this fails then
1137 // getTypeToTransformTo() probably returned a type whose size (in bits)
1138 // isn't a power-of-2 factor of the requested type size.
1139 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1140
1141 SmallVector<SDValue, 2> EltParts;
1142 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1143 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1144 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001145 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001146 }
1147
1148 // EltParts is currently in little endian order. If we actually want
1149 // big-endian order then reverse it now.
1150 if (TLI->isBigEndian())
1151 std::reverse(EltParts.begin(), EltParts.end());
1152
1153 // The elements must be reversed when the element order is different
1154 // to the endianness of the elements (because the BITCAST is itself a
1155 // vector shuffle in this situation). However, we do not need any code to
1156 // perform this reversal because getConstant() is producing a vector
1157 // splat.
1158 // This situation occurs in MIPS MSA.
1159
1160 SmallVector<SDValue, 8> Ops;
1161 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1162 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1163
1164 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1165 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001166 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001167 return Result;
1168 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001169
1170 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1171 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001172 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001173 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001174 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001175 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001176 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001177 void *IP = nullptr;
1178 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001179 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001180 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001181 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001182
Dan Gohman7a7742c2007-12-12 22:21:26 +00001183 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001184 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001185 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001186 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001187 }
1188
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001189 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001190 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001191 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001192 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001193 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001194 }
1195 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001196}
1197
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001198SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Eric Christopher1e845f22014-10-08 21:08:32 +00001199 return getConstant(Val, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001200}
1201
1202
Owen Anderson53aa7a92009-08-10 22:56:29 +00001203SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001204 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001205}
1206
Owen Anderson53aa7a92009-08-10 22:56:29 +00001207SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001208 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001209
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001210 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001211
Chris Lattner381dddc2005-02-17 20:17:32 +00001212 // Do the map lookup using the actual bit pattern for the floating point
1213 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1214 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001215 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001216 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001217 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001218 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001219 void *IP = nullptr;
1220 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001221 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001222 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001223 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001224
Evan Cheng9458e6a2007-06-29 21:36:04 +00001225 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001226 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001227 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001228 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001229 }
1230
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001231 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001232 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001233 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001234 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001235 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001236 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001237 }
1238 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001239}
1240
Owen Anderson53aa7a92009-08-10 22:56:29 +00001241SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001242 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001243 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001244 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001245 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001246 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001247 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1248 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001249 bool ignored;
1250 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001251 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001252 &ignored);
1253 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001254 } else
1255 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001256}
1257
Andrew Trickef9de2a2013-05-25 02:42:55 +00001258SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001259 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001260 bool isTargetGA,
1261 unsigned char TargetFlags) {
1262 assert((TargetFlags == 0 || isTargetGA) &&
1263 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001264
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001265 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001266 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001267 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001268 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001269
Chris Lattner8e34f982009-06-25 21:21:14 +00001270 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001271 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001272 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1273 else
1274 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001275
Jim Laskeyf576b422006-10-27 23:46:08 +00001276 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001277 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001278 ID.AddPointer(GV);
1279 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001280 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001281 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001282 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001283 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001284 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001285
Andrew Trickef9de2a2013-05-25 02:42:55 +00001286 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1287 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001288 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001289 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001290 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001291 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001292}
1293
Owen Anderson53aa7a92009-08-10 22:56:29 +00001294SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001295 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001296 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001297 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001298 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001299 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001300 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001301 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001302
Dan Gohman01c65a22010-03-18 18:49:47 +00001303 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001304 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001305 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001306 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001307}
1308
Owen Anderson53aa7a92009-08-10 22:56:29 +00001309SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001310 unsigned char TargetFlags) {
1311 assert((TargetFlags == 0 || isTarget) &&
1312 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001313 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001314 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001315 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001316 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001317 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001318 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001319 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001320 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001321
Dan Gohman01c65a22010-03-18 18:49:47 +00001322 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1323 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001324 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001325 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001326 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001327}
1328
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001329SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001330 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001331 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001332 unsigned char TargetFlags) {
1333 assert((TargetFlags == 0 || isTarget) &&
1334 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001335 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001336 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001337 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001338 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001339 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001340 ID.AddInteger(Alignment);
1341 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001342 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001343 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001344 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001345 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001346 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001347
Dan Gohman01c65a22010-03-18 18:49:47 +00001348 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1349 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001350 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001351 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001352 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001353}
1354
Chris Lattner061a1ea2005-01-07 07:46:32 +00001355
Owen Anderson53aa7a92009-08-10 22:56:29 +00001356SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001358 bool isTarget,
1359 unsigned char TargetFlags) {
1360 assert((TargetFlags == 0 || isTarget) &&
1361 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001362 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001363 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001364 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001365 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001366 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001367 ID.AddInteger(Alignment);
1368 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001369 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001370 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001371 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001372 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001373 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001374
Dan Gohman01c65a22010-03-18 18:49:47 +00001375 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1376 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001377 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001378 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001379 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001380}
1381
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001382SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1383 unsigned char TargetFlags) {
1384 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001385 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001386 ID.AddInteger(Index);
1387 ID.AddInteger(Offset);
1388 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001389 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001390 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1391 return SDValue(E, 0);
1392
1393 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1394 TargetFlags);
1395 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001396 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001397 return SDValue(N, 0);
1398}
1399
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001400SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001401 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001402 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001403 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001404 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001405 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001406 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001407
Dan Gohman01c65a22010-03-18 18:49:47 +00001408 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001409 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001410 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001411 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001412}
1413
Owen Anderson53aa7a92009-08-10 22:56:29 +00001414SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001415 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1416 ValueTypeNodes.size())
1417 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001418
Duncan Sands13237ac2008-06-06 12:08:01 +00001419 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001420 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001421
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001422 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001423 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001424 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001425 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001426}
1427
Owen Anderson53aa7a92009-08-10 22:56:29 +00001428SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001429 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001430 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001431 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001432 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001433 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001434}
1435
Owen Anderson53aa7a92009-08-10 22:56:29 +00001436SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001437 unsigned char TargetFlags) {
1438 SDNode *&N =
1439 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1440 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001441 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001442 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001443 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001444 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001445}
1446
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001447SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001448 if ((unsigned)Cond >= CondCodeNodes.size())
1449 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001450
Craig Topperc0196b12014-04-14 00:51:57 +00001451 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001452 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001453 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001454 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001455 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001456
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001457 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001458}
1459
Nate Begeman5f829d82009-04-29 05:20:52 +00001460// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1461// the shuffle mask M that point at N1 to point at N2, and indices that point
1462// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001463static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1464 std::swap(N1, N2);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001465 ShuffleVectorSDNode::commuteMask(M);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001466}
1467
Andrew Trickef9de2a2013-05-25 02:42:55 +00001468SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001469 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001470 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1471 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001472
1473 // Canonicalize shuffle undef, undef -> undef
1474 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001475 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001476
Eric Christopherdfda92b2009-08-22 00:40:45 +00001477 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001478 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001479 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001480 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001481 for (unsigned i = 0; i != NElts; ++i) {
1482 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001483 MaskVec.push_back(Mask[i]);
1484 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001485
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001486 // Canonicalize shuffle v, v -> v, undef
1487 if (N1 == N2) {
1488 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001489 for (unsigned i = 0; i != NElts; ++i)
1490 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001491 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001492
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001493 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1494 if (N1.getOpcode() == ISD::UNDEF)
1495 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001496
Chandler Carruth1b5285d2015-02-15 12:18:12 +00001497 // If shuffling a splat, try to blend the splat instead. We do this here so
1498 // that even when this arises during lowering we don't have to re-handle it.
1499 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1500 BitVector UndefElements;
1501 SDValue Splat = BV->getSplatValue(&UndefElements);
1502 if (!Splat)
1503 return;
1504
1505 for (int i = 0; i < (int)NElts; ++i) {
1506 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1507 continue;
1508
1509 // If this input comes from undef, mark it as such.
1510 if (UndefElements[MaskVec[i] - Offset]) {
1511 MaskVec[i] = -1;
1512 continue;
1513 }
1514
1515 // If we can blend a non-undef lane, use that instead.
1516 if (!UndefElements[i])
1517 MaskVec[i] = i + Offset;
1518 }
1519 };
1520 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1521 BlendSplat(N1BV, 0);
1522 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1523 BlendSplat(N2BV, NElts);
1524
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001525 // Canonicalize all index into lhs, -> shuffle lhs, undef
1526 // Canonicalize all index into rhs, -> shuffle rhs, undef
1527 bool AllLHS = true, AllRHS = true;
1528 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001529 for (unsigned i = 0; i != NElts; ++i) {
1530 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001531 if (N2Undef)
1532 MaskVec[i] = -1;
1533 else
1534 AllLHS = false;
1535 } else if (MaskVec[i] >= 0) {
1536 AllRHS = false;
1537 }
1538 }
1539 if (AllLHS && AllRHS)
1540 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001541 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001542 N2 = getUNDEF(VT);
1543 if (AllRHS) {
1544 N1 = getUNDEF(VT);
1545 commuteShuffle(N1, N2, MaskVec);
1546 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001547 // Reset our undef status after accounting for the mask.
1548 N2Undef = N2.getOpcode() == ISD::UNDEF;
1549 // Re-check whether both sides ended up undef.
1550 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1551 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001552
Craig Topper9a39b072013-08-08 08:03:12 +00001553 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001554 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001555 for (unsigned i = 0; i != NElts; ++i) {
1556 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001557 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001558 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001559 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001560 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001561
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001562 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001563 if (N2Undef) {
1564 SDValue V = N1;
1565
1566 // Look through any bitcasts. We check that these don't change the number
1567 // (and size) of elements and just changes their types.
1568 while (V.getOpcode() == ISD::BITCAST)
1569 V = V->getOperand(0);
1570
1571 // A splat should always show up as a build vector node.
1572 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001573 BitVector UndefElements;
1574 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001575 // If this is a splat of an undef, shuffling it is also undef.
1576 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1577 return getUNDEF(VT);
1578
Andrea Di Biagio83814752015-01-24 11:54:29 +00001579 bool SameNumElts =
1580 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1581
Chandler Carruth142e9662014-07-08 08:45:38 +00001582 // We only have a splat which can skip shuffles if there is a splatted
1583 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001584 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001585 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1586 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001587 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001588 return N1;
1589 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1590 if (C->isNullValue())
1591 return N1;
1592 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001593
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001594 // If the shuffle itself creates a splat, build the vector directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001595 if (AllSame && SameNumElts) {
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001596 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1597 SmallVector<SDValue, 8> Ops(NElts, Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001598
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001599 EVT BuildVT = BV->getValueType(0);
1600 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001601
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001602 // We may have jumped through bitcasts, so the type of the
1603 // BUILD_VECTOR may not match the type of the shuffle.
1604 if (BuildVT != VT)
1605 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1606 return NewBV;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001607 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001608 }
1609 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001610
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001611 FoldingSetNodeID ID;
1612 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001613 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001614 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001615 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001616
Craig Topperc0196b12014-04-14 00:51:57 +00001617 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001618 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001619 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001620
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001621 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1622 // SDNode doesn't have access to it. This memory will be "leaked" when
1623 // the node is deallocated, but recovered when the NodeAllocator is released.
1624 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1625 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001626
Dan Gohman01c65a22010-03-18 18:49:47 +00001627 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001628 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1629 dl.getDebugLoc(), N1, N2,
1630 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001631 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001632 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001633 return SDValue(N, 0);
1634}
1635
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001636SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1637 MVT VT = SV.getSimpleValueType(0);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001638 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1639 ShuffleVectorSDNode::commuteMask(MaskVec);
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001640
1641 SDValue Op0 = SV.getOperand(0);
1642 SDValue Op1 = SV.getOperand(1);
1643 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1644}
1645
Andrew Trickef9de2a2013-05-25 02:42:55 +00001646SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001647 SDValue Val, SDValue DTy,
1648 SDValue STy, SDValue Rnd, SDValue Sat,
1649 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001650 // If the src and dest types are the same and the conversion is between
1651 // integer types of the same sign or two floats, no conversion is necessary.
1652 if (DTy == STy &&
1653 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001654 return Val;
1655
1656 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001657 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001658 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001659 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001661 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001662
Jack Carter170a5f22013-09-09 22:02:08 +00001663 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1664 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001665 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001666 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001667 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001668 return SDValue(N, 0);
1669}
1670
Owen Anderson53aa7a92009-08-10 22:56:29 +00001671SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001672 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001673 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001674 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001675 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001676 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001677 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001678
Dan Gohman01c65a22010-03-18 18:49:47 +00001679 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001680 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001681 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001682 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001683}
1684
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001685SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1686 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001687 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001688 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001689 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001690 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1691 return SDValue(E, 0);
1692
1693 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1694 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001695 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001696 return SDValue(N, 0);
1697}
1698
Andrew Trickef9de2a2013-05-25 02:42:55 +00001699SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001700 FoldingSetNodeID ID;
1701 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001702 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001703 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001704 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001705 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001706 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001707
Jack Carter170a5f22013-09-09 22:02:08 +00001708 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1709 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001710 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001711 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001712 return SDValue(N, 0);
1713}
1714
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001715
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001716SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001717 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001718 bool isTarget,
1719 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001720 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1721
1722 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001723 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001724 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001725 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001726 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001727 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001728 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001729 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001730
Michael Liaoabb87d42012-09-12 21:43:09 +00001731 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1732 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001733 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001734 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001735 return SDValue(N, 0);
1736}
1737
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001738SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001739 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001740 "SrcValue is not a pointer?");
1741
Jim Laskeyf576b422006-10-27 23:46:08 +00001742 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001743 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001744 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001745
Craig Topperc0196b12014-04-14 00:51:57 +00001746 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001747 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001748 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001749
Dan Gohman01c65a22010-03-18 18:49:47 +00001750 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001751 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001752 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001753 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001754}
1755
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001756/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1757SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1758 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001759 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001760 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001761
Craig Topperc0196b12014-04-14 00:51:57 +00001762 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001763 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1764 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001765
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001766 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1767 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001768 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001769 return SDValue(N, 0);
1770}
1771
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001772/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1773SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1774 unsigned SrcAS, unsigned DestAS) {
1775 SDValue Ops[] = {Ptr};
1776 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001777 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001778 ID.AddInteger(SrcAS);
1779 ID.AddInteger(DestAS);
1780
Craig Topperc0196b12014-04-14 00:51:57 +00001781 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001782 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1783 return SDValue(E, 0);
1784
1785 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1786 dl.getDebugLoc(),
1787 VT, Ptr, SrcAS, DestAS);
1788 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001789 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001790 return SDValue(N, 0);
1791}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001792
Duncan Sands41826032009-01-31 15:50:11 +00001793/// getShiftAmountOperand - Return the specified value casted to
1794/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001795SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001796 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001797 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001798 if (OpTy == ShTy || OpTy.isVector()) return Op;
1799
1800 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001801 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001802}
1803
Chris Lattner9eb7a822007-10-15 17:47:20 +00001804/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1805/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001806SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001807 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001808 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001809 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001810 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001811 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001812
David Greene1fbe0542009-11-12 20:49:22 +00001813 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001814 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001815}
1816
Duncan Sands445071c2008-12-09 21:33:20 +00001817/// CreateStackTemporary - Create a stack temporary suitable for holding
1818/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001819SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001820 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1821 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001822 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1823 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001824 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001825 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1826 TD->getPrefTypeAlignment(Ty2));
1827
1828 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001829 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001830 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001831}
1832
Owen Anderson53aa7a92009-08-10 22:56:29 +00001833SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001834 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001835 // These setcc operations always fold.
1836 switch (Cond) {
1837 default: break;
1838 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001839 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001840 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001841 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001842 TargetLowering::BooleanContent Cnt =
1843 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001844 return getConstant(
1845 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1846 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001847
Chris Lattner393d96a2006-04-27 05:01:07 +00001848 case ISD::SETOEQ:
1849 case ISD::SETOGT:
1850 case ISD::SETOGE:
1851 case ISD::SETOLT:
1852 case ISD::SETOLE:
1853 case ISD::SETONE:
1854 case ISD::SETO:
1855 case ISD::SETUO:
1856 case ISD::SETUEQ:
1857 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001858 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001859 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001860 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001861
Gabor Greiff304a7a2008-08-28 21:40:38 +00001862 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001863 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001864 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001865 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001866
Chris Lattner061a1ea2005-01-07 07:46:32 +00001867 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001868 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001869 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1870 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001871 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1872 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1873 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1874 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1875 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1876 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1877 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1878 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001879 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001880 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001881 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001882 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1883 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001884 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001885 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001886 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001887 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001888 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001889 // fall through
1890 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001891 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001892 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001893 // fall through
1894 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001895 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001896 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001897 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001898 // fall through
1899 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001900 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001901 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001902 // fall through
1903 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001904 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001905 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001906 // fall through
1907 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001908 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001909 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001910 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001911 // fall through
1912 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001913 R==APFloat::cmpEqual, VT);
1914 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1915 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1916 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1917 R==APFloat::cmpEqual, VT);
1918 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1919 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1920 R==APFloat::cmpLessThan, VT);
1921 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1922 R==APFloat::cmpUnordered, VT);
1923 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1924 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001925 }
1926 } else {
1927 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001928 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1929 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001930 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001931 return SDValue();
1932
1933 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001934 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001935 }
1936
Chris Lattnerd47675e2005-08-09 20:20:18 +00001937 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001938 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001939}
1940
Dan Gohman1f372ed2008-02-25 21:11:39 +00001941/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1942/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001943bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001944 // This predicate is not safe for vector operations.
1945 if (Op.getValueType().isVector())
1946 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001947
Dan Gohman1d459e42009-12-11 21:31:27 +00001948 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001949 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1950}
1951
Dan Gohman309d3d52007-06-22 14:59:07 +00001952/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1953/// this predicate to simplify operations downstream. Mask is known to be zero
1954/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001955bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001956 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001957 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001958 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001959 return (KnownZero & Mask) == Mask;
1960}
1961
Jay Foada0653a32014-05-14 21:14:37 +00001962/// Determine which bits of Op are known to be either zero or one and return
1963/// them in the KnownZero/KnownOne bitsets.
1964void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1965 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001966 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001967
Dan Gohmanf990faf2008-02-13 00:35:47 +00001968 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001969 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001970 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001971
Dan Gohmanf990faf2008-02-13 00:35:47 +00001972 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001973
1974 switch (Op.getOpcode()) {
1975 case ISD::Constant:
1976 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001977 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1978 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001979 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001980 case ISD::AND:
1981 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001982 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1983 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001984
1985 // Output known-1 bits are only known if set in both the LHS & RHS.
1986 KnownOne &= KnownOne2;
1987 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1988 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001989 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001990 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001991 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1992 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001993
Dan Gohman309d3d52007-06-22 14:59:07 +00001994 // Output known-0 bits are only known if clear in both the LHS & RHS.
1995 KnownZero &= KnownZero2;
1996 // Output known-1 are known to be set if set in either the LHS | RHS.
1997 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001998 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001999 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00002000 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2001 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002002
Dan Gohman309d3d52007-06-22 14:59:07 +00002003 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002004 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00002005 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2006 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2007 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00002008 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002009 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002010 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00002011 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2012 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002013
2014 // If low bits are zero in either operand, output low known-0 bits.
2015 // Also compute a conserative estimate for high known-0 bits.
2016 // More trickiness is possible, but this is sufficient for the
2017 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00002018 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00002019 unsigned TrailZ = KnownZero.countTrailingOnes() +
2020 KnownZero2.countTrailingOnes();
2021 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002022 KnownZero2.countLeadingOnes(),
2023 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002024
2025 TrailZ = std::min(TrailZ, BitWidth);
2026 LeadZ = std::min(LeadZ, BitWidth);
2027 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2028 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002029 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002030 }
2031 case ISD::UDIV: {
2032 // For the purposes of computing leading zeros we can conservatively
2033 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002034 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002035 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002036 unsigned LeadZ = KnownZero2.countLeadingOnes();
2037
Jay Foad25a5e4c2010-12-01 08:53:58 +00002038 KnownOne2.clearAllBits();
2039 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002040 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002041 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2042 if (RHSUnknownLeadingOnes != BitWidth)
2043 LeadZ = std::min(BitWidth,
2044 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002045
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002046 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002047 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002048 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002049 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002050 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2051 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002052
Dan Gohman309d3d52007-06-22 14:59:07 +00002053 // Only known if known in both the LHS and RHS.
2054 KnownOne &= KnownOne2;
2055 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002056 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002057 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002058 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2059 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002060
Dan Gohman309d3d52007-06-22 14:59:07 +00002061 // Only known if known in both the LHS and RHS.
2062 KnownOne &= KnownOne2;
2063 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002064 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002065 case ISD::SADDO:
2066 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002067 case ISD::SSUBO:
2068 case ISD::USUBO:
2069 case ISD::SMULO:
2070 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002071 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002072 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002073 // The boolean result conforms to getBooleanContents.
2074 // If we know the result of a setcc has the top bits zero, use this info.
2075 // We know that we have an integer-based boolean since these operations
2076 // are only available for integer.
2077 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2078 TargetLowering::ZeroOrOneBooleanContent &&
2079 BitWidth > 1)
2080 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2081 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002082 case ISD::SETCC:
2083 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002084 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2085 TargetLowering::ZeroOrOneBooleanContent &&
2086 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002087 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002088 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002089 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002090 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002091 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002092 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002093
2094 // If the shift count is an invalid immediate, don't do anything.
2095 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002096 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002097
Jay Foada0653a32014-05-14 21:14:37 +00002098 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002099 KnownZero <<= ShAmt;
2100 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002101 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002102 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002103 }
Jay Foad5a29c362014-05-15 12:12:55 +00002104 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002105 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002106 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002107 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002108 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002109
Dan Gohman9db0aa82008-02-26 18:50:50 +00002110 // If the shift count is an invalid immediate, don't do anything.
2111 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002112 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002113
Jay Foada0653a32014-05-14 21:14:37 +00002114 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002115 KnownZero = KnownZero.lshr(ShAmt);
2116 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002117
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002118 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002119 KnownZero |= HighBits; // High bits known zero.
2120 }
Jay Foad5a29c362014-05-15 12:12:55 +00002121 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002122 case ISD::SRA:
2123 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002124 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002125
Dan Gohman9db0aa82008-02-26 18:50:50 +00002126 // If the shift count is an invalid immediate, don't do anything.
2127 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002128 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002129
Dan Gohman309d3d52007-06-22 14:59:07 +00002130 // If any of the demanded bits are produced by the sign extension, we also
2131 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002132 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002133
Jay Foada0653a32014-05-14 21:14:37 +00002134 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002135 KnownZero = KnownZero.lshr(ShAmt);
2136 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002137
Dan Gohman309d3d52007-06-22 14:59:07 +00002138 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002139 APInt SignBit = APInt::getSignBit(BitWidth);
2140 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002141
Dan Gohmanb717fda2008-02-20 16:30:17 +00002142 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002143 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002144 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002145 KnownOne |= HighBits; // New bits are known one.
2146 }
2147 }
Jay Foad5a29c362014-05-15 12:12:55 +00002148 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002149 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002150 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002151 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002152
2153 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002154 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002155 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002156
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002157 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002158 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002159
Dan Gohman309d3d52007-06-22 14:59:07 +00002160 // If the sign extended bits are demanded, we know that the sign
2161 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002162 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002163 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002164 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002165
Jay Foada0653a32014-05-14 21:14:37 +00002166 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002167 KnownOne &= InputDemandedBits;
2168 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002169
Dan Gohman309d3d52007-06-22 14:59:07 +00002170 // If the sign bit of the input is known set or clear, then we know the
2171 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002172 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002173 KnownZero |= NewBits;
2174 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002175 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002176 KnownOne |= NewBits;
2177 KnownZero &= ~NewBits;
2178 } else { // Input sign bit unknown
2179 KnownZero &= ~NewBits;
2180 KnownOne &= ~NewBits;
2181 }
Jay Foad5a29c362014-05-15 12:12:55 +00002182 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002183 }
2184 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002185 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002186 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002187 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002188 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002189 unsigned LowBits = Log2_32(BitWidth)+1;
2190 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002191 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002192 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002193 }
2194 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002195 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002196 // If this is a ZEXTLoad and we are looking at the loaded value.
2197 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002198 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002199 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002200 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002201 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002202 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002203 }
Jay Foad5a29c362014-05-15 12:12:55 +00002204 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002205 }
2206 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002207 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002208 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002209 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002210 KnownZero = KnownZero.trunc(InBits);
2211 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002212 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002213 KnownZero = KnownZero.zext(BitWidth);
2214 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002215 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002216 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002217 }
2218 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002219 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002220 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002221 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002222
Jay Foad583abbc2010-12-07 08:25:19 +00002223 KnownZero = KnownZero.trunc(InBits);
2224 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002225 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002226
2227 // Note if the sign bit is known to be zero or one.
2228 bool SignBitKnownZero = KnownZero.isNegative();
2229 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002230
Jay Foad583abbc2010-12-07 08:25:19 +00002231 KnownZero = KnownZero.zext(BitWidth);
2232 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002233
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002234 // If the sign bit is known zero or one, the top bits match.
2235 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002236 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002237 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002238 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002239 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002240 }
2241 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002242 EVT InVT = Op.getOperand(0).getValueType();
2243 unsigned InBits = InVT.getScalarType().getSizeInBits();
2244 KnownZero = KnownZero.trunc(InBits);
2245 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002246 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002247 KnownZero = KnownZero.zext(BitWidth);
2248 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002249 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002250 }
2251 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002252 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002253 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002254 KnownZero = KnownZero.zext(InBits);
2255 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002256 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002257 KnownZero = KnownZero.trunc(BitWidth);
2258 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002259 break;
2260 }
2261 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002262 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002263 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002264 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002265 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002266 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002267 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002268 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002269 case ISD::FGETSIGN:
2270 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002271 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002272 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002273
Dan Gohman72ec3f42008-04-28 17:02:21 +00002274 case ISD::SUB: {
2275 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2276 // We know that the top bits of C-X are clear if X contains less bits
2277 // than C (i.e. no wrap-around can happen). For example, 20-X is
2278 // positive if we can prove that X is >= 0 and < 16.
2279 if (CLHS->getAPIntValue().isNonNegative()) {
2280 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2281 // NLZ can't be BitWidth with no sign bit
2282 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002283 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002284
2285 // If all of the MaskV bits are known to be zero, then we know the
2286 // output top bits are zero, because we now know that the output is
2287 // from [0-C].
2288 if ((KnownZero2 & MaskV) == MaskV) {
2289 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2290 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002291 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002292 }
2293 }
2294 }
2295 }
2296 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002297 case ISD::ADD:
2298 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002299 // Output known-0 bits are known if clear or set in both the low clear bits
2300 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2301 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002302 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002303 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2304
Jay Foada0653a32014-05-14 21:14:37 +00002305 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002306 KnownZeroOut = std::min(KnownZeroOut,
2307 KnownZero2.countTrailingOnes());
2308
Chris Lattner440b2802010-12-19 20:38:28 +00002309 if (Op.getOpcode() == ISD::ADD) {
2310 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002311 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002312 }
2313
2314 // With ADDE, a carry bit may be added in, so we can only use this
2315 // information if we know (at least) that the low two bits are clear. We
2316 // then return to the caller that the low bit is unknown but that other bits
2317 // are known zero.
2318 if (KnownZeroOut >= 2) // ADDE
2319 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002320 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002321 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002322 case ISD::SREM:
2323 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002324 const APInt &RA = Rem->getAPIntValue().abs();
2325 if (RA.isPowerOf2()) {
2326 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002327 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002328
Duncan Sands33274982010-01-29 09:45:26 +00002329 // The low bits of the first operand are unchanged by the srem.
2330 KnownZero = KnownZero2 & LowBits;
2331 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002332
Duncan Sands33274982010-01-29 09:45:26 +00002333 // If the first operand is non-negative or has all low bits zero, then
2334 // the upper bits are all zero.
2335 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2336 KnownZero |= ~LowBits;
2337
2338 // If the first operand is negative and not all low bits are zero, then
2339 // the upper bits are all one.
2340 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2341 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002342 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002343 }
2344 }
Jay Foad5a29c362014-05-15 12:12:55 +00002345 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002346 case ISD::UREM: {
2347 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002348 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002349 if (RA.isPowerOf2()) {
2350 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002351 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2352
2353 // The upper bits are all zero, the lower ones are unchanged.
2354 KnownZero = KnownZero2 | ~LowBits;
2355 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002356 break;
2357 }
2358 }
2359
2360 // Since the result is less than or equal to either operand, any leading
2361 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002362 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2363 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002364
2365 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2366 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002367 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002368 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002369 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002370 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002371 case ISD::EXTRACT_ELEMENT: {
2372 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2373 const unsigned Index =
2374 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2375 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2376
2377 // Remove low part of known bits mask
2378 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2379 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2380
2381 // Remove high part of known bit mask
2382 KnownZero = KnownZero.trunc(BitWidth);
2383 KnownOne = KnownOne.trunc(BitWidth);
2384 break;
2385 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002386 case ISD::FrameIndex:
2387 case ISD::TargetFrameIndex:
2388 if (unsigned Align = InferPtrAlignment(Op)) {
2389 // The low bits are known zero if the pointer is aligned.
2390 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002391 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002392 }
2393 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002394
Dan Gohman309d3d52007-06-22 14:59:07 +00002395 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002396 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2397 break;
2398 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002399 case ISD::INTRINSIC_WO_CHAIN:
2400 case ISD::INTRINSIC_W_CHAIN:
2401 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002402 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002403 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002404 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002405 }
Jay Foad5a29c362014-05-15 12:12:55 +00002406
2407 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002408}
2409
2410/// ComputeNumSignBits - Return the number of times the sign bit of the
2411/// register is replicated into the other bits. We know that at least 1 bit
2412/// is always equal to the sign bit (itself), but other cases can give us
2413/// information. For example, immediately after an "SRA X, 2", we know that
2414/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002415unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002416 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002417 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002418 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002419 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002420 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002421
Dan Gohman309d3d52007-06-22 14:59:07 +00002422 if (Depth == 6)
2423 return 1; // Limit search depth.
2424
2425 switch (Op.getOpcode()) {
2426 default: break;
2427 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002428 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002429 return VTBits-Tmp+1;
2430 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002431 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002432 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002433
Dan Gohman309d3d52007-06-22 14:59:07 +00002434 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002435 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002436 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002437 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002438
Dan Gohman309d3d52007-06-22 14:59:07 +00002439 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002440 Tmp =
2441 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002442 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002443
Dan Gohman309d3d52007-06-22 14:59:07 +00002444 case ISD::SIGN_EXTEND_INREG:
2445 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002446 Tmp =
2447 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002448 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002449
Dan Gohman309d3d52007-06-22 14:59:07 +00002450 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2451 return std::max(Tmp, Tmp2);
2452
2453 case ISD::SRA:
2454 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2455 // SRA X, C -> adds C sign bits.
2456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002457 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002458 if (Tmp > VTBits) Tmp = VTBits;
2459 }
2460 return Tmp;
2461 case ISD::SHL:
2462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2463 // shl destroys sign bits.
2464 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002465 if (C->getZExtValue() >= VTBits || // Bad shift.
2466 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2467 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002468 }
2469 break;
2470 case ISD::AND:
2471 case ISD::OR:
2472 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002473 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002474 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002475 if (Tmp != 1) {
2476 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2477 FirstAnswer = std::min(Tmp, Tmp2);
2478 // We computed what we know about the sign bits as our first
2479 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002480 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002481 }
2482 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002483
2484 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002485 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002486 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002487 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002488 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002489
2490 case ISD::SADDO:
2491 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002492 case ISD::SSUBO:
2493 case ISD::USUBO:
2494 case ISD::SMULO:
2495 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002496 if (Op.getResNo() != 1)
2497 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002498 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002499 // If setcc returns 0/-1, all bits are sign bits.
2500 // We know that we have an integer-based boolean since these operations
2501 // are only available for integer.
2502 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2503 TargetLowering::ZeroOrNegativeOneBooleanContent)
2504 return VTBits;
2505 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002506 case ISD::SETCC:
2507 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002508 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002509 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002510 return VTBits;
2511 break;
2512 case ISD::ROTL:
2513 case ISD::ROTR:
2514 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002515 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002516
Dan Gohman309d3d52007-06-22 14:59:07 +00002517 // Handle rotate right by N like a rotate left by 32-N.
2518 if (Op.getOpcode() == ISD::ROTR)
2519 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2520
2521 // If we aren't rotating out all of the known-in sign bits, return the
2522 // number that are left. This handles rotl(sext(x), 1) for example.
2523 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2524 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2525 }
2526 break;
2527 case ISD::ADD:
2528 // Add can have at most one carry bit. Thus we know that the output
2529 // is, at worst, one more bit than the inputs.
2530 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2531 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002532
Dan Gohman309d3d52007-06-22 14:59:07 +00002533 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002534 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002535 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002536 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002537 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002538
Dan Gohman309d3d52007-06-22 14:59:07 +00002539 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2540 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002541 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002542 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002543
Dan Gohman309d3d52007-06-22 14:59:07 +00002544 // If we are subtracting one from a positive number, there is no carry
2545 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002546 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002547 return Tmp;
2548 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002549
Dan Gohman309d3d52007-06-22 14:59:07 +00002550 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2551 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002552 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002553
Dan Gohman309d3d52007-06-22 14:59:07 +00002554 case ISD::SUB:
2555 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2556 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002557
Dan Gohman309d3d52007-06-22 14:59:07 +00002558 // Handle NEG.
2559 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002560 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002561 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002562 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002563 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2564 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002565 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002566 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002567
Dan Gohman309d3d52007-06-22 14:59:07 +00002568 // If the input is known to be positive (the sign bit is known clear),
2569 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002570 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002571 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002572
Dan Gohman309d3d52007-06-22 14:59:07 +00002573 // Otherwise, we treat this like a SUB.
2574 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002575
Dan Gohman309d3d52007-06-22 14:59:07 +00002576 // Sub can have at most one carry bit. Thus we know that the output
2577 // is, at worst, one more bit than the inputs.
2578 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2579 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002580 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002581 case ISD::TRUNCATE:
2582 // FIXME: it's tricky to do anything useful for this, but it is an important
2583 // case for targets like X86.
2584 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002585 case ISD::EXTRACT_ELEMENT: {
2586 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2587 const int BitWidth = Op.getValueType().getSizeInBits();
2588 const int Items =
2589 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2590
2591 // Get reverse index (starting from 1), Op1 value indexes elements from
2592 // little end. Sign starts at big end.
2593 const int rIndex = Items - 1 -
2594 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2595
2596 // If the sign portion ends in our element the substraction gives correct
2597 // result. Otherwise it gives either negative or > bitwidth result
2598 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2599 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002600 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002601
Nadav Rotem4536d582013-03-20 22:53:44 +00002602 // If we are looking at the loaded value of the SDNode.
2603 if (Op.getResNo() == 0) {
2604 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2605 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2606 unsigned ExtType = LD->getExtensionType();
2607 switch (ExtType) {
2608 default: break;
2609 case ISD::SEXTLOAD: // '17' bits known
2610 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2611 return VTBits-Tmp+1;
2612 case ISD::ZEXTLOAD: // '16' bits known
2613 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2614 return VTBits-Tmp;
2615 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002616 }
2617 }
2618
2619 // Allow the target to implement this method for its nodes.
2620 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002621 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002622 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2623 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002624 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002625 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002626 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002627
Dan Gohman309d3d52007-06-22 14:59:07 +00002628 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2629 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002630 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002631 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002632
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002633 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002634 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002635 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002636 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002637 Mask = KnownOne;
2638 } else {
2639 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002640 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002641 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002642
Dan Gohman309d3d52007-06-22 14:59:07 +00002643 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2644 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002645 Mask = ~Mask;
2646 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002647 // Return # leading zeros. We use 'min' here in case Val was zero before
2648 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002649 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002650}
2651
Chris Lattner46c01a32011-02-13 22:25:43 +00002652/// isBaseWithConstantOffset - Return true if the specified operand is an
2653/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2654/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2655/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002656/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002657bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2658 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2659 !isa<ConstantSDNode>(Op.getOperand(1)))
2660 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002661
2662 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002663 !MaskedValueIsZero(Op.getOperand(0),
2664 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2665 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002666
Chris Lattner46c01a32011-02-13 22:25:43 +00002667 return true;
2668}
2669
2670
Dan Gohmand0d5e682009-09-03 20:34:31 +00002671bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2672 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002673 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002674 return true;
2675
2676 // If the value is a constant, we can obviously see if it is a NaN or not.
2677 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2678 return !C->getValueAPF().isNaN();
2679
2680 // TODO: Recognize more cases here.
2681
2682 return false;
2683}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002684
Dan Gohman38605212010-02-24 06:52:40 +00002685bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2686 // If the value is a constant, we can obviously see if it is a zero or not.
2687 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2688 return !C->isZero();
2689
2690 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002691 switch (Op.getOpcode()) {
2692 default: break;
2693 case ISD::OR:
2694 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2695 return !C->isNullValue();
2696 break;
2697 }
Dan Gohman38605212010-02-24 06:52:40 +00002698
2699 return false;
2700}
2701
2702bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2703 // Check the obvious case.
2704 if (A == B) return true;
2705
2706 // For for negative and positive zero.
2707 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2708 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2709 if (CA->isZero() && CB->isZero()) return true;
2710
2711 // Otherwise they may not be equal.
2712 return false;
2713}
2714
Chris Lattner061a1ea2005-01-07 07:46:32 +00002715/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002716///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002717SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002718 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002719 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002720 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002721 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002722 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002723
Jack Carter170a5f22013-09-09 22:02:08 +00002724 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2725 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002726 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002727
Chandler Carruth41b20e72014-07-22 04:07:55 +00002728 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002729 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002730}
2731
Andrew Trickef9de2a2013-05-25 02:42:55 +00002732SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002733 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002734 // Constant fold unary operations with an integer constant operand. Even
2735 // opaque constant will be folded, because the folding of unary operations
2736 // doesn't create new constants with different values. Nevertheless, the
2737 // opaque flag is preserved during folding to prevent future folding with
2738 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002739 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002740 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002741 switch (Opcode) {
2742 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002743 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002744 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2745 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002746 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002747 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002748 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002749 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2750 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002751 case ISD::UINT_TO_FP:
2752 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002753 APFloat apf(EVTToAPFloatSemantics(VT),
2754 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002755 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002756 Opcode==ISD::SINT_TO_FP,
2757 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002758 return getConstantFP(apf, VT);
2759 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002760 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002761 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
2762 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002763 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002764 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002765 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002766 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002767 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002768 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002769 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2770 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002771 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002772 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2773 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002774 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002775 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002776 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2777 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002778 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002779 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002780 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2781 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002782 }
2783 }
2784
Dale Johannesen446b9002007-08-31 23:34:27 +00002785 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002786 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002787 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002788 switch (Opcode) {
2789 case ISD::FNEG:
2790 V.changeSign();
2791 return getConstantFP(V, VT);
2792 case ISD::FABS:
2793 V.clearSign();
2794 return getConstantFP(V, VT);
2795 case ISD::FCEIL: {
2796 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2797 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002798 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002799 break;
2800 }
2801 case ISD::FTRUNC: {
2802 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2803 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002804 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002805 break;
2806 }
2807 case ISD::FFLOOR: {
2808 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2809 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002810 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002811 break;
2812 }
2813 case ISD::FP_EXTEND: {
2814 bool ignored;
2815 // This can return overflow, underflow, or inexact; we don't care.
2816 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002817 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002818 APFloat::rmNearestTiesToEven, &ignored);
2819 return getConstantFP(V, VT);
2820 }
2821 case ISD::FP_TO_SINT:
2822 case ISD::FP_TO_UINT: {
2823 integerPart x[2];
2824 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002825 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002826 // FIXME need to be more flexible about rounding mode.
2827 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2828 Opcode==ISD::FP_TO_SINT,
2829 APFloat::rmTowardZero, &ignored);
2830 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002831 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002832 APInt api(VT.getSizeInBits(), x);
2833 return getConstant(api, VT);
2834 }
2835 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002836 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
2837 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), VT);
2838 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002839 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2840 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2841 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2842 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002843 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002844 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002845
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002846 // Constant fold unary operations with a vector integer or float operand.
Jim Grosbachf7502c42014-07-18 00:40:52 +00002847 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002848 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002849 switch (Opcode) {
2850 default:
2851 // FIXME: Entirely reasonable to perform folding of other unary
2852 // operations here as the need arises.
2853 break;
Simon Pilgrim6bd5d3c2015-04-16 08:21:09 +00002854 case ISD::TRUNCATE:
2855 // Constant build vector truncation can be done with the original scalar
2856 // operands but with a new build vector with the truncated value type.
2857 return getNode(ISD::BUILD_VECTOR, DL, VT, BV->ops());
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002858 case ISD::FNEG:
2859 case ISD::FABS:
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002860 case ISD::FCEIL:
2861 case ISD::FTRUNC:
2862 case ISD::FFLOOR:
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002863 case ISD::FP_EXTEND:
Jim Grosbachf7502c42014-07-18 00:40:52 +00002864 case ISD::UINT_TO_FP:
2865 case ISD::SINT_TO_FP: {
Simon Pilgrim481f4142015-03-23 22:44:55 +00002866 // Let the above scalar folding handle the folding of each element.
Jim Grosbach19dd3082014-07-23 20:41:31 +00002867 SmallVector<SDValue, 8> Ops;
2868 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2869 SDValue OpN = BV->getOperand(i);
Simon Pilgrim481f4142015-03-23 22:44:55 +00002870 OpN = getNode(Opcode, DL, VT.getVectorElementType(), OpN);
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002871 if (OpN.getOpcode() != ISD::UNDEF &&
2872 OpN.getOpcode() != ISD::Constant &&
2873 OpN.getOpcode() != ISD::ConstantFP)
2874 break;
Jim Grosbach19dd3082014-07-23 20:41:31 +00002875 Ops.push_back(OpN);
2876 }
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002877 if (Ops.size() == VT.getVectorNumElements())
2878 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002879 break;
Jim Grosbachf7502c42014-07-18 00:40:52 +00002880 }
2881 }
2882 }
2883 }
2884
Gabor Greiff304a7a2008-08-28 21:40:38 +00002885 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002886 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002887 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002888 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002889 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002890 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002891 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002892 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002893 assert(VT.isFloatingPoint() &&
2894 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002895 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002896 assert((!VT.isVector() ||
2897 VT.getVectorNumElements() ==
2898 Operand.getValueType().getVectorNumElements()) &&
2899 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002900 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002901 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002902 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002903 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002904 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002905 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002906 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002907 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2908 "Invalid sext node, dst < src!");
2909 assert((!VT.isVector() ||
2910 VT.getVectorNumElements() ==
2911 Operand.getValueType().getVectorNumElements()) &&
2912 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002913 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2914 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2915 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002916 // sext(undef) = 0, because the top bits will all be the same.
2917 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002918 break;
2919 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002920 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002921 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002922 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002923 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2924 "Invalid zext node, dst < src!");
2925 assert((!VT.isVector() ||
2926 VT.getVectorNumElements() ==
2927 Operand.getValueType().getVectorNumElements()) &&
2928 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002929 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002930 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002931 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002932 else if (OpOpcode == ISD::UNDEF)
2933 // zext(undef) = 0, because the top bits will be zero.
2934 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002935 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002936 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002937 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002938 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002939 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002940 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2941 "Invalid anyext node, dst < src!");
2942 assert((!VT.isVector() ||
2943 VT.getVectorNumElements() ==
2944 Operand.getValueType().getVectorNumElements()) &&
2945 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002946
Dan Gohman08837892010-06-18 00:08:30 +00002947 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2948 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002949 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002950 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002951 else if (OpOpcode == ISD::UNDEF)
2952 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002953
2954 // (ext (trunx x)) -> x
2955 if (OpOpcode == ISD::TRUNCATE) {
2956 SDValue OpOp = Operand.getNode()->getOperand(0);
2957 if (OpOp.getValueType() == VT)
2958 return OpOp;
2959 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002960 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002961 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002962 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002963 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002964 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002965 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2966 "Invalid truncate node, src < dst!");
2967 assert((!VT.isVector() ||
2968 VT.getVectorNumElements() ==
2969 Operand.getValueType().getVectorNumElements()) &&
2970 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002971 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002972 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002973 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2974 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002975 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002976 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2977 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002978 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002979 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002980 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002981 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002982 }
Craig Topper201c1a32012-01-15 01:05:11 +00002983 if (OpOpcode == ISD::UNDEF)
2984 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002985 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002986 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002987 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002988 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002989 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002990 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002991 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2992 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002993 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002994 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002995 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002996 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002997 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002998 (VT.getVectorElementType() == Operand.getValueType() ||
2999 (VT.getVectorElementType().isInteger() &&
3000 Operand.getValueType().isInteger() &&
3001 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003002 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00003003 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003004 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00003005 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3006 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3007 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3008 Operand.getConstantOperandVal(1) == 0 &&
3009 Operand.getOperand(0).getValueType() == VT)
3010 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003011 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00003012 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00003013 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003014 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00003015 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00003016 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003017 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00003018 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00003019 break;
3020 case ISD::FABS:
3021 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00003022 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003023 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003024 }
3025
Chris Lattnerf9c19152005-08-25 19:12:10 +00003026 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003027 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003028 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00003029 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003030 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00003031 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003032 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003033 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003034 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003035
Jack Carter170a5f22013-09-09 22:02:08 +00003036 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3037 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003038 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003039 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003040 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3041 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003042 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003043
Chandler Carruth41b20e72014-07-22 04:07:55 +00003044 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003045 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003046}
3047
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003048SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
3049 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003050 // If the opcode is a target-specific ISD node, there's nothing we can
3051 // do here and the operand rules may not line up with the below, so
3052 // bail early.
3053 if (Opcode >= ISD::BUILTIN_OP_END)
3054 return SDValue();
3055
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003056 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3057 SmallVector<SDValue, 4> Outputs;
3058 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00003059
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003060 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3061 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003062 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3063 return SDValue();
3064
3065 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003066 // Scalar instruction.
3067 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003068 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003069 // For vectors extract each constant element into Inputs so we can constant
3070 // fold them individually.
3071 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3072 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3073 if (!BV1 || !BV2)
3074 return SDValue();
3075
3076 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3077
3078 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3079 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3080 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3081 if (!V1 || !V2) // Not a constant, bail.
3082 return SDValue();
3083
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003084 if (V1->isOpaque() || V2->isOpaque())
3085 return SDValue();
3086
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003087 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3088 // FIXME: This is valid and could be handled by truncating the APInts.
3089 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3090 return SDValue();
3091
3092 Inputs.push_back(std::make_pair(V1, V2));
3093 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003094 }
3095
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003096 // We have a number of constant values, constant fold them element by element.
3097 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3098 const APInt &C1 = Inputs[I].first->getAPIntValue();
3099 const APInt &C2 = Inputs[I].second->getAPIntValue();
3100
3101 switch (Opcode) {
3102 case ISD::ADD:
3103 Outputs.push_back(getConstant(C1 + C2, SVT));
3104 break;
3105 case ISD::SUB:
3106 Outputs.push_back(getConstant(C1 - C2, SVT));
3107 break;
3108 case ISD::MUL:
3109 Outputs.push_back(getConstant(C1 * C2, SVT));
3110 break;
3111 case ISD::UDIV:
3112 if (!C2.getBoolValue())
3113 return SDValue();
3114 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3115 break;
3116 case ISD::UREM:
3117 if (!C2.getBoolValue())
3118 return SDValue();
3119 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3120 break;
3121 case ISD::SDIV:
3122 if (!C2.getBoolValue())
3123 return SDValue();
3124 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3125 break;
3126 case ISD::SREM:
3127 if (!C2.getBoolValue())
3128 return SDValue();
3129 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3130 break;
3131 case ISD::AND:
3132 Outputs.push_back(getConstant(C1 & C2, SVT));
3133 break;
3134 case ISD::OR:
3135 Outputs.push_back(getConstant(C1 | C2, SVT));
3136 break;
3137 case ISD::XOR:
3138 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3139 break;
3140 case ISD::SHL:
3141 Outputs.push_back(getConstant(C1 << C2, SVT));
3142 break;
3143 case ISD::SRL:
3144 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3145 break;
3146 case ISD::SRA:
3147 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3148 break;
3149 case ISD::ROTL:
3150 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3151 break;
3152 case ISD::ROTR:
3153 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3154 break;
3155 default:
3156 return SDValue();
3157 }
3158 }
3159
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003160 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3161 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003162
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003163 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003164 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003165 return Outputs.back();
3166
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003167 // We may have a vector type but a scalar result. Create a splat.
3168 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3169
3170 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003171 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003172}
3173
Andrew Trickef9de2a2013-05-25 02:42:55 +00003174SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003175 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003176 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3177 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003178 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003179 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003180 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003181 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3182 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003183 // Fold trivial token factors.
3184 if (N1.getOpcode() == ISD::EntryToken) return N2;
3185 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003186 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003187 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003188 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003189 // Concat of UNDEFs is UNDEF.
3190 if (N1.getOpcode() == ISD::UNDEF &&
3191 N2.getOpcode() == ISD::UNDEF)
3192 return getUNDEF(VT);
3193
Dan Gohman550c9af2008-08-14 20:04:46 +00003194 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3195 // one big BUILD_VECTOR.
3196 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3197 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003198 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3199 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003200 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Simon Pilgrim860f0872015-04-21 08:05:43 +00003201
3202 // BUILD_VECTOR requires all inputs to be of the same type, find the
3203 // maximum type and extend them all.
3204 EVT SVT = VT.getScalarType();
3205 for (SDValue Op : Elts)
3206 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
3207 if (SVT.bitsGT(VT.getScalarType()))
3208 for (SDValue &Op : Elts)
3209 Op = TLI->isZExtFree(Op.getValueType(), SVT)
3210 ? getZExtOrTrunc(Op, DL, SVT)
3211 : getSExtOrTrunc(Op, DL, SVT);
3212
Craig Topper48d114b2014-04-26 18:35:24 +00003213 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003214 }
3215 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003216 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003217 assert(VT.isInteger() && "This operator does not apply to FP types!");
3218 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003219 N1.getValueType() == VT && "Binary operator types must match!");
3220 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3221 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003222 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003223 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003224 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3225 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003226 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003227 case ISD::OR:
3228 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003229 case ISD::ADD:
3230 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003231 assert(VT.isInteger() && "This operator does not apply to FP types!");
3232 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003233 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003234 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3235 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003236 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003237 return N1;
3238 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003239 case ISD::UDIV:
3240 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003241 case ISD::MULHU:
3242 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003243 case ISD::MUL:
3244 case ISD::SDIV:
3245 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003246 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003247 assert(N1.getValueType() == N2.getValueType() &&
3248 N1.getValueType() == VT && "Binary operator types must match!");
3249 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003250 case ISD::FADD:
3251 case ISD::FSUB:
3252 case ISD::FMUL:
3253 case ISD::FDIV:
3254 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003255 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003256 if (Opcode == ISD::FADD) {
3257 // 0+x --> x
3258 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3259 if (CFP->getValueAPF().isZero())
3260 return N2;
3261 // x+0 --> x
3262 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3263 if (CFP->getValueAPF().isZero())
3264 return N1;
3265 } else if (Opcode == ISD::FSUB) {
3266 // x-0 --> x
3267 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3268 if (CFP->getValueAPF().isZero())
3269 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003270 } else if (Opcode == ISD::FMUL) {
3271 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3272 SDValue V = N2;
3273
3274 // If the first operand isn't the constant, try the second
3275 if (!CFP) {
3276 CFP = dyn_cast<ConstantFPSDNode>(N2);
3277 V = N1;
3278 }
3279
3280 if (CFP) {
3281 // 0*x --> 0
3282 if (CFP->isZero())
3283 return SDValue(CFP,0);
3284 // 1*x --> x
3285 if (CFP->isExactlyValue(1.0))
3286 return V;
3287 }
Dan Gohman1275e282009-01-23 19:10:37 +00003288 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003289 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003290 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003291 assert(N1.getValueType() == N2.getValueType() &&
3292 N1.getValueType() == VT && "Binary operator types must match!");
3293 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003294 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3295 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003296 N1.getValueType().isFloatingPoint() &&
3297 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003298 "Invalid FCOPYSIGN!");
3299 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003300 case ISD::SHL:
3301 case ISD::SRA:
3302 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003303 case ISD::ROTL:
3304 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003305 assert(VT == N1.getValueType() &&
3306 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003307 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003308 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003309 assert((!VT.isVector() || VT == N2.getValueType()) &&
3310 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003311 // Verify that the shift amount VT is bit enough to hold valid shift
3312 // amounts. This catches things like trying to shift an i1024 value by an
3313 // i8, which is easy to fall into in generic code that uses
3314 // TLI.getShiftAmount().
3315 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003316 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003317 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003318
3319 // Always fold shifts of i1 values so the code generator doesn't need to
3320 // handle them. Since we know the size of the shift has to be less than the
3321 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003322 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003323 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003324 if (N2C && N2C->isNullValue())
3325 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003326 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003327 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003328 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003329 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003330 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003331 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003332 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003333 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003334 "type is vector!");
3335 assert((!EVT.isVector() ||
3336 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3337 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003338 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003339 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003340 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003341 break;
3342 }
Chris Lattner72733e52008-01-17 07:00:52 +00003343 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003344 assert(VT.isFloatingPoint() &&
3345 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003346 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003347 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003348 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003349 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003350 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003351 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003352 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003353 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003354 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003355 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003356 assert(!EVT.isVector() &&
3357 "AssertSExt/AssertZExt type should be the vector element type "
3358 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003359 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003360 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003361 break;
3362 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003363 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003364 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003365 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003366 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003367 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003368 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003369 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003370 "type is vector!");
3371 assert((!EVT.isVector() ||
3372 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3373 "Vector element counts must match in SIGN_EXTEND_INREG");
3374 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003375 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003376
Chris Lattner16713612008-01-22 19:09:33 +00003377 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003378 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003379 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003380 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003381 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003382 return getConstant(Val, VT);
3383 }
Chris Lattner16713612008-01-22 19:09:33 +00003384 break;
3385 }
3386 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003387 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3388 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003389 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003390
Chris Lattner16713612008-01-22 19:09:33 +00003391 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3392 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003393 if (N2C &&
3394 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003395 N1.getNumOperands() > 0) {
3396 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003397 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003398 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003399 N1.getOperand(N2C->getZExtValue() / Factor),
3400 getConstant(N2C->getZExtValue() % Factor,
3401 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003402 }
3403
3404 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3405 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003406 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3407 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003408
3409 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003410 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003411 // are promoted and implicitly truncated, and the result implicitly
3412 // extended. Make that explicit here.
3413 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003414
Bob Wilson59dbbb22009-04-13 22:05:19 +00003415 return Elt;
3416 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003417
Chris Lattner16713612008-01-22 19:09:33 +00003418 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3419 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003420 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003421 // If the indices are the same, return the inserted element else
3422 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003423 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003424 SDValue N1Op2 = N1.getOperand(2);
3425 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3426
3427 if (N1Op2C && N2C) {
3428 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3429 if (VT == N1.getOperand(1).getValueType())
3430 return N1.getOperand(1);
3431 else
3432 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3433 }
3434
Dale Johannesendb393622009-02-03 01:55:44 +00003435 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003436 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003437 }
Chris Lattner16713612008-01-22 19:09:33 +00003438 break;
3439 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003440 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003441 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3442 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003443 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003444 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003445
Chris Lattner16713612008-01-22 19:09:33 +00003446 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3447 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003448 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003449 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003450 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003451
Chris Lattner16713612008-01-22 19:09:33 +00003452 // EXTRACT_ELEMENT of a constant int is also very common.
3453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003454 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003455 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003456 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3457 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003458 }
3459 break;
David Greeneb6f16112011-01-26 15:38:49 +00003460 case ISD::EXTRACT_SUBVECTOR: {
3461 SDValue Index = N2;
3462 if (VT.isSimple() && N1.getValueType().isSimple()) {
3463 assert(VT.isVector() && N1.getValueType().isVector() &&
3464 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003465 assert(VT.getVectorElementType() ==
3466 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003467 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003468 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003469 "Extract subvector must be from larger vector to smaller vector!");
3470
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003471 if (isa<ConstantSDNode>(Index.getNode())) {
3472 assert((VT.getVectorNumElements() +
3473 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003474 <= N1.getValueType().getVectorNumElements())
3475 && "Extract subvector overflow!");
3476 }
3477
3478 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003479 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003480 return N1;
3481 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003482 break;
Chris Lattner16713612008-01-22 19:09:33 +00003483 }
David Greeneb6f16112011-01-26 15:38:49 +00003484 }
Chris Lattner16713612008-01-22 19:09:33 +00003485
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003486 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003487 if (SDValue SV =
3488 FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode()))
3489 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003490
3491 // Canonicalize constant to RHS if commutative.
3492 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3493 std::swap(N1C, N2C);
3494 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003495 }
3496
Chris Lattner16713612008-01-22 19:09:33 +00003497 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003498 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003499 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3500 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003501 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003502 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003503 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003504 std::swap(N1CFP, N2CFP);
3505 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003506 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003507 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3508 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003509 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003510 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003511 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003512 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003513 return getConstantFP(V1, VT);
3514 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003515 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003516 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003517 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003518 return getConstantFP(V1, VT);
3519 break;
3520 case ISD::FMUL:
3521 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003522 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003523 return getConstantFP(V1, VT);
3524 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003525 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003526 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003527 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3528 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003529 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003530 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003531 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003532 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003533 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003534 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3535 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003536 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003537 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003538 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003539 case ISD::FCOPYSIGN:
3540 V1.copySign(V2);
3541 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003542 default: break;
3543 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003544 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003545
3546 if (Opcode == ISD::FP_ROUND) {
3547 APFloat V = N1CFP->getValueAPF(); // make copy
3548 bool ignored;
3549 // This can return overflow, underflow, or inexact; we don't care.
3550 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003551 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003552 APFloat::rmNearestTiesToEven, &ignored);
3553 return getConstantFP(V, VT);
3554 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003555 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003556
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003557 // Canonicalize an UNDEF to the RHS, even over a constant.
3558 if (N1.getOpcode() == ISD::UNDEF) {
3559 if (isCommutativeBinOp(Opcode)) {
3560 std::swap(N1, N2);
3561 } else {
3562 switch (Opcode) {
3563 case ISD::FP_ROUND_INREG:
3564 case ISD::SIGN_EXTEND_INREG:
3565 case ISD::SUB:
3566 case ISD::FSUB:
3567 case ISD::FDIV:
3568 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003569 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003570 return N1; // fold op(undef, arg2) -> undef
3571 case ISD::UDIV:
3572 case ISD::SDIV:
3573 case ISD::UREM:
3574 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003575 case ISD::SRL:
3576 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003577 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003578 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3579 // For vectors, we can't easily build an all zero vector, just return
3580 // the LHS.
3581 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003582 }
3583 }
3584 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003585
3586 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003587 if (N2.getOpcode() == ISD::UNDEF) {
3588 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003589 case ISD::XOR:
3590 if (N1.getOpcode() == ISD::UNDEF)
3591 // Handle undef ^ undef -> 0 special case. This is a common
3592 // idiom (misuse).
3593 return getConstant(0, VT);
3594 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003595 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003596 case ISD::ADDC:
3597 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003598 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003599 case ISD::UDIV:
3600 case ISD::SDIV:
3601 case ISD::UREM:
3602 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003603 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003604 case ISD::FADD:
3605 case ISD::FSUB:
3606 case ISD::FMUL:
3607 case ISD::FDIV:
3608 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003609 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003610 return N2;
3611 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003612 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003613 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003614 case ISD::SRL:
3615 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003616 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003617 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3618 // For vectors, we can't easily build an all zero vector, just return
3619 // the LHS.
3620 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003621 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003622 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003623 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003624 // For vectors, we can't easily build an all one vector, just return
3625 // the LHS.
3626 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003627 case ISD::SRA:
3628 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003629 }
3630 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003631
Chris Lattner724f7ee2005-05-11 18:57:39 +00003632 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003633 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003634 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003635 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003636 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003637 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003638 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003639 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003640 if (BinOpHasFlags)
3641 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003642 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003643 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003644 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003645
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003646 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3647
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003648 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003649 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003650 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003651 }
3652
Chandler Carruth41b20e72014-07-22 04:07:55 +00003653 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003654 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003655}
3656
Andrew Trickef9de2a2013-05-25 02:42:55 +00003657SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003658 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003659 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003660 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003661 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003662 case ISD::FMA: {
3663 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3664 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3665 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3666 if (N1CFP && N2CFP && N3CFP) {
3667 APFloat V1 = N1CFP->getValueAPF();
3668 const APFloat &V2 = N2CFP->getValueAPF();
3669 const APFloat &V3 = N3CFP->getValueAPF();
3670 APFloat::opStatus s =
3671 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003672 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Owen Anderson32baf992013-05-09 22:27:13 +00003673 return getConstantFP(V1, VT);
3674 }
3675 break;
3676 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003677 case ISD::CONCAT_VECTORS:
3678 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3679 // one big BUILD_VECTOR.
3680 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3681 N2.getOpcode() == ISD::BUILD_VECTOR &&
3682 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003683 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3684 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003685 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3686 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003687 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003688 }
3689 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003690 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003691 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003692 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003693 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003694 break;
3695 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003696 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003697 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003698 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003699 return N2; // select true, X, Y -> X
3700 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003701 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003702
3703 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003704 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003705 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003706 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003707 case ISD::INSERT_SUBVECTOR: {
3708 SDValue Index = N3;
3709 if (VT.isSimple() && N1.getValueType().isSimple()
3710 && N2.getValueType().isSimple()) {
3711 assert(VT.isVector() && N1.getValueType().isVector() &&
3712 N2.getValueType().isVector() &&
3713 "Insert subvector VTs must be a vectors");
3714 assert(VT == N1.getValueType() &&
3715 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003716 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003717 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003718 if (isa<ConstantSDNode>(Index.getNode())) {
3719 assert((N2.getValueType().getVectorNumElements() +
3720 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003721 <= VT.getVectorNumElements())
3722 && "Insert subvector overflow!");
3723 }
3724
3725 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003726 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003727 return N2;
3728 }
3729 break;
3730 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003731 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003732 // Fold bit_convert nodes from a type to themselves.
3733 if (N1.getValueType() == VT)
3734 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003735 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003736 }
3737
Chris Lattnerf9c19152005-08-25 19:12:10 +00003738 // Memoize node if it doesn't produce a flag.
3739 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003740 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003741 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003742 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003743 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003744 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003745 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003746 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003747 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003748
Jack Carter170a5f22013-09-09 22:02:08 +00003749 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3750 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003751 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003752 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003753 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3754 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003755 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003756
Chandler Carruth41b20e72014-07-22 04:07:55 +00003757 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003758 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003759}
3760
Andrew Trickef9de2a2013-05-25 02:42:55 +00003761SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003762 SDValue N1, SDValue N2, SDValue N3,
3763 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003764 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003765 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003766}
3767
Andrew Trickef9de2a2013-05-25 02:42:55 +00003768SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003769 SDValue N1, SDValue N2, SDValue N3,
3770 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003771 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003772 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003773}
3774
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003775/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3776/// the incoming stack arguments to be loaded from the stack.
3777SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3778 SmallVector<SDValue, 8> ArgChains;
3779
3780 // Include the original chain at the beginning of the list. When this is
3781 // used by target LowerCall hooks, this helps legalize find the
3782 // CALLSEQ_BEGIN node.
3783 ArgChains.push_back(Chain);
3784
3785 // Add a chain value for each stack argument.
3786 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3787 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3788 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3789 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3790 if (FI->getIndex() < 0)
3791 ArgChains.push_back(SDValue(L, 1));
3792
3793 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003794 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003795}
3796
Dan Gohman544ab2c2008-04-12 04:36:06 +00003797/// getMemsetValue - Vectorized representation of the memset value
3798/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003799static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003800 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003801 assert(Value.getOpcode() != ISD::UNDEF);
3802
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003803 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003804 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003805 assert(C->getAPIntValue().getBitWidth() == 8);
3806 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003807 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003808 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003809 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003810 }
Evan Chengef377ad2008-05-15 08:39:06 +00003811
Hal Finkel17b6d772015-03-31 20:35:26 +00003812 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
3813 EVT IntVT = VT.getScalarType();
3814 if (!IntVT.isInteger())
3815 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
3816
3817 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003818 if (NumBits > 8) {
3819 // Use a multiplication with 0x010101... to extend the input to the
3820 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003821 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Hal Finkel17b6d772015-03-31 20:35:26 +00003822 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
3823 DAG.getConstant(Magic, IntVT));
3824 }
3825
3826 if (VT != Value.getValueType() && !VT.isInteger())
3827 Value = DAG.getNode(ISD::BITCAST, dl, VT.getScalarType(), Value);
3828 if (VT != Value.getValueType()) {
3829 assert(VT.getVectorElementType() == Value.getValueType() &&
3830 "value type should be one vector element here");
3831 SmallVector<SDValue, 8> BVOps(VT.getVectorNumElements(), Value);
3832 Value = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, BVOps);
Evan Chengef377ad2008-05-15 08:39:06 +00003833 }
3834
3835 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003836}
3837
Dan Gohman544ab2c2008-04-12 04:36:06 +00003838/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3839/// used when a memcpy is turned into a memset when the source is a constant
3840/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003841static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003842 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003843 // Handle vector with all elements zero.
3844 if (Str.empty()) {
3845 if (VT.isInteger())
3846 return DAG.getConstant(0, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003847 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003848 return DAG.getConstantFP(0.0, VT);
3849 else if (VT.isVector()) {
3850 unsigned NumElts = VT.getVectorNumElements();
3851 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003852 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003853 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3854 EltVT, NumElts)));
3855 } else
3856 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003857 }
3858
Duncan Sands13237ac2008-06-06 12:08:01 +00003859 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003860 unsigned NumVTBits = VT.getSizeInBits();
3861 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003862 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3863
Evan Chengc8444b12013-01-10 22:13:27 +00003864 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003865 if (TLI.isLittleEndian()) {
3866 for (unsigned i = 0; i != NumBytes; ++i)
3867 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3868 } else {
3869 for (unsigned i = 0; i != NumBytes; ++i)
3870 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003871 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003872
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003873 // If the "cost" of materializing the integer immediate is less than the cost
3874 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003875 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3876 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003877 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003878 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003879}
3880
Scott Michelcf0da6c2009-02-17 22:15:04 +00003881/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003882///
Andrew Tricke2431c62013-05-25 03:08:10 +00003883static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003884 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003885 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003886 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003887 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003888}
3889
Evan Chengef377ad2008-05-15 08:39:06 +00003890/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3891///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003892static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003893 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003894 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003895 if (Src.getOpcode() == ISD::GlobalAddress)
3896 G = cast<GlobalAddressSDNode>(Src);
3897 else if (Src.getOpcode() == ISD::ADD &&
3898 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3899 Src.getOperand(1).getOpcode() == ISD::Constant) {
3900 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003901 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003902 }
3903 if (!G)
3904 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003905
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003906 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003907}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003908
Evan Cheng43cd9e32010-04-01 06:04:33 +00003909/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3910/// to replace the memset / memcpy. Return true if the number of memory ops
3911/// is below the threshold. It returns the types of the sequence of
3912/// memory ops to perform memset / memcpy by reference.
3913static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003914 unsigned Limit, uint64_t Size,
3915 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003916 bool IsMemset,
3917 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003918 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003919 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003920 SelectionDAG &DAG,
3921 const TargetLowering &TLI) {
3922 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3923 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003924 // If 'SrcAlign' is zero, that means the memory operation does not need to
3925 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3926 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3927 // is the specified alignment of the memory operation. If it is zero, that
3928 // means it's possible to change the alignment of the destination.
3929 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3930 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003931 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003932 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003933 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003934
Chris Lattner28dc6c12010-03-07 07:45:08 +00003935 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003936 unsigned AS = 0;
3937 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003938 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003939 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003940 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003941 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003942 case 0: VT = MVT::i64; break;
3943 case 4: VT = MVT::i32; break;
3944 case 2: VT = MVT::i16; break;
3945 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003946 }
3947 }
3948
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003949 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003950 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003951 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003952 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003953
Duncan Sands11dd4242008-06-08 20:54:56 +00003954 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003955 VT = LVT;
3956 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003957
Dan Gohman544ab2c2008-04-12 04:36:06 +00003958 unsigned NumMemOps = 0;
3959 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003960 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003961 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003962 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003963 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003964 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003965
3966 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003967 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003968 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003969 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003970 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003971 Found = true;
3972 else if (NewVT == MVT::i64 &&
3973 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003974 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003975 // i64 is usually not legal on 32-bit targets, but f64 may be.
3976 NewVT = MVT::f64;
3977 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003978 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003979 }
3980
Evan Cheng04e55182012-12-12 00:42:09 +00003981 if (!Found) {
3982 do {
3983 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3984 if (NewVT == MVT::i8)
3985 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003986 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003987 }
3988 NewVTSize = NewVT.getSizeInBits() / 8;
3989
Evan Cheng79e2ca92012-12-10 23:21:26 +00003990 // If the new VT cannot cover all of the remaining bits, then consider
3991 // issuing a (or a pair of) unaligned and overlapping load / store.
3992 // FIXME: Only does this for 64-bit or more since we don't have proper
3993 // cost model for unaligned load / store.
3994 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003995 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003996 if (NumMemOps && AllowOverlap &&
3997 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003998 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003999 VTSize = Size;
4000 else {
4001 VT = NewVT;
4002 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00004003 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004004 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004005
Evan Chengb7d3d032012-12-12 20:43:23 +00004006 if (++NumMemOps > Limit)
4007 return false;
4008
Dan Gohman544ab2c2008-04-12 04:36:06 +00004009 MemOps.push_back(VT);
4010 Size -= VTSize;
4011 }
4012
4013 return true;
4014}
4015
Andrew Trickef9de2a2013-05-25 02:42:55 +00004016static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00004017 SDValue Chain, SDValue Dst,
4018 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004019 unsigned Align, bool isVol,
4020 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004021 MachinePointerInfo DstPtrInfo,
4022 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004023 // Turn a memcpy of undef to nop.
4024 if (Src.getOpcode() == ISD::UNDEF)
4025 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004026
Dan Gohman714663a2008-05-29 19:42:22 +00004027 // Expand memcpy to a series of load and store ops if the size operand falls
4028 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00004029 // TODO: In the AlwaysInline case, if the size is big then generate a loop
4030 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00004031 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004032 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004033 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004034 MachineFunction &MF = DAG.getMachineFunction();
4035 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004036 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004037 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4038 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4039 DstAlignCanChange = true;
4040 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4041 if (Align > SrcAlign)
4042 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004043 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004044 bool CopyFromStr = isMemSrcFromString(Src, Str);
4045 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004046 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00004047
Evan Cheng4c014c82010-04-01 18:19:11 +00004048 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004049 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00004050 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00004051 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004052 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004053
Evan Cheng43cd9e32010-04-01 06:04:33 +00004054 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004055 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004056 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00004057
4058 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00004059 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004060 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004061 if (!TRI->needsStackRealignment(MF))
4062 while (NewAlign > Align &&
4063 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4064 NewAlign /= 2;
4065
Evan Cheng43cd9e32010-04-01 06:04:33 +00004066 if (NewAlign > Align) {
4067 // Give the stack frame object a larger alignment if needed.
4068 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4069 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4070 Align = NewAlign;
4071 }
4072 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004073
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004074 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004075 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004076 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004077 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004078 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004079 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004080 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004081
Evan Cheng79e2ca92012-12-10 23:21:26 +00004082 if (VTSize > Size) {
4083 // Issuing an unaligned load / store pair that overlaps with the previous
4084 // pair. Adjust the offset accordingly.
4085 assert(i == NumMemOps-1 && i != 0);
4086 SrcOff -= VTSize - Size;
4087 DstOff -= VTSize - Size;
4088 }
4089
Evan Cheng43cd9e32010-04-01 06:04:33 +00004090 if (CopyFromStr &&
4091 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004092 // It's unlikely a store of a vector immediate can be done in a single
4093 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004094 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004095 // FIXME: Handle other cases where store of vector immediate is done in
4096 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004097 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004098 if (Value.getNode())
4099 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004100 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004101 DstPtrInfo.getWithOffset(DstOff), isVol,
4102 false, Align);
4103 }
4104
4105 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004106 // The type might not be legal for the target. This should only happen
4107 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004108 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4109 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004110 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004111 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004112 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004113 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004114 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004115 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004116 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004117 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004118 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004119 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4120 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004121 }
4122 OutChains.push_back(Store);
4123 SrcOff += VTSize;
4124 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004125 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004126 }
4127
Craig Topper48d114b2014-04-26 18:35:24 +00004128 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004129}
4130
Andrew Trickef9de2a2013-05-25 02:42:55 +00004131static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004132 SDValue Chain, SDValue Dst,
4133 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004134 unsigned Align, bool isVol,
4135 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004136 MachinePointerInfo DstPtrInfo,
4137 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004138 // Turn a memmove of undef to nop.
4139 if (Src.getOpcode() == ISD::UNDEF)
4140 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004141
4142 // Expand memmove to a series of load and store ops if the size operand falls
4143 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004144 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004145 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004146 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004147 MachineFunction &MF = DAG.getMachineFunction();
4148 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004149 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004150 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4151 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4152 DstAlignCanChange = true;
4153 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4154 if (Align > SrcAlign)
4155 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004156 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004157
Evan Cheng4c014c82010-04-01 18:19:11 +00004158 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004159 (DstAlignCanChange ? 0 : Align), SrcAlign,
4160 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004161 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004162
Evan Cheng43cd9e32010-04-01 06:04:33 +00004163 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004164 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004165 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004166 if (NewAlign > Align) {
4167 // Give the stack frame object a larger alignment if needed.
4168 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4169 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4170 Align = NewAlign;
4171 }
4172 }
Dan Gohman714663a2008-05-29 19:42:22 +00004173
Evan Cheng43cd9e32010-04-01 06:04:33 +00004174 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004175 SmallVector<SDValue, 8> LoadValues;
4176 SmallVector<SDValue, 8> LoadChains;
4177 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004178 unsigned NumMemOps = MemOps.size();
4179 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004180 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004181 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004182 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004183
Dale Johannesenabf66b82009-02-03 22:26:09 +00004184 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004185 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004186 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004187 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004188 LoadValues.push_back(Value);
4189 LoadChains.push_back(Value.getValue(1));
4190 SrcOff += VTSize;
4191 }
Craig Topper48d114b2014-04-26 18:35:24 +00004192 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004193 OutChains.clear();
4194 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004195 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004196 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004197 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004198
Dale Johannesenabf66b82009-02-03 22:26:09 +00004199 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004200 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004201 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004202 OutChains.push_back(Store);
4203 DstOff += VTSize;
4204 }
4205
Craig Topper48d114b2014-04-26 18:35:24 +00004206 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004207}
4208
Serge Pavlov8ec39992013-09-17 16:24:42 +00004209/// \brief Lower the call to 'memset' intrinsic function into a series of store
4210/// operations.
4211///
4212/// \param DAG Selection DAG where lowered code is placed.
4213/// \param dl Link to corresponding IR location.
4214/// \param Chain Control flow dependency.
4215/// \param Dst Pointer to destination memory location.
4216/// \param Src Value of byte to write into the memory.
4217/// \param Size Number of bytes to write.
4218/// \param Align Alignment of the destination in bytes.
4219/// \param isVol True if destination is volatile.
4220/// \param DstPtrInfo IR information on the memory pointer.
4221/// \returns New head in the control flow, if lowering was successful, empty
4222/// SDValue otherwise.
4223///
4224/// The function tries to replace 'llvm.memset' intrinsic with several store
4225/// operations and value calculation code. This is usually profitable for small
4226/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004227static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004228 SDValue Chain, SDValue Dst,
4229 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004230 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004231 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004232 // Turn a memset of undef to nop.
4233 if (Src.getOpcode() == ISD::UNDEF)
4234 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004235
4236 // Expand memset to a series of load/store ops if the size operand
4237 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004238 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004239 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004240 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004241 MachineFunction &MF = DAG.getMachineFunction();
4242 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004243 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004244 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4245 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4246 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004247 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004248 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004249 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004250 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004251 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004252 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004253
Evan Cheng43cd9e32010-04-01 06:04:33 +00004254 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004255 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004256 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004257 if (NewAlign > Align) {
4258 // Give the stack frame object a larger alignment if needed.
4259 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4260 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4261 Align = NewAlign;
4262 }
4263 }
4264
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004265 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004266 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004267 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004268
4269 // Find the largest store and generate the bit pattern for it.
4270 EVT LargestVT = MemOps[0];
4271 for (unsigned i = 1; i < NumMemOps; i++)
4272 if (MemOps[i].bitsGT(LargestVT))
4273 LargestVT = MemOps[i];
4274 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4275
Dan Gohman544ab2c2008-04-12 04:36:06 +00004276 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004277 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004278 unsigned VTSize = VT.getSizeInBits() / 8;
4279 if (VTSize > Size) {
4280 // Issuing an unaligned load / store pair that overlaps with the previous
4281 // pair. Adjust the offset accordingly.
4282 assert(i == NumMemOps-1 && i != 0);
4283 DstOff -= VTSize - Size;
4284 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004285
4286 // If this store is smaller than the largest store see whether we can get
4287 // the smaller value for free with a truncate.
4288 SDValue Value = MemSetValue;
4289 if (VT.bitsLT(LargestVT)) {
4290 if (!LargestVT.isVector() && !VT.isVector() &&
4291 TLI.isTruncateFree(LargestVT, VT))
4292 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4293 else
4294 Value = getMemsetValue(Src, VT, DAG, dl);
4295 }
4296 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004297 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004298 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004299 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004300 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004301 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004302 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004303 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004304 }
4305
Craig Topper48d114b2014-04-26 18:35:24 +00004306 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004307}
4308
Andrew Trickef9de2a2013-05-25 02:42:55 +00004309SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004310 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004311 unsigned Align, bool isVol, bool AlwaysInline,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004312 bool isTailCall, MachinePointerInfo DstPtrInfo,
Chris Lattner2510de22010-09-21 05:40:29 +00004313 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004314 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004315
4316 // Check to see if we should lower the memcpy to loads and stores first.
4317 // For cases within the target-specified limits, this is the best choice.
4318 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4319 if (ConstantSize) {
4320 // Memcpy with size zero? Just return the original chain.
4321 if (ConstantSize->isNullValue())
4322 return Chain;
4323
Evan Cheng43cd9e32010-04-01 06:04:33 +00004324 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4325 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004326 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004327 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004328 return Result;
4329 }
4330
4331 // Then check to see if we should lower the memcpy with target-specific
4332 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004333 if (TSI) {
4334 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4335 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4336 DstPtrInfo, SrcPtrInfo);
4337 if (Result.getNode())
4338 return Result;
4339 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004340
4341 // If we really need inline code and the target declined to provide it,
4342 // use a (potentially long) sequence of loads and stores.
4343 if (AlwaysInline) {
4344 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004345 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004346 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004347 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004348 }
4349
Dan Gohmanf38547c2010-04-05 20:24:08 +00004350 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4351 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4352 // respect volatile, so they may do things like read or write memory
4353 // beyond the given memory regions. But fixing this isn't easy, and most
4354 // people don't care.
4355
Dan Gohman544ab2c2008-04-12 04:36:06 +00004356 // Emit a library call.
4357 TargetLowering::ArgListTy Args;
4358 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004359 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004360 Entry.Node = Dst; Args.push_back(Entry);
4361 Entry.Node = Src; Args.push_back(Entry);
4362 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004363 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004364 TargetLowering::CallLoweringInfo CLI(*this);
4365 CLI.setDebugLoc(dl).setChain(Chain)
4366 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4367 Type::getVoidTy(*getContext()),
4368 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004369 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004370 .setDiscardResult()
4371 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004372
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004373 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004374 return CallResult.second;
4375}
4376
Andrew Trickef9de2a2013-05-25 02:42:55 +00004377SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004378 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004379 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004380 MachinePointerInfo DstPtrInfo,
4381 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004382 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004383
Dan Gohman714663a2008-05-29 19:42:22 +00004384 // Check to see if we should lower the memmove to loads and stores first.
4385 // For cases within the target-specified limits, this is the best choice.
4386 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4387 if (ConstantSize) {
4388 // Memmove with size zero? Just return the original chain.
4389 if (ConstantSize->isNullValue())
4390 return Chain;
4391
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004392 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004393 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004394 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004395 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004396 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004397 return Result;
4398 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004399
4400 // Then check to see if we should lower the memmove with target-specific
4401 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004402 if (TSI) {
4403 SDValue Result = TSI->EmitTargetCodeForMemmove(
4404 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4405 if (Result.getNode())
4406 return Result;
4407 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004408
Mon P Wangbf862242010-04-06 08:27:51 +00004409 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4410 // not be safe. See memcpy above for more details.
4411
Dan Gohman544ab2c2008-04-12 04:36:06 +00004412 // Emit a library call.
4413 TargetLowering::ArgListTy Args;
4414 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004415 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004416 Entry.Node = Dst; Args.push_back(Entry);
4417 Entry.Node = Src; Args.push_back(Entry);
4418 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004419 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004420 TargetLowering::CallLoweringInfo CLI(*this);
4421 CLI.setDebugLoc(dl).setChain(Chain)
4422 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4423 Type::getVoidTy(*getContext()),
4424 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004425 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004426 .setDiscardResult()
4427 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004428
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004429 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004430 return CallResult.second;
4431}
4432
Andrew Trickef9de2a2013-05-25 02:42:55 +00004433SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004434 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004435 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004436 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004437 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004438
4439 // Check to see if we should lower the memset to stores first.
4440 // For cases within the target-specified limits, this is the best choice.
4441 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4442 if (ConstantSize) {
4443 // Memset with size zero? Just return the original chain.
4444 if (ConstantSize->isNullValue())
4445 return Chain;
4446
Mon P Wangc576ee92010-04-04 03:10:48 +00004447 SDValue Result =
4448 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004449 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004450
Gabor Greiff304a7a2008-08-28 21:40:38 +00004451 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004452 return Result;
4453 }
4454
4455 // Then check to see if we should lower the memset with target-specific
4456 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004457 if (TSI) {
4458 SDValue Result = TSI->EmitTargetCodeForMemset(
4459 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4460 if (Result.getNode())
4461 return Result;
4462 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004463
Wesley Peck527da1b2010-11-23 03:31:01 +00004464 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004465 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004466 TargetLowering::ArgListTy Args;
4467 TargetLowering::ArgListEntry Entry;
4468 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4469 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004470 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004471 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004472 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004473 Entry.Node = Size;
4474 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004475 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004476
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004477 // FIXME: pass in SDLoc
4478 TargetLowering::CallLoweringInfo CLI(*this);
4479 CLI.setDebugLoc(dl).setChain(Chain)
4480 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4481 Type::getVoidTy(*getContext()),
4482 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004483 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004484 .setDiscardResult()
4485 .setTailCall(isTailCall);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004486
4487 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004488 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004489}
4490
Andrew Trickef9de2a2013-05-25 02:42:55 +00004491SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004492 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004493 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004494 AtomicOrdering SuccessOrdering,
4495 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004496 SynchronizationScope SynchScope) {
4497 FoldingSetNodeID ID;
4498 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004499 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004500 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004501 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004502 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4503 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4504 return SDValue(E, 0);
4505 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004506
4507 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4508 // SDNode doesn't have access to it. This memory will be "leaked" when
4509 // the node is deallocated, but recovered when the allocator is released.
4510 // If the number of operands is less than 5 we use AtomicSDNode's internal
4511 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004512 unsigned NumOps = Ops.size();
4513 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4514 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004515
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004516 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4517 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004518 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004519 SuccessOrdering, FailureOrdering,
4520 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004521 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004522 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004523 return SDValue(N, 0);
4524}
4525
4526SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004527 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004528 MachineMemOperand *MMO,
4529 AtomicOrdering Ordering,
4530 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004531 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004532 Ordering, SynchScope);
4533}
4534
Tim Northover420a2162014-06-13 14:24:07 +00004535SDValue SelectionDAG::getAtomicCmpSwap(
4536 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4537 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4538 unsigned Alignment, AtomicOrdering SuccessOrdering,
4539 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4540 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4541 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4542 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4543
Dan Gohman48b185d2009-09-25 20:36:54 +00004544 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4545 Alignment = getEVTAlignment(MemVT);
4546
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004547 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004548
Eli Friedmane978d2f2011-09-07 02:23:42 +00004549 // FIXME: Volatile isn't really correct; we should keep track of atomic
4550 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004551 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004552 Flags |= MachineMemOperand::MOLoad;
4553 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004554
4555 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004556 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004557
Tim Northover420a2162014-06-13 14:24:07 +00004558 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4559 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004560}
4561
Tim Northover420a2162014-06-13 14:24:07 +00004562SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4563 SDVTList VTs, SDValue Chain, SDValue Ptr,
4564 SDValue Cmp, SDValue Swp,
4565 MachineMemOperand *MMO,
4566 AtomicOrdering SuccessOrdering,
4567 AtomicOrdering FailureOrdering,
4568 SynchronizationScope SynchScope) {
4569 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4570 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004571 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4572
Dale Johannesen839acbb2009-01-29 00:47:48 +00004573 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004574 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4575 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004576}
4577
Andrew Trickef9de2a2013-05-25 02:42:55 +00004578SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004579 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004580 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004581 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004582 unsigned Alignment,
4583 AtomicOrdering Ordering,
4584 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004585 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4586 Alignment = getEVTAlignment(MemVT);
4587
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004588 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004589 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004590 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004591 // For now, atomics are considered to be volatile always, and they are
4592 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004593 // FIXME: Volatile isn't really correct; we should keep track of atomic
4594 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004595 unsigned Flags = MachineMemOperand::MOVolatile;
4596 if (Opcode != ISD::ATOMIC_STORE)
4597 Flags |= MachineMemOperand::MOLoad;
4598 if (Opcode != ISD::ATOMIC_LOAD)
4599 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004600
4601 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004602 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004603 MemVT.getStoreSize(), Alignment);
4604
Eli Friedmanadec5872011-07-29 03:05:32 +00004605 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4606 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004607}
4608
Andrew Trickef9de2a2013-05-25 02:42:55 +00004609SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004610 SDValue Chain,
4611 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004612 MachineMemOperand *MMO,
4613 AtomicOrdering Ordering,
4614 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004615 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4616 Opcode == ISD::ATOMIC_LOAD_SUB ||
4617 Opcode == ISD::ATOMIC_LOAD_AND ||
4618 Opcode == ISD::ATOMIC_LOAD_OR ||
4619 Opcode == ISD::ATOMIC_LOAD_XOR ||
4620 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004621 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004622 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004623 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004624 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004625 Opcode == ISD::ATOMIC_SWAP ||
4626 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004627 "Invalid Atomic Op");
4628
Owen Anderson53aa7a92009-08-10 22:56:29 +00004629 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004630
Eli Friedman342e8df2011-08-24 20:50:09 +00004631 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4632 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004633 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004634 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004635}
4636
Andrew Trickef9de2a2013-05-25 02:42:55 +00004637SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004638 EVT VT, SDValue Chain,
4639 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004640 MachineMemOperand *MMO,
4641 AtomicOrdering Ordering,
4642 SynchronizationScope SynchScope) {
4643 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4644
4645 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004646 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004647 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004648}
4649
Duncan Sands739a0542008-07-02 17:40:58 +00004650/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004651SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4652 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004653 return Ops[0];
4654
Owen Anderson53aa7a92009-08-10 22:56:29 +00004655 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004656 VTs.reserve(Ops.size());
4657 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004658 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004659 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004660}
4661
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004662SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004663SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004664 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004665 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004666 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004667 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004668 if (Align == 0) // Ensure that codegen never sees alignment 0
4669 Align = getEVTAlignment(MemVT);
4670
4671 MachineFunction &MF = getMachineFunction();
4672 unsigned Flags = 0;
4673 if (WriteMem)
4674 Flags |= MachineMemOperand::MOStore;
4675 if (ReadMem)
4676 Flags |= MachineMemOperand::MOLoad;
4677 if (Vol)
4678 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004679 if (!Size)
4680 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004681 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004682 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004683
Craig Topper206fcd42014-04-26 19:29:41 +00004684 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004685}
4686
4687SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004688SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004689 ArrayRef<SDValue> Ops, EVT MemVT,
4690 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004691 assert((Opcode == ISD::INTRINSIC_VOID ||
4692 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004693 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004694 Opcode == ISD::LIFETIME_START ||
4695 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004696 (Opcode <= INT_MAX &&
4697 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4698 "Opcode is not a memory-accessing opcode!");
4699
Dale Johannesen839acbb2009-01-29 00:47:48 +00004700 // Memoize the node unless it returns a flag.
4701 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004702 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004703 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004704 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004705 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004706 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004707 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4708 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004709 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004710 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004711
Jack Carter170a5f22013-09-09 22:02:08 +00004712 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4713 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004714 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004715 CSEMap.InsertNode(N, IP);
4716 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004717 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4718 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004719 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004720 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004721 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004722 return SDValue(N, 0);
4723}
4724
Chris Lattnerea952f02010-09-21 17:24:05 +00004725/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4726/// MachinePointerInfo record from it. This is particularly useful because the
4727/// code generator has many cases where it doesn't bother passing in a
4728/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4729static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4730 // If this is FI+Offset, we can model it.
4731 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4732 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4733
4734 // If this is (FI+Offset1)+Offset2, we can model it.
4735 if (Ptr.getOpcode() != ISD::ADD ||
4736 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4737 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4738 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004739
Chris Lattnerea952f02010-09-21 17:24:05 +00004740 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4741 return MachinePointerInfo::getFixedStack(FI, Offset+
4742 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4743}
4744
4745/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4746/// MachinePointerInfo record from it. This is particularly useful because the
4747/// code generator has many cases where it doesn't bother passing in a
4748/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4749static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4750 // If the 'Offset' value isn't a constant, we can't handle this.
4751 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4752 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4753 if (OffsetOp.getOpcode() == ISD::UNDEF)
4754 return InferPointerInfo(Ptr);
4755 return MachinePointerInfo();
4756}
Wesley Peck527da1b2010-11-23 03:31:01 +00004757
Chris Lattnerea952f02010-09-21 17:24:05 +00004758
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004759SDValue
4760SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004761 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004762 SDValue Ptr, SDValue Offset,
4763 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004764 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004765 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004766 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004767 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004768 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004769 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4770 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004771
Dan Gohman48b185d2009-09-25 20:36:54 +00004772 unsigned Flags = MachineMemOperand::MOLoad;
4773 if (isVolatile)
4774 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004775 if (isNonTemporal)
4776 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004777 if (isInvariant)
4778 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004779
Chris Lattnerea952f02010-09-21 17:24:05 +00004780 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4781 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004782 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004783 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004784
Chris Lattnerea952f02010-09-21 17:24:05 +00004785 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004786 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004787 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004788 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004789 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004790}
4791
4792SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004793SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004794 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004795 SDValue Ptr, SDValue Offset, EVT MemVT,
4796 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004797 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004798 ExtType = ISD::NON_EXTLOAD;
4799 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004800 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004801 } else {
4802 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004803 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4804 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004805 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004806 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004807 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004808 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004809 assert((!VT.isVector() ||
4810 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004811 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004812 }
4813
4814 bool Indexed = AM != ISD::UNINDEXED;
4815 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4816 "Unindexed load with an offset!");
4817
4818 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004819 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004820 SDValue Ops[] = { Chain, Ptr, Offset };
4821 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004822 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004823 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004824 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004825 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004826 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004827 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004828 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004829 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4830 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004831 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004832 }
Jack Carter170a5f22013-09-09 22:02:08 +00004833 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4834 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004835 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004836 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004837 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004838 return SDValue(N, 0);
4839}
4840
Andrew Trickef9de2a2013-05-25 02:42:55 +00004841SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004842 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004843 MachinePointerInfo PtrInfo,
4844 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004845 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004846 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004847 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004848 SDValue Undef = getUNDEF(Ptr.getValueType());
4849 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004850 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004851 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004852}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004853
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004854SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4855 SDValue Chain, SDValue Ptr,
4856 MachineMemOperand *MMO) {
4857 SDValue Undef = getUNDEF(Ptr.getValueType());
4858 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4859 VT, MMO);
4860}
4861
Andrew Trickef9de2a2013-05-25 02:42:55 +00004862SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004863 SDValue Chain, SDValue Ptr,
4864 MachinePointerInfo PtrInfo, EVT MemVT,
4865 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004866 bool isInvariant, unsigned Alignment,
4867 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004868 SDValue Undef = getUNDEF(Ptr.getValueType());
4869 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004870 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4871 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004872}
4873
4874
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004875SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4876 SDValue Chain, SDValue Ptr, EVT MemVT,
4877 MachineMemOperand *MMO) {
4878 SDValue Undef = getUNDEF(Ptr.getValueType());
4879 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4880 MemVT, MMO);
4881}
4882
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004883SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004884SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004885 SDValue Offset, ISD::MemIndexedMode AM) {
4886 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4887 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4888 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004889 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004890 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004891 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004892 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004893}
4894
Andrew Trickef9de2a2013-05-25 02:42:55 +00004895SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004896 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004897 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004898 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004899 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004900 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004901 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004902 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004903
Dan Gohman48b185d2009-09-25 20:36:54 +00004904 unsigned Flags = MachineMemOperand::MOStore;
4905 if (isVolatile)
4906 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004907 if (isNonTemporal)
4908 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004909
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004910 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004911 PtrInfo = InferPointerInfo(Ptr);
4912
4913 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004914 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004915 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004916 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004917 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004918
4919 return getStore(Chain, dl, Val, Ptr, MMO);
4920}
4921
Andrew Trickef9de2a2013-05-25 02:42:55 +00004922SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004923 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004924 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004925 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004926 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004927 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004928 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004929 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4930 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004931 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004932 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004933 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004934 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004935 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004936 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004937 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4938 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004939 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004940 }
Jack Carter170a5f22013-09-09 22:02:08 +00004941 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4942 dl.getDebugLoc(), VTs,
4943 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004944 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004945 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004946 return SDValue(N, 0);
4947}
4948
Andrew Trickef9de2a2013-05-25 02:42:55 +00004949SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004950 SDValue Ptr, MachinePointerInfo PtrInfo,
4951 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004952 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004953 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004954 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004955 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004956 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4957 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004958
Dan Gohman48b185d2009-09-25 20:36:54 +00004959 unsigned Flags = MachineMemOperand::MOStore;
4960 if (isVolatile)
4961 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004962 if (isNonTemporal)
4963 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004964
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004965 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004966 PtrInfo = InferPointerInfo(Ptr);
4967
4968 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004969 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004970 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004971 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004972
4973 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4974}
4975
Andrew Trickef9de2a2013-05-25 02:42:55 +00004976SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004977 SDValue Ptr, EVT SVT,
4978 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004979 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004980
Michael Ilsemand5f91512012-09-10 16:56:31 +00004981 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004982 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004983 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004984 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004985
Dan Gohmancecad352009-12-14 23:40:38 +00004986 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4987 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004988 assert(VT.isInteger() == SVT.isInteger() &&
4989 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004990 assert(VT.isVector() == SVT.isVector() &&
4991 "Cannot use trunc store to convert to or from a vector!");
4992 assert((!VT.isVector() ||
4993 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4994 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004995
Owen Anderson9f944592009-08-11 20:47:22 +00004996 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004997 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004998 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4999 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005000 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005001 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00005002 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005003 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00005004 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005005 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00005006 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5007 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005008 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005009 }
Jack Carter170a5f22013-09-09 22:02:08 +00005010 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5011 dl.getDebugLoc(), VTs,
5012 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005013 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005014 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005015 return SDValue(N, 0);
5016}
5017
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005018SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005019SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00005020 SDValue Offset, ISD::MemIndexedMode AM) {
5021 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5022 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
5023 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00005024 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005025 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5026 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005027 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005028 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00005029 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00005030 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005031 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005032 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00005033 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005034
Jack Carter170a5f22013-09-09 22:02:08 +00005035 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5036 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00005037 ST->isTruncatingStore(),
5038 ST->getMemoryVT(),
5039 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005040 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005041 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005042 return SDValue(N, 0);
5043}
5044
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005045SDValue
5046SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005047 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5048 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005049
5050 SDVTList VTs = getVTList(VT, MVT::Other);
5051 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5052 FoldingSetNodeID ID;
5053 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5054 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005055 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005056 MMO->isVolatile(),
5057 MMO->isNonTemporal(),
5058 MMO->isInvariant()));
5059 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5060 void *IP = nullptr;
5061 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5062 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5063 return SDValue(E, 0);
5064 }
5065 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5066 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005067 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005068 CSEMap.InsertNode(N, IP);
5069 InsertNode(N);
5070 return SDValue(N, 0);
5071}
5072
5073SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005074 SDValue Ptr, SDValue Mask, EVT MemVT,
5075 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005076 assert(Chain.getValueType() == MVT::Other &&
5077 "Invalid chain type");
5078 EVT VT = Val.getValueType();
5079 SDVTList VTs = getVTList(MVT::Other);
5080 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5081 FoldingSetNodeID ID;
5082 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5083 ID.AddInteger(VT.getRawBits());
5084 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5085 MMO->isNonTemporal(), MMO->isInvariant()));
5086 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5087 void *IP = nullptr;
5088 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5089 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5090 return SDValue(E, 0);
5091 }
5092 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5093 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005094 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005095 CSEMap.InsertNode(N, IP);
5096 InsertNode(N);
5097 return SDValue(N, 0);
5098}
5099
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005100SDValue
5101SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl,
5102 ArrayRef<SDValue> Ops,
5103 MachineMemOperand *MMO) {
5104
5105 FoldingSetNodeID ID;
5106 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5107 ID.AddInteger(VT.getRawBits());
5108 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5109 MMO->isVolatile(),
5110 MMO->isNonTemporal(),
5111 MMO->isInvariant()));
5112 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5113 void *IP = nullptr;
5114 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5115 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5116 return SDValue(E, 0);
5117 }
5118 MaskedGatherSDNode *N =
5119 new (NodeAllocator) MaskedGatherSDNode(dl.getIROrder(), dl.getDebugLoc(),
5120 Ops, VTs, VT, MMO);
5121 CSEMap.InsertNode(N, IP);
5122 InsertNode(N);
5123 return SDValue(N, 0);
5124}
5125
5126SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl,
5127 ArrayRef<SDValue> Ops,
5128 MachineMemOperand *MMO) {
5129 FoldingSetNodeID ID;
5130 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5131 ID.AddInteger(VT.getRawBits());
5132 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5133 MMO->isNonTemporal(),
5134 MMO->isInvariant()));
5135 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5136 void *IP = nullptr;
5137 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5138 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5139 return SDValue(E, 0);
5140 }
5141 SDNode *N =
5142 new (NodeAllocator) MaskedScatterSDNode(dl.getIROrder(), dl.getDebugLoc(),
5143 Ops, VTs, VT, MMO);
5144 CSEMap.InsertNode(N, IP);
5145 InsertNode(N);
5146 return SDValue(N, 0);
5147}
5148
Andrew Trickef9de2a2013-05-25 02:42:55 +00005149SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005150 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005151 SDValue SV,
5152 unsigned Align) {
5153 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005154 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005155}
5156
Andrew Trickef9de2a2013-05-25 02:42:55 +00005157SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005158 ArrayRef<SDUse> Ops) {
5159 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005160 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005161 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005162 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5163 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005164 default: break;
5165 }
5166
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005167 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005168 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005169 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005170 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005171}
5172
Andrew Trickef9de2a2013-05-25 02:42:55 +00005173SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005174 ArrayRef<SDValue> Ops) {
5175 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005176 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005177 case 0: return getNode(Opcode, DL, VT);
5178 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5179 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5180 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005181 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005182 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005183
Chris Lattnerb0713c72005-04-09 03:27:28 +00005184 switch (Opcode) {
5185 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005186 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005187 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005188 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5189 "LHS and RHS of condition must have same type!");
5190 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5191 "True and False arms of SelectCC must have same type!");
5192 assert(Ops[2].getValueType() == VT &&
5193 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005194 break;
5195 }
5196 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005197 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005198 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5199 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005200 break;
5201 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005202 }
5203
Chris Lattner566307f2005-05-14 07:42:29 +00005204 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005205 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005206 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005207
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005208 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005209 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005210 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005211 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005212
Bill Wendling022d18f2009-12-18 23:32:53 +00005213 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005214 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005215
Jack Carter170a5f22013-09-09 22:02:08 +00005216 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005217 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005218 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005219 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005220 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005221 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005222 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005223
Chandler Carruth41b20e72014-07-22 04:07:55 +00005224 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005225 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005226}
5227
Andrew Trickef9de2a2013-05-25 02:42:55 +00005228SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005229 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5230 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005231}
5232
Andrew Trickef9de2a2013-05-25 02:42:55 +00005233SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005234 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005235 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005236 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005237
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005238#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005239 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005240 // FIXME: figure out how to safely handle things like
5241 // int foo(int x) { return 1 << (x & 255); }
5242 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005243 case ISD::SRA_PARTS:
5244 case ISD::SRL_PARTS:
5245 case ISD::SHL_PARTS:
5246 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005247 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005248 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005249 else if (N3.getOpcode() == ISD::AND)
5250 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5251 // If the and is only masking out bits that cannot effect the shift,
5252 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005253 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005254 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005255 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005256 }
5257 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005258 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005259#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005260
Chris Lattnerf9c19152005-08-25 19:12:10 +00005261 // Memoize the node unless it returns a flag.
5262 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005263 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005264 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005265 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005266 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005267 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005268 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005269 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005270
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005271 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005272 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5273 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005274 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005275 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5276 DL.getDebugLoc(), VTList, Ops[0],
5277 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005278 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005279 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5280 DL.getDebugLoc(), VTList, Ops[0],
5281 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005282 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005283 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005284 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005285 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005286 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005287 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005288 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005289 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5290 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005291 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005292 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5293 DL.getDebugLoc(), VTList, Ops[0],
5294 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005295 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005296 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5297 DL.getDebugLoc(), VTList, Ops[0],
5298 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005299 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005300 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005301 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005302 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005303 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005304 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005305 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005306}
5307
Andrew Trickef9de2a2013-05-25 02:42:55 +00005308SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005309 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005310}
5311
Andrew Trickef9de2a2013-05-25 02:42:55 +00005312SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005313 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005314 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005315 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005316}
5317
Andrew Trickef9de2a2013-05-25 02:42:55 +00005318SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005319 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005320 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005321 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005322}
5323
Andrew Trickef9de2a2013-05-25 02:42:55 +00005324SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005325 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005326 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005327 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005328}
5329
Andrew Trickef9de2a2013-05-25 02:42:55 +00005330SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005331 SDValue N1, SDValue N2, SDValue N3,
5332 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005333 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005334 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005335}
5336
Andrew Trickef9de2a2013-05-25 02:42:55 +00005337SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005338 SDValue N1, SDValue N2, SDValue N3,
5339 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005340 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005341 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005342}
5343
Owen Anderson53aa7a92009-08-10 22:56:29 +00005344SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005345 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005346}
5347
Owen Anderson53aa7a92009-08-10 22:56:29 +00005348SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005349 FoldingSetNodeID ID;
5350 ID.AddInteger(2U);
5351 ID.AddInteger(VT1.getRawBits());
5352 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005353
Craig Topperc0196b12014-04-14 00:51:57 +00005354 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005355 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005356 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005357 EVT *Array = Allocator.Allocate<EVT>(2);
5358 Array[0] = VT1;
5359 Array[1] = VT2;
5360 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5361 VTListMap.InsertNode(Result, IP);
5362 }
5363 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005364}
Dan Gohman17059682008-07-17 19:10:17 +00005365
Owen Anderson53aa7a92009-08-10 22:56:29 +00005366SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005367 FoldingSetNodeID ID;
5368 ID.AddInteger(3U);
5369 ID.AddInteger(VT1.getRawBits());
5370 ID.AddInteger(VT2.getRawBits());
5371 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005372
Craig Topperc0196b12014-04-14 00:51:57 +00005373 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005374 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005375 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005376 EVT *Array = Allocator.Allocate<EVT>(3);
5377 Array[0] = VT1;
5378 Array[1] = VT2;
5379 Array[2] = VT3;
5380 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5381 VTListMap.InsertNode(Result, IP);
5382 }
5383 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005384}
5385
Owen Anderson53aa7a92009-08-10 22:56:29 +00005386SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005387 FoldingSetNodeID ID;
5388 ID.AddInteger(4U);
5389 ID.AddInteger(VT1.getRawBits());
5390 ID.AddInteger(VT2.getRawBits());
5391 ID.AddInteger(VT3.getRawBits());
5392 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005393
Craig Topperc0196b12014-04-14 00:51:57 +00005394 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005395 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005396 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005397 EVT *Array = Allocator.Allocate<EVT>(4);
5398 Array[0] = VT1;
5399 Array[1] = VT2;
5400 Array[2] = VT3;
5401 Array[3] = VT4;
5402 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5403 VTListMap.InsertNode(Result, IP);
5404 }
5405 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005406}
5407
Craig Topperabb4ac72014-04-16 06:10:51 +00005408SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5409 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005410 FoldingSetNodeID ID;
5411 ID.AddInteger(NumVTs);
5412 for (unsigned index = 0; index < NumVTs; index++) {
5413 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005414 }
5415
Craig Topperc0196b12014-04-14 00:51:57 +00005416 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005417 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005418 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005419 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005420 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005421 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5422 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005423 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005424 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005425}
5426
5427
Chris Lattnerf34156e2006-01-28 09:32:45 +00005428/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5429/// specified operands. If the resultant node already exists in the DAG,
5430/// this does not modify the specified node, instead it returns the node that
5431/// already exists. If the resultant node does not exist in the DAG, the
5432/// input node is returned. As a degenerate case, if you specify the same
5433/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005434SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005435 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005436
Chris Lattnerf34156e2006-01-28 09:32:45 +00005437 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005438 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005439
Chris Lattnerf34156e2006-01-28 09:32:45 +00005440 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005441 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005442 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005443 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005444
Dan Gohmanebeccb42008-07-21 22:38:59 +00005445 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005446 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005447 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005448 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005449
Chris Lattnerf34156e2006-01-28 09:32:45 +00005450 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005451 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005452
Chris Lattnerf34156e2006-01-28 09:32:45 +00005453 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005454 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005455 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005456}
5457
Dan Gohman92c11ac2010-06-18 15:30:29 +00005458SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005459 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005460
Chris Lattnerf34156e2006-01-28 09:32:45 +00005461 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005462 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005463 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005464
Chris Lattnerf34156e2006-01-28 09:32:45 +00005465 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005466 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005467 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005468 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005469
Dan Gohmanebeccb42008-07-21 22:38:59 +00005470 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005471 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005472 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005473 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005474
Chris Lattnerf34156e2006-01-28 09:32:45 +00005475 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005476 if (N->OperandList[0] != Op1)
5477 N->OperandList[0].set(Op1);
5478 if (N->OperandList[1] != Op2)
5479 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005480
Chris Lattnerf34156e2006-01-28 09:32:45 +00005481 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005482 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005483 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005484}
5485
Dan Gohman92c11ac2010-06-18 15:30:29 +00005486SDNode *SelectionDAG::
5487UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005488 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005489 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005490}
5491
Dan Gohman92c11ac2010-06-18 15:30:29 +00005492SDNode *SelectionDAG::
5493UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005494 SDValue Op3, SDValue Op4) {
5495 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005496 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005497}
5498
Dan Gohman92c11ac2010-06-18 15:30:29 +00005499SDNode *SelectionDAG::
5500UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005501 SDValue Op3, SDValue Op4, SDValue Op5) {
5502 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005503 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005504}
5505
Dan Gohman92c11ac2010-06-18 15:30:29 +00005506SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005507UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5508 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005509 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005510 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005511
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005512 // If no operands changed just return the input node.
Benjamin Kramer0b6742a2015-03-02 16:45:08 +00005513 if (Ops.empty() || std::equal(Ops.begin(), Ops.end(), N->op_begin()))
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005514 return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005515
Chris Lattnerf34156e2006-01-28 09:32:45 +00005516 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005517 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005518 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005519 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005520
Dan Gohman2f83b472008-05-02 00:05:03 +00005521 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005522 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005523 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005524 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005525
Chris Lattnerf34156e2006-01-28 09:32:45 +00005526 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005527 for (unsigned i = 0; i != NumOps; ++i)
5528 if (N->OperandList[i] != Ops[i])
5529 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005530
5531 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005532 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005533 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005534}
5535
Dan Gohman91697632008-07-07 20:57:48 +00005536/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005537/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005538void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005539 // Unlike the code in MorphNodeTo that does this, we don't need to
5540 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005541 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5542 SDUse &Use = *I++;
5543 Use.set(SDValue());
5544 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005545}
Chris Lattner19732782005-08-16 18:17:10 +00005546
Dan Gohman17059682008-07-17 19:10:17 +00005547/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5548/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005549///
Dan Gohman17059682008-07-17 19:10:17 +00005550SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005551 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005552 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005553 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005554}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005555
Dan Gohman17059682008-07-17 19:10:17 +00005556SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005557 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005558 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005559 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005560 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005561}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005562
Dan Gohman17059682008-07-17 19:10:17 +00005563SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005564 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005565 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005566 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005567 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005568 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005569}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005570
Dan Gohman17059682008-07-17 19:10:17 +00005571SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005572 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005573 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005574 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005575 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005576 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005577}
Chris Lattner466fece2005-08-21 22:30:30 +00005578
Dan Gohman17059682008-07-17 19:10:17 +00005579SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005580 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005581 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005582 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005583}
5584
Dan Gohman17059682008-07-17 19:10:17 +00005585SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005586 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005587 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005588 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005589}
5590
Dan Gohman17059682008-07-17 19:10:17 +00005591SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005592 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005593 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005594 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005595}
5596
Dan Gohman17059682008-07-17 19:10:17 +00005597SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005598 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005599 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005600 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005601 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005602}
5603
Bill Wendling2d598632008-12-01 23:28:22 +00005604SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005605 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005606 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005607 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005608 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005609}
5610
Scott Michelcf0da6c2009-02-17 22:15:04 +00005611SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005612 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005613 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005614 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005615 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005616 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005617}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005618
Scott Michelcf0da6c2009-02-17 22:15:04 +00005619SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005620 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005621 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005622 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005623 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005624 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005625}
5626
Dan Gohman17059682008-07-17 19:10:17 +00005627SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005628 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005629 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005630 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005631 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005632 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005633 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005634}
5635
Dan Gohman17059682008-07-17 19:10:17 +00005636SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005637 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005638 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005639 SDValue Op3) {
5640 SDVTList VTs = getVTList(VT1, VT2, VT3);
5641 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005642 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005643}
5644
5645SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005646 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005647 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005648 // Reset the NodeID to -1.
5649 N->setNodeId(-1);
5650 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005651}
5652
Andrew Trickef9de2a2013-05-25 02:42:55 +00005653/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005654/// the line number information on the merged node since it is not possible to
5655/// preserve the information that operation is associated with multiple lines.
5656/// This will make the debugger working better at -O0, were there is a higher
5657/// probability having other instructions associated with that line.
5658///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005659/// For IROrder, we keep the smaller of the two
5660SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005661 DebugLoc NLoc = N->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00005662 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005663 N->setDebugLoc(DebugLoc());
5664 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005665 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5666 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005667 return N;
5668}
5669
Chris Lattneraf197502010-02-28 21:36:14 +00005670/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005671/// return type, opcode, and operands.
5672///
5673/// Note that MorphNodeTo returns the resultant node. If there is already a
5674/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005675/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005676///
5677/// Using MorphNodeTo is faster than creating a new node and swapping it in
5678/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005679/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005680/// the node's users.
5681///
Chandler Carruth356665a2014-08-01 22:09:43 +00005682/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5683/// As a consequence it isn't appropriate to use from within the DAG combiner or
5684/// the legalizer which maintain worklists that would need to be updated when
5685/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005686SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005687 SDVTList VTs, ArrayRef<SDValue> Ops) {
5688 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005689 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005690 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005691 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005692 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005693 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005694 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005695 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005696 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005697
Dan Gohmand3fe1742008-09-13 01:54:27 +00005698 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005699 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005700
Dan Gohman17059682008-07-17 19:10:17 +00005701 // Start the morphing.
5702 N->NodeType = Opc;
5703 N->ValueList = VTs.VTs;
5704 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005705
Dan Gohman17059682008-07-17 19:10:17 +00005706 // Clear the operands list, updating used nodes to remove this from their
5707 // use list. Keep track of any operands that become dead as a result.
5708 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005709 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5710 SDUse &Use = *I++;
5711 SDNode *Used = Use.getNode();
5712 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005713 if (Used->use_empty())
5714 DeadNodeSet.insert(Used);
5715 }
5716
Dan Gohman48b185d2009-09-25 20:36:54 +00005717 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5718 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005719 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005720 // If NumOps is larger than the # of operands we can have in a
5721 // MachineSDNode, reallocate the operand list.
5722 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5723 if (MN->OperandsNeedDelete)
5724 delete[] MN->OperandList;
5725 if (NumOps > array_lengthof(MN->LocalOperands))
5726 // We're creating a final node that will live unmorphed for the
5727 // remainder of the current SelectionDAG iteration, so we can allocate
5728 // the operands directly out of a pool with no recycling metadata.
5729 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005730 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005731 else
Craig Topper131de822014-04-27 19:21:16 +00005732 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005733 MN->OperandsNeedDelete = false;
5734 } else
Craig Topper131de822014-04-27 19:21:16 +00005735 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005736 } else {
5737 // If NumOps is larger than the # of operands we currently have, reallocate
5738 // the operand list.
5739 if (NumOps > N->NumOperands) {
5740 if (N->OperandsNeedDelete)
5741 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005742 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005743 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005744 } else
Craig Topper131de822014-04-27 19:21:16 +00005745 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005746 }
5747
5748 // Delete any nodes that are still dead after adding the uses for the
5749 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005750 if (!DeadNodeSet.empty()) {
5751 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005752 for (SDNode *N : DeadNodeSet)
5753 if (N->use_empty())
5754 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005755 RemoveDeadNodes(DeadNodes);
5756 }
Dan Gohman91697632008-07-07 20:57:48 +00005757
Dan Gohman17059682008-07-17 19:10:17 +00005758 if (IP)
5759 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005760 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005761}
5762
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005763
Dan Gohman32f71d72009-09-25 18:54:59 +00005764/// getMachineNode - These are used for target selectors to create a new node
5765/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005766///
Dan Gohman32f71d72009-09-25 18:54:59 +00005767/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005768/// node of the specified opcode and operands, it returns that node instead of
5769/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005770MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005771SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005772 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005773 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005774}
Bill Wendlinga434d932009-01-29 09:01:55 +00005775
Dan Gohmana22f2d82009-10-10 01:29:16 +00005776MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005777SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005778 SDVTList VTs = getVTList(VT);
5779 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005780 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005781}
Bill Wendlinga434d932009-01-29 09:01:55 +00005782
Dan Gohmana22f2d82009-10-10 01:29:16 +00005783MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005784SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005785 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005786 SDVTList VTs = getVTList(VT);
5787 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005788 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005789}
Bill Wendlinga434d932009-01-29 09:01:55 +00005790
Dan Gohmana22f2d82009-10-10 01:29:16 +00005791MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005792SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005793 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005794 SDVTList VTs = getVTList(VT);
5795 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005796 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005797}
Bill Wendlinga434d932009-01-29 09:01:55 +00005798
Dan Gohmana22f2d82009-10-10 01:29:16 +00005799MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005800SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005801 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005802 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005803 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005804}
Bill Wendlinga434d932009-01-29 09:01:55 +00005805
Dan Gohmana22f2d82009-10-10 01:29:16 +00005806MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005807SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005808 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005809 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005810}
Bill Wendlinga434d932009-01-29 09:01:55 +00005811
Dan Gohmana22f2d82009-10-10 01:29:16 +00005812MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005813SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005814 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005815 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005816 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005817 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005818}
Bill Wendlinga434d932009-01-29 09:01:55 +00005819
Dan Gohmana22f2d82009-10-10 01:29:16 +00005820MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005821SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005822 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005823 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005824 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005825 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005826}
5827
Dan Gohmana22f2d82009-10-10 01:29:16 +00005828MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005829SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005830 EVT VT1, EVT VT2, SDValue Op1,
5831 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005832 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005833 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005834 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005835}
5836
Dan Gohmana22f2d82009-10-10 01:29:16 +00005837MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005838SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005839 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005840 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005841 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005842 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005843}
Bill Wendlinga434d932009-01-29 09:01:55 +00005844
Dan Gohmana22f2d82009-10-10 01:29:16 +00005845MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005846SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005847 EVT VT1, EVT VT2, EVT VT3,
5848 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005849 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005850 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005851 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005852}
Bill Wendlinga434d932009-01-29 09:01:55 +00005853
Dan Gohmana22f2d82009-10-10 01:29:16 +00005854MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005855SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005856 EVT VT1, EVT VT2, EVT VT3,
5857 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005858 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005859 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005860 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005861}
Bill Wendlinga434d932009-01-29 09:01:55 +00005862
Dan Gohmana22f2d82009-10-10 01:29:16 +00005863MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005864SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005865 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005866 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005867 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005868 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005869}
Bill Wendlinga434d932009-01-29 09:01:55 +00005870
Dan Gohmana22f2d82009-10-10 01:29:16 +00005871MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005872SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005873 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005874 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005875 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005876 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005877}
5878
Dan Gohmana22f2d82009-10-10 01:29:16 +00005879MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005880SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005881 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005882 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005883 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005884 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005885}
5886
Dan Gohmana22f2d82009-10-10 01:29:16 +00005887MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005888SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005889 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005890 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005891 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005892 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005893 const SDValue *Ops = OpsArray.data();
5894 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005895
5896 if (DoCSE) {
5897 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005898 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005899 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005900 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005901 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005902 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005903 }
5904
5905 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005906 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5907 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005908
5909 // Initialize the operands list.
5910 if (NumOps > array_lengthof(N->LocalOperands))
5911 // We're creating a final node that will live unmorphed for the
5912 // remainder of the current SelectionDAG iteration, so we can allocate
5913 // the operands directly out of a pool with no recycling metadata.
5914 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5915 Ops, NumOps);
5916 else
5917 N->InitOperands(N->LocalOperands, Ops, NumOps);
5918 N->OperandsNeedDelete = false;
5919
5920 if (DoCSE)
5921 CSEMap.InsertNode(N, IP);
5922
Chandler Carruth41b20e72014-07-22 04:07:55 +00005923 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005924 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005925}
Evan Chengd3f1db92006-02-09 07:15:23 +00005926
Dan Gohmanac33a902009-08-19 18:16:17 +00005927/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005928/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005929SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005930SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005931 SDValue Operand) {
5932 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005933 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005934 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005935 return SDValue(Subreg, 0);
5936}
5937
Bob Wilson2a45a652009-10-08 18:49:46 +00005938/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005939/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005940SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005941SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005942 SDValue Operand, SDValue Subreg) {
5943 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005944 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005945 VT, Operand, Subreg, SRIdxVal);
5946 return SDValue(Result, 0);
5947}
5948
Evan Cheng31604a62008-03-22 01:55:50 +00005949/// getNodeIfExists - Get the specified node if it's already available, or
5950/// else return NULL.
5951SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005952 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5953 bool exact) {
5954 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005955 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005956 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005957 if (isBinOpWithFlags(Opcode))
5958 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005959 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005960 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005961 return E;
5962 }
Craig Topperc0196b12014-04-14 00:51:57 +00005963 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005964}
5965
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005966/// getDbgValue - Creates a SDDbgValue node.
5967///
Adrian Prantl32da8892014-04-25 20:49:25 +00005968/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005969SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5970 unsigned R, bool IsIndirect, uint64_t Off,
5971 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00005972 assert(cast<MDLocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00005973 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005974 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005975}
5976
Adrian Prantl32da8892014-04-25 20:49:25 +00005977/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005978SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5979 const Value *C, uint64_t Off,
5980 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00005981 assert(cast<MDLocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00005982 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005983 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005984}
5985
Adrian Prantl32da8892014-04-25 20:49:25 +00005986/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005987SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
5988 unsigned FI, uint64_t Off,
5989 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00005990 assert(cast<MDLocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00005991 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005992 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005993}
5994
Dan Gohman7d099f72010-03-03 21:33:37 +00005995namespace {
5996
Dan Gohman9cc886b2010-03-04 19:11:28 +00005997/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005998/// pointed to by a use iterator is deleted, increment the use iterator
5999/// so that it doesn't dangle.
6000///
Dan Gohman7d099f72010-03-03 21:33:37 +00006001class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00006002 SDNode::use_iterator &UI;
6003 SDNode::use_iterator &UE;
6004
Craig Topper7b883b32014-03-08 06:31:39 +00006005 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00006006 // Increment the iterator as needed.
6007 while (UI != UE && N == *UI)
6008 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00006009 }
6010
6011public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006012 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00006013 SDNode::use_iterator &ui,
6014 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006015 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00006016};
6017
6018}
6019
Evan Cheng445b91a2006-08-07 22:13:29 +00006020/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006021/// This can cause recursive merging of nodes in the DAG.
6022///
Chris Lattner76858912008-02-03 03:35:22 +00006023/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00006024///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006025void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006026 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006027 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00006028 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00006029 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00006030
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006031 // Iterate over all the existing uses of From. New uses will be added
6032 // to the beginning of the use list, which we avoid visiting.
6033 // This specifically avoids visiting uses of From that arise while the
6034 // replacement is happening, because any such uses would be the result
6035 // of CSE: If an existing node looks like From after one of its operands
6036 // is replaced by To, we don't want to replace of all its users with To
6037 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006038 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006039 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006040 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006041 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006042
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006043 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006044 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006045
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006046 // A user can appear in a use list multiple times, and when this
6047 // happens the uses are usually next to each other in the list.
6048 // To help reduce the number of CSE recomputations, process all
6049 // the uses of this user that we can find this way.
6050 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006051 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006052 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006053 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006054 } while (UI != UE && *UI == User);
6055
6056 // Now that we have modified User, add it back to the CSE maps. If it
6057 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006058 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006059 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006060
6061 // If we just RAUW'd the root, take note.
6062 if (FromN == getRoot())
6063 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00006064}
6065
6066/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6067/// This can cause recursive merging of nodes in the DAG.
6068///
Dan Gohman8aa28b92009-04-15 20:06:30 +00006069/// This version assumes that for each value of From, there is a
6070/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00006071///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006072void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00006073#ifndef NDEBUG
6074 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6075 assert((!From->hasAnyUseOfValue(i) ||
6076 From->getValueType(i) == To->getValueType(i)) &&
6077 "Cannot use this version of ReplaceAllUsesWith!");
6078#endif
Dan Gohman17059682008-07-17 19:10:17 +00006079
6080 // Handle the trivial case.
6081 if (From == To)
6082 return;
6083
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006084 // Iterate over just the existing users of From. See the comments in
6085 // the ReplaceAllUsesWith above.
6086 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006087 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006088 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006089 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006090
Chris Lattner373f0482005-08-26 18:36:28 +00006091 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006092 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006093
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006094 // A user can appear in a use list multiple times, and when this
6095 // happens the uses are usually next to each other in the list.
6096 // To help reduce the number of CSE recomputations, process all
6097 // the uses of this user that we can find this way.
6098 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006099 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006100 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006101 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006102 } while (UI != UE && *UI == User);
6103
6104 // Now that we have modified User, add it back to the CSE maps. If it
6105 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006106 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006107 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006108
6109 // If we just RAUW'd the root, take note.
6110 if (From == getRoot().getNode())
6111 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006112}
6113
Chris Lattner373f0482005-08-26 18:36:28 +00006114/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6115/// This can cause recursive merging of nodes in the DAG.
6116///
6117/// This version can replace From with any result values. To must match the
6118/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006119void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006120 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006121 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006122
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006123 // Iterate over just the existing users of From. See the comments in
6124 // the ReplaceAllUsesWith above.
6125 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006126 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006127 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006128 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006129
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006130 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006131 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006132
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006133 // A user can appear in a use list multiple times, and when this
6134 // happens the uses are usually next to each other in the list.
6135 // To help reduce the number of CSE recomputations, process all
6136 // the uses of this user that we can find this way.
6137 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006138 SDUse &Use = UI.getUse();
6139 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006140 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006141 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006142 } while (UI != UE && *UI == User);
6143
6144 // Now that we have modified User, add it back to the CSE maps. If it
6145 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006146 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006147 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006148
6149 // If we just RAUW'd the root, take note.
6150 if (From == getRoot().getNode())
6151 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006152}
6153
Chris Lattner375e1a72006-02-17 21:58:01 +00006154/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006155/// uses of other values produced by From.getNode() alone. The Deleted
6156/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006157void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006158 // Handle the really simple, really trivial case efficiently.
6159 if (From == To) return;
6160
Chris Lattner375e1a72006-02-17 21:58:01 +00006161 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006162 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006163 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006164 return;
6165 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006166
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006167 // Iterate over just the existing users of From. See the comments in
6168 // the ReplaceAllUsesWith above.
6169 SDNode::use_iterator UI = From.getNode()->use_begin(),
6170 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006171 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006172 while (UI != UE) {
6173 SDNode *User = *UI;
6174 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006175
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006176 // A user can appear in a use list multiple times, and when this
6177 // happens the uses are usually next to each other in the list.
6178 // To help reduce the number of CSE recomputations, process all
6179 // the uses of this user that we can find this way.
6180 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006181 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006182
6183 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006184 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006185 ++UI;
6186 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006187 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006188
6189 // If this node hasn't been modified yet, it's still in the CSE maps,
6190 // so remove its old self from the CSE maps.
6191 if (!UserRemovedFromCSEMaps) {
6192 RemoveNodeFromCSEMaps(User);
6193 UserRemovedFromCSEMaps = true;
6194 }
6195
6196 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006197 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006198 } while (UI != UE && *UI == User);
6199
6200 // We are iterating over all uses of the From node, so if a use
6201 // doesn't use the specific value, no changes are made.
6202 if (!UserRemovedFromCSEMaps)
6203 continue;
6204
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006205 // Now that we have modified User, add it back to the CSE maps. If it
6206 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006207 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006208 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006209
6210 // If we just RAUW'd the root, take note.
6211 if (From == getRoot())
6212 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006213}
6214
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006215namespace {
6216 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6217 /// to record information about a use.
6218 struct UseMemo {
6219 SDNode *User;
6220 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006221 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006222 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006223
6224 /// operator< - Sort Memos by User.
6225 bool operator<(const UseMemo &L, const UseMemo &R) {
6226 return (intptr_t)L.User < (intptr_t)R.User;
6227 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006228}
6229
Dan Gohman17059682008-07-17 19:10:17 +00006230/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006231/// uses of other values produced by From.getNode() alone. The same value
6232/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006233/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006234void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6235 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006236 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006237 // Handle the simple, trivial case efficiently.
6238 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006239 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006240
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006241 // Read up all the uses and make records of them. This helps
6242 // processing new uses that are introduced during the
6243 // replacement process.
6244 SmallVector<UseMemo, 4> Uses;
6245 for (unsigned i = 0; i != Num; ++i) {
6246 unsigned FromResNo = From[i].getResNo();
6247 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006248 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006249 E = FromNode->use_end(); UI != E; ++UI) {
6250 SDUse &Use = UI.getUse();
6251 if (Use.getResNo() == FromResNo) {
6252 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006253 Uses.push_back(Memo);
6254 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006255 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006256 }
Dan Gohman17059682008-07-17 19:10:17 +00006257
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006258 // Sort the uses, so that all the uses from a given User are together.
6259 std::sort(Uses.begin(), Uses.end());
6260
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006261 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6262 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006263 // We know that this user uses some value of From. If it is the right
6264 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006265 SDNode *User = Uses[UseIndex].User;
6266
6267 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006268 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006269
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006270 // The Uses array is sorted, so all the uses for a given User
6271 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006272 // To help reduce the number of CSE recomputations, process all
6273 // the uses of this user that we can find this way.
6274 do {
6275 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006276 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006277 ++UseIndex;
6278
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006279 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006280 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6281
Dan Gohman17059682008-07-17 19:10:17 +00006282 // Now that we have modified User, add it back to the CSE maps. If it
6283 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006284 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006285 }
6286}
6287
Evan Cheng9631a602006-08-01 08:20:41 +00006288/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006289/// based on their topological order. It returns the maximum id and a vector
6290/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006291unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006292
Dan Gohman86aa16a2008-09-30 18:30:35 +00006293 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006294
Dan Gohman86aa16a2008-09-30 18:30:35 +00006295 // SortedPos tracks the progress of the algorithm. Nodes before it are
6296 // sorted, nodes after it are unsorted. When the algorithm completes
6297 // it is at the end of the list.
6298 allnodes_iterator SortedPos = allnodes_begin();
6299
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006300 // Visit all the nodes. Move nodes with no operands to the front of
6301 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006302 // operand count. Before we do this, the Node Id fields of the nodes
6303 // may contain arbitrary values. After, the Node Id fields for nodes
6304 // before SortedPos will contain the topological sort index, and the
6305 // Node Id fields for nodes At SortedPos and after will contain the
6306 // count of outstanding operands.
6307 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6308 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006309 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006310 unsigned Degree = N->getNumOperands();
6311 if (Degree == 0) {
6312 // A node with no uses, add it to the result array immediately.
6313 N->setNodeId(DAGSize++);
6314 allnodes_iterator Q = N;
6315 if (Q != SortedPos)
6316 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006317 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006318 ++SortedPos;
6319 } else {
6320 // Temporarily use the Node Id as scratch space for the degree count.
6321 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006322 }
6323 }
6324
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006325 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006326 // such that by the time the end is reached all nodes will be sorted.
6327 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6328 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006329 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006330 // N is in sorted position, so all its uses have one less operand
6331 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006332 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6333 UI != UE; ++UI) {
6334 SDNode *P = *UI;
6335 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006336 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006337 --Degree;
6338 if (Degree == 0) {
6339 // All of P's operands are sorted, so P may sorted now.
6340 P->setNodeId(DAGSize++);
6341 if (P != SortedPos)
6342 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006343 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006344 ++SortedPos;
6345 } else {
6346 // Update P's outstanding operand count.
6347 P->setNodeId(Degree);
6348 }
6349 }
David Greene3b2a68c2010-01-20 00:59:23 +00006350 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006351#ifndef NDEBUG
6352 SDNode *S = ++I;
6353 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006354 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006355 dbgs() << "Checking if this is due to cycles\n";
6356 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006357#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006358 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006359 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006360 }
6361
6362 assert(SortedPos == AllNodes.end() &&
6363 "Topological sort incomplete!");
6364 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6365 "First node in topological sort is not the entry token!");
6366 assert(AllNodes.front().getNodeId() == 0 &&
6367 "First node in topological sort has non-zero id!");
6368 assert(AllNodes.front().getNumOperands() == 0 &&
6369 "First node in topological sort has operands!");
6370 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6371 "Last node in topologic sort has unexpected id!");
6372 assert(AllNodes.back().use_empty() &&
6373 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006374 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006375 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006376}
6377
Evan Cheng563fe3c2010-03-25 01:38:16 +00006378/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6379/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006380void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006381 if (SD) {
6382 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006383 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006384 }
6385 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006386}
Evan Cheng29eefc12006-07-27 06:39:06 +00006387
Devang Patelefc6b162011-01-25 23:27:42 +00006388/// TransferDbgValues - Transfer SDDbgValues.
6389void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6390 if (From == To || !From.getNode()->getHasDebugValue())
6391 return;
6392 SDNode *FromNode = From.getNode();
6393 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006394 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006395 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006396 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006397 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006398 SDDbgValue *Dbg = *I;
6399 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006400 SDDbgValue *Clone =
6401 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6402 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6403 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006404 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006405 }
6406 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006407 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006408 E = ClonedDVs.end(); I != E; ++I)
6409 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006410}
6411
Jim Laskeyd66e6162005-08-17 20:08:02 +00006412//===----------------------------------------------------------------------===//
6413// SDNode Class
6414//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006415
Chris Lattner3bf17b62007-02-04 02:41:42 +00006416HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006417 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006418}
6419
Andrew Trickef9de2a2013-05-25 02:42:55 +00006420GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6421 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006422 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006423 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006424 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006425}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006426
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006427AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6428 SDValue X, unsigned SrcAS,
6429 unsigned DestAS)
6430 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6431 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6432
Andrew Trickef9de2a2013-05-25 02:42:55 +00006433MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6434 EVT memvt, MachineMemOperand *mmo)
6435 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006436 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006437 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006438 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006439 assert(isNonTemporal() == MMO->isNonTemporal() &&
6440 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006441 // We check here that the size of the memory operand fits within the size of
6442 // the MMO. This is because the MMO might indicate only a possible address
6443 // range instead of specifying the affected memory addresses precisely.
6444 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006445}
6446
Andrew Trickef9de2a2013-05-25 02:42:55 +00006447MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006448 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6449 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006450 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006451 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006452 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006453 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006454 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006455}
6456
Jim Laskeyf576b422006-10-27 23:46:08 +00006457/// Profile - Gather unique data for the node.
6458///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006459void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006460 AddNodeIDNode(ID, this);
6461}
6462
Owen Anderson3b1665e2009-08-25 22:27:22 +00006463namespace {
6464 struct EVTArray {
6465 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006466
Owen Anderson3b1665e2009-08-25 22:27:22 +00006467 EVTArray() {
6468 VTs.reserve(MVT::LAST_VALUETYPE);
6469 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6470 VTs.push_back(MVT((MVT::SimpleValueType)i));
6471 }
6472 };
6473}
6474
Owen Anderson53aa7a92009-08-10 22:56:29 +00006475static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006476static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006477static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006478
Chris Lattner88fa11c2005-11-08 23:30:28 +00006479/// getValueTypeList - Return a pointer to the specified value type.
6480///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006481const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006482 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006483 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006484 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006485 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006486 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006487 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006488 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006489 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006490}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006491
Chris Lattner40e79822005-01-12 18:37:47 +00006492/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6493/// indicated value. This method ignores uses of other values defined by this
6494/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006495bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006496 assert(Value < getNumValues() && "Bad value!");
6497
Roman Levenstein51f532f2008-04-07 10:06:32 +00006498 // TODO: Only iterate over uses of a given value of the node
6499 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006500 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006501 if (NUses == 0)
6502 return false;
6503 --NUses;
6504 }
Chris Lattner40e79822005-01-12 18:37:47 +00006505 }
6506
6507 // Found exactly the right number of uses?
6508 return NUses == 0;
6509}
6510
6511
Evan Cheng358c3d12007-08-02 05:29:38 +00006512/// hasAnyUseOfValue - Return true if there are any use of the indicated
6513/// value. This method ignores uses of other values defined by this operation.
6514bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6515 assert(Value < getNumValues() && "Bad value!");
6516
Dan Gohman7a510c22008-07-09 22:39:01 +00006517 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006518 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006519 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006520
6521 return false;
6522}
6523
6524
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006525/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006526///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006527bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006528 bool Seen = false;
6529 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006530 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006531 if (User == this)
6532 Seen = true;
6533 else
6534 return false;
6535 }
6536
6537 return Seen;
6538}
6539
Evan Cheng9456dd82006-11-03 07:31:32 +00006540/// isOperand - Return true if this node is an operand of N.
6541///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006542bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006543 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6544 if (*this == N->getOperand(i))
6545 return true;
6546 return false;
6547}
6548
Evan Cheng567d2e52008-03-04 00:41:45 +00006549bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006550 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006551 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006552 return true;
6553 return false;
6554}
Evan Chengd37645c2006-02-05 06:29:23 +00006555
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006556/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006557/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006558/// side-effecting instructions on any chain path. In practice, this looks
6559/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006560/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006561bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006562 unsigned Depth) const {
6563 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006564
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006565 // Don't search too deeply, we just want to be able to see through
6566 // TokenFactor's etc.
6567 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006568
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006569 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006570 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006571 if (getOpcode() == ISD::TokenFactor) {
6572 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006573 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6574 return false;
6575 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006576 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006577
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006578 // Loads don't have side effects, look through them.
6579 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6580 if (!Ld->isVolatile())
6581 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6582 }
6583 return false;
6584}
6585
Lang Hames5a004992011-07-07 04:31:51 +00006586/// hasPredecessor - Return true if N is a predecessor of this node.
6587/// N is either an operand of this node, or can be reached by recursively
6588/// traversing up the operands.
6589/// NOTE: This is an expensive method. Use it carefully.
6590bool SDNode::hasPredecessor(const SDNode *N) const {
6591 SmallPtrSet<const SDNode *, 32> Visited;
6592 SmallVector<const SDNode *, 16> Worklist;
6593 return hasPredecessorHelper(N, Visited, Worklist);
6594}
Dan Gohmancd139c02009-10-28 03:44:30 +00006595
Craig Topperb94011f2013-07-14 04:42:23 +00006596bool
6597SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006598 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006599 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006600 if (Visited.empty()) {
6601 Worklist.push_back(this);
6602 } else {
6603 // Take a look in the visited set. If we've already encountered this node
6604 // we needn't search further.
6605 if (Visited.count(N))
6606 return true;
6607 }
6608
6609 // Haven't visited N yet. Continue the search.
6610 while (!Worklist.empty()) {
6611 const SDNode *M = Worklist.pop_back_val();
6612 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6613 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006614 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006615 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006616 if (Op == N)
6617 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006618 }
Lang Hames5a004992011-07-07 04:31:51 +00006619 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006620
6621 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006622}
6623
Evan Cheng5d9fd972006-10-04 00:56:09 +00006624uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6625 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006626 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006627}
6628
Mon P Wang32f8bb92009-11-30 02:42:02 +00006629SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6630 assert(N->getNumValues() == 1 &&
6631 "Can't unroll a vector with multiple results!");
6632
6633 EVT VT = N->getValueType(0);
6634 unsigned NE = VT.getVectorNumElements();
6635 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006636 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006637
6638 SmallVector<SDValue, 8> Scalars;
6639 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6640
6641 // If ResNE is 0, fully unroll the vector op.
6642 if (ResNE == 0)
6643 ResNE = NE;
6644 else if (NE > ResNE)
6645 NE = ResNE;
6646
6647 unsigned i;
6648 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006649 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006650 SDValue Operand = N->getOperand(j);
6651 EVT OperandVT = Operand.getValueType();
6652 if (OperandVT.isVector()) {
6653 // A vector operand; extract a single element.
6654 EVT OperandEltVT = OperandVT.getVectorElementType();
6655 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6656 OperandEltVT,
6657 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006658 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006659 } else {
6660 // A scalar operand; just use it as is.
6661 Operands[j] = Operand;
6662 }
6663 }
6664
6665 switch (N->getOpcode()) {
6666 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006667 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006668 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006669 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006670 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006671 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006672 case ISD::SHL:
6673 case ISD::SRA:
6674 case ISD::SRL:
6675 case ISD::ROTL:
6676 case ISD::ROTR:
6677 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006678 getShiftAmountOperand(Operands[0].getValueType(),
6679 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006680 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006681 case ISD::SIGN_EXTEND_INREG:
6682 case ISD::FP_ROUND_INREG: {
6683 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6684 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6685 Operands[0],
6686 getValueType(ExtVT)));
6687 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006688 }
6689 }
6690
6691 for (; i < ResNE; ++i)
6692 Scalars.push_back(getUNDEF(EltVT));
6693
6694 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006695 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006696}
6697
Evan Chengf5938d52009-12-09 01:36:00 +00006698
Wesley Peck527da1b2010-11-23 03:31:01 +00006699/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6700/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006701/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006702bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006703 unsigned Bytes, int Dist) const {
6704 if (LD->getChain() != Base->getChain())
6705 return false;
6706 EVT VT = LD->getValueType(0);
6707 if (VT.getSizeInBits() / 8 != Bytes)
6708 return false;
6709
6710 SDValue Loc = LD->getOperand(1);
6711 SDValue BaseLoc = Base->getOperand(1);
6712 if (Loc.getOpcode() == ISD::FrameIndex) {
6713 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6714 return false;
6715 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6716 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6717 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6718 int FS = MFI->getObjectSize(FI);
6719 int BFS = MFI->getObjectSize(BFI);
6720 if (FS != BFS || FS != (int)Bytes) return false;
6721 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6722 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006723
Sanjay Patel7129c102014-12-16 21:57:18 +00006724 // Handle X + C.
6725 if (isBaseWithConstantOffset(Loc)) {
6726 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6727 if (Loc.getOperand(0) == BaseLoc) {
6728 // If the base location is a simple address with no offset itself, then
6729 // the second load's first add operand should be the base address.
6730 if (LocOffset == Dist * (int)Bytes)
6731 return true;
6732 } else if (isBaseWithConstantOffset(BaseLoc)) {
6733 // The base location itself has an offset, so subtract that value from the
6734 // second load's offset before comparing to distance * size.
6735 int64_t BOffset =
6736 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6737 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6738 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6739 return true;
6740 }
6741 }
6742 }
Craig Topperc0196b12014-04-14 00:51:57 +00006743 const GlobalValue *GV1 = nullptr;
6744 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006745 int64_t Offset1 = 0;
6746 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006747 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6748 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006749 if (isGA1 && isGA2 && GV1 == GV2)
6750 return Offset1 == (Offset2 + Dist*Bytes);
6751 return false;
6752}
6753
6754
Evan Cheng34a23ea2009-12-09 01:04:59 +00006755/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6756/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006757unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006758 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006759 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006760 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006761 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006762 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006763 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00006764 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
6765 *TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006766 unsigned AlignBits = KnownZero.countTrailingOnes();
6767 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6768 if (Align)
6769 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006770 }
Evan Chengd938faf2009-12-09 01:53:58 +00006771
Evan Cheng34a23ea2009-12-09 01:04:59 +00006772 // If this is a direct reference to a stack slot, use information about the
6773 // stack slot's alignment.
6774 int FrameIdx = 1 << 31;
6775 int64_t FrameOffset = 0;
6776 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6777 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006778 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006779 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006780 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006781 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6782 FrameOffset = Ptr.getConstantOperandVal(1);
6783 }
6784
6785 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006786 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006787 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6788 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006789 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006790 }
6791
6792 return 0;
6793}
6794
Juergen Ributzkab3487102013-11-19 21:20:17 +00006795/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6796/// which is split (or expanded) into two not necessarily identical pieces.
6797std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6798 // Currently all types are split in half.
6799 EVT LoVT, HiVT;
6800 if (!VT.isVector()) {
6801 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6802 } else {
6803 unsigned NumElements = VT.getVectorNumElements();
6804 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6805 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6806 NumElements/2);
6807 }
6808 return std::make_pair(LoVT, HiVT);
6809}
6810
6811/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6812/// low/high part.
6813std::pair<SDValue, SDValue>
6814SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6815 const EVT &HiVT) {
6816 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6817 N.getValueType().getVectorNumElements() &&
6818 "More vector elements requested than available!");
6819 SDValue Lo, Hi;
6820 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6821 getConstant(0, TLI->getVectorIdxTy()));
6822 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6823 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6824 return std::make_pair(Lo, Hi);
6825}
6826
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006827void SelectionDAG::ExtractVectorElements(SDValue Op,
6828 SmallVectorImpl<SDValue> &Args,
6829 unsigned Start, unsigned Count) {
6830 EVT VT = Op.getValueType();
6831 if (Count == 0)
6832 Count = VT.getVectorNumElements();
6833
6834 EVT EltVT = VT.getVectorElementType();
6835 EVT IdxTy = TLI->getVectorIdxTy();
6836 SDLoc SL(Op);
6837 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6838 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6839 Op, getConstant(i, IdxTy)));
6840 }
6841}
6842
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006843// getAddressSpace - Return the address space this GlobalAddress belongs to.
6844unsigned GlobalAddressSDNode::getAddressSpace() const {
6845 return getGlobal()->getType()->getAddressSpace();
6846}
6847
6848
Chris Lattner229907c2011-07-18 04:54:35 +00006849Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006850 if (isMachineConstantPoolEntry())
6851 return Val.MachineCPVal->getType();
6852 return Val.ConstVal->getType();
6853}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006854
Bob Wilson85cefe82009-03-02 23:24:16 +00006855bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6856 APInt &SplatUndef,
6857 unsigned &SplatBitSize,
6858 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006859 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006860 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006861 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006862 assert(VT.isVector() && "Expected a vector type");
6863 unsigned sz = VT.getSizeInBits();
6864 if (MinSplatBits > sz)
6865 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006866
Bob Wilson85cefe82009-03-02 23:24:16 +00006867 SplatValue = APInt(sz, 0);
6868 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006869
Bob Wilson85cefe82009-03-02 23:24:16 +00006870 // Get the bits. Bits with undefined values (when the corresponding element
6871 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6872 // in SplatValue. If any of the values are not constant, give up and return
6873 // false.
6874 unsigned int nOps = getNumOperands();
6875 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6876 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006877
6878 for (unsigned j = 0; j < nOps; ++j) {
6879 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006880 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006881 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006882
Bob Wilson85cefe82009-03-02 23:24:16 +00006883 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006884 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006885 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006886 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006887 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006888 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006889 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006890 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006891 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006892 }
6893
Bob Wilson85cefe82009-03-02 23:24:16 +00006894 // The build_vector is all constants or undefs. Find the smallest element
6895 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006896
Bob Wilson85cefe82009-03-02 23:24:16 +00006897 HasAnyUndefs = (SplatUndef != 0);
6898 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006899
Bob Wilson85cefe82009-03-02 23:24:16 +00006900 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006901 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6902 APInt LowValue = SplatValue.trunc(HalfSize);
6903 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6904 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006905
Bob Wilson85cefe82009-03-02 23:24:16 +00006906 // If the two halves do not match (ignoring undef bits), stop here.
6907 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6908 MinSplatBits > HalfSize)
6909 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006910
Bob Wilson85cefe82009-03-02 23:24:16 +00006911 SplatValue = HighValue | LowValue;
6912 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006913
Bob Wilson85cefe82009-03-02 23:24:16 +00006914 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006915 }
6916
Bob Wilson85cefe82009-03-02 23:24:16 +00006917 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006918 return true;
6919}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006920
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006921SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6922 if (UndefElements) {
6923 UndefElements->clear();
6924 UndefElements->resize(getNumOperands());
6925 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006926 SDValue Splatted;
6927 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6928 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006929 if (Op.getOpcode() == ISD::UNDEF) {
6930 if (UndefElements)
6931 (*UndefElements)[i] = true;
6932 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006933 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006934 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006935 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006936 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006937 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006938
Chandler Carruthb844e722014-07-08 07:19:55 +00006939 if (!Splatted) {
6940 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6941 "Can only have a splat without a constant for all undefs.");
6942 return getOperand(0);
6943 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006944
Chandler Carruthb844e722014-07-08 07:19:55 +00006945 return Splatted;
6946}
6947
6948ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006949BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006950 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006951 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006952}
6953
6954ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006955BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006956 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006957 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006958}
6959
Juergen Ributzka73844052014-01-13 20:51:35 +00006960bool BuildVectorSDNode::isConstant() const {
6961 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6962 unsigned Opc = getOperand(i).getOpcode();
6963 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6964 return false;
6965 }
6966 return true;
6967}
6968
Owen Anderson53aa7a92009-08-10 22:56:29 +00006969bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006970 // Find the first non-undef value in the shuffle mask.
6971 unsigned i, e;
6972 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6973 /* search */;
6974
Nate Begeman39b59db2009-04-29 18:13:31 +00006975 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006976
Nate Begeman5f829d82009-04-29 05:20:52 +00006977 // Make sure all remaining elements are either undef or the same as the first
6978 // non-undef value.
6979 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006980 if (Mask[i] >= 0 && Mask[i] != Idx)
6981 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006982 return true;
6983}
David Greene09851602010-01-20 20:13:31 +00006984
Adam Nemetb4690e32014-05-31 16:23:20 +00006985#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00006986static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006987 SmallPtrSetImpl<const SDNode*> &Visited,
6988 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00006989 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006990 // If this node has already been checked, don't check it again.
6991 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006992 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006993
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006994 // If a node has already been visited on this depth-first walk, reject it as
6995 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00006996 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006997 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006998 dbgs() << "Offending node:\n";
6999 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007000 abort();
David Greene09851602010-01-20 20:13:31 +00007001 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007002
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007003 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00007004 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00007005
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007006 Checked.insert(N);
7007 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00007008}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007009#endif
David Greene09851602010-01-20 20:13:31 +00007010
Adam Nemet7d394302014-05-31 16:23:17 +00007011void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00007012 const llvm::SelectionDAG *DAG,
7013 bool force) {
7014#ifndef NDEBUG
7015 bool check = force;
David Greene09851602010-01-20 20:13:31 +00007016#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00007017 check = true;
7018#endif // XDEBUG
7019 if (check) {
7020 assert(N && "Checking nonexistent SDNode");
7021 SmallPtrSet<const SDNode*, 32> visited;
7022 SmallPtrSet<const SDNode*, 32> checked;
7023 checkForCyclesHelper(N, visited, checked, DAG);
7024 }
7025#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00007026}
7027
Adam Nemetb4690e32014-05-31 16:23:20 +00007028void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7029 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00007030}