blob: 73fe1990711025ad9be9195f5a81a432b49da20f [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
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000403static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
404 bool exact) {
405 ID.AddBoolean(nuw);
406 ID.AddBoolean(nsw);
407 ID.AddBoolean(exact);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000408}
409
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000410/// 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);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000415}
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 }
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +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);
NAKAMURA Takumie4529982015-05-06 14:03:22 +0000518 AddBinaryNodeIDCustom(
519 ID, N->getOpcode(), BinNode->Flags.hasNoUnsignedWrap(),
520 BinNode->Flags.hasNoSignedWrap(), BinNode->Flags.hasExact());
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000521 break;
522 }
Dan Gohman12f24902008-12-23 21:37:04 +0000523 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000524 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000525 case ISD::ATOMIC_SWAP:
526 case ISD::ATOMIC_LOAD_ADD:
527 case ISD::ATOMIC_LOAD_SUB:
528 case ISD::ATOMIC_LOAD_AND:
529 case ISD::ATOMIC_LOAD_OR:
530 case ISD::ATOMIC_LOAD_XOR:
531 case ISD::ATOMIC_LOAD_NAND:
532 case ISD::ATOMIC_LOAD_MIN:
533 case ISD::ATOMIC_LOAD_MAX:
534 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000535 case ISD::ATOMIC_LOAD_UMAX:
536 case ISD::ATOMIC_LOAD:
537 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000538 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000539 ID.AddInteger(AT->getMemoryVT().getRawBits());
540 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000541 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
542 break;
543 }
544 case ISD::PREFETCH: {
545 const MemSDNode *PF = cast<MemSDNode>(N);
546 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000547 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000548 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000549 case ISD::VECTOR_SHUFFLE: {
550 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000551 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000552 i != e; ++i)
553 ID.AddInteger(SVN->getMaskElt(i));
554 break;
555 }
Dan Gohman6c938802009-10-30 01:27:03 +0000556 case ISD::TargetBlockAddress:
557 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000558 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
559 ID.AddPointer(BA->getBlockAddress());
560 ID.AddInteger(BA->getOffset());
561 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000562 break;
563 }
Mon P Wang6a490372008-06-25 08:15:39 +0000564 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000565
566 // Target specific memory nodes could also have address spaces to check.
567 if (N->isTargetMemoryOpcode())
568 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000569}
570
Duncan Sands835bdca2008-10-27 15:30:53 +0000571/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
572/// data.
573static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
574 AddNodeIDOpcode(ID, N->getOpcode());
575 // Add the return value info.
576 AddNodeIDValueTypes(ID, N->getVTList());
577 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000578 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000579
580 // Handle SDNode leafs with special info.
581 AddNodeIDCustom(ID, N);
582}
583
Dan Gohman2da2bed2008-08-20 15:58:01 +0000584/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000585/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000586/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000587///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000588static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000589encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000590 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000591 assert((ConvType & 3) == ConvType &&
592 "ConvType may not require more than 2 bits!");
593 assert((AM & 7) == AM &&
594 "AM may not require more than 3 bits!");
595 return ConvType |
596 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000597 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000598 (isNonTemporal << 6) |
599 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000600}
601
Jim Laskeyf576b422006-10-27 23:46:08 +0000602//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000603// SelectionDAG Class
604//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000605
Duncan Sands835bdca2008-10-27 15:30:53 +0000606/// doNotCSE - Return true if CSE should not be performed for this node.
607static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000608 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000609 return true; // Never CSE anything that produces a flag.
610
611 switch (N->getOpcode()) {
612 default: break;
613 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000614 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000615 return true; // Never CSE these nodes.
616 }
617
618 // Check that remaining values produced are not flags.
619 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000620 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000621 return true; // Never CSE anything that produces a flag.
622
623 return false;
624}
625
Chris Lattner9c667932005-01-07 21:09:16 +0000626/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000627/// SelectionDAG.
628void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000629 // Create a dummy node (which is not added to allnodes), that adds a reference
630 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000631 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000632
Chris Lattner8927c872006-08-04 17:45:20 +0000633 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000634
Chris Lattner8927c872006-08-04 17:45:20 +0000635 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000636 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000637 if (I->use_empty())
638 DeadNodes.push_back(I);
639
Dan Gohman91697632008-07-07 20:57:48 +0000640 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000641
Dan Gohman91697632008-07-07 20:57:48 +0000642 // If the root changed (e.g. it was a dead load, update the root).
643 setRoot(Dummy.getValue());
644}
645
646/// RemoveDeadNodes - This method deletes the unreachable nodes in the
647/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000648void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000649
Chris Lattner8927c872006-08-04 17:45:20 +0000650 // Process the worklist, deleting the nodes and adding their uses to the
651 // worklist.
652 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000653 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000654
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000655 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000656 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000657
Chris Lattner8927c872006-08-04 17:45:20 +0000658 // Take the node out of the appropriate CSE map.
659 RemoveNodeFromCSEMaps(N);
660
661 // Next, brutally remove the operand list. This is safe to do, as there are
662 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000663 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
664 SDUse &Use = *I++;
665 SDNode *Operand = Use.getNode();
666 Use.set(SDValue());
667
Chris Lattner8927c872006-08-04 17:45:20 +0000668 // Now that we removed this operand, see if there are no uses of it left.
669 if (Operand->use_empty())
670 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000671 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000672
Dan Gohman534c8a22009-01-19 22:39:36 +0000673 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000674 }
Chris Lattner9c667932005-01-07 21:09:16 +0000675}
676
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000677void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000678 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000679
680 // Create a dummy node that adds a reference to the root node, preventing
681 // it from being deleted. (This matters if the root is an operand of the
682 // dead node.)
683 HandleSDNode Dummy(getRoot());
684
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000685 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000686}
687
Chris Lattnerc738d002005-08-29 21:59:31 +0000688void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000689 // First take this out of the appropriate CSE map.
690 RemoveNodeFromCSEMaps(N);
691
Scott Michelcf0da6c2009-02-17 22:15:04 +0000692 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000693 // AllNodes list, and delete the node.
694 DeleteNodeNotInCSEMaps(N);
695}
696
697void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000698 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
699 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000700
Dan Gohman86aa16a2008-09-30 18:30:35 +0000701 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000702 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000703
Dan Gohman534c8a22009-01-19 22:39:36 +0000704 DeallocateNode(N);
705}
706
Frederic Riss8ad4f492014-11-11 21:21:08 +0000707void SDDbgInfo::erase(const SDNode *Node) {
708 DbgValMapType::iterator I = DbgValMap.find(Node);
709 if (I == DbgValMap.end())
710 return;
711 for (auto &Val: I->second)
712 Val->setIsInvalidated();
713 DbgValMap.erase(I);
714}
715
Dan Gohman534c8a22009-01-19 22:39:36 +0000716void SelectionDAG::DeallocateNode(SDNode *N) {
717 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000718 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000719
Dan Gohman534c8a22009-01-19 22:39:36 +0000720 // Set the opcode to DELETED_NODE to help catch bugs when node
721 // memory is reallocated.
722 N->NodeType = ISD::DELETED_NODE;
723
Dan Gohman2e834902008-08-26 01:44:34 +0000724 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000725
Frederic Riss8ad4f492014-11-11 21:21:08 +0000726 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
727 // them and forget about that node.
728 DbgInfo->erase(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000729}
730
Chandler Carruth41b20e72014-07-22 04:07:55 +0000731#ifndef NDEBUG
732/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
733static void VerifySDNode(SDNode *N) {
734 switch (N->getOpcode()) {
735 default:
736 break;
737 case ISD::BUILD_PAIR: {
738 EVT VT = N->getValueType(0);
739 assert(N->getNumValues() == 1 && "Too many results!");
740 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
741 "Wrong return type!");
742 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
743 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
744 "Mismatched operand types!");
745 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
746 "Wrong operand type!");
747 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
748 "Wrong return type size");
749 break;
750 }
751 case ISD::BUILD_VECTOR: {
752 assert(N->getNumValues() == 1 && "Too many results!");
753 assert(N->getValueType(0).isVector() && "Wrong return type!");
754 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
755 "Wrong number of operands!");
756 EVT EltVT = N->getValueType(0).getVectorElementType();
757 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
758 assert((I->getValueType() == EltVT ||
759 (EltVT.isInteger() && I->getValueType().isInteger() &&
760 EltVT.bitsLE(I->getValueType()))) &&
761 "Wrong operand type!");
762 assert(I->getValueType() == N->getOperand(0).getValueType() &&
763 "Operands must all have the same type");
764 }
765 break;
766 }
767 }
768}
769#endif // NDEBUG
770
771/// \brief Insert a newly allocated node into the DAG.
772///
773/// Handles insertion into the all nodes list and CSE map, as well as
774/// verification and other common operations when a new node is allocated.
775void SelectionDAG::InsertNode(SDNode *N) {
776 AllNodes.push_back(N);
777#ifndef NDEBUG
778 VerifySDNode(N);
779#endif
780}
781
Chris Lattner19732782005-08-16 18:17:10 +0000782/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
783/// correspond to it. This is useful when we're about to delete or repurpose
784/// the node. We don't want future request for structurally identical nodes
785/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000786bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000787 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000788 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000789 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000790 case ISD::CONDCODE:
791 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
792 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000793 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
794 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000795 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000796 case ISD::ExternalSymbol:
797 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000798 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000799 case ISD::TargetExternalSymbol: {
800 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
801 Erased = TargetExternalSymbols.erase(
802 std::pair<std::string,unsigned char>(ESN->getSymbol(),
803 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000804 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000805 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000806 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000807 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000808 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000809 Erased = ExtendedValueTypeNodes.erase(VT);
810 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000811 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
812 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000813 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000814 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000815 }
Chris Lattner9c667932005-01-07 21:09:16 +0000816 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000817 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000818 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
819 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000820 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000821 break;
822 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000823#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000824 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000825 // flag result (which cannot be CSE'd) or is one of the special cases that are
826 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000827 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000828 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000829 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000830 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000831 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000832 }
833#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000834 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000835}
836
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000837/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
838/// maps and modified in place. Add it back to the CSE maps, unless an identical
839/// node already exists, in which case transfer all its users to the existing
840/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000841///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000842void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000843SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000844 // For node types that aren't CSE'd, just act as if no identical node
845 // already exists.
846 if (!doNotCSE(N)) {
847 SDNode *Existing = CSEMap.GetOrInsertNode(N);
848 if (Existing != N) {
849 // If there was already an existing matching node, use ReplaceAllUsesWith
850 // to replace the dead one with the existing one. This can cause
851 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000852 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000853
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000854 // N is now dead. Inform the listeners and delete it.
855 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
856 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000857 DeleteNodeNotInCSEMaps(N);
858 return;
859 }
860 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000861
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000862 // If the node doesn't already exist, we updated it. Inform listeners.
863 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
864 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000865}
866
Chris Lattnerf34156e2006-01-28 09:32:45 +0000867/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000868/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000869/// return null, otherwise return a pointer to the slot it would take. If a
870/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000871SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000872 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000873 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000874 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000875
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000876 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000877 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000878 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000879 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000880 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000881 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000882}
883
884/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000885/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000886/// return null, otherwise return a pointer to the slot it would take. If a
887/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000888SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000889 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000890 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000891 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000892 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000893
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000894 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000895 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000896 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000897 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000898 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000899 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000900}
901
902
903/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000904/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000905/// return null, otherwise return a pointer to the slot it would take. If a
906/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000907SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000908 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000909 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000910 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000911
Jim Laskeyf576b422006-10-27 23:46:08 +0000912 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000913 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000914 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000915 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000916 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000917}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000918
Owen Anderson53aa7a92009-08-10 22:56:29 +0000919/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000920/// given type.
921///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000922unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000923 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000924 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000925 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000926
Eric Christopher1e845f22014-10-08 21:08:32 +0000927 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000928}
Chris Lattner9c667932005-01-07 21:09:16 +0000929
Dale Johannesen8ba713212009-02-07 02:15:05 +0000930// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000931SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000932 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000933 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
934 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
935 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000936 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000937 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000938}
939
Eric Christopher8d07f442014-10-08 01:57:58 +0000940void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000941 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000942 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000943 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000944 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000945}
946
Chris Lattner600d3082003-08-11 14:57:33 +0000947SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000948 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000949 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000950 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000951}
952
953void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000954 assert(&*AllNodes.begin() == &EntryNode);
955 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000956 while (!AllNodes.empty())
957 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000958}
959
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000960BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
961 SDVTList VTs, SDValue N1,
962 SDValue N2, bool nuw, bool nsw,
963 bool exact) {
964 if (isBinOpWithFlags(Opcode)) {
965 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
966 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
967 FN->Flags.setNoUnsignedWrap(nuw);
968 FN->Flags.setNoSignedWrap(nsw);
969 FN->Flags.setExact(exact);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000970
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000971 return FN;
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000972 }
973
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000974 BinarySDNode *N = new (NodeAllocator)
975 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000976 return N;
977}
978
Dan Gohmane1a9a782008-08-27 23:52:12 +0000979void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000980 allnodes_clear();
981 OperandAllocator.Reset();
982 CSEMap.clear();
983
984 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000985 ExternalSymbols.clear();
986 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000987 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000988 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000989 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000990 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000991
Craig Topperc0196b12014-04-14 00:51:57 +0000992 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000993 AllNodes.push_back(&EntryNode);
994 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000995 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000996}
997
Andrew Trickef9de2a2013-05-25 02:42:55 +0000998SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000999 return VT.bitsGT(Op.getValueType()) ?
1000 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1001 getNode(ISD::TRUNCATE, DL, VT, Op);
1002}
1003
Andrew Trickef9de2a2013-05-25 02:42:55 +00001004SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001005 return VT.bitsGT(Op.getValueType()) ?
1006 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1007 getNode(ISD::TRUNCATE, DL, VT, Op);
1008}
1009
Andrew Trickef9de2a2013-05-25 02:42:55 +00001010SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001011 return VT.bitsGT(Op.getValueType()) ?
1012 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1013 getNode(ISD::TRUNCATE, DL, VT, Op);
1014}
1015
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001016SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1017 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001018 if (VT.bitsLE(Op.getValueType()))
1019 return getNode(ISD::TRUNCATE, SL, VT, Op);
1020
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001021 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001022 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1023}
1024
Andrew Trickef9de2a2013-05-25 02:42:55 +00001025SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001026 assert(!VT.isVector() &&
1027 "getZeroExtendInReg should use the vector element type instead of "
1028 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001029 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001030 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1031 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001032 VT.getSizeInBits());
1033 return getNode(ISD::AND, DL, Op.getValueType(), Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001034 getConstant(Imm, DL, Op.getValueType()));
Bill Wendlingc4093182009-01-30 22:23:15 +00001035}
1036
Chandler Carruth0b666e02014-07-10 12:32:32 +00001037SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1038 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1039 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1040 "The sizes of the input and result must match in order to perform the "
1041 "extend in-register.");
1042 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1043 "The destination vector type must have fewer lanes than the input.");
1044 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1045}
1046
1047SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1048 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1049 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1050 "The sizes of the input and result must match in order to perform the "
1051 "extend in-register.");
1052 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1053 "The destination vector type must have fewer lanes than the input.");
1054 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1055}
1056
Chandler Carruthafe4b252014-07-09 10:58:18 +00001057SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1058 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001059 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1060 "The sizes of the input and result must match in order to perform the "
1061 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001062 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1063 "The destination vector type must have fewer lanes than the input.");
1064 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1065}
1066
Bob Wilsonc5890052009-01-22 17:39:32 +00001067/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1068///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001069SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001070 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001071 SDValue NegOne =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001072 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001073 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1074}
1075
Pete Cooper7fd1d722014-05-12 23:26:58 +00001076SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1077 EVT EltVT = VT.getScalarType();
1078 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001079 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001080 case TargetLowering::ZeroOrOneBooleanContent:
1081 case TargetLowering::UndefinedBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001082 TrueValue = getConstant(1, DL, VT);
Pete Cooper7fd1d722014-05-12 23:26:58 +00001083 break;
1084 case TargetLowering::ZeroOrNegativeOneBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001085 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
Pete Cooper7fd1d722014-05-12 23:26:58 +00001086 VT);
1087 break;
1088 }
1089 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1090}
1091
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001092SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT,
1093 bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001094 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001095 assert((EltVT.getSizeInBits() >= 64 ||
1096 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1097 "getConstant with a uint64_t value that doesn't fit in the type!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001098 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001099}
1100
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001101SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT,
1102 bool isO)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001103{
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001104 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001105}
1106
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001107SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT,
1108 bool isT, bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001109 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001110
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001111 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001112 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001113
Duncan Sandsf2641e12011-09-06 19:07:46 +00001114 // In some cases the vector type is legal but the element type is illegal and
1115 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1116 // inserted value (the type does not need to match the vector element type).
1117 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001118 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001119 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001120 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001121 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1122 Elt = ConstantInt::get(*getContext(), NewVal);
1123 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001124 // In other cases the element type is illegal and needs to be expanded, for
1125 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1126 // the value into n parts and use a vector type with n-times the elements.
1127 // Then bitcast to the type requested.
1128 // Legalizing constants too early makes the DAGCombiner's job harder so we
1129 // only legalize if the DAG tells us we must produce legal types.
1130 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1131 TLI->getTypeAction(*getContext(), EltVT) ==
1132 TargetLowering::TypeExpandInteger) {
1133 APInt NewVal = Elt->getValue();
1134 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1135 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1136 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1137 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1138
1139 // Check the temporary vector is the correct size. If this fails then
1140 // getTypeToTransformTo() probably returned a type whose size (in bits)
1141 // isn't a power-of-2 factor of the requested type size.
1142 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1143
1144 SmallVector<SDValue, 2> EltParts;
1145 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1146 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001147 .trunc(ViaEltSizeInBits), DL,
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001148 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001149 }
1150
1151 // EltParts is currently in little endian order. If we actually want
1152 // big-endian order then reverse it now.
1153 if (TLI->isBigEndian())
1154 std::reverse(EltParts.begin(), EltParts.end());
1155
1156 // The elements must be reversed when the element order is different
1157 // to the endianness of the elements (because the BITCAST is itself a
1158 // vector shuffle in this situation). However, we do not need any code to
1159 // perform this reversal because getConstant() is producing a vector
1160 // splat.
1161 // This situation occurs in MIPS MSA.
1162
1163 SmallVector<SDValue, 8> Ops;
1164 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1165 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1166
1167 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1168 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001169 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001170 return Result;
1171 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001172
1173 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1174 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001175 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001176 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001177 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001178 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001179 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001180 void *IP = nullptr;
1181 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001182 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001183 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001184 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001185
Dan Gohman7a7742c2007-12-12 22:21:26 +00001186 if (!N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001187 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, DL.getDebugLoc(),
1188 EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001189 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001190 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001191 }
1192
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001193 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001194 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001195 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001196 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001197 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001198 }
1199 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001200}
1201
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001202SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) {
1203 return getConstant(Val, DL, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001204}
1205
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001206SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT,
1207 bool isTarget) {
1208 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001209}
1210
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001211SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT,
1212 bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001213 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001214
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001215 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001216
Chris Lattner381dddc2005-02-17 20:17:32 +00001217 // Do the map lookup using the actual bit pattern for the floating point
1218 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1219 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001220 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001221 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001222 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001223 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001224 void *IP = nullptr;
1225 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001226 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001227 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001228 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001229
Evan Cheng9458e6a2007-06-29 21:36:04 +00001230 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001231 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001232 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001233 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001234 }
1235
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001236 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001237 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001239 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001240 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001241 }
1242 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001243}
1244
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001245SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT,
1246 bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001247 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001248 if (EltVT==MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001249 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001250 else if (EltVT==MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001251 return getConstantFP(APFloat(Val), DL, VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001252 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1253 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001254 bool ignored;
1255 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001256 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001257 &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001258 return getConstantFP(apf, DL, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001259 } else
1260 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001261}
1262
Andrew Trickef9de2a2013-05-25 02:42:55 +00001263SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001264 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001265 bool isTargetGA,
1266 unsigned char TargetFlags) {
1267 assert((TargetFlags == 0 || isTargetGA) &&
1268 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001269
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001270 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001271 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001272 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001273 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001274
Chris Lattner8e34f982009-06-25 21:21:14 +00001275 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001276 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001277 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1278 else
1279 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001280
Jim Laskeyf576b422006-10-27 23:46:08 +00001281 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001282 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001283 ID.AddPointer(GV);
1284 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001285 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001286 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001287 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001288 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001289 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001290
Andrew Trickef9de2a2013-05-25 02:42:55 +00001291 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1292 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001293 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001294 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001295 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001296 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001297}
1298
Owen Anderson53aa7a92009-08-10 22:56:29 +00001299SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001300 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001301 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001302 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001303 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001304 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001305 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001306 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001307
Dan Gohman01c65a22010-03-18 18:49:47 +00001308 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001309 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001310 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001311 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001312}
1313
Owen Anderson53aa7a92009-08-10 22:56:29 +00001314SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001315 unsigned char TargetFlags) {
1316 assert((TargetFlags == 0 || isTarget) &&
1317 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001318 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001319 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001320 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001321 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001322 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001323 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001324 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001325 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001326
Dan Gohman01c65a22010-03-18 18:49:47 +00001327 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1328 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001329 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001330 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001331 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001332}
1333
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001334SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001335 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001336 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001337 unsigned char TargetFlags) {
1338 assert((TargetFlags == 0 || isTarget) &&
1339 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001340 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001341 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001342 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001343 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001344 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001345 ID.AddInteger(Alignment);
1346 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001347 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001348 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001349 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001350 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001351 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001352
Dan Gohman01c65a22010-03-18 18:49:47 +00001353 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1354 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001355 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001356 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001358}
1359
Chris Lattner061a1ea2005-01-07 07:46:32 +00001360
Owen Anderson53aa7a92009-08-10 22:56:29 +00001361SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001362 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001363 bool isTarget,
1364 unsigned char TargetFlags) {
1365 assert((TargetFlags == 0 || isTarget) &&
1366 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001367 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001368 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001369 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001370 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001371 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001372 ID.AddInteger(Alignment);
1373 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001374 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001375 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001376 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001377 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001378 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001379
Dan Gohman01c65a22010-03-18 18:49:47 +00001380 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1381 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001382 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001383 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001384 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001385}
1386
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001387SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1388 unsigned char TargetFlags) {
1389 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001390 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001391 ID.AddInteger(Index);
1392 ID.AddInteger(Offset);
1393 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001394 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1396 return SDValue(E, 0);
1397
1398 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1399 TargetFlags);
1400 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001401 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001402 return SDValue(N, 0);
1403}
1404
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001405SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001406 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001407 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001408 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001409 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001410 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001411 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001412
Dan Gohman01c65a22010-03-18 18:49:47 +00001413 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001414 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001415 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001416 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001417}
1418
Owen Anderson53aa7a92009-08-10 22:56:29 +00001419SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001420 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1421 ValueTypeNodes.size())
1422 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001423
Duncan Sands13237ac2008-06-06 12:08:01 +00001424 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001425 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001426
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001427 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001428 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001429 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001430 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001431}
1432
Owen Anderson53aa7a92009-08-10 22:56:29 +00001433SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001434 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001435 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001436 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001437 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001438 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001439}
1440
Owen Anderson53aa7a92009-08-10 22:56:29 +00001441SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001442 unsigned char TargetFlags) {
1443 SDNode *&N =
1444 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1445 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001446 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001447 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001448 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001449 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001450}
1451
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001452SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001453 if ((unsigned)Cond >= CondCodeNodes.size())
1454 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001455
Craig Topperc0196b12014-04-14 00:51:57 +00001456 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001457 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001458 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001459 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001460 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001461
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001462 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001463}
1464
Nate Begeman5f829d82009-04-29 05:20:52 +00001465// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1466// the shuffle mask M that point at N1 to point at N2, and indices that point
1467// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001468static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1469 std::swap(N1, N2);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001470 ShuffleVectorSDNode::commuteMask(M);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001471}
1472
Andrew Trickef9de2a2013-05-25 02:42:55 +00001473SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001474 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001475 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1476 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001477
1478 // Canonicalize shuffle undef, undef -> undef
1479 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001480 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001481
Eric Christopherdfda92b2009-08-22 00:40:45 +00001482 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001483 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001484 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001485 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001486 for (unsigned i = 0; i != NElts; ++i) {
1487 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001488 MaskVec.push_back(Mask[i]);
1489 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001490
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001491 // Canonicalize shuffle v, v -> v, undef
1492 if (N1 == N2) {
1493 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001494 for (unsigned i = 0; i != NElts; ++i)
1495 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001496 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001497
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001498 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1499 if (N1.getOpcode() == ISD::UNDEF)
1500 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001501
Chandler Carruth1b5285d2015-02-15 12:18:12 +00001502 // If shuffling a splat, try to blend the splat instead. We do this here so
1503 // that even when this arises during lowering we don't have to re-handle it.
1504 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1505 BitVector UndefElements;
1506 SDValue Splat = BV->getSplatValue(&UndefElements);
1507 if (!Splat)
1508 return;
1509
1510 for (int i = 0; i < (int)NElts; ++i) {
1511 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1512 continue;
1513
1514 // If this input comes from undef, mark it as such.
1515 if (UndefElements[MaskVec[i] - Offset]) {
1516 MaskVec[i] = -1;
1517 continue;
1518 }
1519
1520 // If we can blend a non-undef lane, use that instead.
1521 if (!UndefElements[i])
1522 MaskVec[i] = i + Offset;
1523 }
1524 };
1525 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1526 BlendSplat(N1BV, 0);
1527 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1528 BlendSplat(N2BV, NElts);
1529
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001530 // Canonicalize all index into lhs, -> shuffle lhs, undef
1531 // Canonicalize all index into rhs, -> shuffle rhs, undef
1532 bool AllLHS = true, AllRHS = true;
1533 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001534 for (unsigned i = 0; i != NElts; ++i) {
1535 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001536 if (N2Undef)
1537 MaskVec[i] = -1;
1538 else
1539 AllLHS = false;
1540 } else if (MaskVec[i] >= 0) {
1541 AllRHS = false;
1542 }
1543 }
1544 if (AllLHS && AllRHS)
1545 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001546 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001547 N2 = getUNDEF(VT);
1548 if (AllRHS) {
1549 N1 = getUNDEF(VT);
1550 commuteShuffle(N1, N2, MaskVec);
1551 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001552 // Reset our undef status after accounting for the mask.
1553 N2Undef = N2.getOpcode() == ISD::UNDEF;
1554 // Re-check whether both sides ended up undef.
1555 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1556 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001557
Craig Topper9a39b072013-08-08 08:03:12 +00001558 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001559 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001560 for (unsigned i = 0; i != NElts; ++i) {
1561 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001562 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001563 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001564 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001565 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001566
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001567 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001568 if (N2Undef) {
1569 SDValue V = N1;
1570
1571 // Look through any bitcasts. We check that these don't change the number
1572 // (and size) of elements and just changes their types.
1573 while (V.getOpcode() == ISD::BITCAST)
1574 V = V->getOperand(0);
1575
1576 // A splat should always show up as a build vector node.
1577 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001578 BitVector UndefElements;
1579 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001580 // If this is a splat of an undef, shuffling it is also undef.
1581 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1582 return getUNDEF(VT);
1583
Andrea Di Biagio83814752015-01-24 11:54:29 +00001584 bool SameNumElts =
1585 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1586
Chandler Carruth142e9662014-07-08 08:45:38 +00001587 // We only have a splat which can skip shuffles if there is a splatted
1588 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001589 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001590 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1591 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001592 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001593 return N1;
1594 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1595 if (C->isNullValue())
1596 return N1;
1597 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001598
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001599 // If the shuffle itself creates a splat, build the vector directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001600 if (AllSame && SameNumElts) {
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001601 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1602 SmallVector<SDValue, 8> Ops(NElts, Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001603
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001604 EVT BuildVT = BV->getValueType(0);
1605 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001606
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001607 // We may have jumped through bitcasts, so the type of the
1608 // BUILD_VECTOR may not match the type of the shuffle.
1609 if (BuildVT != VT)
1610 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1611 return NewBV;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001612 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001613 }
1614 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001615
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001616 FoldingSetNodeID ID;
1617 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001618 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001619 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001620 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001621
Craig Topperc0196b12014-04-14 00:51:57 +00001622 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001623 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001624 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001625
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001626 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1627 // SDNode doesn't have access to it. This memory will be "leaked" when
1628 // the node is deallocated, but recovered when the NodeAllocator is released.
1629 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1630 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001631
Dan Gohman01c65a22010-03-18 18:49:47 +00001632 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001633 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1634 dl.getDebugLoc(), N1, N2,
1635 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001636 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001637 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001638 return SDValue(N, 0);
1639}
1640
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001641SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1642 MVT VT = SV.getSimpleValueType(0);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001643 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1644 ShuffleVectorSDNode::commuteMask(MaskVec);
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001645
1646 SDValue Op0 = SV.getOperand(0);
1647 SDValue Op1 = SV.getOperand(1);
1648 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1649}
1650
Andrew Trickef9de2a2013-05-25 02:42:55 +00001651SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001652 SDValue Val, SDValue DTy,
1653 SDValue STy, SDValue Rnd, SDValue Sat,
1654 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001655 // If the src and dest types are the same and the conversion is between
1656 // integer types of the same sign or two floats, no conversion is necessary.
1657 if (DTy == STy &&
1658 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001659 return Val;
1660
1661 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001662 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001663 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001664 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001665 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001666 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001667
Jack Carter170a5f22013-09-09 22:02:08 +00001668 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1669 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001670 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001671 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001672 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001673 return SDValue(N, 0);
1674}
1675
Owen Anderson53aa7a92009-08-10 22:56:29 +00001676SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001677 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001678 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001679 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001680 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001681 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001682 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001683
Dan Gohman01c65a22010-03-18 18:49:47 +00001684 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001685 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001686 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001687 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001688}
1689
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001690SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1691 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001692 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001693 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001694 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001695 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1696 return SDValue(E, 0);
1697
1698 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1699 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001700 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001701 return SDValue(N, 0);
1702}
1703
Andrew Trickef9de2a2013-05-25 02:42:55 +00001704SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001705 FoldingSetNodeID ID;
1706 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001707 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001708 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001709 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001710 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001711 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001712
Jack Carter170a5f22013-09-09 22:02:08 +00001713 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1714 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001715 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001716 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001717 return SDValue(N, 0);
1718}
1719
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001720
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001721SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001722 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001723 bool isTarget,
1724 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001725 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1726
1727 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001728 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001729 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001730 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001731 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001732 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001733 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001734 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001735
Michael Liaoabb87d42012-09-12 21:43:09 +00001736 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1737 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001738 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001739 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001740 return SDValue(N, 0);
1741}
1742
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001743SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001744 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001745 "SrcValue is not a pointer?");
1746
Jim Laskeyf576b422006-10-27 23:46:08 +00001747 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001748 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001749 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001750
Craig Topperc0196b12014-04-14 00:51:57 +00001751 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001752 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001753 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001754
Dan Gohman01c65a22010-03-18 18:49:47 +00001755 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001756 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001757 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001758 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001759}
1760
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001761/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1762SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1763 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001764 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001765 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001766
Craig Topperc0196b12014-04-14 00:51:57 +00001767 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001768 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1769 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001770
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001771 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1772 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001773 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001774 return SDValue(N, 0);
1775}
1776
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001777/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1778SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1779 unsigned SrcAS, unsigned DestAS) {
1780 SDValue Ops[] = {Ptr};
1781 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001782 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001783 ID.AddInteger(SrcAS);
1784 ID.AddInteger(DestAS);
1785
Craig Topperc0196b12014-04-14 00:51:57 +00001786 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1788 return SDValue(E, 0);
1789
1790 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1791 dl.getDebugLoc(),
1792 VT, Ptr, SrcAS, DestAS);
1793 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001794 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001795 return SDValue(N, 0);
1796}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001797
Duncan Sands41826032009-01-31 15:50:11 +00001798/// getShiftAmountOperand - Return the specified value casted to
1799/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001800SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001801 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001802 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001803 if (OpTy == ShTy || OpTy.isVector()) return Op;
1804
1805 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001806 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001807}
1808
Chris Lattner9eb7a822007-10-15 17:47:20 +00001809/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1810/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001811SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001812 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001813 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001814 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001815 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001816 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001817
David Greene1fbe0542009-11-12 20:49:22 +00001818 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001819 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001820}
1821
Duncan Sands445071c2008-12-09 21:33:20 +00001822/// CreateStackTemporary - Create a stack temporary suitable for holding
1823/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001824SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001825 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1826 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001827 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1828 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001829 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001830 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1831 TD->getPrefTypeAlignment(Ty2));
1832
1833 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001834 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001835 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001836}
1837
Owen Anderson53aa7a92009-08-10 22:56:29 +00001838SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001839 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001840 // These setcc operations always fold.
1841 switch (Cond) {
1842 default: break;
1843 case ISD::SETFALSE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001844 case ISD::SETFALSE2: return getConstant(0, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001845 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001846 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001847 TargetLowering::BooleanContent Cnt =
1848 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001849 return getConstant(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001850 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1851 VT);
Tim Northover950fcc02013-09-06 12:38:12 +00001852 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001853
Chris Lattner393d96a2006-04-27 05:01:07 +00001854 case ISD::SETOEQ:
1855 case ISD::SETOGT:
1856 case ISD::SETOGE:
1857 case ISD::SETOLT:
1858 case ISD::SETOLE:
1859 case ISD::SETONE:
1860 case ISD::SETO:
1861 case ISD::SETUO:
1862 case ISD::SETUEQ:
1863 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001864 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001865 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001866 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001867
Gabor Greiff304a7a2008-08-28 21:40:38 +00001868 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001869 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001870 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001871 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001872
Chris Lattner061a1ea2005-01-07 07:46:32 +00001873 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001874 default: llvm_unreachable("Unknown integer setcc!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001875 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT);
1876 case ISD::SETNE: return getConstant(C1 != C2, dl, VT);
1877 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1878 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1879 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1880 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1881 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT);
1882 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT);
1883 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT);
1884 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001885 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001886 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001887 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001888 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1889 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001890 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001891 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001892 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001893 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001894 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001895 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001896 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001897 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001898 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001899 // fall through
1900 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001901 R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001902 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001903 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001904 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001905 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001906 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001907 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001908 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001909 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001910 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001911 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001912 // fall through
1913 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001914 R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001915 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001916 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001917 // fall through
1918 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001919 R==APFloat::cmpEqual, dl, VT);
1920 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1921 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001922 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001923 R==APFloat::cmpEqual, dl, VT);
1924 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001925 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001926 R==APFloat::cmpLessThan, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001927 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001928 R==APFloat::cmpUnordered, dl, VT);
1929 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1930 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001931 }
1932 } else {
1933 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001934 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1935 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001936 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001937 return SDValue();
1938
1939 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001940 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001941 }
1942
Chris Lattnerd47675e2005-08-09 20:20:18 +00001943 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001944 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001945}
1946
Dan Gohman1f372ed2008-02-25 21:11:39 +00001947/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1948/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001949bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001950 // This predicate is not safe for vector operations.
1951 if (Op.getValueType().isVector())
1952 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001953
Dan Gohman1d459e42009-12-11 21:31:27 +00001954 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001955 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1956}
1957
Dan Gohman309d3d52007-06-22 14:59:07 +00001958/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1959/// this predicate to simplify operations downstream. Mask is known to be zero
1960/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001961bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001962 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001963 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001964 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001965 return (KnownZero & Mask) == Mask;
1966}
1967
Jay Foada0653a32014-05-14 21:14:37 +00001968/// Determine which bits of Op are known to be either zero or one and return
1969/// them in the KnownZero/KnownOne bitsets.
1970void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1971 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001972 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001973
Dan Gohmanf990faf2008-02-13 00:35:47 +00001974 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001975 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001976 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001977
Dan Gohmanf990faf2008-02-13 00:35:47 +00001978 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001979
1980 switch (Op.getOpcode()) {
1981 case ISD::Constant:
1982 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001983 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1984 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001985 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001986 case ISD::AND:
1987 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001988 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1989 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001990
1991 // Output known-1 bits are only known if set in both the LHS & RHS.
1992 KnownOne &= KnownOne2;
1993 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1994 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001995 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001996 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001997 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1998 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001999
Dan Gohman309d3d52007-06-22 14:59:07 +00002000 // Output known-0 bits are only known if clear in both the LHS & RHS.
2001 KnownZero &= KnownZero2;
2002 // Output known-1 are known to be set if set in either the LHS | RHS.
2003 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00002004 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002005 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00002006 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2007 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002008
Dan Gohman309d3d52007-06-22 14:59:07 +00002009 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002010 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00002011 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2012 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2013 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00002014 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002015 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002016 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00002017 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2018 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002019
2020 // If low bits are zero in either operand, output low known-0 bits.
2021 // Also compute a conserative estimate for high known-0 bits.
2022 // More trickiness is possible, but this is sufficient for the
2023 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00002024 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00002025 unsigned TrailZ = KnownZero.countTrailingOnes() +
2026 KnownZero2.countTrailingOnes();
2027 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002028 KnownZero2.countLeadingOnes(),
2029 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002030
2031 TrailZ = std::min(TrailZ, BitWidth);
2032 LeadZ = std::min(LeadZ, BitWidth);
2033 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2034 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002035 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002036 }
2037 case ISD::UDIV: {
2038 // For the purposes of computing leading zeros we can conservatively
2039 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002040 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002041 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002042 unsigned LeadZ = KnownZero2.countLeadingOnes();
2043
Jay Foad25a5e4c2010-12-01 08:53:58 +00002044 KnownOne2.clearAllBits();
2045 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002046 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002047 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2048 if (RHSUnknownLeadingOnes != BitWidth)
2049 LeadZ = std::min(BitWidth,
2050 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002051
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002052 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002053 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002054 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002055 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002056 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2057 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002058
Dan Gohman309d3d52007-06-22 14:59:07 +00002059 // Only known if known in both the LHS and RHS.
2060 KnownOne &= KnownOne2;
2061 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002062 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002063 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002064 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2065 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002066
Dan Gohman309d3d52007-06-22 14:59:07 +00002067 // Only known if known in both the LHS and RHS.
2068 KnownOne &= KnownOne2;
2069 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002070 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002071 case ISD::SADDO:
2072 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002073 case ISD::SSUBO:
2074 case ISD::USUBO:
2075 case ISD::SMULO:
2076 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002077 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002078 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002079 // The boolean result conforms to getBooleanContents.
2080 // If we know the result of a setcc has the top bits zero, use this info.
2081 // We know that we have an integer-based boolean since these operations
2082 // are only available for integer.
2083 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2084 TargetLowering::ZeroOrOneBooleanContent &&
2085 BitWidth > 1)
2086 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2087 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002088 case ISD::SETCC:
2089 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002090 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2091 TargetLowering::ZeroOrOneBooleanContent &&
2092 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002093 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002094 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002095 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002096 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002097 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002098 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002099
2100 // If the shift count is an invalid immediate, don't do anything.
2101 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002102 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002103
Jay Foada0653a32014-05-14 21:14:37 +00002104 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002105 KnownZero <<= ShAmt;
2106 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002107 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002108 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002109 }
Jay Foad5a29c362014-05-15 12:12:55 +00002110 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002111 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002112 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002113 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002114 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002115
Dan Gohman9db0aa82008-02-26 18:50:50 +00002116 // If the shift count is an invalid immediate, don't do anything.
2117 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002118 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002119
Jay Foada0653a32014-05-14 21:14:37 +00002120 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002121 KnownZero = KnownZero.lshr(ShAmt);
2122 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002123
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002124 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002125 KnownZero |= HighBits; // High bits known zero.
2126 }
Jay Foad5a29c362014-05-15 12:12:55 +00002127 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002128 case ISD::SRA:
2129 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002130 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002131
Dan Gohman9db0aa82008-02-26 18:50:50 +00002132 // If the shift count is an invalid immediate, don't do anything.
2133 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002134 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002135
Dan Gohman309d3d52007-06-22 14:59:07 +00002136 // If any of the demanded bits are produced by the sign extension, we also
2137 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002138 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002139
Jay Foada0653a32014-05-14 21:14:37 +00002140 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002141 KnownZero = KnownZero.lshr(ShAmt);
2142 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002143
Dan Gohman309d3d52007-06-22 14:59:07 +00002144 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002145 APInt SignBit = APInt::getSignBit(BitWidth);
2146 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002147
Dan Gohmanb717fda2008-02-20 16:30:17 +00002148 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002149 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002150 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002151 KnownOne |= HighBits; // New bits are known one.
2152 }
2153 }
Jay Foad5a29c362014-05-15 12:12:55 +00002154 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002155 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002156 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002157 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002158
2159 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002160 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002161 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002162
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002163 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002164 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002165
Dan Gohman309d3d52007-06-22 14:59:07 +00002166 // If the sign extended bits are demanded, we know that the sign
2167 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002168 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002169 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002170 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002171
Jay Foada0653a32014-05-14 21:14:37 +00002172 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002173 KnownOne &= InputDemandedBits;
2174 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002175
Dan Gohman309d3d52007-06-22 14:59:07 +00002176 // If the sign bit of the input is known set or clear, then we know the
2177 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002178 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002179 KnownZero |= NewBits;
2180 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002181 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002182 KnownOne |= NewBits;
2183 KnownZero &= ~NewBits;
2184 } else { // Input sign bit unknown
2185 KnownZero &= ~NewBits;
2186 KnownOne &= ~NewBits;
2187 }
Jay Foad5a29c362014-05-15 12:12:55 +00002188 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002189 }
2190 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002191 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002192 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002193 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002194 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002195 unsigned LowBits = Log2_32(BitWidth)+1;
2196 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002197 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002198 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002199 }
2200 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002201 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002202 // If this is a ZEXTLoad and we are looking at the loaded value.
2203 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002204 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002205 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002206 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002207 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002208 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002209 }
Jay Foad5a29c362014-05-15 12:12:55 +00002210 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002211 }
2212 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002213 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002214 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002215 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002216 KnownZero = KnownZero.trunc(InBits);
2217 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002218 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002219 KnownZero = KnownZero.zext(BitWidth);
2220 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002221 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002222 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002223 }
2224 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002225 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002226 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002227 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002228
Jay Foad583abbc2010-12-07 08:25:19 +00002229 KnownZero = KnownZero.trunc(InBits);
2230 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002231 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002232
2233 // Note if the sign bit is known to be zero or one.
2234 bool SignBitKnownZero = KnownZero.isNegative();
2235 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002236
Jay Foad583abbc2010-12-07 08:25:19 +00002237 KnownZero = KnownZero.zext(BitWidth);
2238 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002239
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002240 // If the sign bit is known zero or one, the top bits match.
2241 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002242 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002243 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002244 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002245 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002246 }
2247 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002248 EVT InVT = Op.getOperand(0).getValueType();
2249 unsigned InBits = InVT.getScalarType().getSizeInBits();
2250 KnownZero = KnownZero.trunc(InBits);
2251 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002252 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002253 KnownZero = KnownZero.zext(BitWidth);
2254 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002255 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002256 }
2257 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002258 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002259 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002260 KnownZero = KnownZero.zext(InBits);
2261 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002262 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002263 KnownZero = KnownZero.trunc(BitWidth);
2264 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002265 break;
2266 }
2267 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002268 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002269 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002270 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002271 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002272 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002273 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002274 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002275 case ISD::FGETSIGN:
2276 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002277 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002278 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002279
Dan Gohman72ec3f42008-04-28 17:02:21 +00002280 case ISD::SUB: {
2281 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2282 // We know that the top bits of C-X are clear if X contains less bits
2283 // than C (i.e. no wrap-around can happen). For example, 20-X is
2284 // positive if we can prove that X is >= 0 and < 16.
2285 if (CLHS->getAPIntValue().isNonNegative()) {
2286 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2287 // NLZ can't be BitWidth with no sign bit
2288 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002289 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002290
2291 // If all of the MaskV bits are known to be zero, then we know the
2292 // output top bits are zero, because we now know that the output is
2293 // from [0-C].
2294 if ((KnownZero2 & MaskV) == MaskV) {
2295 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2296 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002297 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002298 }
2299 }
2300 }
2301 }
2302 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002303 case ISD::ADD:
2304 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002305 // Output known-0 bits are known if clear or set in both the low clear bits
2306 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2307 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002308 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002309 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2310
Jay Foada0653a32014-05-14 21:14:37 +00002311 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002312 KnownZeroOut = std::min(KnownZeroOut,
2313 KnownZero2.countTrailingOnes());
2314
Chris Lattner440b2802010-12-19 20:38:28 +00002315 if (Op.getOpcode() == ISD::ADD) {
2316 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002317 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002318 }
2319
2320 // With ADDE, a carry bit may be added in, so we can only use this
2321 // information if we know (at least) that the low two bits are clear. We
2322 // then return to the caller that the low bit is unknown but that other bits
2323 // are known zero.
2324 if (KnownZeroOut >= 2) // ADDE
2325 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002326 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002327 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002328 case ISD::SREM:
2329 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002330 const APInt &RA = Rem->getAPIntValue().abs();
2331 if (RA.isPowerOf2()) {
2332 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002333 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002334
Duncan Sands33274982010-01-29 09:45:26 +00002335 // The low bits of the first operand are unchanged by the srem.
2336 KnownZero = KnownZero2 & LowBits;
2337 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002338
Duncan Sands33274982010-01-29 09:45:26 +00002339 // If the first operand is non-negative or has all low bits zero, then
2340 // the upper bits are all zero.
2341 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2342 KnownZero |= ~LowBits;
2343
2344 // If the first operand is negative and not all low bits are zero, then
2345 // the upper bits are all one.
2346 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2347 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002348 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002349 }
2350 }
Jay Foad5a29c362014-05-15 12:12:55 +00002351 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002352 case ISD::UREM: {
2353 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002354 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002355 if (RA.isPowerOf2()) {
2356 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002357 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2358
2359 // The upper bits are all zero, the lower ones are unchanged.
2360 KnownZero = KnownZero2 | ~LowBits;
2361 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002362 break;
2363 }
2364 }
2365
2366 // Since the result is less than or equal to either operand, any leading
2367 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002368 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2369 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002370
2371 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2372 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002373 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002374 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002375 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002376 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002377 case ISD::EXTRACT_ELEMENT: {
2378 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2379 const unsigned Index =
2380 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2381 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2382
2383 // Remove low part of known bits mask
2384 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2385 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2386
2387 // Remove high part of known bit mask
2388 KnownZero = KnownZero.trunc(BitWidth);
2389 KnownOne = KnownOne.trunc(BitWidth);
2390 break;
2391 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002392 case ISD::FrameIndex:
2393 case ISD::TargetFrameIndex:
2394 if (unsigned Align = InferPtrAlignment(Op)) {
2395 // The low bits are known zero if the pointer is aligned.
2396 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002397 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002398 }
2399 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002400
Dan Gohman309d3d52007-06-22 14:59:07 +00002401 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002402 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2403 break;
2404 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002405 case ISD::INTRINSIC_WO_CHAIN:
2406 case ISD::INTRINSIC_W_CHAIN:
2407 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002408 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002409 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002410 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002411 }
Jay Foad5a29c362014-05-15 12:12:55 +00002412
2413 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002414}
2415
2416/// ComputeNumSignBits - Return the number of times the sign bit of the
2417/// register is replicated into the other bits. We know that at least 1 bit
2418/// is always equal to the sign bit (itself), but other cases can give us
2419/// information. For example, immediately after an "SRA X, 2", we know that
2420/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002421unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002422 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002423 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002424 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002425 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002426 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002427
Dan Gohman309d3d52007-06-22 14:59:07 +00002428 if (Depth == 6)
2429 return 1; // Limit search depth.
2430
2431 switch (Op.getOpcode()) {
2432 default: break;
2433 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002434 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002435 return VTBits-Tmp+1;
2436 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002437 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002438 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002439
Dan Gohman309d3d52007-06-22 14:59:07 +00002440 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002441 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002442 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002443 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002444
Dan Gohman309d3d52007-06-22 14:59:07 +00002445 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002446 Tmp =
2447 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002448 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002449
Dan Gohman309d3d52007-06-22 14:59:07 +00002450 case ISD::SIGN_EXTEND_INREG:
2451 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002452 Tmp =
2453 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002454 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002455
Dan Gohman309d3d52007-06-22 14:59:07 +00002456 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2457 return std::max(Tmp, Tmp2);
2458
2459 case ISD::SRA:
2460 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2461 // SRA X, C -> adds C sign bits.
2462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002463 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002464 if (Tmp > VTBits) Tmp = VTBits;
2465 }
2466 return Tmp;
2467 case ISD::SHL:
2468 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2469 // shl destroys sign bits.
2470 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002471 if (C->getZExtValue() >= VTBits || // Bad shift.
2472 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2473 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002474 }
2475 break;
2476 case ISD::AND:
2477 case ISD::OR:
2478 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002479 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002480 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002481 if (Tmp != 1) {
2482 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2483 FirstAnswer = std::min(Tmp, Tmp2);
2484 // We computed what we know about the sign bits as our first
2485 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002486 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002487 }
2488 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002489
2490 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002491 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002492 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002493 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002494 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002495
2496 case ISD::SADDO:
2497 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002498 case ISD::SSUBO:
2499 case ISD::USUBO:
2500 case ISD::SMULO:
2501 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002502 if (Op.getResNo() != 1)
2503 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002504 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002505 // If setcc returns 0/-1, all bits are sign bits.
2506 // We know that we have an integer-based boolean since these operations
2507 // are only available for integer.
2508 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2509 TargetLowering::ZeroOrNegativeOneBooleanContent)
2510 return VTBits;
2511 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002512 case ISD::SETCC:
2513 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002514 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002515 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002516 return VTBits;
2517 break;
2518 case ISD::ROTL:
2519 case ISD::ROTR:
2520 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002521 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002522
Dan Gohman309d3d52007-06-22 14:59:07 +00002523 // Handle rotate right by N like a rotate left by 32-N.
2524 if (Op.getOpcode() == ISD::ROTR)
2525 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2526
2527 // If we aren't rotating out all of the known-in sign bits, return the
2528 // number that are left. This handles rotl(sext(x), 1) for example.
2529 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2530 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2531 }
2532 break;
2533 case ISD::ADD:
2534 // Add can have at most one carry bit. Thus we know that the output
2535 // is, at worst, one more bit than the inputs.
2536 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2537 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002538
Dan Gohman309d3d52007-06-22 14:59:07 +00002539 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002540 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002541 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002542 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002543 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002544
Dan Gohman309d3d52007-06-22 14:59:07 +00002545 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2546 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002547 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002548 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002549
Dan Gohman309d3d52007-06-22 14:59:07 +00002550 // If we are subtracting one from a positive number, there is no carry
2551 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002552 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002553 return Tmp;
2554 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002555
Dan Gohman309d3d52007-06-22 14:59:07 +00002556 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2557 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002558 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002559
Dan Gohman309d3d52007-06-22 14:59:07 +00002560 case ISD::SUB:
2561 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2562 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002563
Dan Gohman309d3d52007-06-22 14:59:07 +00002564 // Handle NEG.
2565 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002566 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002567 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002568 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002569 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2570 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002571 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002572 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002573
Dan Gohman309d3d52007-06-22 14:59:07 +00002574 // If the input is known to be positive (the sign bit is known clear),
2575 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002576 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002577 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002578
Dan Gohman309d3d52007-06-22 14:59:07 +00002579 // Otherwise, we treat this like a SUB.
2580 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002581
Dan Gohman309d3d52007-06-22 14:59:07 +00002582 // Sub can have at most one carry bit. Thus we know that the output
2583 // is, at worst, one more bit than the inputs.
2584 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2585 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002586 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002587 case ISD::TRUNCATE:
2588 // FIXME: it's tricky to do anything useful for this, but it is an important
2589 // case for targets like X86.
2590 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002591 case ISD::EXTRACT_ELEMENT: {
2592 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2593 const int BitWidth = Op.getValueType().getSizeInBits();
2594 const int Items =
2595 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2596
2597 // Get reverse index (starting from 1), Op1 value indexes elements from
2598 // little end. Sign starts at big end.
2599 const int rIndex = Items - 1 -
2600 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2601
2602 // If the sign portion ends in our element the substraction gives correct
2603 // result. Otherwise it gives either negative or > bitwidth result
2604 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2605 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002606 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002607
Nadav Rotem4536d582013-03-20 22:53:44 +00002608 // If we are looking at the loaded value of the SDNode.
2609 if (Op.getResNo() == 0) {
2610 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2611 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2612 unsigned ExtType = LD->getExtensionType();
2613 switch (ExtType) {
2614 default: break;
2615 case ISD::SEXTLOAD: // '17' bits known
2616 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2617 return VTBits-Tmp+1;
2618 case ISD::ZEXTLOAD: // '16' bits known
2619 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2620 return VTBits-Tmp;
2621 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002622 }
2623 }
2624
2625 // Allow the target to implement this method for its nodes.
2626 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002627 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002628 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2629 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002630 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002631 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002632 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002633
Dan Gohman309d3d52007-06-22 14:59:07 +00002634 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2635 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002636 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002637 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002638
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002639 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002640 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002641 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002642 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002643 Mask = KnownOne;
2644 } else {
2645 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002646 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002647 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002648
Dan Gohman309d3d52007-06-22 14:59:07 +00002649 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2650 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002651 Mask = ~Mask;
2652 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002653 // Return # leading zeros. We use 'min' here in case Val was zero before
2654 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002655 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002656}
2657
Chris Lattner46c01a32011-02-13 22:25:43 +00002658/// isBaseWithConstantOffset - Return true if the specified operand is an
2659/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2660/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2661/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002662/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002663bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2664 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2665 !isa<ConstantSDNode>(Op.getOperand(1)))
2666 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002667
2668 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002669 !MaskedValueIsZero(Op.getOperand(0),
2670 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2671 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002672
Chris Lattner46c01a32011-02-13 22:25:43 +00002673 return true;
2674}
2675
2676
Dan Gohmand0d5e682009-09-03 20:34:31 +00002677bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2678 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002679 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002680 return true;
2681
2682 // If the value is a constant, we can obviously see if it is a NaN or not.
2683 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2684 return !C->getValueAPF().isNaN();
2685
2686 // TODO: Recognize more cases here.
2687
2688 return false;
2689}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002690
Dan Gohman38605212010-02-24 06:52:40 +00002691bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2692 // If the value is a constant, we can obviously see if it is a zero or not.
2693 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2694 return !C->isZero();
2695
2696 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002697 switch (Op.getOpcode()) {
2698 default: break;
2699 case ISD::OR:
2700 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2701 return !C->isNullValue();
2702 break;
2703 }
Dan Gohman38605212010-02-24 06:52:40 +00002704
2705 return false;
2706}
2707
2708bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2709 // Check the obvious case.
2710 if (A == B) return true;
2711
2712 // For for negative and positive zero.
2713 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2714 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2715 if (CA->isZero() && CB->isZero()) return true;
2716
2717 // Otherwise they may not be equal.
2718 return false;
2719}
2720
Chris Lattner061a1ea2005-01-07 07:46:32 +00002721/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002722///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002723SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002724 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002725 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002726 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002727 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002728 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002729
Jack Carter170a5f22013-09-09 22:02:08 +00002730 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2731 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002732 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002733
Chandler Carruth41b20e72014-07-22 04:07:55 +00002734 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002735 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002736}
2737
Andrew Trickef9de2a2013-05-25 02:42:55 +00002738SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002739 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002740 // Constant fold unary operations with an integer constant operand. Even
2741 // opaque constant will be folded, because the folding of unary operations
2742 // doesn't create new constants with different values. Nevertheless, the
2743 // opaque flag is preserved during folding to prevent future folding with
2744 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002745 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002746 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002747 switch (Opcode) {
2748 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002749 case ISD::SIGN_EXTEND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002750 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002751 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002752 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002753 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002754 case ISD::TRUNCATE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002755 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002756 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002757 case ISD::UINT_TO_FP:
2758 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002759 APFloat apf(EVTToAPFloatSemantics(VT),
2760 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002761 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002762 Opcode==ISD::SINT_TO_FP,
2763 APFloat::rmNearestTiesToEven);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002764 return getConstantFP(apf, DL, VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002765 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002766 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002767 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002768 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002769 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002770 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002771 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002772 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002773 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002774 case ISD::BSWAP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002775 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002776 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002777 case ISD::CTPOP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002778 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002779 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002780 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002781 case ISD::CTLZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002782 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002783 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002784 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002785 case ISD::CTTZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002786 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002787 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002788 }
2789 }
2790
Dale Johannesen446b9002007-08-31 23:34:27 +00002791 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002792 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002793 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002794 switch (Opcode) {
2795 case ISD::FNEG:
2796 V.changeSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002797 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002798 case ISD::FABS:
2799 V.clearSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002800 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002801 case ISD::FCEIL: {
2802 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2803 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002804 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002805 break;
2806 }
2807 case ISD::FTRUNC: {
2808 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2809 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002810 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002811 break;
2812 }
2813 case ISD::FFLOOR: {
2814 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2815 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002816 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002817 break;
2818 }
2819 case ISD::FP_EXTEND: {
2820 bool ignored;
2821 // This can return overflow, underflow, or inexact; we don't care.
2822 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002823 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002824 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002825 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002826 }
2827 case ISD::FP_TO_SINT:
2828 case ISD::FP_TO_UINT: {
2829 integerPart x[2];
2830 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002831 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002832 // FIXME need to be more flexible about rounding mode.
2833 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2834 Opcode==ISD::FP_TO_SINT,
2835 APFloat::rmTowardZero, &ignored);
2836 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002837 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002838 APInt api(VT.getSizeInBits(), x);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002839 return getConstant(api, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002840 }
2841 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002842 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002843 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Owen Anderson558012a2014-12-09 06:50:39 +00002844 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002845 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002846 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002847 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002848 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002849 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002850 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002851
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002852 // Constant fold unary operations with a vector integer or float operand.
Jim Grosbachf7502c42014-07-18 00:40:52 +00002853 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002854 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002855 switch (Opcode) {
2856 default:
2857 // FIXME: Entirely reasonable to perform folding of other unary
2858 // operations here as the need arises.
2859 break;
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002860 case ISD::FNEG:
2861 case ISD::FABS:
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002862 case ISD::FCEIL:
2863 case ISD::FTRUNC:
2864 case ISD::FFLOOR:
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002865 case ISD::FP_EXTEND:
Simon Pilgrim017ca192015-05-02 13:04:07 +00002866 case ISD::FP_TO_SINT:
2867 case ISD::FP_TO_UINT:
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002868 case ISD::TRUNCATE:
Jim Grosbachf7502c42014-07-18 00:40:52 +00002869 case ISD::UINT_TO_FP:
2870 case ISD::SINT_TO_FP: {
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002871 EVT SVT = VT.getScalarType();
2872 EVT InVT = BV->getValueType(0);
2873 EVT InSVT = InVT.getScalarType();
2874
Simon Pilgrime09584c2015-05-10 14:14:51 +00002875 // Find legal integer scalar type for constant promotion and
2876 // ensure that its scalar size is at least as large as source.
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002877 EVT LegalSVT = SVT;
2878 if (SVT.isInteger()) {
2879 LegalSVT = TLI->getTypeToTransformTo(*getContext(), SVT);
Simon Pilgrime09584c2015-05-10 14:14:51 +00002880 if (LegalSVT.bitsLT(SVT)) break;
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002881 }
2882
Simon Pilgrim481f4142015-03-23 22:44:55 +00002883 // Let the above scalar folding handle the folding of each element.
Jim Grosbach19dd3082014-07-23 20:41:31 +00002884 SmallVector<SDValue, 8> Ops;
2885 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2886 SDValue OpN = BV->getOperand(i);
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002887 EVT OpVT = OpN.getValueType();
2888
2889 // Build vector (integer) scalar operands may need implicit
2890 // truncation - do this before constant folding.
2891 if (OpVT.isInteger() && OpVT.bitsGT(InSVT))
2892 OpN = getNode(ISD::TRUNCATE, DL, InSVT, OpN);
2893
2894 OpN = getNode(Opcode, DL, SVT, OpN);
2895
2896 // Legalize the (integer) scalar constant if necessary.
2897 if (LegalSVT != SVT)
2898 OpN = getNode(ISD::ANY_EXTEND, DL, LegalSVT, OpN);
2899
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002900 if (OpN.getOpcode() != ISD::UNDEF &&
2901 OpN.getOpcode() != ISD::Constant &&
2902 OpN.getOpcode() != ISD::ConstantFP)
2903 break;
Jim Grosbach19dd3082014-07-23 20:41:31 +00002904 Ops.push_back(OpN);
2905 }
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002906 if (Ops.size() == VT.getVectorNumElements())
2907 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002908 break;
Jim Grosbachf7502c42014-07-18 00:40:52 +00002909 }
2910 }
2911 }
2912 }
2913
Gabor Greiff304a7a2008-08-28 21:40:38 +00002914 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002915 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002916 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002917 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002918 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002919 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002920 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002921 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002922 assert(VT.isFloatingPoint() &&
2923 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002924 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002925 assert((!VT.isVector() ||
2926 VT.getVectorNumElements() ==
2927 Operand.getValueType().getVectorNumElements()) &&
2928 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002929 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002930 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002931 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002932 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002933 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002934 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002935 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002936 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2937 "Invalid sext node, dst < src!");
2938 assert((!VT.isVector() ||
2939 VT.getVectorNumElements() ==
2940 Operand.getValueType().getVectorNumElements()) &&
2941 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002942 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2943 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2944 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002945 // sext(undef) = 0, because the top bits will all be the same.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002946 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002947 break;
2948 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002949 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002950 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002951 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002952 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2953 "Invalid zext node, dst < src!");
2954 assert((!VT.isVector() ||
2955 VT.getVectorNumElements() ==
2956 Operand.getValueType().getVectorNumElements()) &&
2957 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002958 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002959 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002960 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002961 else if (OpOpcode == ISD::UNDEF)
2962 // zext(undef) = 0, because the top bits will be zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002963 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002964 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002965 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002966 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002967 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002968 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002969 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2970 "Invalid anyext node, dst < src!");
2971 assert((!VT.isVector() ||
2972 VT.getVectorNumElements() ==
2973 Operand.getValueType().getVectorNumElements()) &&
2974 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002975
Dan Gohman08837892010-06-18 00:08:30 +00002976 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2977 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002978 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002979 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002980 else if (OpOpcode == ISD::UNDEF)
2981 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002982
2983 // (ext (trunx x)) -> x
2984 if (OpOpcode == ISD::TRUNCATE) {
2985 SDValue OpOp = Operand.getNode()->getOperand(0);
2986 if (OpOp.getValueType() == VT)
2987 return OpOp;
2988 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002989 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002990 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002991 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002992 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002993 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002994 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2995 "Invalid truncate node, src < dst!");
2996 assert((!VT.isVector() ||
2997 VT.getVectorNumElements() ==
2998 Operand.getValueType().getVectorNumElements()) &&
2999 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003000 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00003001 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003002 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3003 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00003004 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00003005 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
3006 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00003007 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003008 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00003009 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003010 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00003011 }
Craig Topper201c1a32012-01-15 01:05:11 +00003012 if (OpOpcode == ISD::UNDEF)
3013 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003014 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003015 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00003016 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00003017 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00003018 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00003019 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00003020 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
3021 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00003022 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003023 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003024 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003025 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003026 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00003027 (VT.getVectorElementType() == Operand.getValueType() ||
3028 (VT.getVectorElementType().isInteger() &&
3029 Operand.getValueType().isInteger() &&
3030 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003031 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00003032 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003033 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00003034 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3035 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3036 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3037 Operand.getConstantOperandVal(1) == 0 &&
3038 Operand.getOperand(0).getValueType() == VT)
3039 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003040 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00003041 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00003042 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003043 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00003044 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00003045 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003046 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00003047 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00003048 break;
3049 case ISD::FABS:
3050 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00003051 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003052 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003053 }
3054
Chris Lattnerf9c19152005-08-25 19:12:10 +00003055 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003056 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003057 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00003058 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003059 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00003060 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003061 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003062 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003063 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003064
Jack Carter170a5f22013-09-09 22:02:08 +00003065 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3066 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003067 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003068 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003069 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3070 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003071 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003072
Chandler Carruth41b20e72014-07-22 04:07:55 +00003073 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003074 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003075}
3076
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003077SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003078 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003079 // If the opcode is a target-specific ISD node, there's nothing we can
3080 // do here and the operand rules may not line up with the below, so
3081 // bail early.
3082 if (Opcode >= ISD::BUILTIN_OP_END)
3083 return SDValue();
3084
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003085 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3086 SmallVector<SDValue, 4> Outputs;
3087 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00003088
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003089 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3090 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003091 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3092 return SDValue();
3093
3094 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003095 // Scalar instruction.
3096 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003097 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003098 // For vectors extract each constant element into Inputs so we can constant
3099 // fold them individually.
3100 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3101 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3102 if (!BV1 || !BV2)
3103 return SDValue();
3104
3105 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3106
3107 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3108 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3109 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3110 if (!V1 || !V2) // Not a constant, bail.
3111 return SDValue();
3112
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003113 if (V1->isOpaque() || V2->isOpaque())
3114 return SDValue();
3115
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003116 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3117 // FIXME: This is valid and could be handled by truncating the APInts.
3118 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3119 return SDValue();
3120
3121 Inputs.push_back(std::make_pair(V1, V2));
3122 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003123 }
3124
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003125 // We have a number of constant values, constant fold them element by element.
3126 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3127 const APInt &C1 = Inputs[I].first->getAPIntValue();
3128 const APInt &C2 = Inputs[I].second->getAPIntValue();
3129
3130 switch (Opcode) {
3131 case ISD::ADD:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003132 Outputs.push_back(getConstant(C1 + C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003133 break;
3134 case ISD::SUB:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003135 Outputs.push_back(getConstant(C1 - C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003136 break;
3137 case ISD::MUL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003138 Outputs.push_back(getConstant(C1 * C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003139 break;
3140 case ISD::UDIV:
3141 if (!C2.getBoolValue())
3142 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003143 Outputs.push_back(getConstant(C1.udiv(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003144 break;
3145 case ISD::UREM:
3146 if (!C2.getBoolValue())
3147 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003148 Outputs.push_back(getConstant(C1.urem(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003149 break;
3150 case ISD::SDIV:
3151 if (!C2.getBoolValue())
3152 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003153 Outputs.push_back(getConstant(C1.sdiv(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003154 break;
3155 case ISD::SREM:
3156 if (!C2.getBoolValue())
3157 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003158 Outputs.push_back(getConstant(C1.srem(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003159 break;
3160 case ISD::AND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003161 Outputs.push_back(getConstant(C1 & C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003162 break;
3163 case ISD::OR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003164 Outputs.push_back(getConstant(C1 | C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003165 break;
3166 case ISD::XOR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003167 Outputs.push_back(getConstant(C1 ^ C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003168 break;
3169 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003170 Outputs.push_back(getConstant(C1 << C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003171 break;
3172 case ISD::SRL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003173 Outputs.push_back(getConstant(C1.lshr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003174 break;
3175 case ISD::SRA:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003176 Outputs.push_back(getConstant(C1.ashr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003177 break;
3178 case ISD::ROTL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003179 Outputs.push_back(getConstant(C1.rotl(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003180 break;
3181 case ISD::ROTR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003182 Outputs.push_back(getConstant(C1.rotr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003183 break;
3184 default:
3185 return SDValue();
3186 }
3187 }
3188
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003189 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3190 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003191
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003192 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003193 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003194 return Outputs.back();
3195
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003196 // We may have a vector type but a scalar result. Create a splat.
3197 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3198
3199 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003200 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003201}
3202
Andrew Trickef9de2a2013-05-25 02:42:55 +00003203SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003204 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003205 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3206 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003207 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003208 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003209 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003210 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3211 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003212 // Fold trivial token factors.
3213 if (N1.getOpcode() == ISD::EntryToken) return N2;
3214 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003215 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003216 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003217 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003218 // Concat of UNDEFs is UNDEF.
3219 if (N1.getOpcode() == ISD::UNDEF &&
3220 N2.getOpcode() == ISD::UNDEF)
3221 return getUNDEF(VT);
3222
Dan Gohman550c9af2008-08-14 20:04:46 +00003223 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3224 // one big BUILD_VECTOR.
3225 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3226 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003227 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3228 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003229 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Simon Pilgrim860f0872015-04-21 08:05:43 +00003230
3231 // BUILD_VECTOR requires all inputs to be of the same type, find the
3232 // maximum type and extend them all.
3233 EVT SVT = VT.getScalarType();
3234 for (SDValue Op : Elts)
3235 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
3236 if (SVT.bitsGT(VT.getScalarType()))
3237 for (SDValue &Op : Elts)
3238 Op = TLI->isZExtFree(Op.getValueType(), SVT)
3239 ? getZExtOrTrunc(Op, DL, SVT)
3240 : getSExtOrTrunc(Op, DL, SVT);
3241
Craig Topper48d114b2014-04-26 18:35:24 +00003242 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003243 }
3244 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003245 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003246 assert(VT.isInteger() && "This operator does not apply to FP types!");
3247 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003248 N1.getValueType() == VT && "Binary operator types must match!");
3249 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3250 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003251 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003252 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003253 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3254 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003255 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003256 case ISD::OR:
3257 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003258 case ISD::ADD:
3259 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003260 assert(VT.isInteger() && "This operator does not apply to FP types!");
3261 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003262 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003263 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3264 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003265 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003266 return N1;
3267 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003268 case ISD::UDIV:
3269 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003270 case ISD::MULHU:
3271 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003272 case ISD::MUL:
3273 case ISD::SDIV:
3274 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003275 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003276 assert(N1.getValueType() == N2.getValueType() &&
3277 N1.getValueType() == VT && "Binary operator types must match!");
3278 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003279 case ISD::FADD:
3280 case ISD::FSUB:
3281 case ISD::FMUL:
3282 case ISD::FDIV:
3283 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003284 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003285 if (Opcode == ISD::FADD) {
3286 // 0+x --> x
3287 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3288 if (CFP->getValueAPF().isZero())
3289 return N2;
3290 // x+0 --> x
3291 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3292 if (CFP->getValueAPF().isZero())
3293 return N1;
3294 } else if (Opcode == ISD::FSUB) {
3295 // x-0 --> x
3296 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3297 if (CFP->getValueAPF().isZero())
3298 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003299 } else if (Opcode == ISD::FMUL) {
3300 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3301 SDValue V = N2;
3302
3303 // If the first operand isn't the constant, try the second
3304 if (!CFP) {
3305 CFP = dyn_cast<ConstantFPSDNode>(N2);
3306 V = N1;
3307 }
3308
3309 if (CFP) {
3310 // 0*x --> 0
3311 if (CFP->isZero())
3312 return SDValue(CFP,0);
3313 // 1*x --> x
3314 if (CFP->isExactlyValue(1.0))
3315 return V;
3316 }
Dan Gohman1275e282009-01-23 19:10:37 +00003317 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003318 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003319 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003320 assert(N1.getValueType() == N2.getValueType() &&
3321 N1.getValueType() == VT && "Binary operator types must match!");
3322 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003323 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3324 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003325 N1.getValueType().isFloatingPoint() &&
3326 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003327 "Invalid FCOPYSIGN!");
3328 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003329 case ISD::SHL:
3330 case ISD::SRA:
3331 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003332 case ISD::ROTL:
3333 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003334 assert(VT == N1.getValueType() &&
3335 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003336 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003337 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003338 assert((!VT.isVector() || VT == N2.getValueType()) &&
3339 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003340 // Verify that the shift amount VT is bit enough to hold valid shift
3341 // amounts. This catches things like trying to shift an i1024 value by an
3342 // i8, which is easy to fall into in generic code that uses
3343 // TLI.getShiftAmount().
3344 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003345 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003346 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003347
3348 // Always fold shifts of i1 values so the code generator doesn't need to
3349 // handle them. Since we know the size of the shift has to be less than the
3350 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003351 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003352 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003353 if (N2C && N2C->isNullValue())
3354 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003355 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003356 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003357 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003358 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003359 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003360 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003361 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003362 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003363 "type is vector!");
3364 assert((!EVT.isVector() ||
3365 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3366 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003367 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003368 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003369 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003370 break;
3371 }
Chris Lattner72733e52008-01-17 07:00:52 +00003372 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003373 assert(VT.isFloatingPoint() &&
3374 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003375 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003376 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003377 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003378 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003379 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003380 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003381 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003382 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003383 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003384 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003385 assert(!EVT.isVector() &&
3386 "AssertSExt/AssertZExt type should be the vector element type "
3387 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003388 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003389 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003390 break;
3391 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003392 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003393 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003394 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003395 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003396 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003397 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003398 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003399 "type is vector!");
3400 assert((!EVT.isVector() ||
3401 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3402 "Vector element counts must match in SIGN_EXTEND_INREG");
3403 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003404 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003405
Chris Lattner16713612008-01-22 19:09:33 +00003406 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003407 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003408 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003409 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003410 Val = Val.ashr(Val.getBitWidth()-FromBits);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003411 return getConstant(Val, DL, VT);
Chris Lattner751817c2006-05-06 23:05:41 +00003412 }
Chris Lattner16713612008-01-22 19:09:33 +00003413 break;
3414 }
3415 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003416 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3417 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003418 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003419
Pawel Bylica9f1fb9d2015-05-06 10:19:14 +00003420 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF
3421 if (N2C && N2C->getZExtValue() >= N1.getValueType().getVectorNumElements())
3422 return getUNDEF(VT);
3423
Chris Lattner16713612008-01-22 19:09:33 +00003424 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3425 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003426 if (N2C &&
3427 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003428 N1.getNumOperands() > 0) {
3429 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003430 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003431 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003432 N1.getOperand(N2C->getZExtValue() / Factor),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003433 getConstant(N2C->getZExtValue() % Factor, DL,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003434 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003435 }
3436
3437 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3438 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003439 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3440 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003441
3442 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003443 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003444 // are promoted and implicitly truncated, and the result implicitly
3445 // extended. Make that explicit here.
3446 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003447
Bob Wilson59dbbb22009-04-13 22:05:19 +00003448 return Elt;
3449 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003450
Chris Lattner16713612008-01-22 19:09:33 +00003451 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3452 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003453 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003454 // If the indices are the same, return the inserted element else
3455 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003456 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003457 SDValue N1Op2 = N1.getOperand(2);
3458 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3459
3460 if (N1Op2C && N2C) {
3461 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3462 if (VT == N1.getOperand(1).getValueType())
3463 return N1.getOperand(1);
3464 else
3465 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3466 }
3467
Dale Johannesendb393622009-02-03 01:55:44 +00003468 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003469 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003470 }
Chris Lattner16713612008-01-22 19:09:33 +00003471 break;
3472 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003473 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003474 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3475 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003476 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003477 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003478
Chris Lattner16713612008-01-22 19:09:33 +00003479 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3480 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003481 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003482 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003483 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003484
Chris Lattner16713612008-01-22 19:09:33 +00003485 // EXTRACT_ELEMENT of a constant int is also very common.
3486 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003487 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003488 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003489 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003490 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
Chris Lattner16713612008-01-22 19:09:33 +00003491 }
3492 break;
David Greeneb6f16112011-01-26 15:38:49 +00003493 case ISD::EXTRACT_SUBVECTOR: {
3494 SDValue Index = N2;
3495 if (VT.isSimple() && N1.getValueType().isSimple()) {
3496 assert(VT.isVector() && N1.getValueType().isVector() &&
3497 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003498 assert(VT.getVectorElementType() ==
3499 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003500 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003501 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003502 "Extract subvector must be from larger vector to smaller vector!");
3503
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003504 if (isa<ConstantSDNode>(Index.getNode())) {
3505 assert((VT.getVectorNumElements() +
3506 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003507 <= N1.getValueType().getVectorNumElements())
3508 && "Extract subvector overflow!");
3509 }
3510
3511 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003512 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003513 return N1;
3514 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003515 break;
Chris Lattner16713612008-01-22 19:09:33 +00003516 }
David Greeneb6f16112011-01-26 15:38:49 +00003517 }
Chris Lattner16713612008-01-22 19:09:33 +00003518
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003519 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003520 if (SDValue SV =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003521 FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
Matthias Braunf50ab432015-01-13 22:17:46 +00003522 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003523
3524 // Canonicalize constant to RHS if commutative.
3525 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3526 std::swap(N1C, N2C);
3527 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003528 }
3529
Chris Lattner16713612008-01-22 19:09:33 +00003530 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003531 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003532 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3533 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003534 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003535 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003536 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003537 std::swap(N1CFP, N2CFP);
3538 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003539 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003540 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3541 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003542 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003543 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003544 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003545 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003546 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003547 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003548 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003549 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003550 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003551 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003552 break;
3553 case ISD::FMUL:
3554 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003555 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003556 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003557 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003558 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003559 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003560 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3561 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003562 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003563 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003564 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003565 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003566 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003567 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3568 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003569 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003570 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003571 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003572 case ISD::FCOPYSIGN:
3573 V1.copySign(V2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003574 return getConstantFP(V1, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003575 default: break;
3576 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003577 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003578
3579 if (Opcode == ISD::FP_ROUND) {
3580 APFloat V = N1CFP->getValueAPF(); // make copy
3581 bool ignored;
3582 // This can return overflow, underflow, or inexact; we don't care.
3583 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003584 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003585 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003586 return getConstantFP(V, DL, VT);
Owen Anderson6f1ee162012-04-10 22:46:53 +00003587 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003588 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003589
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003590 // Canonicalize an UNDEF to the RHS, even over a constant.
3591 if (N1.getOpcode() == ISD::UNDEF) {
3592 if (isCommutativeBinOp(Opcode)) {
3593 std::swap(N1, N2);
3594 } else {
3595 switch (Opcode) {
3596 case ISD::FP_ROUND_INREG:
3597 case ISD::SIGN_EXTEND_INREG:
3598 case ISD::SUB:
3599 case ISD::FSUB:
3600 case ISD::FDIV:
3601 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003602 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003603 return N1; // fold op(undef, arg2) -> undef
3604 case ISD::UDIV:
3605 case ISD::SDIV:
3606 case ISD::UREM:
3607 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003608 case ISD::SRL:
3609 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003610 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003611 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003612 // For vectors, we can't easily build an all zero vector, just return
3613 // the LHS.
3614 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003615 }
3616 }
3617 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003618
3619 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003620 if (N2.getOpcode() == ISD::UNDEF) {
3621 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003622 case ISD::XOR:
3623 if (N1.getOpcode() == ISD::UNDEF)
3624 // Handle undef ^ undef -> 0 special case. This is a common
3625 // idiom (misuse).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003626 return getConstant(0, DL, VT);
Evan Chengdf1690d2008-03-25 20:08:07 +00003627 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003628 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003629 case ISD::ADDC:
3630 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003631 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003632 case ISD::UDIV:
3633 case ISD::SDIV:
3634 case ISD::UREM:
3635 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003636 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003637 case ISD::FADD:
3638 case ISD::FSUB:
3639 case ISD::FMUL:
3640 case ISD::FDIV:
3641 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003642 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003643 return N2;
3644 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003645 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003646 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003647 case ISD::SRL:
3648 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003649 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003650 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003651 // For vectors, we can't easily build an all zero vector, just return
3652 // the LHS.
3653 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003654 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003655 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003656 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003657 // For vectors, we can't easily build an all one vector, just return
3658 // the LHS.
3659 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003660 case ISD::SRA:
3661 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003662 }
3663 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003664
Chris Lattner724f7ee2005-05-11 18:57:39 +00003665 // Memoize this node if possible.
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003666 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003667 SDVTList VTs = getVTList(VT);
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003668 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003669 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003670 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003671 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003672 AddNodeIDNode(ID, Opcode, VTs, Ops);
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003673 if (BinOpHasFlags)
3674 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003675 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003676 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003677 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003678
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003679 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3680
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003681 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003682 } else {
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003683 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003684 }
3685
Chandler Carruth41b20e72014-07-22 04:07:55 +00003686 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003687 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003688}
3689
Andrew Trickef9de2a2013-05-25 02:42:55 +00003690SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003691 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003692 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003693 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003694 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003695 case ISD::FMA: {
3696 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3697 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3698 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3699 if (N1CFP && N2CFP && N3CFP) {
3700 APFloat V1 = N1CFP->getValueAPF();
3701 const APFloat &V2 = N2CFP->getValueAPF();
3702 const APFloat &V3 = N3CFP->getValueAPF();
3703 APFloat::opStatus s =
3704 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003705 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003706 return getConstantFP(V1, DL, VT);
Owen Anderson32baf992013-05-09 22:27:13 +00003707 }
3708 break;
3709 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003710 case ISD::CONCAT_VECTORS:
3711 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3712 // one big BUILD_VECTOR.
3713 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3714 N2.getOpcode() == ISD::BUILD_VECTOR &&
3715 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003716 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3717 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003718 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3719 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003720 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003721 }
3722 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003723 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003724 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003725 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003726 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003727 break;
3728 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003729 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003730 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003731 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003732 return N2; // select true, X, Y -> X
3733 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003734 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003735
3736 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003737 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003738 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003739 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003740 case ISD::INSERT_SUBVECTOR: {
3741 SDValue Index = N3;
3742 if (VT.isSimple() && N1.getValueType().isSimple()
3743 && N2.getValueType().isSimple()) {
3744 assert(VT.isVector() && N1.getValueType().isVector() &&
3745 N2.getValueType().isVector() &&
3746 "Insert subvector VTs must be a vectors");
3747 assert(VT == N1.getValueType() &&
3748 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003749 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003750 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003751 if (isa<ConstantSDNode>(Index.getNode())) {
3752 assert((N2.getValueType().getVectorNumElements() +
3753 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003754 <= VT.getVectorNumElements())
3755 && "Insert subvector overflow!");
3756 }
3757
3758 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003759 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003760 return N2;
3761 }
3762 break;
3763 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003764 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003765 // Fold bit_convert nodes from a type to themselves.
3766 if (N1.getValueType() == VT)
3767 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003768 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003769 }
3770
Chris Lattnerf9c19152005-08-25 19:12:10 +00003771 // Memoize node if it doesn't produce a flag.
3772 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003773 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003774 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003775 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003776 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003777 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003778 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003779 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003780 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003781
Jack Carter170a5f22013-09-09 22:02:08 +00003782 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3783 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003784 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003785 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003786 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3787 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003788 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003789
Chandler Carruth41b20e72014-07-22 04:07:55 +00003790 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003791 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003792}
3793
Andrew Trickef9de2a2013-05-25 02:42:55 +00003794SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003795 SDValue N1, SDValue N2, SDValue N3,
3796 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003797 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003798 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003799}
3800
Andrew Trickef9de2a2013-05-25 02:42:55 +00003801SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003802 SDValue N1, SDValue N2, SDValue N3,
3803 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003804 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003805 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003806}
3807
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003808/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3809/// the incoming stack arguments to be loaded from the stack.
3810SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3811 SmallVector<SDValue, 8> ArgChains;
3812
3813 // Include the original chain at the beginning of the list. When this is
3814 // used by target LowerCall hooks, this helps legalize find the
3815 // CALLSEQ_BEGIN node.
3816 ArgChains.push_back(Chain);
3817
3818 // Add a chain value for each stack argument.
3819 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3820 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3821 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3822 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3823 if (FI->getIndex() < 0)
3824 ArgChains.push_back(SDValue(L, 1));
3825
3826 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003827 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003828}
3829
Dan Gohman544ab2c2008-04-12 04:36:06 +00003830/// getMemsetValue - Vectorized representation of the memset value
3831/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003832static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003833 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003834 assert(Value.getOpcode() != ISD::UNDEF);
3835
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003836 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003837 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003838 assert(C->getAPIntValue().getBitWidth() == 8);
3839 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003840 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003841 return DAG.getConstant(Val, dl, VT);
3842 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
3843 VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003844 }
Evan Chengef377ad2008-05-15 08:39:06 +00003845
Hal Finkel17b6d772015-03-31 20:35:26 +00003846 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
3847 EVT IntVT = VT.getScalarType();
3848 if (!IntVT.isInteger())
3849 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
3850
3851 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003852 if (NumBits > 8) {
3853 // Use a multiplication with 0x010101... to extend the input to the
3854 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003855 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Hal Finkel17b6d772015-03-31 20:35:26 +00003856 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003857 DAG.getConstant(Magic, dl, IntVT));
Hal Finkel17b6d772015-03-31 20:35:26 +00003858 }
3859
3860 if (VT != Value.getValueType() && !VT.isInteger())
3861 Value = DAG.getNode(ISD::BITCAST, dl, VT.getScalarType(), Value);
3862 if (VT != Value.getValueType()) {
3863 assert(VT.getVectorElementType() == Value.getValueType() &&
3864 "value type should be one vector element here");
3865 SmallVector<SDValue, 8> BVOps(VT.getVectorNumElements(), Value);
3866 Value = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, BVOps);
Evan Chengef377ad2008-05-15 08:39:06 +00003867 }
3868
3869 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003870}
3871
Dan Gohman544ab2c2008-04-12 04:36:06 +00003872/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3873/// used when a memcpy is turned into a memset when the source is a constant
3874/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003875static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003876 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003877 // Handle vector with all elements zero.
3878 if (Str.empty()) {
3879 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003880 return DAG.getConstant(0, dl, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003881 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003882 return DAG.getConstantFP(0.0, dl, VT);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003883 else if (VT.isVector()) {
3884 unsigned NumElts = VT.getVectorNumElements();
3885 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003886 return DAG.getNode(ISD::BITCAST, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003887 DAG.getConstant(0, dl,
3888 EVT::getVectorVT(*DAG.getContext(),
3889 EltVT, NumElts)));
Evan Cheng43cd9e32010-04-01 06:04:33 +00003890 } else
3891 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003892 }
3893
Duncan Sands13237ac2008-06-06 12:08:01 +00003894 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003895 unsigned NumVTBits = VT.getSizeInBits();
3896 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003897 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3898
Evan Chengc8444b12013-01-10 22:13:27 +00003899 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003900 if (TLI.isLittleEndian()) {
3901 for (unsigned i = 0; i != NumBytes; ++i)
3902 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3903 } else {
3904 for (unsigned i = 0; i != NumBytes; ++i)
3905 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003906 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003907
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003908 // If the "cost" of materializing the integer immediate is less than the cost
3909 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003910 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3911 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003912 return DAG.getConstant(Val, dl, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003913 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003914}
3915
Scott Michelcf0da6c2009-02-17 22:15:04 +00003916/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003917///
Andrew Tricke2431c62013-05-25 03:08:10 +00003918static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003919 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003920 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003921 return DAG.getNode(ISD::ADD, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003922 VT, Base, DAG.getConstant(Offset, dl, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003923}
3924
Evan Chengef377ad2008-05-15 08:39:06 +00003925/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3926///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003927static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003928 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003929 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003930 if (Src.getOpcode() == ISD::GlobalAddress)
3931 G = cast<GlobalAddressSDNode>(Src);
3932 else if (Src.getOpcode() == ISD::ADD &&
3933 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3934 Src.getOperand(1).getOpcode() == ISD::Constant) {
3935 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003936 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003937 }
3938 if (!G)
3939 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003940
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003941 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003942}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003943
Evan Cheng43cd9e32010-04-01 06:04:33 +00003944/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3945/// to replace the memset / memcpy. Return true if the number of memory ops
3946/// is below the threshold. It returns the types of the sequence of
3947/// memory ops to perform memset / memcpy by reference.
3948static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003949 unsigned Limit, uint64_t Size,
3950 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003951 bool IsMemset,
3952 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003953 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003954 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003955 SelectionDAG &DAG,
3956 const TargetLowering &TLI) {
3957 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3958 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003959 // If 'SrcAlign' is zero, that means the memory operation does not need to
3960 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3961 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3962 // is the specified alignment of the memory operation. If it is zero, that
3963 // means it's possible to change the alignment of the destination.
3964 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3965 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003966 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003967 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003968 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003969
Chris Lattner28dc6c12010-03-07 07:45:08 +00003970 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003971 unsigned AS = 0;
3972 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003973 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003974 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003975 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003976 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003977 case 0: VT = MVT::i64; break;
3978 case 4: VT = MVT::i32; break;
3979 case 2: VT = MVT::i16; break;
3980 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003981 }
3982 }
3983
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003984 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003985 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003986 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003987 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003988
Duncan Sands11dd4242008-06-08 20:54:56 +00003989 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003990 VT = LVT;
3991 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003992
Dan Gohman544ab2c2008-04-12 04:36:06 +00003993 unsigned NumMemOps = 0;
3994 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003995 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003996 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003997 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003998 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003999 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00004000
4001 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004002 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00004003 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00004004 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004005 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00004006 Found = true;
4007 else if (NewVT == MVT::i64 &&
4008 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004009 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00004010 // i64 is usually not legal on 32-bit targets, but f64 may be.
4011 NewVT = MVT::f64;
4012 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004013 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00004014 }
4015
Evan Cheng04e55182012-12-12 00:42:09 +00004016 if (!Found) {
4017 do {
4018 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
4019 if (NewVT == MVT::i8)
4020 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00004021 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00004022 }
4023 NewVTSize = NewVT.getSizeInBits() / 8;
4024
Evan Cheng79e2ca92012-12-10 23:21:26 +00004025 // If the new VT cannot cover all of the remaining bits, then consider
4026 // issuing a (or a pair of) unaligned and overlapping load / store.
4027 // FIXME: Only does this for 64-bit or more since we don't have proper
4028 // cost model for unaligned load / store.
4029 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00004030 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00004031 if (NumMemOps && AllowOverlap &&
4032 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00004033 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00004034 VTSize = Size;
4035 else {
4036 VT = NewVT;
4037 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00004038 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004039 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004040
Evan Chengb7d3d032012-12-12 20:43:23 +00004041 if (++NumMemOps > Limit)
4042 return false;
4043
Dan Gohman544ab2c2008-04-12 04:36:06 +00004044 MemOps.push_back(VT);
4045 Size -= VTSize;
4046 }
4047
4048 return true;
4049}
4050
Andrew Trickef9de2a2013-05-25 02:42:55 +00004051static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00004052 SDValue Chain, SDValue Dst,
4053 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004054 unsigned Align, bool isVol,
4055 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004056 MachinePointerInfo DstPtrInfo,
4057 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004058 // Turn a memcpy of undef to nop.
4059 if (Src.getOpcode() == ISD::UNDEF)
4060 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004061
Dan Gohman714663a2008-05-29 19:42:22 +00004062 // Expand memcpy to a series of load and store ops if the size operand falls
4063 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00004064 // TODO: In the AlwaysInline case, if the size is big then generate a loop
4065 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00004066 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004067 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004068 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004069 MachineFunction &MF = DAG.getMachineFunction();
4070 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004071 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004072 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4073 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4074 DstAlignCanChange = true;
4075 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4076 if (Align > SrcAlign)
4077 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004078 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004079 bool CopyFromStr = isMemSrcFromString(Src, Str);
4080 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004081 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00004082
Evan Cheng4c014c82010-04-01 18:19:11 +00004083 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004084 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00004085 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00004086 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004087 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004088
Evan Cheng43cd9e32010-04-01 06:04:33 +00004089 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004090 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004091 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00004092
4093 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00004094 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004095 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004096 if (!TRI->needsStackRealignment(MF))
4097 while (NewAlign > Align &&
4098 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4099 NewAlign /= 2;
4100
Evan Cheng43cd9e32010-04-01 06:04:33 +00004101 if (NewAlign > Align) {
4102 // Give the stack frame object a larger alignment if needed.
4103 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4104 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4105 Align = NewAlign;
4106 }
4107 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004108
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004109 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004110 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004111 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004112 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004113 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004114 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004115 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004116
Evan Cheng79e2ca92012-12-10 23:21:26 +00004117 if (VTSize > Size) {
4118 // Issuing an unaligned load / store pair that overlaps with the previous
4119 // pair. Adjust the offset accordingly.
4120 assert(i == NumMemOps-1 && i != 0);
4121 SrcOff -= VTSize - Size;
4122 DstOff -= VTSize - Size;
4123 }
4124
Evan Cheng43cd9e32010-04-01 06:04:33 +00004125 if (CopyFromStr &&
4126 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004127 // It's unlikely a store of a vector immediate can be done in a single
4128 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004129 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004130 // FIXME: Handle other cases where store of vector immediate is done in
4131 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004132 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004133 if (Value.getNode())
4134 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004135 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004136 DstPtrInfo.getWithOffset(DstOff), isVol,
4137 false, Align);
4138 }
4139
4140 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004141 // The type might not be legal for the target. This should only happen
4142 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004143 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4144 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004145 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004146 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004147 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004148 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004149 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004150 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004151 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004152 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004153 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004154 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4155 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004156 }
4157 OutChains.push_back(Store);
4158 SrcOff += VTSize;
4159 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004160 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004161 }
4162
Craig Topper48d114b2014-04-26 18:35:24 +00004163 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004164}
4165
Andrew Trickef9de2a2013-05-25 02:42:55 +00004166static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004167 SDValue Chain, SDValue Dst,
4168 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004169 unsigned Align, bool isVol,
4170 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004171 MachinePointerInfo DstPtrInfo,
4172 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004173 // Turn a memmove of undef to nop.
4174 if (Src.getOpcode() == ISD::UNDEF)
4175 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004176
4177 // Expand memmove to a series of load and store ops if the size operand falls
4178 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004179 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004180 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004181 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004182 MachineFunction &MF = DAG.getMachineFunction();
4183 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004184 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004185 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4186 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4187 DstAlignCanChange = true;
4188 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4189 if (Align > SrcAlign)
4190 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004191 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004192
Evan Cheng4c014c82010-04-01 18:19:11 +00004193 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004194 (DstAlignCanChange ? 0 : Align), SrcAlign,
4195 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004196 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004197
Evan Cheng43cd9e32010-04-01 06:04:33 +00004198 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004199 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004200 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004201 if (NewAlign > Align) {
4202 // Give the stack frame object a larger alignment if needed.
4203 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4204 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4205 Align = NewAlign;
4206 }
4207 }
Dan Gohman714663a2008-05-29 19:42:22 +00004208
Evan Cheng43cd9e32010-04-01 06:04:33 +00004209 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004210 SmallVector<SDValue, 8> LoadValues;
4211 SmallVector<SDValue, 8> LoadChains;
4212 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004213 unsigned NumMemOps = MemOps.size();
4214 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004215 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004216 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004217 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004218
Dale Johannesenabf66b82009-02-03 22:26:09 +00004219 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004220 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004221 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004222 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004223 LoadValues.push_back(Value);
4224 LoadChains.push_back(Value.getValue(1));
4225 SrcOff += VTSize;
4226 }
Craig Topper48d114b2014-04-26 18:35:24 +00004227 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004228 OutChains.clear();
4229 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004230 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004231 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004232 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004233
Dale Johannesenabf66b82009-02-03 22:26:09 +00004234 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004235 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004236 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004237 OutChains.push_back(Store);
4238 DstOff += VTSize;
4239 }
4240
Craig Topper48d114b2014-04-26 18:35:24 +00004241 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004242}
4243
Serge Pavlov8ec39992013-09-17 16:24:42 +00004244/// \brief Lower the call to 'memset' intrinsic function into a series of store
4245/// operations.
4246///
4247/// \param DAG Selection DAG where lowered code is placed.
4248/// \param dl Link to corresponding IR location.
4249/// \param Chain Control flow dependency.
4250/// \param Dst Pointer to destination memory location.
4251/// \param Src Value of byte to write into the memory.
4252/// \param Size Number of bytes to write.
4253/// \param Align Alignment of the destination in bytes.
4254/// \param isVol True if destination is volatile.
4255/// \param DstPtrInfo IR information on the memory pointer.
4256/// \returns New head in the control flow, if lowering was successful, empty
4257/// SDValue otherwise.
4258///
4259/// The function tries to replace 'llvm.memset' intrinsic with several store
4260/// operations and value calculation code. This is usually profitable for small
4261/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004262static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004263 SDValue Chain, SDValue Dst,
4264 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004265 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004266 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004267 // Turn a memset of undef to nop.
4268 if (Src.getOpcode() == ISD::UNDEF)
4269 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004270
4271 // Expand memset to a series of load/store ops if the size operand
4272 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004273 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004274 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004275 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004276 MachineFunction &MF = DAG.getMachineFunction();
4277 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004278 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004279 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4280 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4281 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004282 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004283 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004284 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004285 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004286 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004287 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004288
Evan Cheng43cd9e32010-04-01 06:04:33 +00004289 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004290 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004291 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004292 if (NewAlign > Align) {
4293 // Give the stack frame object a larger alignment if needed.
4294 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4295 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4296 Align = NewAlign;
4297 }
4298 }
4299
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004300 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004301 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004302 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004303
4304 // Find the largest store and generate the bit pattern for it.
4305 EVT LargestVT = MemOps[0];
4306 for (unsigned i = 1; i < NumMemOps; i++)
4307 if (MemOps[i].bitsGT(LargestVT))
4308 LargestVT = MemOps[i];
4309 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4310
Dan Gohman544ab2c2008-04-12 04:36:06 +00004311 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004312 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004313 unsigned VTSize = VT.getSizeInBits() / 8;
4314 if (VTSize > Size) {
4315 // Issuing an unaligned load / store pair that overlaps with the previous
4316 // pair. Adjust the offset accordingly.
4317 assert(i == NumMemOps-1 && i != 0);
4318 DstOff -= VTSize - Size;
4319 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004320
4321 // If this store is smaller than the largest store see whether we can get
4322 // the smaller value for free with a truncate.
4323 SDValue Value = MemSetValue;
4324 if (VT.bitsLT(LargestVT)) {
4325 if (!LargestVT.isVector() && !VT.isVector() &&
4326 TLI.isTruncateFree(LargestVT, VT))
4327 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4328 else
4329 Value = getMemsetValue(Src, VT, DAG, dl);
4330 }
4331 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004332 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004333 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004334 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004335 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004336 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004337 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004338 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004339 }
4340
Craig Topper48d114b2014-04-26 18:35:24 +00004341 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004342}
4343
Andrew Trickef9de2a2013-05-25 02:42:55 +00004344SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004345 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004346 unsigned Align, bool isVol, bool AlwaysInline,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004347 bool isTailCall, MachinePointerInfo DstPtrInfo,
Chris Lattner2510de22010-09-21 05:40:29 +00004348 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004349 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004350
4351 // Check to see if we should lower the memcpy to loads and stores first.
4352 // For cases within the target-specified limits, this is the best choice.
4353 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4354 if (ConstantSize) {
4355 // Memcpy with size zero? Just return the original chain.
4356 if (ConstantSize->isNullValue())
4357 return Chain;
4358
Evan Cheng43cd9e32010-04-01 06:04:33 +00004359 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4360 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004361 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004362 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004363 return Result;
4364 }
4365
4366 // Then check to see if we should lower the memcpy with target-specific
4367 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004368 if (TSI) {
4369 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4370 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4371 DstPtrInfo, SrcPtrInfo);
4372 if (Result.getNode())
4373 return Result;
4374 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004375
4376 // If we really need inline code and the target declined to provide it,
4377 // use a (potentially long) sequence of loads and stores.
4378 if (AlwaysInline) {
4379 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004380 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004381 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004382 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004383 }
4384
Dan Gohmanf38547c2010-04-05 20:24:08 +00004385 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4386 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4387 // respect volatile, so they may do things like read or write memory
4388 // beyond the given memory regions. But fixing this isn't easy, and most
4389 // people don't care.
4390
Dan Gohman544ab2c2008-04-12 04:36:06 +00004391 // Emit a library call.
4392 TargetLowering::ArgListTy Args;
4393 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004394 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004395 Entry.Node = Dst; Args.push_back(Entry);
4396 Entry.Node = Src; Args.push_back(Entry);
4397 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004398 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004399 TargetLowering::CallLoweringInfo CLI(*this);
4400 CLI.setDebugLoc(dl).setChain(Chain)
4401 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4402 Type::getVoidTy(*getContext()),
4403 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004404 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004405 .setDiscardResult()
4406 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004407
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004408 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004409 return CallResult.second;
4410}
4411
Andrew Trickef9de2a2013-05-25 02:42:55 +00004412SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004413 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004414 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004415 MachinePointerInfo DstPtrInfo,
4416 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004417 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004418
Dan Gohman714663a2008-05-29 19:42:22 +00004419 // Check to see if we should lower the memmove to loads and stores first.
4420 // For cases within the target-specified limits, this is the best choice.
4421 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4422 if (ConstantSize) {
4423 // Memmove with size zero? Just return the original chain.
4424 if (ConstantSize->isNullValue())
4425 return Chain;
4426
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004427 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004428 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004429 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004430 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004431 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004432 return Result;
4433 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004434
4435 // Then check to see if we should lower the memmove with target-specific
4436 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004437 if (TSI) {
4438 SDValue Result = TSI->EmitTargetCodeForMemmove(
4439 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4440 if (Result.getNode())
4441 return Result;
4442 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004443
Mon P Wangbf862242010-04-06 08:27:51 +00004444 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4445 // not be safe. See memcpy above for more details.
4446
Dan Gohman544ab2c2008-04-12 04:36:06 +00004447 // Emit a library call.
4448 TargetLowering::ArgListTy Args;
4449 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004450 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004451 Entry.Node = Dst; Args.push_back(Entry);
4452 Entry.Node = Src; Args.push_back(Entry);
4453 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004454 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004455 TargetLowering::CallLoweringInfo CLI(*this);
4456 CLI.setDebugLoc(dl).setChain(Chain)
4457 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4458 Type::getVoidTy(*getContext()),
4459 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004460 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004461 .setDiscardResult()
4462 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004463
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004464 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004465 return CallResult.second;
4466}
4467
Andrew Trickef9de2a2013-05-25 02:42:55 +00004468SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004469 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004470 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004471 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004472 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004473
4474 // Check to see if we should lower the memset to stores first.
4475 // For cases within the target-specified limits, this is the best choice.
4476 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4477 if (ConstantSize) {
4478 // Memset with size zero? Just return the original chain.
4479 if (ConstantSize->isNullValue())
4480 return Chain;
4481
Mon P Wangc576ee92010-04-04 03:10:48 +00004482 SDValue Result =
4483 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004484 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004485
Gabor Greiff304a7a2008-08-28 21:40:38 +00004486 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004487 return Result;
4488 }
4489
4490 // Then check to see if we should lower the memset with target-specific
4491 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004492 if (TSI) {
4493 SDValue Result = TSI->EmitTargetCodeForMemset(
4494 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4495 if (Result.getNode())
4496 return Result;
4497 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004498
Wesley Peck527da1b2010-11-23 03:31:01 +00004499 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004500 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004501 TargetLowering::ArgListTy Args;
4502 TargetLowering::ArgListEntry Entry;
4503 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4504 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004505 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004506 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004507 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004508 Entry.Node = Size;
4509 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004510 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004511
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004512 // FIXME: pass in SDLoc
4513 TargetLowering::CallLoweringInfo CLI(*this);
4514 CLI.setDebugLoc(dl).setChain(Chain)
4515 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4516 Type::getVoidTy(*getContext()),
4517 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004518 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004519 .setDiscardResult()
4520 .setTailCall(isTailCall);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004521
4522 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004523 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004524}
4525
Andrew Trickef9de2a2013-05-25 02:42:55 +00004526SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004527 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004528 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004529 AtomicOrdering SuccessOrdering,
4530 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004531 SynchronizationScope SynchScope) {
4532 FoldingSetNodeID ID;
4533 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004534 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004535 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004536 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4538 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4539 return SDValue(E, 0);
4540 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004541
4542 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4543 // SDNode doesn't have access to it. This memory will be "leaked" when
4544 // the node is deallocated, but recovered when the allocator is released.
4545 // If the number of operands is less than 5 we use AtomicSDNode's internal
4546 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004547 unsigned NumOps = Ops.size();
4548 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4549 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004550
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004551 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4552 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004553 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004554 SuccessOrdering, FailureOrdering,
4555 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004556 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004557 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004558 return SDValue(N, 0);
4559}
4560
4561SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004562 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004563 MachineMemOperand *MMO,
4564 AtomicOrdering Ordering,
4565 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004566 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004567 Ordering, SynchScope);
4568}
4569
Tim Northover420a2162014-06-13 14:24:07 +00004570SDValue SelectionDAG::getAtomicCmpSwap(
4571 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4572 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4573 unsigned Alignment, AtomicOrdering SuccessOrdering,
4574 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4575 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4576 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4577 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4578
Dan Gohman48b185d2009-09-25 20:36:54 +00004579 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4580 Alignment = getEVTAlignment(MemVT);
4581
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004582 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004583
Eli Friedmane978d2f2011-09-07 02:23:42 +00004584 // FIXME: Volatile isn't really correct; we should keep track of atomic
4585 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004586 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004587 Flags |= MachineMemOperand::MOLoad;
4588 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004589
4590 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004591 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004592
Tim Northover420a2162014-06-13 14:24:07 +00004593 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4594 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004595}
4596
Tim Northover420a2162014-06-13 14:24:07 +00004597SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4598 SDVTList VTs, SDValue Chain, SDValue Ptr,
4599 SDValue Cmp, SDValue Swp,
4600 MachineMemOperand *MMO,
4601 AtomicOrdering SuccessOrdering,
4602 AtomicOrdering FailureOrdering,
4603 SynchronizationScope SynchScope) {
4604 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4605 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004606 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4607
Dale Johannesen839acbb2009-01-29 00:47:48 +00004608 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004609 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4610 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004611}
4612
Andrew Trickef9de2a2013-05-25 02:42:55 +00004613SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004614 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004615 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004616 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004617 unsigned Alignment,
4618 AtomicOrdering Ordering,
4619 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004620 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4621 Alignment = getEVTAlignment(MemVT);
4622
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004623 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004624 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004625 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004626 // For now, atomics are considered to be volatile always, and they are
4627 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004628 // FIXME: Volatile isn't really correct; we should keep track of atomic
4629 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004630 unsigned Flags = MachineMemOperand::MOVolatile;
4631 if (Opcode != ISD::ATOMIC_STORE)
4632 Flags |= MachineMemOperand::MOLoad;
4633 if (Opcode != ISD::ATOMIC_LOAD)
4634 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004635
4636 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004637 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004638 MemVT.getStoreSize(), Alignment);
4639
Eli Friedmanadec5872011-07-29 03:05:32 +00004640 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4641 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004642}
4643
Andrew Trickef9de2a2013-05-25 02:42:55 +00004644SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004645 SDValue Chain,
4646 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004647 MachineMemOperand *MMO,
4648 AtomicOrdering Ordering,
4649 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004650 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4651 Opcode == ISD::ATOMIC_LOAD_SUB ||
4652 Opcode == ISD::ATOMIC_LOAD_AND ||
4653 Opcode == ISD::ATOMIC_LOAD_OR ||
4654 Opcode == ISD::ATOMIC_LOAD_XOR ||
4655 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004656 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004657 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004658 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004659 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004660 Opcode == ISD::ATOMIC_SWAP ||
4661 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004662 "Invalid Atomic Op");
4663
Owen Anderson53aa7a92009-08-10 22:56:29 +00004664 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004665
Eli Friedman342e8df2011-08-24 20:50:09 +00004666 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4667 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004668 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004669 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004670}
4671
Andrew Trickef9de2a2013-05-25 02:42:55 +00004672SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004673 EVT VT, SDValue Chain,
4674 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004675 MachineMemOperand *MMO,
4676 AtomicOrdering Ordering,
4677 SynchronizationScope SynchScope) {
4678 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4679
4680 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004681 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004682 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004683}
4684
Duncan Sands739a0542008-07-02 17:40:58 +00004685/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004686SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4687 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004688 return Ops[0];
4689
Owen Anderson53aa7a92009-08-10 22:56:29 +00004690 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004691 VTs.reserve(Ops.size());
4692 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004693 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004694 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004695}
4696
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004697SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004698SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004699 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004700 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004701 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004702 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004703 if (Align == 0) // Ensure that codegen never sees alignment 0
4704 Align = getEVTAlignment(MemVT);
4705
4706 MachineFunction &MF = getMachineFunction();
4707 unsigned Flags = 0;
4708 if (WriteMem)
4709 Flags |= MachineMemOperand::MOStore;
4710 if (ReadMem)
4711 Flags |= MachineMemOperand::MOLoad;
4712 if (Vol)
4713 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004714 if (!Size)
4715 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004716 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004717 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004718
Craig Topper206fcd42014-04-26 19:29:41 +00004719 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004720}
4721
4722SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004723SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004724 ArrayRef<SDValue> Ops, EVT MemVT,
4725 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004726 assert((Opcode == ISD::INTRINSIC_VOID ||
4727 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004728 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004729 Opcode == ISD::LIFETIME_START ||
4730 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004731 (Opcode <= INT_MAX &&
4732 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4733 "Opcode is not a memory-accessing opcode!");
4734
Dale Johannesen839acbb2009-01-29 00:47:48 +00004735 // Memoize the node unless it returns a flag.
4736 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004737 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004738 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004739 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004740 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004741 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004742 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4743 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004744 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004745 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004746
Jack Carter170a5f22013-09-09 22:02:08 +00004747 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4748 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004749 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004750 CSEMap.InsertNode(N, IP);
4751 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004752 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4753 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004754 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004755 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004756 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004757 return SDValue(N, 0);
4758}
4759
Chris Lattnerea952f02010-09-21 17:24:05 +00004760/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4761/// MachinePointerInfo record from it. This is particularly useful because the
4762/// code generator has many cases where it doesn't bother passing in a
4763/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4764static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4765 // If this is FI+Offset, we can model it.
4766 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4767 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4768
4769 // If this is (FI+Offset1)+Offset2, we can model it.
4770 if (Ptr.getOpcode() != ISD::ADD ||
4771 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4772 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4773 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004774
Chris Lattnerea952f02010-09-21 17:24:05 +00004775 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4776 return MachinePointerInfo::getFixedStack(FI, Offset+
4777 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4778}
4779
4780/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4781/// MachinePointerInfo record from it. This is particularly useful because the
4782/// code generator has many cases where it doesn't bother passing in a
4783/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4784static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4785 // If the 'Offset' value isn't a constant, we can't handle this.
4786 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4787 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4788 if (OffsetOp.getOpcode() == ISD::UNDEF)
4789 return InferPointerInfo(Ptr);
4790 return MachinePointerInfo();
4791}
Wesley Peck527da1b2010-11-23 03:31:01 +00004792
Chris Lattnerea952f02010-09-21 17:24:05 +00004793
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004794SDValue
4795SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004796 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004797 SDValue Ptr, SDValue Offset,
4798 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004799 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004800 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004801 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004802 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004803 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004804 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4805 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004806
Dan Gohman48b185d2009-09-25 20:36:54 +00004807 unsigned Flags = MachineMemOperand::MOLoad;
4808 if (isVolatile)
4809 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004810 if (isNonTemporal)
4811 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004812 if (isInvariant)
4813 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004814
Chris Lattnerea952f02010-09-21 17:24:05 +00004815 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4816 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004817 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004818 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004819
Chris Lattnerea952f02010-09-21 17:24:05 +00004820 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004821 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004822 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004823 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004824 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004825}
4826
4827SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004828SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004829 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004830 SDValue Ptr, SDValue Offset, EVT MemVT,
4831 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004832 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004833 ExtType = ISD::NON_EXTLOAD;
4834 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004835 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004836 } else {
4837 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004838 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4839 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004840 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004841 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004842 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004843 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004844 assert((!VT.isVector() ||
4845 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004846 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004847 }
4848
4849 bool Indexed = AM != ISD::UNINDEXED;
4850 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4851 "Unindexed load with an offset!");
4852
4853 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004854 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004855 SDValue Ops[] = { Chain, Ptr, Offset };
4856 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004857 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004858 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004859 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004860 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004861 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004862 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004863 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004864 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4865 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004866 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004867 }
Jack Carter170a5f22013-09-09 22:02:08 +00004868 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4869 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004870 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004871 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004872 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004873 return SDValue(N, 0);
4874}
4875
Andrew Trickef9de2a2013-05-25 02:42:55 +00004876SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004877 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004878 MachinePointerInfo PtrInfo,
4879 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004880 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004881 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004882 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004883 SDValue Undef = getUNDEF(Ptr.getValueType());
4884 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004885 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004886 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004887}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004888
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004889SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4890 SDValue Chain, SDValue Ptr,
4891 MachineMemOperand *MMO) {
4892 SDValue Undef = getUNDEF(Ptr.getValueType());
4893 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4894 VT, MMO);
4895}
4896
Andrew Trickef9de2a2013-05-25 02:42:55 +00004897SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004898 SDValue Chain, SDValue Ptr,
4899 MachinePointerInfo PtrInfo, EVT MemVT,
4900 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004901 bool isInvariant, unsigned Alignment,
4902 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004903 SDValue Undef = getUNDEF(Ptr.getValueType());
4904 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004905 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4906 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004907}
4908
4909
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004910SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4911 SDValue Chain, SDValue Ptr, EVT MemVT,
4912 MachineMemOperand *MMO) {
4913 SDValue Undef = getUNDEF(Ptr.getValueType());
4914 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4915 MemVT, MMO);
4916}
4917
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004918SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004919SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004920 SDValue Offset, ISD::MemIndexedMode AM) {
4921 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4922 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4923 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004924 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004925 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004926 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004927 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004928}
4929
Andrew Trickef9de2a2013-05-25 02:42:55 +00004930SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004931 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004932 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004933 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004934 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004935 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004936 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004937 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004938
Dan Gohman48b185d2009-09-25 20:36:54 +00004939 unsigned Flags = MachineMemOperand::MOStore;
4940 if (isVolatile)
4941 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004942 if (isNonTemporal)
4943 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004944
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004945 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004946 PtrInfo = InferPointerInfo(Ptr);
4947
4948 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004949 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004950 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004951 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004952 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004953
4954 return getStore(Chain, dl, Val, Ptr, MMO);
4955}
4956
Andrew Trickef9de2a2013-05-25 02:42:55 +00004957SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004958 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004959 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004960 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004961 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004962 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004963 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004964 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4965 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004966 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004967 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004968 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004969 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004970 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004971 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004972 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4973 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004974 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004975 }
Jack Carter170a5f22013-09-09 22:02:08 +00004976 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4977 dl.getDebugLoc(), VTs,
4978 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004979 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004980 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004981 return SDValue(N, 0);
4982}
4983
Andrew Trickef9de2a2013-05-25 02:42:55 +00004984SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004985 SDValue Ptr, MachinePointerInfo PtrInfo,
4986 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004987 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004988 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004989 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004990 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004991 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4992 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004993
Dan Gohman48b185d2009-09-25 20:36:54 +00004994 unsigned Flags = MachineMemOperand::MOStore;
4995 if (isVolatile)
4996 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004997 if (isNonTemporal)
4998 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004999
Nick Lewyckyaad475b2014-04-15 07:22:52 +00005000 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00005001 PtrInfo = InferPointerInfo(Ptr);
5002
5003 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00005004 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00005005 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00005006 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00005007
5008 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
5009}
5010
Andrew Trickef9de2a2013-05-25 02:42:55 +00005011SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00005012 SDValue Ptr, EVT SVT,
5013 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005014 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00005015
Michael Ilsemand5f91512012-09-10 16:56:31 +00005016 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005017 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005018 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00005019 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005020
Dan Gohmancecad352009-12-14 23:40:38 +00005021 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
5022 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005023 assert(VT.isInteger() == SVT.isInteger() &&
5024 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00005025 assert(VT.isVector() == SVT.isVector() &&
5026 "Cannot use trunc store to convert to or from a vector!");
5027 assert((!VT.isVector() ||
5028 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
5029 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005030
Owen Anderson9f944592009-08-11 20:47:22 +00005031 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00005032 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005033 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5034 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005035 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005036 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00005037 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005038 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00005039 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005040 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00005041 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5042 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005043 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005044 }
Jack Carter170a5f22013-09-09 22:02:08 +00005045 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5046 dl.getDebugLoc(), VTs,
5047 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005048 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005049 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005050 return SDValue(N, 0);
5051}
5052
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005053SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005054SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00005055 SDValue Offset, ISD::MemIndexedMode AM) {
5056 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5057 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
5058 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00005059 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005060 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5061 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005062 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005063 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00005064 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00005065 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005066 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005067 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00005068 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005069
Jack Carter170a5f22013-09-09 22:02:08 +00005070 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5071 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00005072 ST->isTruncatingStore(),
5073 ST->getMemoryVT(),
5074 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005075 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005076 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005077 return SDValue(N, 0);
5078}
5079
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005080SDValue
5081SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005082 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5083 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005084
5085 SDVTList VTs = getVTList(VT, MVT::Other);
5086 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5087 FoldingSetNodeID ID;
5088 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5089 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005090 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005091 MMO->isVolatile(),
5092 MMO->isNonTemporal(),
5093 MMO->isInvariant()));
5094 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5095 void *IP = nullptr;
5096 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5097 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5098 return SDValue(E, 0);
5099 }
5100 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5101 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005102 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005103 CSEMap.InsertNode(N, IP);
5104 InsertNode(N);
5105 return SDValue(N, 0);
5106}
5107
5108SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005109 SDValue Ptr, SDValue Mask, EVT MemVT,
5110 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005111 assert(Chain.getValueType() == MVT::Other &&
5112 "Invalid chain type");
5113 EVT VT = Val.getValueType();
5114 SDVTList VTs = getVTList(MVT::Other);
5115 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5116 FoldingSetNodeID ID;
5117 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5118 ID.AddInteger(VT.getRawBits());
5119 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5120 MMO->isNonTemporal(), MMO->isInvariant()));
5121 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5122 void *IP = nullptr;
5123 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5124 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5125 return SDValue(E, 0);
5126 }
5127 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5128 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005129 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005130 CSEMap.InsertNode(N, IP);
5131 InsertNode(N);
5132 return SDValue(N, 0);
5133}
5134
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005135SDValue
5136SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl,
5137 ArrayRef<SDValue> Ops,
5138 MachineMemOperand *MMO) {
5139
5140 FoldingSetNodeID ID;
5141 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5142 ID.AddInteger(VT.getRawBits());
5143 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5144 MMO->isVolatile(),
5145 MMO->isNonTemporal(),
5146 MMO->isInvariant()));
5147 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5148 void *IP = nullptr;
5149 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5150 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5151 return SDValue(E, 0);
5152 }
5153 MaskedGatherSDNode *N =
5154 new (NodeAllocator) MaskedGatherSDNode(dl.getIROrder(), dl.getDebugLoc(),
5155 Ops, VTs, VT, MMO);
5156 CSEMap.InsertNode(N, IP);
5157 InsertNode(N);
5158 return SDValue(N, 0);
5159}
5160
5161SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl,
5162 ArrayRef<SDValue> Ops,
5163 MachineMemOperand *MMO) {
5164 FoldingSetNodeID ID;
5165 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5166 ID.AddInteger(VT.getRawBits());
5167 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5168 MMO->isNonTemporal(),
5169 MMO->isInvariant()));
5170 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5171 void *IP = nullptr;
5172 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5173 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5174 return SDValue(E, 0);
5175 }
5176 SDNode *N =
5177 new (NodeAllocator) MaskedScatterSDNode(dl.getIROrder(), dl.getDebugLoc(),
5178 Ops, VTs, VT, MMO);
5179 CSEMap.InsertNode(N, IP);
5180 InsertNode(N);
5181 return SDValue(N, 0);
5182}
5183
Andrew Trickef9de2a2013-05-25 02:42:55 +00005184SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005185 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005186 SDValue SV,
5187 unsigned Align) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005188 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005189 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005190}
5191
Andrew Trickef9de2a2013-05-25 02:42:55 +00005192SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005193 ArrayRef<SDUse> Ops) {
5194 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005195 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005196 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005197 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5198 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005199 default: break;
5200 }
5201
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005202 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005203 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005204 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005205 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005206}
5207
Andrew Trickef9de2a2013-05-25 02:42:55 +00005208SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005209 ArrayRef<SDValue> Ops) {
5210 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005211 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005212 case 0: return getNode(Opcode, DL, VT);
5213 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5214 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5215 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005216 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005217 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005218
Chris Lattnerb0713c72005-04-09 03:27:28 +00005219 switch (Opcode) {
5220 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005221 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005222 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005223 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5224 "LHS and RHS of condition must have same type!");
5225 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5226 "True and False arms of SelectCC must have same type!");
5227 assert(Ops[2].getValueType() == VT &&
5228 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005229 break;
5230 }
5231 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005232 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005233 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5234 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005235 break;
5236 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005237 }
5238
Chris Lattner566307f2005-05-14 07:42:29 +00005239 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005240 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005241 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005242
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005243 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005244 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005245 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005246 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005247
Bill Wendling022d18f2009-12-18 23:32:53 +00005248 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005249 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005250
Jack Carter170a5f22013-09-09 22:02:08 +00005251 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005252 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005253 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005254 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005255 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005256 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005257 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005258
Chandler Carruth41b20e72014-07-22 04:07:55 +00005259 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005260 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005261}
5262
Andrew Trickef9de2a2013-05-25 02:42:55 +00005263SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005264 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5265 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005266}
5267
Andrew Trickef9de2a2013-05-25 02:42:55 +00005268SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005269 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005270 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005271 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005272
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005273#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005274 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005275 // FIXME: figure out how to safely handle things like
5276 // int foo(int x) { return 1 << (x & 255); }
5277 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005278 case ISD::SRA_PARTS:
5279 case ISD::SRL_PARTS:
5280 case ISD::SHL_PARTS:
5281 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005282 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005283 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005284 else if (N3.getOpcode() == ISD::AND)
5285 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5286 // If the and is only masking out bits that cannot effect the shift,
5287 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005288 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005289 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005290 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005291 }
5292 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005293 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005294#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005295
Chris Lattnerf9c19152005-08-25 19:12:10 +00005296 // Memoize the node unless it returns a flag.
5297 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005298 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005299 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005300 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005301 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005302 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005304 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005305
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005306 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005307 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5308 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005309 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005310 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5311 DL.getDebugLoc(), VTList, Ops[0],
5312 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005313 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005314 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5315 DL.getDebugLoc(), VTList, Ops[0],
5316 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005317 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005318 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005319 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005320 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005321 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005322 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005323 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005324 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5325 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005326 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005327 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5328 DL.getDebugLoc(), VTList, Ops[0],
5329 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005330 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005331 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5332 DL.getDebugLoc(), VTList, Ops[0],
5333 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005334 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005335 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005336 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005337 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005338 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005339 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005340 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005341}
5342
Andrew Trickef9de2a2013-05-25 02:42:55 +00005343SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005344 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005345}
5346
Andrew Trickef9de2a2013-05-25 02:42:55 +00005347SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005348 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005349 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005350 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005351}
5352
Andrew Trickef9de2a2013-05-25 02:42:55 +00005353SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005354 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005355 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005356 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005357}
5358
Andrew Trickef9de2a2013-05-25 02:42:55 +00005359SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005360 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005361 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005362 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005363}
5364
Andrew Trickef9de2a2013-05-25 02:42:55 +00005365SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005366 SDValue N1, SDValue N2, SDValue N3,
5367 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005368 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005369 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005370}
5371
Andrew Trickef9de2a2013-05-25 02:42:55 +00005372SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005373 SDValue N1, SDValue N2, SDValue N3,
5374 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005375 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005376 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005377}
5378
Owen Anderson53aa7a92009-08-10 22:56:29 +00005379SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005380 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005381}
5382
Owen Anderson53aa7a92009-08-10 22:56:29 +00005383SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005384 FoldingSetNodeID ID;
5385 ID.AddInteger(2U);
5386 ID.AddInteger(VT1.getRawBits());
5387 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005388
Craig Topperc0196b12014-04-14 00:51:57 +00005389 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005390 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005391 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005392 EVT *Array = Allocator.Allocate<EVT>(2);
5393 Array[0] = VT1;
5394 Array[1] = VT2;
5395 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5396 VTListMap.InsertNode(Result, IP);
5397 }
5398 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005399}
Dan Gohman17059682008-07-17 19:10:17 +00005400
Owen Anderson53aa7a92009-08-10 22:56:29 +00005401SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005402 FoldingSetNodeID ID;
5403 ID.AddInteger(3U);
5404 ID.AddInteger(VT1.getRawBits());
5405 ID.AddInteger(VT2.getRawBits());
5406 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005407
Craig Topperc0196b12014-04-14 00:51:57 +00005408 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005409 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005410 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005411 EVT *Array = Allocator.Allocate<EVT>(3);
5412 Array[0] = VT1;
5413 Array[1] = VT2;
5414 Array[2] = VT3;
5415 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5416 VTListMap.InsertNode(Result, IP);
5417 }
5418 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005419}
5420
Owen Anderson53aa7a92009-08-10 22:56:29 +00005421SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005422 FoldingSetNodeID ID;
5423 ID.AddInteger(4U);
5424 ID.AddInteger(VT1.getRawBits());
5425 ID.AddInteger(VT2.getRawBits());
5426 ID.AddInteger(VT3.getRawBits());
5427 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005428
Craig Topperc0196b12014-04-14 00:51:57 +00005429 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005430 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005431 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005432 EVT *Array = Allocator.Allocate<EVT>(4);
5433 Array[0] = VT1;
5434 Array[1] = VT2;
5435 Array[2] = VT3;
5436 Array[3] = VT4;
5437 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5438 VTListMap.InsertNode(Result, IP);
5439 }
5440 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005441}
5442
Craig Topperabb4ac72014-04-16 06:10:51 +00005443SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5444 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005445 FoldingSetNodeID ID;
5446 ID.AddInteger(NumVTs);
5447 for (unsigned index = 0; index < NumVTs; index++) {
5448 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005449 }
5450
Craig Topperc0196b12014-04-14 00:51:57 +00005451 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005452 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005453 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005454 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005455 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005456 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5457 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005458 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005459 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005460}
5461
5462
Chris Lattnerf34156e2006-01-28 09:32:45 +00005463/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5464/// specified operands. If the resultant node already exists in the DAG,
5465/// this does not modify the specified node, instead it returns the node that
5466/// already exists. If the resultant node does not exist in the DAG, the
5467/// input node is returned. As a degenerate case, if you specify the same
5468/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005469SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005470 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005471
Chris Lattnerf34156e2006-01-28 09:32:45 +00005472 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005473 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005474
Chris Lattnerf34156e2006-01-28 09:32:45 +00005475 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005476 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005477 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005478 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005479
Dan Gohmanebeccb42008-07-21 22:38:59 +00005480 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005481 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005482 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005483 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005484
Chris Lattnerf34156e2006-01-28 09:32:45 +00005485 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005486 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005487
Chris Lattnerf34156e2006-01-28 09:32:45 +00005488 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005489 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005490 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005491}
5492
Dan Gohman92c11ac2010-06-18 15:30:29 +00005493SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005494 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005495
Chris Lattnerf34156e2006-01-28 09:32:45 +00005496 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005497 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005498 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005499
Chris Lattnerf34156e2006-01-28 09:32:45 +00005500 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005501 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005502 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005503 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005504
Dan Gohmanebeccb42008-07-21 22:38:59 +00005505 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005506 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005507 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005508 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005509
Chris Lattnerf34156e2006-01-28 09:32:45 +00005510 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005511 if (N->OperandList[0] != Op1)
5512 N->OperandList[0].set(Op1);
5513 if (N->OperandList[1] != Op2)
5514 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005515
Chris Lattnerf34156e2006-01-28 09:32:45 +00005516 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005517 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005518 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005519}
5520
Dan Gohman92c11ac2010-06-18 15:30:29 +00005521SDNode *SelectionDAG::
5522UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005523 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005524 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005525}
5526
Dan Gohman92c11ac2010-06-18 15:30:29 +00005527SDNode *SelectionDAG::
5528UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005529 SDValue Op3, SDValue Op4) {
5530 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005531 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005532}
5533
Dan Gohman92c11ac2010-06-18 15:30:29 +00005534SDNode *SelectionDAG::
5535UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005536 SDValue Op3, SDValue Op4, SDValue Op5) {
5537 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005538 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005539}
5540
Dan Gohman92c11ac2010-06-18 15:30:29 +00005541SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005542UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5543 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005544 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005545 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005546
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005547 // If no operands changed just return the input node.
Benjamin Kramer0b6742a2015-03-02 16:45:08 +00005548 if (Ops.empty() || std::equal(Ops.begin(), Ops.end(), N->op_begin()))
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005549 return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005550
Chris Lattnerf34156e2006-01-28 09:32:45 +00005551 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005552 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005553 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005554 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005555
Dan Gohman2f83b472008-05-02 00:05:03 +00005556 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005557 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005558 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005559 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005560
Chris Lattnerf34156e2006-01-28 09:32:45 +00005561 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005562 for (unsigned i = 0; i != NumOps; ++i)
5563 if (N->OperandList[i] != Ops[i])
5564 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005565
5566 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005567 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005568 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005569}
5570
Dan Gohman91697632008-07-07 20:57:48 +00005571/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005572/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005573void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005574 // Unlike the code in MorphNodeTo that does this, we don't need to
5575 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005576 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5577 SDUse &Use = *I++;
5578 Use.set(SDValue());
5579 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005580}
Chris Lattner19732782005-08-16 18:17:10 +00005581
Dan Gohman17059682008-07-17 19:10:17 +00005582/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5583/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005584///
Dan Gohman17059682008-07-17 19:10:17 +00005585SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005586 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005587 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005588 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005589}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005590
Dan Gohman17059682008-07-17 19:10:17 +00005591SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005592 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005593 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005594 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005595 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005596}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005597
Dan Gohman17059682008-07-17 19:10:17 +00005598SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005599 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005600 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005601 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005602 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005603 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005604}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005605
Dan Gohman17059682008-07-17 19:10:17 +00005606SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005607 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005608 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005609 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005610 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005611 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005612}
Chris Lattner466fece2005-08-21 22:30:30 +00005613
Dan Gohman17059682008-07-17 19:10:17 +00005614SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005615 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005616 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005617 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005618}
5619
Dan Gohman17059682008-07-17 19:10:17 +00005620SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005621 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005622 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005623 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005624}
5625
Dan Gohman17059682008-07-17 19:10:17 +00005626SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005627 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005628 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005629 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005630}
5631
Dan Gohman17059682008-07-17 19:10:17 +00005632SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005633 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005634 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005635 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005636 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005637}
5638
Bill Wendling2d598632008-12-01 23:28:22 +00005639SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005640 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005641 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005642 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005643 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005644}
5645
Scott Michelcf0da6c2009-02-17 22:15:04 +00005646SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005647 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005648 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005649 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005650 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005651 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005652}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005653
Scott Michelcf0da6c2009-02-17 22:15:04 +00005654SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005655 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005656 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005657 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005658 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005659 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005660}
5661
Dan Gohman17059682008-07-17 19:10:17 +00005662SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005663 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005664 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005665 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005666 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005667 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005668 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005669}
5670
Dan Gohman17059682008-07-17 19:10:17 +00005671SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005672 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005673 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005674 SDValue Op3) {
5675 SDVTList VTs = getVTList(VT1, VT2, VT3);
5676 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005677 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005678}
5679
5680SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005681 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005682 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005683 // Reset the NodeID to -1.
5684 N->setNodeId(-1);
5685 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005686}
5687
Andrew Trickef9de2a2013-05-25 02:42:55 +00005688/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005689/// the line number information on the merged node since it is not possible to
5690/// preserve the information that operation is associated with multiple lines.
5691/// This will make the debugger working better at -O0, were there is a higher
5692/// probability having other instructions associated with that line.
5693///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005694/// For IROrder, we keep the smaller of the two
5695SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005696 DebugLoc NLoc = N->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00005697 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005698 N->setDebugLoc(DebugLoc());
5699 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005700 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5701 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005702 return N;
5703}
5704
Chris Lattneraf197502010-02-28 21:36:14 +00005705/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005706/// return type, opcode, and operands.
5707///
5708/// Note that MorphNodeTo returns the resultant node. If there is already a
5709/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005710/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005711///
5712/// Using MorphNodeTo is faster than creating a new node and swapping it in
5713/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005714/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005715/// the node's users.
5716///
Chandler Carruth356665a2014-08-01 22:09:43 +00005717/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5718/// As a consequence it isn't appropriate to use from within the DAG combiner or
5719/// the legalizer which maintain worklists that would need to be updated when
5720/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005721SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005722 SDVTList VTs, ArrayRef<SDValue> Ops) {
5723 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005724 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005725 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005726 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005727 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005728 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005729 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005730 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005731 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005732
Dan Gohmand3fe1742008-09-13 01:54:27 +00005733 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005734 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005735
Dan Gohman17059682008-07-17 19:10:17 +00005736 // Start the morphing.
5737 N->NodeType = Opc;
5738 N->ValueList = VTs.VTs;
5739 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005740
Dan Gohman17059682008-07-17 19:10:17 +00005741 // Clear the operands list, updating used nodes to remove this from their
5742 // use list. Keep track of any operands that become dead as a result.
5743 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005744 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5745 SDUse &Use = *I++;
5746 SDNode *Used = Use.getNode();
5747 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005748 if (Used->use_empty())
5749 DeadNodeSet.insert(Used);
5750 }
5751
Dan Gohman48b185d2009-09-25 20:36:54 +00005752 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5753 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005754 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005755 // If NumOps is larger than the # of operands we can have in a
5756 // MachineSDNode, reallocate the operand list.
5757 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5758 if (MN->OperandsNeedDelete)
5759 delete[] MN->OperandList;
5760 if (NumOps > array_lengthof(MN->LocalOperands))
5761 // We're creating a final node that will live unmorphed for the
5762 // remainder of the current SelectionDAG iteration, so we can allocate
5763 // the operands directly out of a pool with no recycling metadata.
5764 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005765 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005766 else
Craig Topper131de822014-04-27 19:21:16 +00005767 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005768 MN->OperandsNeedDelete = false;
5769 } else
Craig Topper131de822014-04-27 19:21:16 +00005770 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005771 } else {
5772 // If NumOps is larger than the # of operands we currently have, reallocate
5773 // the operand list.
5774 if (NumOps > N->NumOperands) {
5775 if (N->OperandsNeedDelete)
5776 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005777 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005778 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005779 } else
Craig Topper131de822014-04-27 19:21:16 +00005780 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005781 }
5782
5783 // Delete any nodes that are still dead after adding the uses for the
5784 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005785 if (!DeadNodeSet.empty()) {
5786 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005787 for (SDNode *N : DeadNodeSet)
5788 if (N->use_empty())
5789 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005790 RemoveDeadNodes(DeadNodes);
5791 }
Dan Gohman91697632008-07-07 20:57:48 +00005792
Dan Gohman17059682008-07-17 19:10:17 +00005793 if (IP)
5794 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005795 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005796}
5797
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005798
Dan Gohman32f71d72009-09-25 18:54:59 +00005799/// getMachineNode - These are used for target selectors to create a new node
5800/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005801///
Dan Gohman32f71d72009-09-25 18:54:59 +00005802/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005803/// node of the specified opcode and operands, it returns that node instead of
5804/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005805MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005806SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005807 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005808 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005809}
Bill Wendlinga434d932009-01-29 09:01:55 +00005810
Dan Gohmana22f2d82009-10-10 01:29:16 +00005811MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005812SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005813 SDVTList VTs = getVTList(VT);
5814 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005815 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005816}
Bill Wendlinga434d932009-01-29 09:01:55 +00005817
Dan Gohmana22f2d82009-10-10 01:29:16 +00005818MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005819SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005820 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005821 SDVTList VTs = getVTList(VT);
5822 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005823 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005824}
Bill Wendlinga434d932009-01-29 09:01:55 +00005825
Dan Gohmana22f2d82009-10-10 01:29:16 +00005826MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005827SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005828 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005829 SDVTList VTs = getVTList(VT);
5830 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005831 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005832}
Bill Wendlinga434d932009-01-29 09:01:55 +00005833
Dan Gohmana22f2d82009-10-10 01:29:16 +00005834MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005835SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005836 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005837 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005838 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005839}
Bill Wendlinga434d932009-01-29 09:01:55 +00005840
Dan Gohmana22f2d82009-10-10 01:29:16 +00005841MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005842SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005843 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005844 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005845}
Bill Wendlinga434d932009-01-29 09:01:55 +00005846
Dan Gohmana22f2d82009-10-10 01:29:16 +00005847MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005848SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005849 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005850 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005851 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005852 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005853}
Bill Wendlinga434d932009-01-29 09:01:55 +00005854
Dan Gohmana22f2d82009-10-10 01:29:16 +00005855MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005856SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005857 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005858 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005859 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005860 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005861}
5862
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, SDValue Op1,
5866 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005867 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005868 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005869 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005870}
5871
Dan Gohmana22f2d82009-10-10 01:29:16 +00005872MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005873SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005874 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005875 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005876 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005877 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005878}
Bill Wendlinga434d932009-01-29 09:01:55 +00005879
Dan Gohmana22f2d82009-10-10 01:29:16 +00005880MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005881SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005882 EVT VT1, EVT VT2, EVT VT3,
5883 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005884 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005885 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005886 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005887}
Bill Wendlinga434d932009-01-29 09:01:55 +00005888
Dan Gohmana22f2d82009-10-10 01:29:16 +00005889MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005890SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005891 EVT VT1, EVT VT2, EVT VT3,
5892 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005893 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005894 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005895 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005896}
Bill Wendlinga434d932009-01-29 09:01:55 +00005897
Dan Gohmana22f2d82009-10-10 01:29:16 +00005898MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005899SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005900 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005901 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005902 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005903 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005904}
Bill Wendlinga434d932009-01-29 09:01:55 +00005905
Dan Gohmana22f2d82009-10-10 01:29:16 +00005906MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005907SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005908 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005909 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005910 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005911 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005912}
5913
Dan Gohmana22f2d82009-10-10 01:29:16 +00005914MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005915SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005916 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005917 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005918 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005919 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005920}
5921
Dan Gohmana22f2d82009-10-10 01:29:16 +00005922MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005923SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005924 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005925 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005926 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005927 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005928 const SDValue *Ops = OpsArray.data();
5929 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005930
5931 if (DoCSE) {
5932 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005933 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005934 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005935 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005936 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005937 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005938 }
5939
5940 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005941 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5942 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005943
5944 // Initialize the operands list.
5945 if (NumOps > array_lengthof(N->LocalOperands))
5946 // We're creating a final node that will live unmorphed for the
5947 // remainder of the current SelectionDAG iteration, so we can allocate
5948 // the operands directly out of a pool with no recycling metadata.
5949 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5950 Ops, NumOps);
5951 else
5952 N->InitOperands(N->LocalOperands, Ops, NumOps);
5953 N->OperandsNeedDelete = false;
5954
5955 if (DoCSE)
5956 CSEMap.InsertNode(N, IP);
5957
Chandler Carruth41b20e72014-07-22 04:07:55 +00005958 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005959 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005960}
Evan Chengd3f1db92006-02-09 07:15:23 +00005961
Dan Gohmanac33a902009-08-19 18:16:17 +00005962/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005963/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005964SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005965SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005966 SDValue Operand) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005967 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005968 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005969 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005970 return SDValue(Subreg, 0);
5971}
5972
Bob Wilson2a45a652009-10-08 18:49:46 +00005973/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005974/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005975SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005976SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005977 SDValue Operand, SDValue Subreg) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005978 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005979 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005980 VT, Operand, Subreg, SRIdxVal);
5981 return SDValue(Result, 0);
5982}
5983
Evan Cheng31604a62008-03-22 01:55:50 +00005984/// getNodeIfExists - Get the specified node if it's already available, or
5985/// else return NULL.
5986SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00005987 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5988 bool exact) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005989 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005990 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005991 AddNodeIDNode(ID, Opcode, VTList, Ops);
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00005992 if (isBinOpWithFlags(Opcode))
5993 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005994 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005995 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005996 return E;
5997 }
Craig Topperc0196b12014-04-14 00:51:57 +00005998 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005999}
6000
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006001/// getDbgValue - Creates a SDDbgValue node.
6002///
Adrian Prantl32da8892014-04-25 20:49:25 +00006003/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006004SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
6005 unsigned R, bool IsIndirect, uint64_t Off,
6006 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006007 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006008 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006009 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006010}
6011
Adrian Prantl32da8892014-04-25 20:49:25 +00006012/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006013SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
6014 const Value *C, uint64_t Off,
6015 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006016 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006017 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006018 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006019}
6020
Adrian Prantl32da8892014-04-25 20:49:25 +00006021/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006022SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
6023 unsigned FI, uint64_t Off,
6024 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006025 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006026 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006027 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006028}
6029
Dan Gohman7d099f72010-03-03 21:33:37 +00006030namespace {
6031
Dan Gohman9cc886b2010-03-04 19:11:28 +00006032/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00006033/// pointed to by a use iterator is deleted, increment the use iterator
6034/// so that it doesn't dangle.
6035///
Dan Gohman7d099f72010-03-03 21:33:37 +00006036class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00006037 SDNode::use_iterator &UI;
6038 SDNode::use_iterator &UE;
6039
Craig Topper7b883b32014-03-08 06:31:39 +00006040 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00006041 // Increment the iterator as needed.
6042 while (UI != UE && N == *UI)
6043 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00006044 }
6045
6046public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006047 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00006048 SDNode::use_iterator &ui,
6049 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006050 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00006051};
6052
6053}
6054
Evan Cheng445b91a2006-08-07 22:13:29 +00006055/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006056/// This can cause recursive merging of nodes in the DAG.
6057///
Chris Lattner76858912008-02-03 03:35:22 +00006058/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00006059///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006060void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006061 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006062 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00006063 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00006064 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00006065
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006066 // Iterate over all the existing uses of From. New uses will be added
6067 // to the beginning of the use list, which we avoid visiting.
6068 // This specifically avoids visiting uses of From that arise while the
6069 // replacement is happening, because any such uses would be the result
6070 // of CSE: If an existing node looks like From after one of its operands
6071 // is replaced by To, we don't want to replace of all its users with To
6072 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006073 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006074 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006075 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006076 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006077
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006078 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006079 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006080
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006081 // A user can appear in a use list multiple times, and when this
6082 // happens the uses are usually next to each other in the list.
6083 // To help reduce the number of CSE recomputations, process all
6084 // the uses of this user that we can find this way.
6085 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006086 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006087 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006088 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006089 } while (UI != UE && *UI == User);
6090
6091 // Now that we have modified User, add it back to the CSE maps. If it
6092 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006093 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006094 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006095
6096 // If we just RAUW'd the root, take note.
6097 if (FromN == getRoot())
6098 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00006099}
6100
6101/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6102/// This can cause recursive merging of nodes in the DAG.
6103///
Dan Gohman8aa28b92009-04-15 20:06:30 +00006104/// This version assumes that for each value of From, there is a
6105/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00006106///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006107void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00006108#ifndef NDEBUG
6109 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6110 assert((!From->hasAnyUseOfValue(i) ||
6111 From->getValueType(i) == To->getValueType(i)) &&
6112 "Cannot use this version of ReplaceAllUsesWith!");
6113#endif
Dan Gohman17059682008-07-17 19:10:17 +00006114
6115 // Handle the trivial case.
6116 if (From == To)
6117 return;
6118
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006119 // Iterate over just the existing users of From. See the comments in
6120 // the ReplaceAllUsesWith above.
6121 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006122 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006123 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006124 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006125
Chris Lattner373f0482005-08-26 18:36:28 +00006126 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006127 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006128
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006129 // A user can appear in a use list multiple times, and when this
6130 // happens the uses are usually next to each other in the list.
6131 // To help reduce the number of CSE recomputations, process all
6132 // the uses of this user that we can find this way.
6133 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006134 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006135 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006136 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006137 } while (UI != UE && *UI == User);
6138
6139 // Now that we have modified User, add it back to the CSE maps. If it
6140 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006141 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006142 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006143
6144 // If we just RAUW'd the root, take note.
6145 if (From == getRoot().getNode())
6146 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006147}
6148
Chris Lattner373f0482005-08-26 18:36:28 +00006149/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6150/// This can cause recursive merging of nodes in the DAG.
6151///
6152/// This version can replace From with any result values. To must match the
6153/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006154void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006155 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006156 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006157
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006158 // Iterate over just the existing users of From. See the comments in
6159 // the ReplaceAllUsesWith above.
6160 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006161 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006162 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006163 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006164
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006165 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006166 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006167
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006168 // A user can appear in a use list multiple times, and when this
6169 // happens the uses are usually next to each other in the list.
6170 // To help reduce the number of CSE recomputations, process all
6171 // the uses of this user that we can find this way.
6172 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006173 SDUse &Use = UI.getUse();
6174 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006175 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006176 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006177 } while (UI != UE && *UI == User);
6178
6179 // Now that we have modified User, add it back to the CSE maps. If it
6180 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006181 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006182 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006183
6184 // If we just RAUW'd the root, take note.
6185 if (From == getRoot().getNode())
6186 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006187}
6188
Chris Lattner375e1a72006-02-17 21:58:01 +00006189/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006190/// uses of other values produced by From.getNode() alone. The Deleted
6191/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006192void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006193 // Handle the really simple, really trivial case efficiently.
6194 if (From == To) return;
6195
Chris Lattner375e1a72006-02-17 21:58:01 +00006196 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006197 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006198 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006199 return;
6200 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006201
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006202 // Iterate over just the existing users of From. See the comments in
6203 // the ReplaceAllUsesWith above.
6204 SDNode::use_iterator UI = From.getNode()->use_begin(),
6205 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006206 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006207 while (UI != UE) {
6208 SDNode *User = *UI;
6209 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006210
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006211 // A user can appear in a use list multiple times, and when this
6212 // happens the uses are usually next to each other in the list.
6213 // To help reduce the number of CSE recomputations, process all
6214 // the uses of this user that we can find this way.
6215 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006216 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006217
6218 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006219 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006220 ++UI;
6221 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006222 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006223
6224 // If this node hasn't been modified yet, it's still in the CSE maps,
6225 // so remove its old self from the CSE maps.
6226 if (!UserRemovedFromCSEMaps) {
6227 RemoveNodeFromCSEMaps(User);
6228 UserRemovedFromCSEMaps = true;
6229 }
6230
6231 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006232 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006233 } while (UI != UE && *UI == User);
6234
6235 // We are iterating over all uses of the From node, so if a use
6236 // doesn't use the specific value, no changes are made.
6237 if (!UserRemovedFromCSEMaps)
6238 continue;
6239
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006240 // Now that we have modified User, add it back to the CSE maps. If it
6241 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006242 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006243 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006244
6245 // If we just RAUW'd the root, take note.
6246 if (From == getRoot())
6247 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006248}
6249
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006250namespace {
6251 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6252 /// to record information about a use.
6253 struct UseMemo {
6254 SDNode *User;
6255 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006256 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006257 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006258
6259 /// operator< - Sort Memos by User.
6260 bool operator<(const UseMemo &L, const UseMemo &R) {
6261 return (intptr_t)L.User < (intptr_t)R.User;
6262 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006263}
6264
Dan Gohman17059682008-07-17 19:10:17 +00006265/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006266/// uses of other values produced by From.getNode() alone. The same value
6267/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006268/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006269void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6270 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006271 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006272 // Handle the simple, trivial case efficiently.
6273 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006274 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006275
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006276 // Read up all the uses and make records of them. This helps
6277 // processing new uses that are introduced during the
6278 // replacement process.
6279 SmallVector<UseMemo, 4> Uses;
6280 for (unsigned i = 0; i != Num; ++i) {
6281 unsigned FromResNo = From[i].getResNo();
6282 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006283 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006284 E = FromNode->use_end(); UI != E; ++UI) {
6285 SDUse &Use = UI.getUse();
6286 if (Use.getResNo() == FromResNo) {
6287 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006288 Uses.push_back(Memo);
6289 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006290 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006291 }
Dan Gohman17059682008-07-17 19:10:17 +00006292
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006293 // Sort the uses, so that all the uses from a given User are together.
6294 std::sort(Uses.begin(), Uses.end());
6295
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006296 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6297 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006298 // We know that this user uses some value of From. If it is the right
6299 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006300 SDNode *User = Uses[UseIndex].User;
6301
6302 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006303 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006304
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006305 // The Uses array is sorted, so all the uses for a given User
6306 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006307 // To help reduce the number of CSE recomputations, process all
6308 // the uses of this user that we can find this way.
6309 do {
6310 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006311 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006312 ++UseIndex;
6313
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006314 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006315 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6316
Dan Gohman17059682008-07-17 19:10:17 +00006317 // Now that we have modified User, add it back to the CSE maps. If it
6318 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006319 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006320 }
6321}
6322
Evan Cheng9631a602006-08-01 08:20:41 +00006323/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006324/// based on their topological order. It returns the maximum id and a vector
6325/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006326unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006327
Dan Gohman86aa16a2008-09-30 18:30:35 +00006328 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006329
Dan Gohman86aa16a2008-09-30 18:30:35 +00006330 // SortedPos tracks the progress of the algorithm. Nodes before it are
6331 // sorted, nodes after it are unsorted. When the algorithm completes
6332 // it is at the end of the list.
6333 allnodes_iterator SortedPos = allnodes_begin();
6334
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006335 // Visit all the nodes. Move nodes with no operands to the front of
6336 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006337 // operand count. Before we do this, the Node Id fields of the nodes
6338 // may contain arbitrary values. After, the Node Id fields for nodes
6339 // before SortedPos will contain the topological sort index, and the
6340 // Node Id fields for nodes At SortedPos and after will contain the
6341 // count of outstanding operands.
6342 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6343 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006344 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006345 unsigned Degree = N->getNumOperands();
6346 if (Degree == 0) {
6347 // A node with no uses, add it to the result array immediately.
6348 N->setNodeId(DAGSize++);
6349 allnodes_iterator Q = N;
6350 if (Q != SortedPos)
6351 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006352 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006353 ++SortedPos;
6354 } else {
6355 // Temporarily use the Node Id as scratch space for the degree count.
6356 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006357 }
6358 }
6359
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006360 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006361 // such that by the time the end is reached all nodes will be sorted.
6362 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6363 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006364 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006365 // N is in sorted position, so all its uses have one less operand
6366 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006367 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6368 UI != UE; ++UI) {
6369 SDNode *P = *UI;
6370 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006371 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006372 --Degree;
6373 if (Degree == 0) {
6374 // All of P's operands are sorted, so P may sorted now.
6375 P->setNodeId(DAGSize++);
6376 if (P != SortedPos)
6377 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006378 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006379 ++SortedPos;
6380 } else {
6381 // Update P's outstanding operand count.
6382 P->setNodeId(Degree);
6383 }
6384 }
David Greene3b2a68c2010-01-20 00:59:23 +00006385 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006386#ifndef NDEBUG
6387 SDNode *S = ++I;
6388 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006389 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006390 dbgs() << "Checking if this is due to cycles\n";
6391 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006392#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006393 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006394 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006395 }
6396
6397 assert(SortedPos == AllNodes.end() &&
6398 "Topological sort incomplete!");
6399 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6400 "First node in topological sort is not the entry token!");
6401 assert(AllNodes.front().getNodeId() == 0 &&
6402 "First node in topological sort has non-zero id!");
6403 assert(AllNodes.front().getNumOperands() == 0 &&
6404 "First node in topological sort has operands!");
6405 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6406 "Last node in topologic sort has unexpected id!");
6407 assert(AllNodes.back().use_empty() &&
6408 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006409 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006410 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006411}
6412
Evan Cheng563fe3c2010-03-25 01:38:16 +00006413/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6414/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006415void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006416 if (SD) {
6417 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006418 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006419 }
6420 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006421}
Evan Cheng29eefc12006-07-27 06:39:06 +00006422
Devang Patelefc6b162011-01-25 23:27:42 +00006423/// TransferDbgValues - Transfer SDDbgValues.
6424void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6425 if (From == To || !From.getNode()->getHasDebugValue())
6426 return;
6427 SDNode *FromNode = From.getNode();
6428 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006429 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006430 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006431 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006432 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006433 SDDbgValue *Dbg = *I;
6434 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006435 SDDbgValue *Clone =
6436 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6437 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6438 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006439 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006440 }
6441 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006442 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006443 E = ClonedDVs.end(); I != E; ++I)
6444 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006445}
6446
Jim Laskeyd66e6162005-08-17 20:08:02 +00006447//===----------------------------------------------------------------------===//
6448// SDNode Class
6449//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006450
Chris Lattner3bf17b62007-02-04 02:41:42 +00006451HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006452 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006453}
6454
Andrew Trickef9de2a2013-05-25 02:42:55 +00006455GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6456 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006457 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006458 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006459 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006460}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006461
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006462AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6463 SDValue X, unsigned SrcAS,
6464 unsigned DestAS)
6465 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6466 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6467
Andrew Trickef9de2a2013-05-25 02:42:55 +00006468MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6469 EVT memvt, MachineMemOperand *mmo)
6470 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006471 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006472 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006473 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006474 assert(isNonTemporal() == MMO->isNonTemporal() &&
6475 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006476 // We check here that the size of the memory operand fits within the size of
6477 // the MMO. This is because the MMO might indicate only a possible address
6478 // range instead of specifying the affected memory addresses precisely.
6479 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006480}
6481
Andrew Trickef9de2a2013-05-25 02:42:55 +00006482MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006483 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6484 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006485 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006486 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006487 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006488 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006489 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006490}
6491
Jim Laskeyf576b422006-10-27 23:46:08 +00006492/// Profile - Gather unique data for the node.
6493///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006494void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006495 AddNodeIDNode(ID, this);
6496}
6497
Owen Anderson3b1665e2009-08-25 22:27:22 +00006498namespace {
6499 struct EVTArray {
6500 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006501
Owen Anderson3b1665e2009-08-25 22:27:22 +00006502 EVTArray() {
6503 VTs.reserve(MVT::LAST_VALUETYPE);
6504 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6505 VTs.push_back(MVT((MVT::SimpleValueType)i));
6506 }
6507 };
6508}
6509
Owen Anderson53aa7a92009-08-10 22:56:29 +00006510static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006511static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006512static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006513
Chris Lattner88fa11c2005-11-08 23:30:28 +00006514/// getValueTypeList - Return a pointer to the specified value type.
6515///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006516const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006517 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006518 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006519 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006520 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006521 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006522 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006523 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006524 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006525}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006526
Chris Lattner40e79822005-01-12 18:37:47 +00006527/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6528/// indicated value. This method ignores uses of other values defined by this
6529/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006530bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006531 assert(Value < getNumValues() && "Bad value!");
6532
Roman Levenstein51f532f2008-04-07 10:06:32 +00006533 // TODO: Only iterate over uses of a given value of the node
6534 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006535 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006536 if (NUses == 0)
6537 return false;
6538 --NUses;
6539 }
Chris Lattner40e79822005-01-12 18:37:47 +00006540 }
6541
6542 // Found exactly the right number of uses?
6543 return NUses == 0;
6544}
6545
6546
Evan Cheng358c3d12007-08-02 05:29:38 +00006547/// hasAnyUseOfValue - Return true if there are any use of the indicated
6548/// value. This method ignores uses of other values defined by this operation.
6549bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6550 assert(Value < getNumValues() && "Bad value!");
6551
Dan Gohman7a510c22008-07-09 22:39:01 +00006552 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006553 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006554 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006555
6556 return false;
6557}
6558
6559
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006560/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006561///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006562bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006563 bool Seen = false;
6564 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006565 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006566 if (User == this)
6567 Seen = true;
6568 else
6569 return false;
6570 }
6571
6572 return Seen;
6573}
6574
Evan Cheng9456dd82006-11-03 07:31:32 +00006575/// isOperand - Return true if this node is an operand of N.
6576///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006577bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006578 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6579 if (*this == N->getOperand(i))
6580 return true;
6581 return false;
6582}
6583
Evan Cheng567d2e52008-03-04 00:41:45 +00006584bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006585 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006586 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006587 return true;
6588 return false;
6589}
Evan Chengd37645c2006-02-05 06:29:23 +00006590
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006591/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006592/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006593/// side-effecting instructions on any chain path. In practice, this looks
6594/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006595/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006596bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006597 unsigned Depth) const {
6598 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006599
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006600 // Don't search too deeply, we just want to be able to see through
6601 // TokenFactor's etc.
6602 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006603
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006604 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006605 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006606 if (getOpcode() == ISD::TokenFactor) {
6607 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006608 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6609 return false;
6610 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006611 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006612
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006613 // Loads don't have side effects, look through them.
6614 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6615 if (!Ld->isVolatile())
6616 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6617 }
6618 return false;
6619}
6620
Lang Hames5a004992011-07-07 04:31:51 +00006621/// hasPredecessor - Return true if N is a predecessor of this node.
6622/// N is either an operand of this node, or can be reached by recursively
6623/// traversing up the operands.
6624/// NOTE: This is an expensive method. Use it carefully.
6625bool SDNode::hasPredecessor(const SDNode *N) const {
6626 SmallPtrSet<const SDNode *, 32> Visited;
6627 SmallVector<const SDNode *, 16> Worklist;
6628 return hasPredecessorHelper(N, Visited, Worklist);
6629}
Dan Gohmancd139c02009-10-28 03:44:30 +00006630
Craig Topperb94011f2013-07-14 04:42:23 +00006631bool
6632SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006633 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006634 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006635 if (Visited.empty()) {
6636 Worklist.push_back(this);
6637 } else {
6638 // Take a look in the visited set. If we've already encountered this node
6639 // we needn't search further.
6640 if (Visited.count(N))
6641 return true;
6642 }
6643
6644 // Haven't visited N yet. Continue the search.
6645 while (!Worklist.empty()) {
6646 const SDNode *M = Worklist.pop_back_val();
6647 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6648 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006649 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006650 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006651 if (Op == N)
6652 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006653 }
Lang Hames5a004992011-07-07 04:31:51 +00006654 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006655
6656 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006657}
6658
Evan Cheng5d9fd972006-10-04 00:56:09 +00006659uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6660 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006661 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006662}
6663
Mon P Wang32f8bb92009-11-30 02:42:02 +00006664SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6665 assert(N->getNumValues() == 1 &&
6666 "Can't unroll a vector with multiple results!");
6667
6668 EVT VT = N->getValueType(0);
6669 unsigned NE = VT.getVectorNumElements();
6670 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006671 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006672
6673 SmallVector<SDValue, 8> Scalars;
6674 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6675
6676 // If ResNE is 0, fully unroll the vector op.
6677 if (ResNE == 0)
6678 ResNE = NE;
6679 else if (NE > ResNE)
6680 NE = ResNE;
6681
6682 unsigned i;
6683 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006684 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006685 SDValue Operand = N->getOperand(j);
6686 EVT OperandVT = Operand.getValueType();
6687 if (OperandVT.isVector()) {
6688 // A vector operand; extract a single element.
6689 EVT OperandEltVT = OperandVT.getVectorElementType();
6690 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6691 OperandEltVT,
6692 Operand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006693 getConstant(i, dl, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006694 } else {
6695 // A scalar operand; just use it as is.
6696 Operands[j] = Operand;
6697 }
6698 }
6699
6700 switch (N->getOpcode()) {
6701 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006702 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006703 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006704 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006705 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006706 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006707 case ISD::SHL:
6708 case ISD::SRA:
6709 case ISD::SRL:
6710 case ISD::ROTL:
6711 case ISD::ROTR:
6712 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006713 getShiftAmountOperand(Operands[0].getValueType(),
6714 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006715 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006716 case ISD::SIGN_EXTEND_INREG:
6717 case ISD::FP_ROUND_INREG: {
6718 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6719 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6720 Operands[0],
6721 getValueType(ExtVT)));
6722 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006723 }
6724 }
6725
6726 for (; i < ResNE; ++i)
6727 Scalars.push_back(getUNDEF(EltVT));
6728
6729 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006730 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006731}
6732
Evan Chengf5938d52009-12-09 01:36:00 +00006733
Wesley Peck527da1b2010-11-23 03:31:01 +00006734/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6735/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006736/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006737bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006738 unsigned Bytes, int Dist) const {
6739 if (LD->getChain() != Base->getChain())
6740 return false;
6741 EVT VT = LD->getValueType(0);
6742 if (VT.getSizeInBits() / 8 != Bytes)
6743 return false;
6744
6745 SDValue Loc = LD->getOperand(1);
6746 SDValue BaseLoc = Base->getOperand(1);
6747 if (Loc.getOpcode() == ISD::FrameIndex) {
6748 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6749 return false;
6750 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6751 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6752 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6753 int FS = MFI->getObjectSize(FI);
6754 int BFS = MFI->getObjectSize(BFI);
6755 if (FS != BFS || FS != (int)Bytes) return false;
6756 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6757 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006758
Sanjay Patel7129c102014-12-16 21:57:18 +00006759 // Handle X + C.
6760 if (isBaseWithConstantOffset(Loc)) {
6761 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6762 if (Loc.getOperand(0) == BaseLoc) {
6763 // If the base location is a simple address with no offset itself, then
6764 // the second load's first add operand should be the base address.
6765 if (LocOffset == Dist * (int)Bytes)
6766 return true;
6767 } else if (isBaseWithConstantOffset(BaseLoc)) {
6768 // The base location itself has an offset, so subtract that value from the
6769 // second load's offset before comparing to distance * size.
6770 int64_t BOffset =
6771 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6772 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6773 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6774 return true;
6775 }
6776 }
6777 }
Craig Topperc0196b12014-04-14 00:51:57 +00006778 const GlobalValue *GV1 = nullptr;
6779 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006780 int64_t Offset1 = 0;
6781 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006782 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6783 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006784 if (isGA1 && isGA2 && GV1 == GV2)
6785 return Offset1 == (Offset2 + Dist*Bytes);
6786 return false;
6787}
6788
6789
Evan Cheng34a23ea2009-12-09 01:04:59 +00006790/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6791/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006792unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006793 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006794 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006795 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006796 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006797 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006798 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00006799 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
6800 *TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006801 unsigned AlignBits = KnownZero.countTrailingOnes();
6802 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6803 if (Align)
6804 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006805 }
Evan Chengd938faf2009-12-09 01:53:58 +00006806
Evan Cheng34a23ea2009-12-09 01:04:59 +00006807 // If this is a direct reference to a stack slot, use information about the
6808 // stack slot's alignment.
6809 int FrameIdx = 1 << 31;
6810 int64_t FrameOffset = 0;
6811 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6812 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006813 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006814 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006815 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006816 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6817 FrameOffset = Ptr.getConstantOperandVal(1);
6818 }
6819
6820 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006821 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006822 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6823 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006824 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006825 }
6826
6827 return 0;
6828}
6829
Juergen Ributzkab3487102013-11-19 21:20:17 +00006830/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6831/// which is split (or expanded) into two not necessarily identical pieces.
6832std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6833 // Currently all types are split in half.
6834 EVT LoVT, HiVT;
6835 if (!VT.isVector()) {
6836 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6837 } else {
6838 unsigned NumElements = VT.getVectorNumElements();
6839 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6840 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6841 NumElements/2);
6842 }
6843 return std::make_pair(LoVT, HiVT);
6844}
6845
6846/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6847/// low/high part.
6848std::pair<SDValue, SDValue>
6849SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6850 const EVT &HiVT) {
6851 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6852 N.getValueType().getVectorNumElements() &&
6853 "More vector elements requested than available!");
6854 SDValue Lo, Hi;
6855 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006856 getConstant(0, DL, TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006857 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006858 getConstant(LoVT.getVectorNumElements(), DL,
6859 TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006860 return std::make_pair(Lo, Hi);
6861}
6862
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006863void SelectionDAG::ExtractVectorElements(SDValue Op,
6864 SmallVectorImpl<SDValue> &Args,
6865 unsigned Start, unsigned Count) {
6866 EVT VT = Op.getValueType();
6867 if (Count == 0)
6868 Count = VT.getVectorNumElements();
6869
6870 EVT EltVT = VT.getVectorElementType();
6871 EVT IdxTy = TLI->getVectorIdxTy();
6872 SDLoc SL(Op);
6873 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6874 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006875 Op, getConstant(i, SL, IdxTy)));
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006876 }
6877}
6878
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006879// getAddressSpace - Return the address space this GlobalAddress belongs to.
6880unsigned GlobalAddressSDNode::getAddressSpace() const {
6881 return getGlobal()->getType()->getAddressSpace();
6882}
6883
6884
Chris Lattner229907c2011-07-18 04:54:35 +00006885Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006886 if (isMachineConstantPoolEntry())
6887 return Val.MachineCPVal->getType();
6888 return Val.ConstVal->getType();
6889}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006890
Bob Wilson85cefe82009-03-02 23:24:16 +00006891bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6892 APInt &SplatUndef,
6893 unsigned &SplatBitSize,
6894 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006895 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006896 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006897 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006898 assert(VT.isVector() && "Expected a vector type");
6899 unsigned sz = VT.getSizeInBits();
6900 if (MinSplatBits > sz)
6901 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006902
Bob Wilson85cefe82009-03-02 23:24:16 +00006903 SplatValue = APInt(sz, 0);
6904 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006905
Bob Wilson85cefe82009-03-02 23:24:16 +00006906 // Get the bits. Bits with undefined values (when the corresponding element
6907 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6908 // in SplatValue. If any of the values are not constant, give up and return
6909 // false.
6910 unsigned int nOps = getNumOperands();
6911 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6912 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006913
6914 for (unsigned j = 0; j < nOps; ++j) {
6915 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006916 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006917 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006918
Bob Wilson85cefe82009-03-02 23:24:16 +00006919 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006920 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006921 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006922 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006923 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006924 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006925 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006926 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006927 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006928 }
6929
Bob Wilson85cefe82009-03-02 23:24:16 +00006930 // The build_vector is all constants or undefs. Find the smallest element
6931 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006932
Bob Wilson85cefe82009-03-02 23:24:16 +00006933 HasAnyUndefs = (SplatUndef != 0);
6934 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006935
Bob Wilson85cefe82009-03-02 23:24:16 +00006936 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006937 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6938 APInt LowValue = SplatValue.trunc(HalfSize);
6939 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6940 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006941
Bob Wilson85cefe82009-03-02 23:24:16 +00006942 // If the two halves do not match (ignoring undef bits), stop here.
6943 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6944 MinSplatBits > HalfSize)
6945 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006946
Bob Wilson85cefe82009-03-02 23:24:16 +00006947 SplatValue = HighValue | LowValue;
6948 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006949
Bob Wilson85cefe82009-03-02 23:24:16 +00006950 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006951 }
6952
Bob Wilson85cefe82009-03-02 23:24:16 +00006953 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006954 return true;
6955}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006956
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006957SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6958 if (UndefElements) {
6959 UndefElements->clear();
6960 UndefElements->resize(getNumOperands());
6961 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006962 SDValue Splatted;
6963 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6964 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006965 if (Op.getOpcode() == ISD::UNDEF) {
6966 if (UndefElements)
6967 (*UndefElements)[i] = true;
6968 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006969 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006970 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006971 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006972 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006973 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006974
Chandler Carruthb844e722014-07-08 07:19:55 +00006975 if (!Splatted) {
6976 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6977 "Can only have a splat without a constant for all undefs.");
6978 return getOperand(0);
6979 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006980
Chandler Carruthb844e722014-07-08 07:19:55 +00006981 return Splatted;
6982}
6983
6984ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006985BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006986 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006987 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006988}
6989
6990ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006991BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006992 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006993 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006994}
6995
Juergen Ributzka73844052014-01-13 20:51:35 +00006996bool BuildVectorSDNode::isConstant() const {
6997 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6998 unsigned Opc = getOperand(i).getOpcode();
6999 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
7000 return false;
7001 }
7002 return true;
7003}
7004
Owen Anderson53aa7a92009-08-10 22:56:29 +00007005bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00007006 // Find the first non-undef value in the shuffle mask.
7007 unsigned i, e;
7008 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
7009 /* search */;
7010
Nate Begeman39b59db2009-04-29 18:13:31 +00007011 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00007012
Nate Begeman5f829d82009-04-29 05:20:52 +00007013 // Make sure all remaining elements are either undef or the same as the first
7014 // non-undef value.
7015 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007016 if (Mask[i] >= 0 && Mask[i] != Idx)
7017 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007018 return true;
7019}
David Greene09851602010-01-20 20:13:31 +00007020
Adam Nemetb4690e32014-05-31 16:23:20 +00007021#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00007022static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00007023 SmallPtrSetImpl<const SDNode*> &Visited,
7024 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00007025 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007026 // If this node has already been checked, don't check it again.
7027 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00007028 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00007029
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007030 // If a node has already been visited on this depth-first walk, reject it as
7031 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00007032 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007033 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00007034 dbgs() << "Offending node:\n";
7035 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007036 abort();
David Greene09851602010-01-20 20:13:31 +00007037 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007038
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007039 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00007040 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00007041
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007042 Checked.insert(N);
7043 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00007044}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007045#endif
David Greene09851602010-01-20 20:13:31 +00007046
Adam Nemet7d394302014-05-31 16:23:17 +00007047void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00007048 const llvm::SelectionDAG *DAG,
7049 bool force) {
7050#ifndef NDEBUG
7051 bool check = force;
David Greene09851602010-01-20 20:13:31 +00007052#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00007053 check = true;
7054#endif // XDEBUG
7055 if (check) {
7056 assert(N && "Checking nonexistent SDNode");
7057 SmallPtrSet<const SDNode*, 32> visited;
7058 SmallPtrSet<const SDNode*, 32> checked;
7059 checkForCyclesHelper(N, visited, checked, DAG);
7060 }
7061#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00007062}
7063
Adam Nemetb4690e32014-05-31 16:23:20 +00007064void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7065 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00007066}