blob: 3b22e9715d13d5d664b3982da4b8b7e5d42b5b52 [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
Sanjay Patel801caff2015-05-05 21:40:38 +0000403/// Add logical or fast math flag values to FoldingSetNodeID value.
404static void AddNodeIDFlags(FoldingSetNodeID &ID, unsigned Opcode,
405 const SDNodeFlags *Flags) {
406 if (!Flags || !mayHaveOptimizationFlags(Opcode))
407 return;
408
409 unsigned RawFlags = Flags->getRawFlags();
410 // If no flags are set, do not alter the ID. This saves time and allows
411 // a gradual increase in API usage of the optional optimization flags.
412 if (RawFlags != 0)
413 ID.AddInteger(RawFlags);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000414}
415
Sanjay Patel801caff2015-05-05 21:40:38 +0000416static void AddNodeIDFlags(FoldingSetNodeID &ID, const SDNode *N) {
417 if (auto *Node = dyn_cast<SDNodeWithFlags>(N))
418 AddNodeIDFlags(ID, Node->getOpcode(), &Node->Flags);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000419}
420
Craig Topper633d99b2014-04-27 23:22:43 +0000421static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
422 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000423 AddNodeIDOpcode(ID, OpC);
424 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000425 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000426}
427
Duncan Sands835bdca2008-10-27 15:30:53 +0000428/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
429/// the NodeID data.
430static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000431 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000432 case ISD::TargetExternalSymbol:
433 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000434 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000435 default: break; // Normal nodes don't need extra info.
436 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000437 case ISD::Constant: {
438 const ConstantSDNode *C = cast<ConstantSDNode>(N);
439 ID.AddPointer(C->getConstantIntValue());
440 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000441 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000442 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000443 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000444 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000445 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000446 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000447 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000448 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000449 case ISD::GlobalAddress:
450 case ISD::TargetGlobalTLSAddress:
451 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000452 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000453 ID.AddPointer(GA->getGlobal());
454 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000455 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000456 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000457 break;
458 }
459 case ISD::BasicBlock:
460 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
461 break;
462 case ISD::Register:
463 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
464 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000465 case ISD::RegisterMask:
466 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
467 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000468 case ISD::SRCVALUE:
469 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
470 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000471 case ISD::FrameIndex:
472 case ISD::TargetFrameIndex:
473 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
474 break;
475 case ISD::JumpTable:
476 case ISD::TargetJumpTable:
477 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000478 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000479 break;
480 case ISD::ConstantPool:
481 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000482 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000483 ID.AddInteger(CP->getAlignment());
484 ID.AddInteger(CP->getOffset());
485 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000486 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000487 else
488 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000489 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000490 break;
491 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000492 case ISD::TargetIndex: {
493 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
494 ID.AddInteger(TI->getIndex());
495 ID.AddInteger(TI->getOffset());
496 ID.AddInteger(TI->getTargetFlags());
497 break;
498 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000499 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000500 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000501 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000502 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000503 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000504 break;
505 }
506 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000507 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000508 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000509 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000510 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000511 break;
512 }
Dan Gohman12f24902008-12-23 21:37:04 +0000513 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000514 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000515 case ISD::ATOMIC_SWAP:
516 case ISD::ATOMIC_LOAD_ADD:
517 case ISD::ATOMIC_LOAD_SUB:
518 case ISD::ATOMIC_LOAD_AND:
519 case ISD::ATOMIC_LOAD_OR:
520 case ISD::ATOMIC_LOAD_XOR:
521 case ISD::ATOMIC_LOAD_NAND:
522 case ISD::ATOMIC_LOAD_MIN:
523 case ISD::ATOMIC_LOAD_MAX:
524 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000525 case ISD::ATOMIC_LOAD_UMAX:
526 case ISD::ATOMIC_LOAD:
527 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000528 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000529 ID.AddInteger(AT->getMemoryVT().getRawBits());
530 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000531 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
532 break;
533 }
534 case ISD::PREFETCH: {
535 const MemSDNode *PF = cast<MemSDNode>(N);
536 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000537 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000538 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000539 case ISD::VECTOR_SHUFFLE: {
540 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000541 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000542 i != e; ++i)
543 ID.AddInteger(SVN->getMaskElt(i));
544 break;
545 }
Dan Gohman6c938802009-10-30 01:27:03 +0000546 case ISD::TargetBlockAddress:
547 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000548 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
549 ID.AddPointer(BA->getBlockAddress());
550 ID.AddInteger(BA->getOffset());
551 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000552 break;
553 }
Mon P Wang6a490372008-06-25 08:15:39 +0000554 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000555
Sanjay Patel801caff2015-05-05 21:40:38 +0000556 AddNodeIDFlags(ID, N);
557
Pete Cooper91244262012-07-30 20:23:19 +0000558 // Target specific memory nodes could also have address spaces to check.
559 if (N->isTargetMemoryOpcode())
560 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000561}
562
Duncan Sands835bdca2008-10-27 15:30:53 +0000563/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
564/// data.
565static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
566 AddNodeIDOpcode(ID, N->getOpcode());
567 // Add the return value info.
568 AddNodeIDValueTypes(ID, N->getVTList());
569 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000570 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000571
572 // Handle SDNode leafs with special info.
573 AddNodeIDCustom(ID, N);
574}
575
Dan Gohman2da2bed2008-08-20 15:58:01 +0000576/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000577/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000578/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000579///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000580static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000581encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000582 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000583 assert((ConvType & 3) == ConvType &&
584 "ConvType may not require more than 2 bits!");
585 assert((AM & 7) == AM &&
586 "AM may not require more than 3 bits!");
587 return ConvType |
588 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000589 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000590 (isNonTemporal << 6) |
591 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000592}
593
Jim Laskeyf576b422006-10-27 23:46:08 +0000594//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000595// SelectionDAG Class
596//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000597
Duncan Sands835bdca2008-10-27 15:30:53 +0000598/// doNotCSE - Return true if CSE should not be performed for this node.
599static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000600 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000601 return true; // Never CSE anything that produces a flag.
602
603 switch (N->getOpcode()) {
604 default: break;
605 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000606 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000607 return true; // Never CSE these nodes.
608 }
609
610 // Check that remaining values produced are not flags.
611 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000612 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000613 return true; // Never CSE anything that produces a flag.
614
615 return false;
616}
617
Chris Lattner9c667932005-01-07 21:09:16 +0000618/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000619/// SelectionDAG.
620void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000621 // Create a dummy node (which is not added to allnodes), that adds a reference
622 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000623 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000624
Chris Lattner8927c872006-08-04 17:45:20 +0000625 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000626
Chris Lattner8927c872006-08-04 17:45:20 +0000627 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000628 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000629 if (I->use_empty())
630 DeadNodes.push_back(I);
631
Dan Gohman91697632008-07-07 20:57:48 +0000632 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000633
Dan Gohman91697632008-07-07 20:57:48 +0000634 // If the root changed (e.g. it was a dead load, update the root).
635 setRoot(Dummy.getValue());
636}
637
638/// RemoveDeadNodes - This method deletes the unreachable nodes in the
639/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000640void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000641
Chris Lattner8927c872006-08-04 17:45:20 +0000642 // Process the worklist, deleting the nodes and adding their uses to the
643 // worklist.
644 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000645 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000646
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000647 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000648 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000649
Chris Lattner8927c872006-08-04 17:45:20 +0000650 // Take the node out of the appropriate CSE map.
651 RemoveNodeFromCSEMaps(N);
652
653 // Next, brutally remove the operand list. This is safe to do, as there are
654 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000655 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
656 SDUse &Use = *I++;
657 SDNode *Operand = Use.getNode();
658 Use.set(SDValue());
659
Chris Lattner8927c872006-08-04 17:45:20 +0000660 // Now that we removed this operand, see if there are no uses of it left.
661 if (Operand->use_empty())
662 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000663 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000664
Dan Gohman534c8a22009-01-19 22:39:36 +0000665 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000666 }
Chris Lattner9c667932005-01-07 21:09:16 +0000667}
668
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000669void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000670 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000671
672 // Create a dummy node that adds a reference to the root node, preventing
673 // it from being deleted. (This matters if the root is an operand of the
674 // dead node.)
675 HandleSDNode Dummy(getRoot());
676
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000677 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000678}
679
Chris Lattnerc738d002005-08-29 21:59:31 +0000680void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000681 // First take this out of the appropriate CSE map.
682 RemoveNodeFromCSEMaps(N);
683
Scott Michelcf0da6c2009-02-17 22:15:04 +0000684 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000685 // AllNodes list, and delete the node.
686 DeleteNodeNotInCSEMaps(N);
687}
688
689void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000690 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
691 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000692
Dan Gohman86aa16a2008-09-30 18:30:35 +0000693 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000694 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000695
Dan Gohman534c8a22009-01-19 22:39:36 +0000696 DeallocateNode(N);
697}
698
Frederic Riss8ad4f492014-11-11 21:21:08 +0000699void SDDbgInfo::erase(const SDNode *Node) {
700 DbgValMapType::iterator I = DbgValMap.find(Node);
701 if (I == DbgValMap.end())
702 return;
703 for (auto &Val: I->second)
704 Val->setIsInvalidated();
705 DbgValMap.erase(I);
706}
707
Dan Gohman534c8a22009-01-19 22:39:36 +0000708void SelectionDAG::DeallocateNode(SDNode *N) {
709 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000710 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000711
Dan Gohman534c8a22009-01-19 22:39:36 +0000712 // Set the opcode to DELETED_NODE to help catch bugs when node
713 // memory is reallocated.
714 N->NodeType = ISD::DELETED_NODE;
715
Dan Gohman2e834902008-08-26 01:44:34 +0000716 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000717
Frederic Riss8ad4f492014-11-11 21:21:08 +0000718 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
719 // them and forget about that node.
720 DbgInfo->erase(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000721}
722
Chandler Carruth41b20e72014-07-22 04:07:55 +0000723#ifndef NDEBUG
724/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
725static void VerifySDNode(SDNode *N) {
726 switch (N->getOpcode()) {
727 default:
728 break;
729 case ISD::BUILD_PAIR: {
730 EVT VT = N->getValueType(0);
731 assert(N->getNumValues() == 1 && "Too many results!");
732 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
733 "Wrong return type!");
734 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
735 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
736 "Mismatched operand types!");
737 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
738 "Wrong operand type!");
739 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
740 "Wrong return type size");
741 break;
742 }
743 case ISD::BUILD_VECTOR: {
744 assert(N->getNumValues() == 1 && "Too many results!");
745 assert(N->getValueType(0).isVector() && "Wrong return type!");
746 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
747 "Wrong number of operands!");
748 EVT EltVT = N->getValueType(0).getVectorElementType();
749 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
750 assert((I->getValueType() == EltVT ||
751 (EltVT.isInteger() && I->getValueType().isInteger() &&
752 EltVT.bitsLE(I->getValueType()))) &&
753 "Wrong operand type!");
754 assert(I->getValueType() == N->getOperand(0).getValueType() &&
755 "Operands must all have the same type");
756 }
757 break;
758 }
759 }
760}
761#endif // NDEBUG
762
763/// \brief Insert a newly allocated node into the DAG.
764///
765/// Handles insertion into the all nodes list and CSE map, as well as
766/// verification and other common operations when a new node is allocated.
767void SelectionDAG::InsertNode(SDNode *N) {
768 AllNodes.push_back(N);
769#ifndef NDEBUG
770 VerifySDNode(N);
771#endif
772}
773
Chris Lattner19732782005-08-16 18:17:10 +0000774/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
775/// correspond to it. This is useful when we're about to delete or repurpose
776/// the node. We don't want future request for structurally identical nodes
777/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000778bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000779 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000780 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000781 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000782 case ISD::CONDCODE:
783 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
784 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000785 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
786 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000787 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000788 case ISD::ExternalSymbol:
789 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000790 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000791 case ISD::TargetExternalSymbol: {
792 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
793 Erased = TargetExternalSymbols.erase(
794 std::pair<std::string,unsigned char>(ESN->getSymbol(),
795 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000796 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000797 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000798 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000799 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000800 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000801 Erased = ExtendedValueTypeNodes.erase(VT);
802 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000803 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
804 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000805 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000806 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000807 }
Chris Lattner9c667932005-01-07 21:09:16 +0000808 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000809 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000810 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
811 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000812 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000813 break;
814 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000815#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000816 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000817 // flag result (which cannot be CSE'd) or is one of the special cases that are
818 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000819 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000820 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000821 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000822 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000823 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000824 }
825#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000826 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000827}
828
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000829/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
830/// maps and modified in place. Add it back to the CSE maps, unless an identical
831/// node already exists, in which case transfer all its users to the existing
832/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000833///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000834void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000835SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000836 // For node types that aren't CSE'd, just act as if no identical node
837 // already exists.
838 if (!doNotCSE(N)) {
839 SDNode *Existing = CSEMap.GetOrInsertNode(N);
840 if (Existing != N) {
841 // If there was already an existing matching node, use ReplaceAllUsesWith
842 // to replace the dead one with the existing one. This can cause
843 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000844 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000845
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000846 // N is now dead. Inform the listeners and delete it.
847 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
848 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000849 DeleteNodeNotInCSEMaps(N);
850 return;
851 }
852 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000853
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000854 // If the node doesn't already exist, we updated it. Inform listeners.
855 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
856 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000857}
858
Chris Lattnerf34156e2006-01-28 09:32:45 +0000859/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000860/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000861/// return null, otherwise return a pointer to the slot it would take. If a
862/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000863SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000864 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000865 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000866 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000867
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000868 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000869 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000870 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000871 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000872 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000873 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000874}
875
876/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000877/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000878/// return null, otherwise return a pointer to the slot it would take. If a
879/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000880SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000881 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000882 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000883 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000884 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000885
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000886 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000887 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000888 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000889 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000890 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000891 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000892}
893
894
895/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000896/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000897/// return null, otherwise return a pointer to the slot it would take. If a
898/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000899SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000900 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000901 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000902 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000903
Jim Laskeyf576b422006-10-27 23:46:08 +0000904 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000905 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000906 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000907 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000908 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000909}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000910
Owen Anderson53aa7a92009-08-10 22:56:29 +0000911/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000912/// given type.
913///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000914unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000915 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000916 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000917 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000918
Eric Christopher1e845f22014-10-08 21:08:32 +0000919 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000920}
Chris Lattner9c667932005-01-07 21:09:16 +0000921
Dale Johannesen8ba713212009-02-07 02:15:05 +0000922// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000923SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000924 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000925 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
926 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
927 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000928 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000929 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000930}
931
Eric Christopher8d07f442014-10-08 01:57:58 +0000932void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000933 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000934 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000935 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000936 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000937}
938
Chris Lattner600d3082003-08-11 14:57:33 +0000939SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000940 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000941 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000942 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000943}
944
945void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000946 assert(&*AllNodes.begin() == &EntryNode);
947 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000948 while (!AllNodes.empty())
949 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000950}
951
Sanjay Patel801caff2015-05-05 21:40:38 +0000952SDNode *SelectionDAG::GetSDNodeWithFlags(unsigned Opcode, SDLoc DL,
953 SDVTList VTs, ArrayRef<SDValue> Ops,
954 const SDNodeFlags *Flags) {
955 if (mayHaveOptimizationFlags(Opcode)) {
956 // If no flags were passed in, use a default flags object.
957 SDNodeFlags F;
958 if (Flags == nullptr)
959 Flags = &F;
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000960
Sanjay Patel801caff2015-05-05 21:40:38 +0000961 SDNodeWithFlags *NodeWithFlags = new (NodeAllocator) SDNodeWithFlags(
962 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, Ops, *Flags);
963 return NodeWithFlags;
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000964 }
965
Sanjay Patel801caff2015-05-05 21:40:38 +0000966 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
967 DL.getDebugLoc(), VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000968 return N;
969}
970
Dan Gohmane1a9a782008-08-27 23:52:12 +0000971void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000972 allnodes_clear();
973 OperandAllocator.Reset();
974 CSEMap.clear();
975
976 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000977 ExternalSymbols.clear();
978 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000979 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000980 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000981 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000982 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000983
Craig Topperc0196b12014-04-14 00:51:57 +0000984 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000985 AllNodes.push_back(&EntryNode);
986 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000987 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000988}
989
Andrew Trickef9de2a2013-05-25 02:42:55 +0000990SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000991 return VT.bitsGT(Op.getValueType()) ?
992 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
993 getNode(ISD::TRUNCATE, DL, VT, Op);
994}
995
Andrew Trickef9de2a2013-05-25 02:42:55 +0000996SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000997 return VT.bitsGT(Op.getValueType()) ?
998 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
999 getNode(ISD::TRUNCATE, DL, VT, Op);
1000}
1001
Andrew Trickef9de2a2013-05-25 02:42:55 +00001002SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001003 return VT.bitsGT(Op.getValueType()) ?
1004 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1005 getNode(ISD::TRUNCATE, DL, VT, Op);
1006}
1007
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001008SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1009 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001010 if (VT.bitsLE(Op.getValueType()))
1011 return getNode(ISD::TRUNCATE, SL, VT, Op);
1012
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001013 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001014 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1015}
1016
Andrew Trickef9de2a2013-05-25 02:42:55 +00001017SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001018 assert(!VT.isVector() &&
1019 "getZeroExtendInReg should use the vector element type instead of "
1020 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001021 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001022 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1023 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001024 VT.getSizeInBits());
1025 return getNode(ISD::AND, DL, Op.getValueType(), Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001026 getConstant(Imm, DL, Op.getValueType()));
Bill Wendlingc4093182009-01-30 22:23:15 +00001027}
1028
Chandler Carruth0b666e02014-07-10 12:32:32 +00001029SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1030 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1031 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1032 "The sizes of the input and result must match in order to perform the "
1033 "extend in-register.");
1034 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1035 "The destination vector type must have fewer lanes than the input.");
1036 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1037}
1038
1039SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1040 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1041 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1042 "The sizes of the input and result must match in order to perform the "
1043 "extend in-register.");
1044 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1045 "The destination vector type must have fewer lanes than the input.");
1046 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1047}
1048
Chandler Carruthafe4b252014-07-09 10:58:18 +00001049SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1050 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001051 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1052 "The sizes of the input and result must match in order to perform the "
1053 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001054 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1055 "The destination vector type must have fewer lanes than the input.");
1056 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1057}
1058
Bob Wilsonc5890052009-01-22 17:39:32 +00001059/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1060///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001061SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001062 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001063 SDValue NegOne =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001064 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001065 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1066}
1067
Pete Cooper7fd1d722014-05-12 23:26:58 +00001068SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1069 EVT EltVT = VT.getScalarType();
1070 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001071 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001072 case TargetLowering::ZeroOrOneBooleanContent:
1073 case TargetLowering::UndefinedBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001074 TrueValue = getConstant(1, DL, VT);
Pete Cooper7fd1d722014-05-12 23:26:58 +00001075 break;
1076 case TargetLowering::ZeroOrNegativeOneBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001077 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
Pete Cooper7fd1d722014-05-12 23:26:58 +00001078 VT);
1079 break;
1080 }
1081 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1082}
1083
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001084SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT,
1085 bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001086 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001087 assert((EltVT.getSizeInBits() >= 64 ||
1088 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1089 "getConstant with a uint64_t value that doesn't fit in the type!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001090 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001091}
1092
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001093SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT,
1094 bool isO)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001095{
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001096 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001097}
1098
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001099SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT,
1100 bool isT, bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001101 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001102
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001103 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001104 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001105
Duncan Sandsf2641e12011-09-06 19:07:46 +00001106 // In some cases the vector type is legal but the element type is illegal and
1107 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1108 // inserted value (the type does not need to match the vector element type).
1109 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001110 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001111 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001112 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001113 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1114 Elt = ConstantInt::get(*getContext(), NewVal);
1115 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001116 // In other cases the element type is illegal and needs to be expanded, for
1117 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1118 // the value into n parts and use a vector type with n-times the elements.
1119 // Then bitcast to the type requested.
1120 // Legalizing constants too early makes the DAGCombiner's job harder so we
1121 // only legalize if the DAG tells us we must produce legal types.
1122 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1123 TLI->getTypeAction(*getContext(), EltVT) ==
1124 TargetLowering::TypeExpandInteger) {
1125 APInt NewVal = Elt->getValue();
1126 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1127 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1128 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1129 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1130
1131 // Check the temporary vector is the correct size. If this fails then
1132 // getTypeToTransformTo() probably returned a type whose size (in bits)
1133 // isn't a power-of-2 factor of the requested type size.
1134 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1135
1136 SmallVector<SDValue, 2> EltParts;
1137 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1138 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001139 .trunc(ViaEltSizeInBits), DL,
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001140 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001141 }
1142
1143 // EltParts is currently in little endian order. If we actually want
1144 // big-endian order then reverse it now.
1145 if (TLI->isBigEndian())
1146 std::reverse(EltParts.begin(), EltParts.end());
1147
1148 // The elements must be reversed when the element order is different
1149 // to the endianness of the elements (because the BITCAST is itself a
1150 // vector shuffle in this situation). However, we do not need any code to
1151 // perform this reversal because getConstant() is producing a vector
1152 // splat.
1153 // This situation occurs in MIPS MSA.
1154
1155 SmallVector<SDValue, 8> Ops;
1156 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1157 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1158
1159 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1160 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001161 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001162 return Result;
1163 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001164
1165 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1166 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001167 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001168 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001169 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001170 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001171 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001172 void *IP = nullptr;
1173 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001174 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001175 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001176 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001177
Dan Gohman7a7742c2007-12-12 22:21:26 +00001178 if (!N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001179 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, DL.getDebugLoc(),
1180 EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001181 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001182 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001183 }
1184
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001185 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001186 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001187 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001188 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001189 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001190 }
1191 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001192}
1193
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001194SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) {
1195 return getConstant(Val, DL, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001196}
1197
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001198SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT,
1199 bool isTarget) {
1200 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001201}
1202
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001203SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT,
1204 bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001205 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001206
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001207 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001208
Chris Lattner381dddc2005-02-17 20:17:32 +00001209 // Do the map lookup using the actual bit pattern for the floating point
1210 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1211 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001212 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001213 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001214 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001215 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001216 void *IP = nullptr;
1217 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001218 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001219 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001220 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001221
Evan Cheng9458e6a2007-06-29 21:36:04 +00001222 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001223 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001224 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001225 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001226 }
1227
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001228 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001229 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001230 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001231 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001232 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001233 }
1234 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001235}
1236
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001237SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT,
1238 bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001239 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001240 if (EltVT==MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001241 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001242 else if (EltVT==MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001243 return getConstantFP(APFloat(Val), DL, VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001244 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1245 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001246 bool ignored;
1247 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001248 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001249 &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001250 return getConstantFP(apf, DL, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001251 } else
1252 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001253}
1254
Andrew Trickef9de2a2013-05-25 02:42:55 +00001255SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001256 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001257 bool isTargetGA,
1258 unsigned char TargetFlags) {
1259 assert((TargetFlags == 0 || isTargetGA) &&
1260 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001261
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001262 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001263 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001264 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001265 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001266
Chris Lattner8e34f982009-06-25 21:21:14 +00001267 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001268 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001269 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1270 else
1271 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001272
Jim Laskeyf576b422006-10-27 23:46:08 +00001273 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001274 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001275 ID.AddPointer(GV);
1276 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001277 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001278 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001279 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001280 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001281 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001282
Andrew Trickef9de2a2013-05-25 02:42:55 +00001283 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1284 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001285 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001286 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001287 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001288 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001289}
1290
Owen Anderson53aa7a92009-08-10 22:56:29 +00001291SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001292 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001293 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001294 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001295 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001296 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001297 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001298 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001299
Dan Gohman01c65a22010-03-18 18:49:47 +00001300 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001301 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001302 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001303 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001304}
1305
Owen Anderson53aa7a92009-08-10 22:56:29 +00001306SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001307 unsigned char TargetFlags) {
1308 assert((TargetFlags == 0 || isTarget) &&
1309 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001310 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001311 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001312 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001313 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001314 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001315 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001316 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001317 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001318
Dan Gohman01c65a22010-03-18 18:49:47 +00001319 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1320 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001321 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001322 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001323 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001324}
1325
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001326SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001327 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001328 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001329 unsigned char TargetFlags) {
1330 assert((TargetFlags == 0 || isTarget) &&
1331 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001332 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001333 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001334 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001335 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001336 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001337 ID.AddInteger(Alignment);
1338 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001339 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001340 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001341 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001342 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001343 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001344
Dan Gohman01c65a22010-03-18 18:49:47 +00001345 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1346 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001347 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001348 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001349 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001350}
1351
Chris Lattner061a1ea2005-01-07 07:46:32 +00001352
Owen Anderson53aa7a92009-08-10 22:56:29 +00001353SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001354 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001355 bool isTarget,
1356 unsigned char TargetFlags) {
1357 assert((TargetFlags == 0 || isTarget) &&
1358 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001359 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001360 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001361 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001362 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001363 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001364 ID.AddInteger(Alignment);
1365 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001366 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001367 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001368 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001369 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001370 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001371
Dan Gohman01c65a22010-03-18 18:49:47 +00001372 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1373 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001374 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001375 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001376 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001377}
1378
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001379SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1380 unsigned char TargetFlags) {
1381 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001382 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001383 ID.AddInteger(Index);
1384 ID.AddInteger(Offset);
1385 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001386 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001387 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1388 return SDValue(E, 0);
1389
1390 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1391 TargetFlags);
1392 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001393 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001394 return SDValue(N, 0);
1395}
1396
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001397SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001398 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001399 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001400 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001401 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001402 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001403 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001404
Dan Gohman01c65a22010-03-18 18:49:47 +00001405 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001406 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001407 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001408 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001409}
1410
Owen Anderson53aa7a92009-08-10 22:56:29 +00001411SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001412 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1413 ValueTypeNodes.size())
1414 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001415
Duncan Sands13237ac2008-06-06 12:08:01 +00001416 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001417 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001418
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001419 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001420 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001421 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001422 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001423}
1424
Owen Anderson53aa7a92009-08-10 22:56:29 +00001425SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001426 SDNode *&N = ExternalSymbols[Sym];
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) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001429 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001430 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001431}
1432
Owen Anderson53aa7a92009-08-10 22:56:29 +00001433SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001434 unsigned char TargetFlags) {
1435 SDNode *&N =
1436 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1437 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001438 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001439 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001440 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001441 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001442}
1443
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001444SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001445 if ((unsigned)Cond >= CondCodeNodes.size())
1446 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001447
Craig Topperc0196b12014-04-14 00:51:57 +00001448 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001449 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001450 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001451 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001452 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001453
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001454 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001455}
1456
Nate Begeman5f829d82009-04-29 05:20:52 +00001457// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1458// the shuffle mask M that point at N1 to point at N2, and indices that point
1459// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001460static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1461 std::swap(N1, N2);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001462 ShuffleVectorSDNode::commuteMask(M);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001463}
1464
Andrew Trickef9de2a2013-05-25 02:42:55 +00001465SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001466 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001467 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1468 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001469
1470 // Canonicalize shuffle undef, undef -> undef
1471 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001472 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001473
Eric Christopherdfda92b2009-08-22 00:40:45 +00001474 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001475 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001476 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001477 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001478 for (unsigned i = 0; i != NElts; ++i) {
1479 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001480 MaskVec.push_back(Mask[i]);
1481 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001482
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001483 // Canonicalize shuffle v, v -> v, undef
1484 if (N1 == N2) {
1485 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001486 for (unsigned i = 0; i != NElts; ++i)
1487 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001488 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001489
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001490 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1491 if (N1.getOpcode() == ISD::UNDEF)
1492 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001493
Chandler Carruth1b5285d2015-02-15 12:18:12 +00001494 // If shuffling a splat, try to blend the splat instead. We do this here so
1495 // that even when this arises during lowering we don't have to re-handle it.
1496 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1497 BitVector UndefElements;
1498 SDValue Splat = BV->getSplatValue(&UndefElements);
1499 if (!Splat)
1500 return;
1501
1502 for (int i = 0; i < (int)NElts; ++i) {
1503 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1504 continue;
1505
1506 // If this input comes from undef, mark it as such.
1507 if (UndefElements[MaskVec[i] - Offset]) {
1508 MaskVec[i] = -1;
1509 continue;
1510 }
1511
1512 // If we can blend a non-undef lane, use that instead.
1513 if (!UndefElements[i])
1514 MaskVec[i] = i + Offset;
1515 }
1516 };
1517 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1518 BlendSplat(N1BV, 0);
1519 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1520 BlendSplat(N2BV, NElts);
1521
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001522 // Canonicalize all index into lhs, -> shuffle lhs, undef
1523 // Canonicalize all index into rhs, -> shuffle rhs, undef
1524 bool AllLHS = true, AllRHS = true;
1525 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001526 for (unsigned i = 0; i != NElts; ++i) {
1527 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001528 if (N2Undef)
1529 MaskVec[i] = -1;
1530 else
1531 AllLHS = false;
1532 } else if (MaskVec[i] >= 0) {
1533 AllRHS = false;
1534 }
1535 }
1536 if (AllLHS && AllRHS)
1537 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001538 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001539 N2 = getUNDEF(VT);
1540 if (AllRHS) {
1541 N1 = getUNDEF(VT);
1542 commuteShuffle(N1, N2, MaskVec);
1543 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001544 // Reset our undef status after accounting for the mask.
1545 N2Undef = N2.getOpcode() == ISD::UNDEF;
1546 // Re-check whether both sides ended up undef.
1547 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1548 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001549
Craig Topper9a39b072013-08-08 08:03:12 +00001550 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001551 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001552 for (unsigned i = 0; i != NElts; ++i) {
1553 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001554 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001555 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001556 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001557 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001558
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001559 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001560 if (N2Undef) {
1561 SDValue V = N1;
1562
1563 // Look through any bitcasts. We check that these don't change the number
1564 // (and size) of elements and just changes their types.
1565 while (V.getOpcode() == ISD::BITCAST)
1566 V = V->getOperand(0);
1567
1568 // A splat should always show up as a build vector node.
1569 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001570 BitVector UndefElements;
1571 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001572 // If this is a splat of an undef, shuffling it is also undef.
1573 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1574 return getUNDEF(VT);
1575
Andrea Di Biagio83814752015-01-24 11:54:29 +00001576 bool SameNumElts =
1577 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1578
Chandler Carruth142e9662014-07-08 08:45:38 +00001579 // We only have a splat which can skip shuffles if there is a splatted
1580 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001581 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001582 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1583 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001584 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001585 return N1;
1586 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1587 if (C->isNullValue())
1588 return N1;
1589 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001590
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001591 // If the shuffle itself creates a splat, build the vector directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001592 if (AllSame && SameNumElts) {
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001593 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1594 SmallVector<SDValue, 8> Ops(NElts, Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001595
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001596 EVT BuildVT = BV->getValueType(0);
1597 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001598
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001599 // We may have jumped through bitcasts, so the type of the
1600 // BUILD_VECTOR may not match the type of the shuffle.
1601 if (BuildVT != VT)
1602 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1603 return NewBV;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001604 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001605 }
1606 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001607
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001608 FoldingSetNodeID ID;
1609 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001610 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001611 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001612 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001613
Craig Topperc0196b12014-04-14 00:51:57 +00001614 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001616 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001617
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001618 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1619 // SDNode doesn't have access to it. This memory will be "leaked" when
1620 // the node is deallocated, but recovered when the NodeAllocator is released.
1621 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1622 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001623
Dan Gohman01c65a22010-03-18 18:49:47 +00001624 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001625 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1626 dl.getDebugLoc(), N1, N2,
1627 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001628 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001629 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001630 return SDValue(N, 0);
1631}
1632
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001633SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1634 MVT VT = SV.getSimpleValueType(0);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001635 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1636 ShuffleVectorSDNode::commuteMask(MaskVec);
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001637
1638 SDValue Op0 = SV.getOperand(0);
1639 SDValue Op1 = SV.getOperand(1);
1640 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1641}
1642
Andrew Trickef9de2a2013-05-25 02:42:55 +00001643SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001644 SDValue Val, SDValue DTy,
1645 SDValue STy, SDValue Rnd, SDValue Sat,
1646 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001647 // If the src and dest types are the same and the conversion is between
1648 // integer types of the same sign or two floats, no conversion is necessary.
1649 if (DTy == STy &&
1650 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001651 return Val;
1652
1653 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001654 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001655 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001656 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001657 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001658 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001659
Jack Carter170a5f22013-09-09 22:02:08 +00001660 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1661 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001662 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001663 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001664 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001665 return SDValue(N, 0);
1666}
1667
Owen Anderson53aa7a92009-08-10 22:56:29 +00001668SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001669 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001670 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001671 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001672 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001673 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001674 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001675
Dan Gohman01c65a22010-03-18 18:49:47 +00001676 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001677 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001678 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001679 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001680}
1681
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001682SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1683 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001684 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001685 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001686 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001687 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1688 return SDValue(E, 0);
1689
1690 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1691 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001692 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001693 return SDValue(N, 0);
1694}
1695
Andrew Trickef9de2a2013-05-25 02:42:55 +00001696SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001697 FoldingSetNodeID ID;
1698 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001699 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001700 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001701 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001702 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001703 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001704
Jack Carter170a5f22013-09-09 22:02:08 +00001705 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1706 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001707 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001708 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001709 return SDValue(N, 0);
1710}
1711
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001712
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001713SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001714 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001715 bool isTarget,
1716 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001717 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1718
1719 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001720 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001721 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001722 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001723 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001724 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001725 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001726 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001727
Michael Liaoabb87d42012-09-12 21:43:09 +00001728 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1729 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001730 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001731 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001732 return SDValue(N, 0);
1733}
1734
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001735SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001736 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001737 "SrcValue is not a pointer?");
1738
Jim Laskeyf576b422006-10-27 23:46:08 +00001739 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001740 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001741 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001742
Craig Topperc0196b12014-04-14 00:51:57 +00001743 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001744 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001745 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001746
Dan Gohman01c65a22010-03-18 18:49:47 +00001747 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001748 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001749 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001750 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001751}
1752
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001753/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1754SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1755 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001756 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001757 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001758
Craig Topperc0196b12014-04-14 00:51:57 +00001759 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001760 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1761 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001762
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001763 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1764 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001765 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001766 return SDValue(N, 0);
1767}
1768
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001769/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1770SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1771 unsigned SrcAS, unsigned DestAS) {
1772 SDValue Ops[] = {Ptr};
1773 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001774 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001775 ID.AddInteger(SrcAS);
1776 ID.AddInteger(DestAS);
1777
Craig Topperc0196b12014-04-14 00:51:57 +00001778 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001779 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1780 return SDValue(E, 0);
1781
1782 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1783 dl.getDebugLoc(),
1784 VT, Ptr, SrcAS, DestAS);
1785 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001786 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001787 return SDValue(N, 0);
1788}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001789
Duncan Sands41826032009-01-31 15:50:11 +00001790/// getShiftAmountOperand - Return the specified value casted to
1791/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001792SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001793 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001794 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001795 if (OpTy == ShTy || OpTy.isVector()) return Op;
1796
1797 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001798 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001799}
1800
Chris Lattner9eb7a822007-10-15 17:47:20 +00001801/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1802/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001803SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001804 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001805 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001806 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001807 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001808 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001809
David Greene1fbe0542009-11-12 20:49:22 +00001810 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001811 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001812}
1813
Duncan Sands445071c2008-12-09 21:33:20 +00001814/// CreateStackTemporary - Create a stack temporary suitable for holding
1815/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001816SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001817 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1818 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001819 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1820 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001821 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001822 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1823 TD->getPrefTypeAlignment(Ty2));
1824
1825 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001826 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001827 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001828}
1829
Owen Anderson53aa7a92009-08-10 22:56:29 +00001830SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001831 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001832 // These setcc operations always fold.
1833 switch (Cond) {
1834 default: break;
1835 case ISD::SETFALSE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001836 case ISD::SETFALSE2: return getConstant(0, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001837 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001838 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001839 TargetLowering::BooleanContent Cnt =
1840 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001841 return getConstant(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001842 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1843 VT);
Tim Northover950fcc02013-09-06 12:38:12 +00001844 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001845
Chris Lattner393d96a2006-04-27 05:01:07 +00001846 case ISD::SETOEQ:
1847 case ISD::SETOGT:
1848 case ISD::SETOGE:
1849 case ISD::SETOLT:
1850 case ISD::SETOLE:
1851 case ISD::SETONE:
1852 case ISD::SETO:
1853 case ISD::SETUO:
1854 case ISD::SETUEQ:
1855 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001856 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001857 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001858 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001859
Gabor Greiff304a7a2008-08-28 21:40:38 +00001860 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001861 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001862 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001863 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001864
Chris Lattner061a1ea2005-01-07 07:46:32 +00001865 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001866 default: llvm_unreachable("Unknown integer setcc!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001867 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT);
1868 case ISD::SETNE: return getConstant(C1 != C2, dl, VT);
1869 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1870 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1871 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1872 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1873 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT);
1874 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT);
1875 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT);
1876 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001877 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001878 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001879 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001880 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1881 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001882 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001883 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001884 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001885 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001886 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001887 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001888 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001889 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001890 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001891 // fall through
1892 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001893 R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001894 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001895 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001896 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001897 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001898 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001899 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001900 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001901 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001902 case ISD::SETLE: 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
1905 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001906 R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001907 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001908 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001909 // fall through
1910 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001911 R==APFloat::cmpEqual, dl, VT);
1912 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1913 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001914 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001915 R==APFloat::cmpEqual, dl, VT);
1916 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001917 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001918 R==APFloat::cmpLessThan, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001919 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001920 R==APFloat::cmpUnordered, dl, VT);
1921 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1922 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001923 }
1924 } else {
1925 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001926 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1927 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001928 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001929 return SDValue();
1930
1931 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001932 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001933 }
1934
Chris Lattnerd47675e2005-08-09 20:20:18 +00001935 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001936 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001937}
1938
Dan Gohman1f372ed2008-02-25 21:11:39 +00001939/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1940/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001941bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001942 // This predicate is not safe for vector operations.
1943 if (Op.getValueType().isVector())
1944 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001945
Dan Gohman1d459e42009-12-11 21:31:27 +00001946 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001947 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1948}
1949
Dan Gohman309d3d52007-06-22 14:59:07 +00001950/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1951/// this predicate to simplify operations downstream. Mask is known to be zero
1952/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001953bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001954 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001955 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001956 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001957 return (KnownZero & Mask) == Mask;
1958}
1959
Jay Foada0653a32014-05-14 21:14:37 +00001960/// Determine which bits of Op are known to be either zero or one and return
1961/// them in the KnownZero/KnownOne bitsets.
1962void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1963 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001964 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001965
Dan Gohmanf990faf2008-02-13 00:35:47 +00001966 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001967 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001968 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001969
Dan Gohmanf990faf2008-02-13 00:35:47 +00001970 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001971
1972 switch (Op.getOpcode()) {
1973 case ISD::Constant:
1974 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001975 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1976 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001977 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001978 case ISD::AND:
1979 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001980 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1981 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001982
1983 // Output known-1 bits are only known if set in both the LHS & RHS.
1984 KnownOne &= KnownOne2;
1985 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1986 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001987 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001988 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001989 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1990 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001991
Dan Gohman309d3d52007-06-22 14:59:07 +00001992 // Output known-0 bits are only known if clear in both the LHS & RHS.
1993 KnownZero &= KnownZero2;
1994 // Output known-1 are known to be set if set in either the LHS | RHS.
1995 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001996 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001997 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001998 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1999 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002000
Dan Gohman309d3d52007-06-22 14:59:07 +00002001 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002002 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00002003 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2004 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2005 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00002006 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002007 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002008 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00002009 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2010 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002011
2012 // If low bits are zero in either operand, output low known-0 bits.
2013 // Also compute a conserative estimate for high known-0 bits.
2014 // More trickiness is possible, but this is sufficient for the
2015 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00002016 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00002017 unsigned TrailZ = KnownZero.countTrailingOnes() +
2018 KnownZero2.countTrailingOnes();
2019 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002020 KnownZero2.countLeadingOnes(),
2021 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002022
2023 TrailZ = std::min(TrailZ, BitWidth);
2024 LeadZ = std::min(LeadZ, BitWidth);
2025 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2026 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002027 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002028 }
2029 case ISD::UDIV: {
2030 // For the purposes of computing leading zeros we can conservatively
2031 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002032 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002033 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002034 unsigned LeadZ = KnownZero2.countLeadingOnes();
2035
Jay Foad25a5e4c2010-12-01 08:53:58 +00002036 KnownOne2.clearAllBits();
2037 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002038 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002039 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2040 if (RHSUnknownLeadingOnes != BitWidth)
2041 LeadZ = std::min(BitWidth,
2042 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002043
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002044 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002045 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002046 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002047 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002048 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2049 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002050
Dan Gohman309d3d52007-06-22 14:59:07 +00002051 // Only known if known in both the LHS and RHS.
2052 KnownOne &= KnownOne2;
2053 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002054 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002055 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002056 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2057 computeKnownBits(Op.getOperand(2), 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;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002063 case ISD::SADDO:
2064 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002065 case ISD::SSUBO:
2066 case ISD::USUBO:
2067 case ISD::SMULO:
2068 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002069 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002070 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002071 // The boolean result conforms to getBooleanContents.
2072 // If we know the result of a setcc has the top bits zero, use this info.
2073 // We know that we have an integer-based boolean since these operations
2074 // are only available for integer.
2075 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2076 TargetLowering::ZeroOrOneBooleanContent &&
2077 BitWidth > 1)
2078 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2079 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002080 case ISD::SETCC:
2081 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002082 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2083 TargetLowering::ZeroOrOneBooleanContent &&
2084 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002085 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002086 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002087 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002088 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002089 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002090 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002091
2092 // If the shift count is an invalid immediate, don't do anything.
2093 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002094 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002095
Jay Foada0653a32014-05-14 21:14:37 +00002096 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002097 KnownZero <<= ShAmt;
2098 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002099 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002100 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002101 }
Jay Foad5a29c362014-05-15 12:12:55 +00002102 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002103 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002104 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002105 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002106 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002107
Dan Gohman9db0aa82008-02-26 18:50:50 +00002108 // If the shift count is an invalid immediate, don't do anything.
2109 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002110 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002111
Jay Foada0653a32014-05-14 21:14:37 +00002112 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002113 KnownZero = KnownZero.lshr(ShAmt);
2114 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002115
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002116 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002117 KnownZero |= HighBits; // High bits known zero.
2118 }
Jay Foad5a29c362014-05-15 12:12:55 +00002119 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002120 case ISD::SRA:
2121 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002122 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002123
Dan Gohman9db0aa82008-02-26 18:50:50 +00002124 // If the shift count is an invalid immediate, don't do anything.
2125 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002126 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002127
Dan Gohman309d3d52007-06-22 14:59:07 +00002128 // If any of the demanded bits are produced by the sign extension, we also
2129 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002130 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002131
Jay Foada0653a32014-05-14 21:14:37 +00002132 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002133 KnownZero = KnownZero.lshr(ShAmt);
2134 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002135
Dan Gohman309d3d52007-06-22 14:59:07 +00002136 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002137 APInt SignBit = APInt::getSignBit(BitWidth);
2138 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002139
Dan Gohmanb717fda2008-02-20 16:30:17 +00002140 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002141 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002142 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002143 KnownOne |= HighBits; // New bits are known one.
2144 }
2145 }
Jay Foad5a29c362014-05-15 12:12:55 +00002146 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002147 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002148 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002149 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002150
2151 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002152 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002153 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002154
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002155 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002156 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002157
Dan Gohman309d3d52007-06-22 14:59:07 +00002158 // If the sign extended bits are demanded, we know that the sign
2159 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002160 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002161 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002162 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002163
Jay Foada0653a32014-05-14 21:14:37 +00002164 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002165 KnownOne &= InputDemandedBits;
2166 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002167
Dan Gohman309d3d52007-06-22 14:59:07 +00002168 // If the sign bit of the input is known set or clear, then we know the
2169 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002170 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002171 KnownZero |= NewBits;
2172 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002173 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002174 KnownOne |= NewBits;
2175 KnownZero &= ~NewBits;
2176 } else { // Input sign bit unknown
2177 KnownZero &= ~NewBits;
2178 KnownOne &= ~NewBits;
2179 }
Jay Foad5a29c362014-05-15 12:12:55 +00002180 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002181 }
2182 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002183 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002184 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002185 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002186 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002187 unsigned LowBits = Log2_32(BitWidth)+1;
2188 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002189 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002190 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002191 }
2192 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002193 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002194 // If this is a ZEXTLoad and we are looking at the loaded value.
2195 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002196 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002197 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002198 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002199 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002200 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002201 }
Jay Foad5a29c362014-05-15 12:12:55 +00002202 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002203 }
2204 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002205 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002206 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002207 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002208 KnownZero = KnownZero.trunc(InBits);
2209 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002210 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002211 KnownZero = KnownZero.zext(BitWidth);
2212 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002213 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002214 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002215 }
2216 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002217 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002218 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002219 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002220
Jay Foad583abbc2010-12-07 08:25:19 +00002221 KnownZero = KnownZero.trunc(InBits);
2222 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002223 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002224
2225 // Note if the sign bit is known to be zero or one.
2226 bool SignBitKnownZero = KnownZero.isNegative();
2227 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002228
Jay Foad583abbc2010-12-07 08:25:19 +00002229 KnownZero = KnownZero.zext(BitWidth);
2230 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002231
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002232 // If the sign bit is known zero or one, the top bits match.
2233 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002234 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002235 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002236 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002237 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002238 }
2239 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002240 EVT InVT = Op.getOperand(0).getValueType();
2241 unsigned InBits = InVT.getScalarType().getSizeInBits();
2242 KnownZero = KnownZero.trunc(InBits);
2243 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002244 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002245 KnownZero = KnownZero.zext(BitWidth);
2246 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002247 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002248 }
2249 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002250 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002251 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002252 KnownZero = KnownZero.zext(InBits);
2253 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002254 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002255 KnownZero = KnownZero.trunc(BitWidth);
2256 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002257 break;
2258 }
2259 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002260 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002261 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002262 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002263 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002264 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002265 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002266 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002267 case ISD::FGETSIGN:
2268 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002269 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002270 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002271
Dan Gohman72ec3f42008-04-28 17:02:21 +00002272 case ISD::SUB: {
2273 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2274 // We know that the top bits of C-X are clear if X contains less bits
2275 // than C (i.e. no wrap-around can happen). For example, 20-X is
2276 // positive if we can prove that X is >= 0 and < 16.
2277 if (CLHS->getAPIntValue().isNonNegative()) {
2278 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2279 // NLZ can't be BitWidth with no sign bit
2280 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002281 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002282
2283 // If all of the MaskV bits are known to be zero, then we know the
2284 // output top bits are zero, because we now know that the output is
2285 // from [0-C].
2286 if ((KnownZero2 & MaskV) == MaskV) {
2287 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2288 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002289 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002290 }
2291 }
2292 }
2293 }
2294 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002295 case ISD::ADD:
2296 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002297 // Output known-0 bits are known if clear or set in both the low clear bits
2298 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2299 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002300 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002301 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2302
Jay Foada0653a32014-05-14 21:14:37 +00002303 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002304 KnownZeroOut = std::min(KnownZeroOut,
2305 KnownZero2.countTrailingOnes());
2306
Chris Lattner440b2802010-12-19 20:38:28 +00002307 if (Op.getOpcode() == ISD::ADD) {
2308 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002309 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002310 }
2311
2312 // With ADDE, a carry bit may be added in, so we can only use this
2313 // information if we know (at least) that the low two bits are clear. We
2314 // then return to the caller that the low bit is unknown but that other bits
2315 // are known zero.
2316 if (KnownZeroOut >= 2) // ADDE
2317 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002318 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002319 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002320 case ISD::SREM:
2321 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002322 const APInt &RA = Rem->getAPIntValue().abs();
2323 if (RA.isPowerOf2()) {
2324 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002325 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002326
Duncan Sands33274982010-01-29 09:45:26 +00002327 // The low bits of the first operand are unchanged by the srem.
2328 KnownZero = KnownZero2 & LowBits;
2329 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002330
Duncan Sands33274982010-01-29 09:45:26 +00002331 // If the first operand is non-negative or has all low bits zero, then
2332 // the upper bits are all zero.
2333 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2334 KnownZero |= ~LowBits;
2335
2336 // If the first operand is negative and not all low bits are zero, then
2337 // the upper bits are all one.
2338 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2339 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002340 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002341 }
2342 }
Jay Foad5a29c362014-05-15 12:12:55 +00002343 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002344 case ISD::UREM: {
2345 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002346 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002347 if (RA.isPowerOf2()) {
2348 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002349 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2350
2351 // The upper bits are all zero, the lower ones are unchanged.
2352 KnownZero = KnownZero2 | ~LowBits;
2353 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002354 break;
2355 }
2356 }
2357
2358 // Since the result is less than or equal to either operand, any leading
2359 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002360 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2361 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002362
2363 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2364 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002365 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002366 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002367 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002368 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002369 case ISD::EXTRACT_ELEMENT: {
2370 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2371 const unsigned Index =
2372 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2373 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2374
2375 // Remove low part of known bits mask
2376 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2377 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2378
2379 // Remove high part of known bit mask
2380 KnownZero = KnownZero.trunc(BitWidth);
2381 KnownOne = KnownOne.trunc(BitWidth);
2382 break;
2383 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002384 case ISD::FrameIndex:
2385 case ISD::TargetFrameIndex:
2386 if (unsigned Align = InferPtrAlignment(Op)) {
2387 // The low bits are known zero if the pointer is aligned.
2388 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002389 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002390 }
2391 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002392
Dan Gohman309d3d52007-06-22 14:59:07 +00002393 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002394 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2395 break;
2396 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002397 case ISD::INTRINSIC_WO_CHAIN:
2398 case ISD::INTRINSIC_W_CHAIN:
2399 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002400 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002401 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002402 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002403 }
Jay Foad5a29c362014-05-15 12:12:55 +00002404
2405 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002406}
2407
2408/// ComputeNumSignBits - Return the number of times the sign bit of the
2409/// register is replicated into the other bits. We know that at least 1 bit
2410/// is always equal to the sign bit (itself), but other cases can give us
2411/// information. For example, immediately after an "SRA X, 2", we know that
2412/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002413unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002414 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002415 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002416 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002417 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002418 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002419
Dan Gohman309d3d52007-06-22 14:59:07 +00002420 if (Depth == 6)
2421 return 1; // Limit search depth.
2422
2423 switch (Op.getOpcode()) {
2424 default: break;
2425 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002426 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002427 return VTBits-Tmp+1;
2428 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002429 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002430 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002431
Dan Gohman309d3d52007-06-22 14:59:07 +00002432 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002433 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002434 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002435 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002436
Dan Gohman309d3d52007-06-22 14:59:07 +00002437 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002438 Tmp =
2439 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002440 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002441
Dan Gohman309d3d52007-06-22 14:59:07 +00002442 case ISD::SIGN_EXTEND_INREG:
2443 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002444 Tmp =
2445 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002447
Dan Gohman309d3d52007-06-22 14:59:07 +00002448 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2449 return std::max(Tmp, Tmp2);
2450
2451 case ISD::SRA:
2452 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2453 // SRA X, C -> adds C sign bits.
2454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002455 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002456 if (Tmp > VTBits) Tmp = VTBits;
2457 }
2458 return Tmp;
2459 case ISD::SHL:
2460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2461 // shl destroys sign bits.
2462 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002463 if (C->getZExtValue() >= VTBits || // Bad shift.
2464 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2465 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002466 }
2467 break;
2468 case ISD::AND:
2469 case ISD::OR:
2470 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002471 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002472 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002473 if (Tmp != 1) {
2474 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2475 FirstAnswer = std::min(Tmp, Tmp2);
2476 // We computed what we know about the sign bits as our first
2477 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002478 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002479 }
2480 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002481
2482 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002483 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002484 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002485 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002486 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002487
2488 case ISD::SADDO:
2489 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002490 case ISD::SSUBO:
2491 case ISD::USUBO:
2492 case ISD::SMULO:
2493 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002494 if (Op.getResNo() != 1)
2495 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002496 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002497 // If setcc returns 0/-1, all bits are sign bits.
2498 // We know that we have an integer-based boolean since these operations
2499 // are only available for integer.
2500 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2501 TargetLowering::ZeroOrNegativeOneBooleanContent)
2502 return VTBits;
2503 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002504 case ISD::SETCC:
2505 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002506 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002507 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002508 return VTBits;
2509 break;
2510 case ISD::ROTL:
2511 case ISD::ROTR:
2512 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002513 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002514
Dan Gohman309d3d52007-06-22 14:59:07 +00002515 // Handle rotate right by N like a rotate left by 32-N.
2516 if (Op.getOpcode() == ISD::ROTR)
2517 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2518
2519 // If we aren't rotating out all of the known-in sign bits, return the
2520 // number that are left. This handles rotl(sext(x), 1) for example.
2521 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2522 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2523 }
2524 break;
2525 case ISD::ADD:
2526 // Add can have at most one carry bit. Thus we know that the output
2527 // is, at worst, one more bit than the inputs.
2528 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2529 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002530
Dan Gohman309d3d52007-06-22 14:59:07 +00002531 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002532 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002533 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002534 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002535 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002536
Dan Gohman309d3d52007-06-22 14:59:07 +00002537 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2538 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002539 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002540 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002541
Dan Gohman309d3d52007-06-22 14:59:07 +00002542 // If we are subtracting one from a positive number, there is no carry
2543 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002544 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002545 return Tmp;
2546 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002547
Dan Gohman309d3d52007-06-22 14:59:07 +00002548 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2549 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002550 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002551
Dan Gohman309d3d52007-06-22 14:59:07 +00002552 case ISD::SUB:
2553 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2554 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002555
Dan Gohman309d3d52007-06-22 14:59:07 +00002556 // Handle NEG.
2557 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002558 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002559 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002560 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002561 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2562 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002563 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002564 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002565
Dan Gohman309d3d52007-06-22 14:59:07 +00002566 // If the input is known to be positive (the sign bit is known clear),
2567 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002568 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002569 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002570
Dan Gohman309d3d52007-06-22 14:59:07 +00002571 // Otherwise, we treat this like a SUB.
2572 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002573
Dan Gohman309d3d52007-06-22 14:59:07 +00002574 // Sub can have at most one carry bit. Thus we know that the output
2575 // is, at worst, one more bit than the inputs.
2576 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2577 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002578 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002579 case ISD::TRUNCATE:
2580 // FIXME: it's tricky to do anything useful for this, but it is an important
2581 // case for targets like X86.
2582 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002583 case ISD::EXTRACT_ELEMENT: {
2584 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2585 const int BitWidth = Op.getValueType().getSizeInBits();
2586 const int Items =
2587 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2588
2589 // Get reverse index (starting from 1), Op1 value indexes elements from
2590 // little end. Sign starts at big end.
2591 const int rIndex = Items - 1 -
2592 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2593
2594 // If the sign portion ends in our element the substraction gives correct
2595 // result. Otherwise it gives either negative or > bitwidth result
2596 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2597 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002598 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002599
Nadav Rotem4536d582013-03-20 22:53:44 +00002600 // If we are looking at the loaded value of the SDNode.
2601 if (Op.getResNo() == 0) {
2602 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2603 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2604 unsigned ExtType = LD->getExtensionType();
2605 switch (ExtType) {
2606 default: break;
2607 case ISD::SEXTLOAD: // '17' bits known
2608 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2609 return VTBits-Tmp+1;
2610 case ISD::ZEXTLOAD: // '16' bits known
2611 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2612 return VTBits-Tmp;
2613 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002614 }
2615 }
2616
2617 // Allow the target to implement this method for its nodes.
2618 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002619 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002620 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2621 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002622 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002623 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002624 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002625
Dan Gohman309d3d52007-06-22 14:59:07 +00002626 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2627 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002628 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002629 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002630
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002631 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002632 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002633 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002634 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002635 Mask = KnownOne;
2636 } else {
2637 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002638 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002639 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002640
Dan Gohman309d3d52007-06-22 14:59:07 +00002641 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2642 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002643 Mask = ~Mask;
2644 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002645 // Return # leading zeros. We use 'min' here in case Val was zero before
2646 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002647 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002648}
2649
Chris Lattner46c01a32011-02-13 22:25:43 +00002650/// isBaseWithConstantOffset - Return true if the specified operand is an
2651/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2652/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2653/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002654/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002655bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2656 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2657 !isa<ConstantSDNode>(Op.getOperand(1)))
2658 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002659
2660 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002661 !MaskedValueIsZero(Op.getOperand(0),
2662 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2663 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002664
Chris Lattner46c01a32011-02-13 22:25:43 +00002665 return true;
2666}
2667
2668
Dan Gohmand0d5e682009-09-03 20:34:31 +00002669bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2670 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002671 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002672 return true;
2673
2674 // If the value is a constant, we can obviously see if it is a NaN or not.
2675 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2676 return !C->getValueAPF().isNaN();
2677
2678 // TODO: Recognize more cases here.
2679
2680 return false;
2681}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002682
Dan Gohman38605212010-02-24 06:52:40 +00002683bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2684 // If the value is a constant, we can obviously see if it is a zero or not.
2685 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2686 return !C->isZero();
2687
2688 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002689 switch (Op.getOpcode()) {
2690 default: break;
2691 case ISD::OR:
2692 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2693 return !C->isNullValue();
2694 break;
2695 }
Dan Gohman38605212010-02-24 06:52:40 +00002696
2697 return false;
2698}
2699
2700bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2701 // Check the obvious case.
2702 if (A == B) return true;
2703
2704 // For for negative and positive zero.
2705 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2706 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2707 if (CA->isZero() && CB->isZero()) return true;
2708
2709 // Otherwise they may not be equal.
2710 return false;
2711}
2712
Chris Lattner061a1ea2005-01-07 07:46:32 +00002713/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002714///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002715SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002716 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002717 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002718 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002719 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002720 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002721
Jack Carter170a5f22013-09-09 22:02:08 +00002722 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2723 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002724 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002725
Chandler Carruth41b20e72014-07-22 04:07:55 +00002726 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002727 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002728}
2729
Andrew Trickef9de2a2013-05-25 02:42:55 +00002730SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002731 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002732 // Constant fold unary operations with an integer constant operand. Even
2733 // opaque constant will be folded, because the folding of unary operations
2734 // doesn't create new constants with different values. Nevertheless, the
2735 // opaque flag is preserved during folding to prevent future folding with
2736 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002737 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002738 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002739 switch (Opcode) {
2740 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002741 case ISD::SIGN_EXTEND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002742 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002743 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002744 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002745 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002746 case ISD::TRUNCATE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002747 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002748 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002749 case ISD::UINT_TO_FP:
2750 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002751 APFloat apf(EVTToAPFloatSemantics(VT),
2752 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002753 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002754 Opcode==ISD::SINT_TO_FP,
2755 APFloat::rmNearestTiesToEven);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002756 return getConstantFP(apf, DL, VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002757 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002758 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002759 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002760 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002761 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002762 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002763 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002764 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002765 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002766 case ISD::BSWAP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002767 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002768 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002769 case ISD::CTPOP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002770 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002771 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002772 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002773 case ISD::CTLZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002774 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002775 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002776 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002777 case ISD::CTTZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002778 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002779 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002780 }
2781 }
2782
Dale Johannesen446b9002007-08-31 23:34:27 +00002783 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002784 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002785 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002786 switch (Opcode) {
2787 case ISD::FNEG:
2788 V.changeSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002789 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002790 case ISD::FABS:
2791 V.clearSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002792 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002793 case ISD::FCEIL: {
2794 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2795 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002796 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002797 break;
2798 }
2799 case ISD::FTRUNC: {
2800 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2801 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002802 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002803 break;
2804 }
2805 case ISD::FFLOOR: {
2806 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2807 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002808 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002809 break;
2810 }
2811 case ISD::FP_EXTEND: {
2812 bool ignored;
2813 // This can return overflow, underflow, or inexact; we don't care.
2814 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002815 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002816 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002817 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002818 }
2819 case ISD::FP_TO_SINT:
2820 case ISD::FP_TO_UINT: {
2821 integerPart x[2];
2822 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002823 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002824 // FIXME need to be more flexible about rounding mode.
2825 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2826 Opcode==ISD::FP_TO_SINT,
2827 APFloat::rmTowardZero, &ignored);
2828 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002829 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002830 APInt api(VT.getSizeInBits(), x);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002831 return getConstant(api, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002832 }
2833 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002834 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002835 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Owen Anderson558012a2014-12-09 06:50:39 +00002836 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002837 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002838 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002839 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002840 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002841 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002842 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002843
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002844 // Constant fold unary operations with a vector integer or float operand.
Jim Grosbachf7502c42014-07-18 00:40:52 +00002845 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002846 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002847 switch (Opcode) {
2848 default:
2849 // FIXME: Entirely reasonable to perform folding of other unary
2850 // operations here as the need arises.
2851 break;
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002852 case ISD::FNEG:
2853 case ISD::FABS:
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002854 case ISD::FCEIL:
2855 case ISD::FTRUNC:
2856 case ISD::FFLOOR:
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002857 case ISD::FP_EXTEND:
Simon Pilgrim017ca192015-05-02 13:04:07 +00002858 case ISD::FP_TO_SINT:
2859 case ISD::FP_TO_UINT:
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002860 case ISD::TRUNCATE:
Jim Grosbachf7502c42014-07-18 00:40:52 +00002861 case ISD::UINT_TO_FP:
2862 case ISD::SINT_TO_FP: {
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002863 EVT SVT = VT.getScalarType();
2864 EVT InVT = BV->getValueType(0);
2865 EVT InSVT = InVT.getScalarType();
2866
2867 // Find legal integer scalar type for constant promotion.
2868 EVT LegalSVT = SVT;
2869 if (SVT.isInteger()) {
2870 LegalSVT = TLI->getTypeToTransformTo(*getContext(), SVT);
2871 assert(LegalSVT.bitsGE(SVT) && "Unexpected legal scalar type size");
2872 }
2873
Simon Pilgrim481f4142015-03-23 22:44:55 +00002874 // Let the above scalar folding handle the folding of each element.
Jim Grosbach19dd3082014-07-23 20:41:31 +00002875 SmallVector<SDValue, 8> Ops;
2876 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2877 SDValue OpN = BV->getOperand(i);
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002878 EVT OpVT = OpN.getValueType();
2879
2880 // Build vector (integer) scalar operands may need implicit
2881 // truncation - do this before constant folding.
2882 if (OpVT.isInteger() && OpVT.bitsGT(InSVT))
2883 OpN = getNode(ISD::TRUNCATE, DL, InSVT, OpN);
2884
2885 OpN = getNode(Opcode, DL, SVT, OpN);
2886
2887 // Legalize the (integer) scalar constant if necessary.
2888 if (LegalSVT != SVT)
2889 OpN = getNode(ISD::ANY_EXTEND, DL, LegalSVT, OpN);
2890
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002891 if (OpN.getOpcode() != ISD::UNDEF &&
2892 OpN.getOpcode() != ISD::Constant &&
2893 OpN.getOpcode() != ISD::ConstantFP)
2894 break;
Jim Grosbach19dd3082014-07-23 20:41:31 +00002895 Ops.push_back(OpN);
2896 }
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002897 if (Ops.size() == VT.getVectorNumElements())
2898 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002899 break;
Jim Grosbachf7502c42014-07-18 00:40:52 +00002900 }
2901 }
2902 }
2903 }
2904
Gabor Greiff304a7a2008-08-28 21:40:38 +00002905 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002906 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002907 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002908 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002909 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002910 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002911 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002912 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002913 assert(VT.isFloatingPoint() &&
2914 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002915 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002916 assert((!VT.isVector() ||
2917 VT.getVectorNumElements() ==
2918 Operand.getValueType().getVectorNumElements()) &&
2919 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002920 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002921 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002922 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002923 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002924 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002925 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002926 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002927 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2928 "Invalid sext node, dst < src!");
2929 assert((!VT.isVector() ||
2930 VT.getVectorNumElements() ==
2931 Operand.getValueType().getVectorNumElements()) &&
2932 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002933 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2934 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2935 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002936 // sext(undef) = 0, because the top bits will all be the same.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002937 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002938 break;
2939 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002940 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002941 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002942 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002943 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2944 "Invalid zext node, dst < src!");
2945 assert((!VT.isVector() ||
2946 VT.getVectorNumElements() ==
2947 Operand.getValueType().getVectorNumElements()) &&
2948 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002949 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002950 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002951 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002952 else if (OpOpcode == ISD::UNDEF)
2953 // zext(undef) = 0, because the top bits will be zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002954 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002955 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002956 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002957 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002958 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002959 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002960 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2961 "Invalid anyext node, dst < src!");
2962 assert((!VT.isVector() ||
2963 VT.getVectorNumElements() ==
2964 Operand.getValueType().getVectorNumElements()) &&
2965 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002966
Dan Gohman08837892010-06-18 00:08:30 +00002967 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2968 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002969 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002970 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002971 else if (OpOpcode == ISD::UNDEF)
2972 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002973
2974 // (ext (trunx x)) -> x
2975 if (OpOpcode == ISD::TRUNCATE) {
2976 SDValue OpOp = Operand.getNode()->getOperand(0);
2977 if (OpOp.getValueType() == VT)
2978 return OpOp;
2979 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002980 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002981 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002982 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002983 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002984 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002985 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2986 "Invalid truncate node, src < dst!");
2987 assert((!VT.isVector() ||
2988 VT.getVectorNumElements() ==
2989 Operand.getValueType().getVectorNumElements()) &&
2990 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002991 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002992 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002993 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2994 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002995 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002996 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2997 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002998 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002999 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00003000 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003001 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00003002 }
Craig Topper201c1a32012-01-15 01:05:11 +00003003 if (OpOpcode == ISD::UNDEF)
3004 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003005 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003006 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00003007 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00003008 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00003009 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00003010 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00003011 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
3012 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00003013 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003014 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003015 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003016 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003017 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00003018 (VT.getVectorElementType() == Operand.getValueType() ||
3019 (VT.getVectorElementType().isInteger() &&
3020 Operand.getValueType().isInteger() &&
3021 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003022 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00003023 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003024 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00003025 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3026 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3027 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3028 Operand.getConstantOperandVal(1) == 0 &&
3029 Operand.getOperand(0).getValueType() == VT)
3030 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003031 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00003032 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00003033 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003034 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00003035 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00003036 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003037 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00003038 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00003039 break;
3040 case ISD::FABS:
3041 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00003042 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003043 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003044 }
3045
Chris Lattnerf9c19152005-08-25 19:12:10 +00003046 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003047 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003048 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00003049 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003050 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00003051 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003052 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003053 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003054 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003055
Jack Carter170a5f22013-09-09 22:02:08 +00003056 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3057 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003058 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003059 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003060 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3061 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003062 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003063
Chandler Carruth41b20e72014-07-22 04:07:55 +00003064 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003065 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003066}
3067
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003068SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003069 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003070 // If the opcode is a target-specific ISD node, there's nothing we can
3071 // do here and the operand rules may not line up with the below, so
3072 // bail early.
3073 if (Opcode >= ISD::BUILTIN_OP_END)
3074 return SDValue();
3075
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003076 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3077 SmallVector<SDValue, 4> Outputs;
3078 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00003079
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003080 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3081 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003082 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3083 return SDValue();
3084
3085 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003086 // Scalar instruction.
3087 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003088 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003089 // For vectors extract each constant element into Inputs so we can constant
3090 // fold them individually.
3091 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3092 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3093 if (!BV1 || !BV2)
3094 return SDValue();
3095
3096 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3097
3098 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3099 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3100 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3101 if (!V1 || !V2) // Not a constant, bail.
3102 return SDValue();
3103
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003104 if (V1->isOpaque() || V2->isOpaque())
3105 return SDValue();
3106
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003107 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3108 // FIXME: This is valid and could be handled by truncating the APInts.
3109 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3110 return SDValue();
3111
3112 Inputs.push_back(std::make_pair(V1, V2));
3113 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003114 }
3115
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003116 // We have a number of constant values, constant fold them element by element.
3117 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3118 const APInt &C1 = Inputs[I].first->getAPIntValue();
3119 const APInt &C2 = Inputs[I].second->getAPIntValue();
3120
3121 switch (Opcode) {
3122 case ISD::ADD:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003123 Outputs.push_back(getConstant(C1 + C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003124 break;
3125 case ISD::SUB:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003126 Outputs.push_back(getConstant(C1 - C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003127 break;
3128 case ISD::MUL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003129 Outputs.push_back(getConstant(C1 * C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003130 break;
3131 case ISD::UDIV:
3132 if (!C2.getBoolValue())
3133 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003134 Outputs.push_back(getConstant(C1.udiv(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003135 break;
3136 case ISD::UREM:
3137 if (!C2.getBoolValue())
3138 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003139 Outputs.push_back(getConstant(C1.urem(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003140 break;
3141 case ISD::SDIV:
3142 if (!C2.getBoolValue())
3143 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003144 Outputs.push_back(getConstant(C1.sdiv(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003145 break;
3146 case ISD::SREM:
3147 if (!C2.getBoolValue())
3148 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003149 Outputs.push_back(getConstant(C1.srem(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003150 break;
3151 case ISD::AND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003152 Outputs.push_back(getConstant(C1 & C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003153 break;
3154 case ISD::OR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003155 Outputs.push_back(getConstant(C1 | C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003156 break;
3157 case ISD::XOR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003158 Outputs.push_back(getConstant(C1 ^ C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003159 break;
3160 case ISD::SHL:
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::SRL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003164 Outputs.push_back(getConstant(C1.lshr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003165 break;
3166 case ISD::SRA:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003167 Outputs.push_back(getConstant(C1.ashr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003168 break;
3169 case ISD::ROTL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003170 Outputs.push_back(getConstant(C1.rotl(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003171 break;
3172 case ISD::ROTR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003173 Outputs.push_back(getConstant(C1.rotr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003174 break;
3175 default:
3176 return SDValue();
3177 }
3178 }
3179
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003180 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3181 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003182
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003183 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003184 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003185 return Outputs.back();
3186
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003187 // We may have a vector type but a scalar result. Create a splat.
3188 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3189
3190 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003191 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003192}
3193
Andrew Trickef9de2a2013-05-25 02:42:55 +00003194SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Sanjay Patel801caff2015-05-05 21:40:38 +00003195 SDValue N2, const SDNodeFlags *Flags) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003196 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3197 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003198 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003199 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003200 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003201 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3202 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003203 // Fold trivial token factors.
3204 if (N1.getOpcode() == ISD::EntryToken) return N2;
3205 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003206 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003207 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003208 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003209 // Concat of UNDEFs is UNDEF.
3210 if (N1.getOpcode() == ISD::UNDEF &&
3211 N2.getOpcode() == ISD::UNDEF)
3212 return getUNDEF(VT);
3213
Dan Gohman550c9af2008-08-14 20:04:46 +00003214 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3215 // one big BUILD_VECTOR.
3216 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3217 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003218 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3219 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003220 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Simon Pilgrim860f0872015-04-21 08:05:43 +00003221
3222 // BUILD_VECTOR requires all inputs to be of the same type, find the
3223 // maximum type and extend them all.
3224 EVT SVT = VT.getScalarType();
3225 for (SDValue Op : Elts)
3226 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
3227 if (SVT.bitsGT(VT.getScalarType()))
3228 for (SDValue &Op : Elts)
3229 Op = TLI->isZExtFree(Op.getValueType(), SVT)
3230 ? getZExtOrTrunc(Op, DL, SVT)
3231 : getSExtOrTrunc(Op, DL, SVT);
3232
Craig Topper48d114b2014-04-26 18:35:24 +00003233 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003234 }
3235 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003236 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003237 assert(VT.isInteger() && "This operator does not apply to FP types!");
3238 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003239 N1.getValueType() == VT && "Binary operator types must match!");
3240 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3241 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003242 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003243 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003244 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3245 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003246 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003247 case ISD::OR:
3248 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003249 case ISD::ADD:
3250 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003251 assert(VT.isInteger() && "This operator does not apply to FP types!");
3252 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003253 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003254 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3255 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003256 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003257 return N1;
3258 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003259 case ISD::UDIV:
3260 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003261 case ISD::MULHU:
3262 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003263 case ISD::MUL:
3264 case ISD::SDIV:
3265 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003266 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003267 assert(N1.getValueType() == N2.getValueType() &&
3268 N1.getValueType() == VT && "Binary operator types must match!");
3269 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003270 case ISD::FADD:
3271 case ISD::FSUB:
3272 case ISD::FMUL:
3273 case ISD::FDIV:
3274 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003275 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003276 if (Opcode == ISD::FADD) {
3277 // 0+x --> x
3278 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3279 if (CFP->getValueAPF().isZero())
3280 return N2;
3281 // x+0 --> x
3282 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3283 if (CFP->getValueAPF().isZero())
3284 return N1;
3285 } else if (Opcode == ISD::FSUB) {
3286 // x-0 --> x
3287 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3288 if (CFP->getValueAPF().isZero())
3289 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003290 } else if (Opcode == ISD::FMUL) {
3291 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3292 SDValue V = N2;
3293
3294 // If the first operand isn't the constant, try the second
3295 if (!CFP) {
3296 CFP = dyn_cast<ConstantFPSDNode>(N2);
3297 V = N1;
3298 }
3299
3300 if (CFP) {
3301 // 0*x --> 0
3302 if (CFP->isZero())
3303 return SDValue(CFP,0);
3304 // 1*x --> x
3305 if (CFP->isExactlyValue(1.0))
3306 return V;
3307 }
Dan Gohman1275e282009-01-23 19:10:37 +00003308 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003309 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003310 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003311 assert(N1.getValueType() == N2.getValueType() &&
3312 N1.getValueType() == VT && "Binary operator types must match!");
3313 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003314 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3315 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003316 N1.getValueType().isFloatingPoint() &&
3317 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003318 "Invalid FCOPYSIGN!");
3319 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003320 case ISD::SHL:
3321 case ISD::SRA:
3322 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003323 case ISD::ROTL:
3324 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003325 assert(VT == N1.getValueType() &&
3326 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003327 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003328 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003329 assert((!VT.isVector() || VT == N2.getValueType()) &&
3330 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003331 // Verify that the shift amount VT is bit enough to hold valid shift
3332 // amounts. This catches things like trying to shift an i1024 value by an
3333 // i8, which is easy to fall into in generic code that uses
3334 // TLI.getShiftAmount().
3335 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003336 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003337 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003338
3339 // Always fold shifts of i1 values so the code generator doesn't need to
3340 // handle them. Since we know the size of the shift has to be less than the
3341 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003342 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003343 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003344 if (N2C && N2C->isNullValue())
3345 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003346 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003347 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003348 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003349 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003350 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003351 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003352 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003353 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003354 "type is vector!");
3355 assert((!EVT.isVector() ||
3356 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3357 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003358 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003359 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003360 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003361 break;
3362 }
Chris Lattner72733e52008-01-17 07:00:52 +00003363 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003364 assert(VT.isFloatingPoint() &&
3365 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003366 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003367 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003368 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003369 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003370 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003371 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003372 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003373 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003374 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003375 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003376 assert(!EVT.isVector() &&
3377 "AssertSExt/AssertZExt type should be the vector element type "
3378 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003379 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003380 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003381 break;
3382 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003383 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003384 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003385 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003386 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003387 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003388 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003389 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003390 "type is vector!");
3391 assert((!EVT.isVector() ||
3392 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3393 "Vector element counts must match in SIGN_EXTEND_INREG");
3394 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003395 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003396
Chris Lattner16713612008-01-22 19:09:33 +00003397 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003398 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003399 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003400 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003401 Val = Val.ashr(Val.getBitWidth()-FromBits);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003402 return getConstant(Val, DL, VT);
Chris Lattner751817c2006-05-06 23:05:41 +00003403 }
Chris Lattner16713612008-01-22 19:09:33 +00003404 break;
3405 }
3406 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003407 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3408 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003409 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003410
Chris Lattner16713612008-01-22 19:09:33 +00003411 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3412 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003413 if (N2C &&
3414 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003415 N1.getNumOperands() > 0) {
3416 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003417 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003418 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003419 N1.getOperand(N2C->getZExtValue() / Factor),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003420 getConstant(N2C->getZExtValue() % Factor, DL,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003421 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003422 }
3423
3424 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3425 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003426 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3427 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003428
3429 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003430 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003431 // are promoted and implicitly truncated, and the result implicitly
3432 // extended. Make that explicit here.
3433 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003434
Bob Wilson59dbbb22009-04-13 22:05:19 +00003435 return Elt;
3436 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003437
Chris Lattner16713612008-01-22 19:09:33 +00003438 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3439 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003440 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003441 // If the indices are the same, return the inserted element else
3442 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003443 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003444 SDValue N1Op2 = N1.getOperand(2);
3445 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3446
3447 if (N1Op2C && N2C) {
3448 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3449 if (VT == N1.getOperand(1).getValueType())
3450 return N1.getOperand(1);
3451 else
3452 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3453 }
3454
Dale Johannesendb393622009-02-03 01:55:44 +00003455 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003456 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003457 }
Chris Lattner16713612008-01-22 19:09:33 +00003458 break;
3459 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003460 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003461 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3462 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003463 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003464 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003465
Chris Lattner16713612008-01-22 19:09:33 +00003466 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3467 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003468 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003469 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003470 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003471
Chris Lattner16713612008-01-22 19:09:33 +00003472 // EXTRACT_ELEMENT of a constant int is also very common.
3473 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003474 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003475 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003476 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003477 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
Chris Lattner16713612008-01-22 19:09:33 +00003478 }
3479 break;
David Greeneb6f16112011-01-26 15:38:49 +00003480 case ISD::EXTRACT_SUBVECTOR: {
3481 SDValue Index = N2;
3482 if (VT.isSimple() && N1.getValueType().isSimple()) {
3483 assert(VT.isVector() && N1.getValueType().isVector() &&
3484 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003485 assert(VT.getVectorElementType() ==
3486 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003487 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003488 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003489 "Extract subvector must be from larger vector to smaller vector!");
3490
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003491 if (isa<ConstantSDNode>(Index.getNode())) {
3492 assert((VT.getVectorNumElements() +
3493 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003494 <= N1.getValueType().getVectorNumElements())
3495 && "Extract subvector overflow!");
3496 }
3497
3498 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003499 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003500 return N1;
3501 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003502 break;
Chris Lattner16713612008-01-22 19:09:33 +00003503 }
David Greeneb6f16112011-01-26 15:38:49 +00003504 }
Chris Lattner16713612008-01-22 19:09:33 +00003505
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003506 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003507 if (SDValue SV =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003508 FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
Matthias Braunf50ab432015-01-13 22:17:46 +00003509 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003510
3511 // Canonicalize constant to RHS if commutative.
3512 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3513 std::swap(N1C, N2C);
3514 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003515 }
3516
Chris Lattner16713612008-01-22 19:09:33 +00003517 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003518 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003519 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3520 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003521 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003522 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003523 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003524 std::swap(N1CFP, N2CFP);
3525 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003526 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003527 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3528 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003529 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003530 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003531 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003532 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003533 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003534 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003535 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003536 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003537 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003538 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003539 break;
3540 case ISD::FMUL:
3541 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003542 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003543 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003544 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003545 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003546 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003547 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3548 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003549 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003550 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003551 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003552 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003553 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003554 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3555 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003556 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003557 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003558 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003559 case ISD::FCOPYSIGN:
3560 V1.copySign(V2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003561 return getConstantFP(V1, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003562 default: break;
3563 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003564 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003565
3566 if (Opcode == ISD::FP_ROUND) {
3567 APFloat V = N1CFP->getValueAPF(); // make copy
3568 bool ignored;
3569 // This can return overflow, underflow, or inexact; we don't care.
3570 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003571 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003572 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003573 return getConstantFP(V, DL, VT);
Owen Anderson6f1ee162012-04-10 22:46:53 +00003574 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003575 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003576
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003577 // Canonicalize an UNDEF to the RHS, even over a constant.
3578 if (N1.getOpcode() == ISD::UNDEF) {
3579 if (isCommutativeBinOp(Opcode)) {
3580 std::swap(N1, N2);
3581 } else {
3582 switch (Opcode) {
3583 case ISD::FP_ROUND_INREG:
3584 case ISD::SIGN_EXTEND_INREG:
3585 case ISD::SUB:
3586 case ISD::FSUB:
3587 case ISD::FDIV:
3588 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003589 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003590 return N1; // fold op(undef, arg2) -> undef
3591 case ISD::UDIV:
3592 case ISD::SDIV:
3593 case ISD::UREM:
3594 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003595 case ISD::SRL:
3596 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003597 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003598 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003599 // For vectors, we can't easily build an all zero vector, just return
3600 // the LHS.
3601 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003602 }
3603 }
3604 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003605
3606 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003607 if (N2.getOpcode() == ISD::UNDEF) {
3608 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003609 case ISD::XOR:
3610 if (N1.getOpcode() == ISD::UNDEF)
3611 // Handle undef ^ undef -> 0 special case. This is a common
3612 // idiom (misuse).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003613 return getConstant(0, DL, VT);
Evan Chengdf1690d2008-03-25 20:08:07 +00003614 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003615 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003616 case ISD::ADDC:
3617 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003618 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003619 case ISD::UDIV:
3620 case ISD::SDIV:
3621 case ISD::UREM:
3622 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003623 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003624 case ISD::FADD:
3625 case ISD::FSUB:
3626 case ISD::FMUL:
3627 case ISD::FDIV:
3628 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003629 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003630 return N2;
3631 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003632 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003633 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003634 case ISD::SRL:
3635 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003636 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003637 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003638 // For vectors, we can't easily build an all zero vector, just return
3639 // the LHS.
3640 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003641 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003642 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003643 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003644 // For vectors, we can't easily build an all one vector, just return
3645 // the LHS.
3646 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003647 case ISD::SRA:
3648 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003649 }
3650 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003651
Chris Lattner724f7ee2005-05-11 18:57:39 +00003652 // Memoize this node if possible.
Sanjay Patel801caff2015-05-05 21:40:38 +00003653 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003654 SDVTList VTs = getVTList(VT);
Sanjay Patel801caff2015-05-05 21:40:38 +00003655 SDValue Ops[] = { N1, N2 };
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003656 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003657 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003658 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003659 AddNodeIDNode(ID, Opcode, VTs, Ops);
Sanjay Patel801caff2015-05-05 21:40:38 +00003660 AddNodeIDFlags(ID, Opcode, Flags);
Craig Topperc0196b12014-04-14 00:51:57 +00003661 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003662 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003663 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003664
Sanjay Patel801caff2015-05-05 21:40:38 +00003665 N = GetSDNodeWithFlags(Opcode, DL, VTs, Ops, Flags);
3666
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003667 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003668 } else {
Sanjay Patel801caff2015-05-05 21:40:38 +00003669 N = GetSDNodeWithFlags(Opcode, DL, VTs, Ops, Flags);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003670 }
3671
Chandler Carruth41b20e72014-07-22 04:07:55 +00003672 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003673 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003674}
3675
Andrew Trickef9de2a2013-05-25 02:42:55 +00003676SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003677 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003678 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003679 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003680 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003681 case ISD::FMA: {
3682 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3683 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3684 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3685 if (N1CFP && N2CFP && N3CFP) {
3686 APFloat V1 = N1CFP->getValueAPF();
3687 const APFloat &V2 = N2CFP->getValueAPF();
3688 const APFloat &V3 = N3CFP->getValueAPF();
3689 APFloat::opStatus s =
3690 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003691 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003692 return getConstantFP(V1, DL, VT);
Owen Anderson32baf992013-05-09 22:27:13 +00003693 }
3694 break;
3695 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003696 case ISD::CONCAT_VECTORS:
3697 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3698 // one big BUILD_VECTOR.
3699 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3700 N2.getOpcode() == ISD::BUILD_VECTOR &&
3701 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003702 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3703 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003704 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3705 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003706 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003707 }
3708 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003709 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003710 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003711 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003712 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003713 break;
3714 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003715 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003716 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003717 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003718 return N2; // select true, X, Y -> X
3719 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003720 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003721
3722 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003723 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003724 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003725 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003726 case ISD::INSERT_SUBVECTOR: {
3727 SDValue Index = N3;
3728 if (VT.isSimple() && N1.getValueType().isSimple()
3729 && N2.getValueType().isSimple()) {
3730 assert(VT.isVector() && N1.getValueType().isVector() &&
3731 N2.getValueType().isVector() &&
3732 "Insert subvector VTs must be a vectors");
3733 assert(VT == N1.getValueType() &&
3734 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003735 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003736 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003737 if (isa<ConstantSDNode>(Index.getNode())) {
3738 assert((N2.getValueType().getVectorNumElements() +
3739 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003740 <= VT.getVectorNumElements())
3741 && "Insert subvector overflow!");
3742 }
3743
3744 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003745 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003746 return N2;
3747 }
3748 break;
3749 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003750 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003751 // Fold bit_convert nodes from a type to themselves.
3752 if (N1.getValueType() == VT)
3753 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003754 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003755 }
3756
Chris Lattnerf9c19152005-08-25 19:12:10 +00003757 // Memoize node if it doesn't produce a flag.
3758 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003759 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003760 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003761 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003762 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003763 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003764 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003765 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003766 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003767
Jack Carter170a5f22013-09-09 22:02:08 +00003768 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3769 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003770 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003771 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003772 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3773 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003774 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003775
Chandler Carruth41b20e72014-07-22 04:07:55 +00003776 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003777 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003778}
3779
Andrew Trickef9de2a2013-05-25 02:42:55 +00003780SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003781 SDValue N1, SDValue N2, SDValue N3,
3782 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003783 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003784 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003785}
3786
Andrew Trickef9de2a2013-05-25 02:42:55 +00003787SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003788 SDValue N1, SDValue N2, SDValue N3,
3789 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003790 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003791 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003792}
3793
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003794/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3795/// the incoming stack arguments to be loaded from the stack.
3796SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3797 SmallVector<SDValue, 8> ArgChains;
3798
3799 // Include the original chain at the beginning of the list. When this is
3800 // used by target LowerCall hooks, this helps legalize find the
3801 // CALLSEQ_BEGIN node.
3802 ArgChains.push_back(Chain);
3803
3804 // Add a chain value for each stack argument.
3805 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3806 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3807 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3808 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3809 if (FI->getIndex() < 0)
3810 ArgChains.push_back(SDValue(L, 1));
3811
3812 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003813 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003814}
3815
Dan Gohman544ab2c2008-04-12 04:36:06 +00003816/// getMemsetValue - Vectorized representation of the memset value
3817/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003818static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003819 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003820 assert(Value.getOpcode() != ISD::UNDEF);
3821
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003822 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003823 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003824 assert(C->getAPIntValue().getBitWidth() == 8);
3825 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003826 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003827 return DAG.getConstant(Val, dl, VT);
3828 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
3829 VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003830 }
Evan Chengef377ad2008-05-15 08:39:06 +00003831
Hal Finkel17b6d772015-03-31 20:35:26 +00003832 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
3833 EVT IntVT = VT.getScalarType();
3834 if (!IntVT.isInteger())
3835 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
3836
3837 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003838 if (NumBits > 8) {
3839 // Use a multiplication with 0x010101... to extend the input to the
3840 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003841 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Hal Finkel17b6d772015-03-31 20:35:26 +00003842 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003843 DAG.getConstant(Magic, dl, IntVT));
Hal Finkel17b6d772015-03-31 20:35:26 +00003844 }
3845
3846 if (VT != Value.getValueType() && !VT.isInteger())
3847 Value = DAG.getNode(ISD::BITCAST, dl, VT.getScalarType(), Value);
3848 if (VT != Value.getValueType()) {
3849 assert(VT.getVectorElementType() == Value.getValueType() &&
3850 "value type should be one vector element here");
3851 SmallVector<SDValue, 8> BVOps(VT.getVectorNumElements(), Value);
3852 Value = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, BVOps);
Evan Chengef377ad2008-05-15 08:39:06 +00003853 }
3854
3855 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003856}
3857
Dan Gohman544ab2c2008-04-12 04:36:06 +00003858/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3859/// used when a memcpy is turned into a memset when the source is a constant
3860/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003861static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003862 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003863 // Handle vector with all elements zero.
3864 if (Str.empty()) {
3865 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003866 return DAG.getConstant(0, dl, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003867 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003868 return DAG.getConstantFP(0.0, dl, VT);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003869 else if (VT.isVector()) {
3870 unsigned NumElts = VT.getVectorNumElements();
3871 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003872 return DAG.getNode(ISD::BITCAST, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003873 DAG.getConstant(0, dl,
3874 EVT::getVectorVT(*DAG.getContext(),
3875 EltVT, NumElts)));
Evan Cheng43cd9e32010-04-01 06:04:33 +00003876 } else
3877 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003878 }
3879
Duncan Sands13237ac2008-06-06 12:08:01 +00003880 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003881 unsigned NumVTBits = VT.getSizeInBits();
3882 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003883 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3884
Evan Chengc8444b12013-01-10 22:13:27 +00003885 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003886 if (TLI.isLittleEndian()) {
3887 for (unsigned i = 0; i != NumBytes; ++i)
3888 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3889 } else {
3890 for (unsigned i = 0; i != NumBytes; ++i)
3891 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003892 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003893
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003894 // If the "cost" of materializing the integer immediate is less than the cost
3895 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003896 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3897 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003898 return DAG.getConstant(Val, dl, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003899 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003900}
3901
Scott Michelcf0da6c2009-02-17 22:15:04 +00003902/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003903///
Andrew Tricke2431c62013-05-25 03:08:10 +00003904static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003905 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003906 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003907 return DAG.getNode(ISD::ADD, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003908 VT, Base, DAG.getConstant(Offset, dl, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003909}
3910
Evan Chengef377ad2008-05-15 08:39:06 +00003911/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3912///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003913static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003914 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003915 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003916 if (Src.getOpcode() == ISD::GlobalAddress)
3917 G = cast<GlobalAddressSDNode>(Src);
3918 else if (Src.getOpcode() == ISD::ADD &&
3919 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3920 Src.getOperand(1).getOpcode() == ISD::Constant) {
3921 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003922 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003923 }
3924 if (!G)
3925 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003926
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003927 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003928}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003929
Evan Cheng43cd9e32010-04-01 06:04:33 +00003930/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3931/// to replace the memset / memcpy. Return true if the number of memory ops
3932/// is below the threshold. It returns the types of the sequence of
3933/// memory ops to perform memset / memcpy by reference.
3934static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003935 unsigned Limit, uint64_t Size,
3936 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003937 bool IsMemset,
3938 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003939 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003940 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003941 SelectionDAG &DAG,
3942 const TargetLowering &TLI) {
3943 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3944 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003945 // If 'SrcAlign' is zero, that means the memory operation does not need to
3946 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3947 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3948 // is the specified alignment of the memory operation. If it is zero, that
3949 // means it's possible to change the alignment of the destination.
3950 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3951 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003952 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003953 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003954 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003955
Chris Lattner28dc6c12010-03-07 07:45:08 +00003956 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003957 unsigned AS = 0;
3958 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003959 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003960 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003961 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003962 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003963 case 0: VT = MVT::i64; break;
3964 case 4: VT = MVT::i32; break;
3965 case 2: VT = MVT::i16; break;
3966 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003967 }
3968 }
3969
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003970 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003971 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003972 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003973 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003974
Duncan Sands11dd4242008-06-08 20:54:56 +00003975 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003976 VT = LVT;
3977 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003978
Dan Gohman544ab2c2008-04-12 04:36:06 +00003979 unsigned NumMemOps = 0;
3980 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003981 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003982 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003983 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003984 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003985 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003986
3987 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003988 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003989 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003990 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003991 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003992 Found = true;
3993 else if (NewVT == MVT::i64 &&
3994 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003995 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003996 // i64 is usually not legal on 32-bit targets, but f64 may be.
3997 NewVT = MVT::f64;
3998 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003999 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00004000 }
4001
Evan Cheng04e55182012-12-12 00:42:09 +00004002 if (!Found) {
4003 do {
4004 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
4005 if (NewVT == MVT::i8)
4006 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00004007 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00004008 }
4009 NewVTSize = NewVT.getSizeInBits() / 8;
4010
Evan Cheng79e2ca92012-12-10 23:21:26 +00004011 // If the new VT cannot cover all of the remaining bits, then consider
4012 // issuing a (or a pair of) unaligned and overlapping load / store.
4013 // FIXME: Only does this for 64-bit or more since we don't have proper
4014 // cost model for unaligned load / store.
4015 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00004016 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00004017 if (NumMemOps && AllowOverlap &&
4018 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00004019 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00004020 VTSize = Size;
4021 else {
4022 VT = NewVT;
4023 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00004024 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004025 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004026
Evan Chengb7d3d032012-12-12 20:43:23 +00004027 if (++NumMemOps > Limit)
4028 return false;
4029
Dan Gohman544ab2c2008-04-12 04:36:06 +00004030 MemOps.push_back(VT);
4031 Size -= VTSize;
4032 }
4033
4034 return true;
4035}
4036
Andrew Trickef9de2a2013-05-25 02:42:55 +00004037static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00004038 SDValue Chain, SDValue Dst,
4039 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004040 unsigned Align, bool isVol,
4041 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004042 MachinePointerInfo DstPtrInfo,
4043 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004044 // Turn a memcpy of undef to nop.
4045 if (Src.getOpcode() == ISD::UNDEF)
4046 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004047
Dan Gohman714663a2008-05-29 19:42:22 +00004048 // Expand memcpy to a series of load and store ops if the size operand falls
4049 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00004050 // TODO: In the AlwaysInline case, if the size is big then generate a loop
4051 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00004052 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004053 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004054 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004055 MachineFunction &MF = DAG.getMachineFunction();
4056 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004057 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004058 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4059 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4060 DstAlignCanChange = true;
4061 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4062 if (Align > SrcAlign)
4063 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004064 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004065 bool CopyFromStr = isMemSrcFromString(Src, Str);
4066 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004067 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00004068
Evan Cheng4c014c82010-04-01 18:19:11 +00004069 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004070 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00004071 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00004072 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004073 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004074
Evan Cheng43cd9e32010-04-01 06:04:33 +00004075 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004076 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004077 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00004078
4079 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00004080 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004081 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004082 if (!TRI->needsStackRealignment(MF))
4083 while (NewAlign > Align &&
4084 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4085 NewAlign /= 2;
4086
Evan Cheng43cd9e32010-04-01 06:04:33 +00004087 if (NewAlign > Align) {
4088 // Give the stack frame object a larger alignment if needed.
4089 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4090 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4091 Align = NewAlign;
4092 }
4093 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004094
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004095 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004096 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004097 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004098 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004099 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004100 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004101 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004102
Evan Cheng79e2ca92012-12-10 23:21:26 +00004103 if (VTSize > Size) {
4104 // Issuing an unaligned load / store pair that overlaps with the previous
4105 // pair. Adjust the offset accordingly.
4106 assert(i == NumMemOps-1 && i != 0);
4107 SrcOff -= VTSize - Size;
4108 DstOff -= VTSize - Size;
4109 }
4110
Evan Cheng43cd9e32010-04-01 06:04:33 +00004111 if (CopyFromStr &&
4112 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004113 // It's unlikely a store of a vector immediate can be done in a single
4114 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004115 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004116 // FIXME: Handle other cases where store of vector immediate is done in
4117 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004118 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004119 if (Value.getNode())
4120 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004121 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004122 DstPtrInfo.getWithOffset(DstOff), isVol,
4123 false, Align);
4124 }
4125
4126 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004127 // The type might not be legal for the target. This should only happen
4128 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004129 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4130 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004131 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004132 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004133 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004134 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004135 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004136 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004137 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004138 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004139 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004140 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4141 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004142 }
4143 OutChains.push_back(Store);
4144 SrcOff += VTSize;
4145 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004146 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004147 }
4148
Craig Topper48d114b2014-04-26 18:35:24 +00004149 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004150}
4151
Andrew Trickef9de2a2013-05-25 02:42:55 +00004152static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004153 SDValue Chain, SDValue Dst,
4154 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004155 unsigned Align, bool isVol,
4156 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004157 MachinePointerInfo DstPtrInfo,
4158 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004159 // Turn a memmove of undef to nop.
4160 if (Src.getOpcode() == ISD::UNDEF)
4161 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004162
4163 // Expand memmove to a series of load and store ops if the size operand falls
4164 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004165 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004166 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004167 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004168 MachineFunction &MF = DAG.getMachineFunction();
4169 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004170 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004171 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4172 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4173 DstAlignCanChange = true;
4174 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4175 if (Align > SrcAlign)
4176 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004177 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004178
Evan Cheng4c014c82010-04-01 18:19:11 +00004179 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004180 (DstAlignCanChange ? 0 : Align), SrcAlign,
4181 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004182 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004183
Evan Cheng43cd9e32010-04-01 06:04:33 +00004184 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004185 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004186 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004187 if (NewAlign > Align) {
4188 // Give the stack frame object a larger alignment if needed.
4189 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4190 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4191 Align = NewAlign;
4192 }
4193 }
Dan Gohman714663a2008-05-29 19:42:22 +00004194
Evan Cheng43cd9e32010-04-01 06:04:33 +00004195 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004196 SmallVector<SDValue, 8> LoadValues;
4197 SmallVector<SDValue, 8> LoadChains;
4198 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004199 unsigned NumMemOps = MemOps.size();
4200 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004201 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004202 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004203 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004204
Dale Johannesenabf66b82009-02-03 22:26:09 +00004205 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004206 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004207 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004208 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004209 LoadValues.push_back(Value);
4210 LoadChains.push_back(Value.getValue(1));
4211 SrcOff += VTSize;
4212 }
Craig Topper48d114b2014-04-26 18:35:24 +00004213 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004214 OutChains.clear();
4215 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004216 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004217 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004218 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004219
Dale Johannesenabf66b82009-02-03 22:26:09 +00004220 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004221 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004222 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004223 OutChains.push_back(Store);
4224 DstOff += VTSize;
4225 }
4226
Craig Topper48d114b2014-04-26 18:35:24 +00004227 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004228}
4229
Serge Pavlov8ec39992013-09-17 16:24:42 +00004230/// \brief Lower the call to 'memset' intrinsic function into a series of store
4231/// operations.
4232///
4233/// \param DAG Selection DAG where lowered code is placed.
4234/// \param dl Link to corresponding IR location.
4235/// \param Chain Control flow dependency.
4236/// \param Dst Pointer to destination memory location.
4237/// \param Src Value of byte to write into the memory.
4238/// \param Size Number of bytes to write.
4239/// \param Align Alignment of the destination in bytes.
4240/// \param isVol True if destination is volatile.
4241/// \param DstPtrInfo IR information on the memory pointer.
4242/// \returns New head in the control flow, if lowering was successful, empty
4243/// SDValue otherwise.
4244///
4245/// The function tries to replace 'llvm.memset' intrinsic with several store
4246/// operations and value calculation code. This is usually profitable for small
4247/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004248static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004249 SDValue Chain, SDValue Dst,
4250 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004251 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004252 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004253 // Turn a memset of undef to nop.
4254 if (Src.getOpcode() == ISD::UNDEF)
4255 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004256
4257 // Expand memset to a series of load/store ops if the size operand
4258 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004259 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004260 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004261 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004262 MachineFunction &MF = DAG.getMachineFunction();
4263 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004264 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004265 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4266 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4267 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004268 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004269 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004270 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004271 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004272 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004273 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004274
Evan Cheng43cd9e32010-04-01 06:04:33 +00004275 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004276 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004277 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004278 if (NewAlign > Align) {
4279 // Give the stack frame object a larger alignment if needed.
4280 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4281 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4282 Align = NewAlign;
4283 }
4284 }
4285
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004286 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004287 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004288 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004289
4290 // Find the largest store and generate the bit pattern for it.
4291 EVT LargestVT = MemOps[0];
4292 for (unsigned i = 1; i < NumMemOps; i++)
4293 if (MemOps[i].bitsGT(LargestVT))
4294 LargestVT = MemOps[i];
4295 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4296
Dan Gohman544ab2c2008-04-12 04:36:06 +00004297 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004298 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004299 unsigned VTSize = VT.getSizeInBits() / 8;
4300 if (VTSize > Size) {
4301 // Issuing an unaligned load / store pair that overlaps with the previous
4302 // pair. Adjust the offset accordingly.
4303 assert(i == NumMemOps-1 && i != 0);
4304 DstOff -= VTSize - Size;
4305 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004306
4307 // If this store is smaller than the largest store see whether we can get
4308 // the smaller value for free with a truncate.
4309 SDValue Value = MemSetValue;
4310 if (VT.bitsLT(LargestVT)) {
4311 if (!LargestVT.isVector() && !VT.isVector() &&
4312 TLI.isTruncateFree(LargestVT, VT))
4313 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4314 else
4315 Value = getMemsetValue(Src, VT, DAG, dl);
4316 }
4317 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004318 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004319 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004320 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004321 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004322 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004323 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004324 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004325 }
4326
Craig Topper48d114b2014-04-26 18:35:24 +00004327 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004328}
4329
Andrew Trickef9de2a2013-05-25 02:42:55 +00004330SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004331 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004332 unsigned Align, bool isVol, bool AlwaysInline,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004333 bool isTailCall, MachinePointerInfo DstPtrInfo,
Chris Lattner2510de22010-09-21 05:40:29 +00004334 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004335 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004336
4337 // Check to see if we should lower the memcpy to loads and stores first.
4338 // For cases within the target-specified limits, this is the best choice.
4339 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4340 if (ConstantSize) {
4341 // Memcpy with size zero? Just return the original chain.
4342 if (ConstantSize->isNullValue())
4343 return Chain;
4344
Evan Cheng43cd9e32010-04-01 06:04:33 +00004345 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4346 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004347 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004348 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004349 return Result;
4350 }
4351
4352 // Then check to see if we should lower the memcpy with target-specific
4353 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004354 if (TSI) {
4355 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4356 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4357 DstPtrInfo, SrcPtrInfo);
4358 if (Result.getNode())
4359 return Result;
4360 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004361
4362 // If we really need inline code and the target declined to provide it,
4363 // use a (potentially long) sequence of loads and stores.
4364 if (AlwaysInline) {
4365 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004366 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004367 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004368 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004369 }
4370
Dan Gohmanf38547c2010-04-05 20:24:08 +00004371 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4372 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4373 // respect volatile, so they may do things like read or write memory
4374 // beyond the given memory regions. But fixing this isn't easy, and most
4375 // people don't care.
4376
Dan Gohman544ab2c2008-04-12 04:36:06 +00004377 // Emit a library call.
4378 TargetLowering::ArgListTy Args;
4379 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004380 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004381 Entry.Node = Dst; Args.push_back(Entry);
4382 Entry.Node = Src; Args.push_back(Entry);
4383 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004384 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004385 TargetLowering::CallLoweringInfo CLI(*this);
4386 CLI.setDebugLoc(dl).setChain(Chain)
4387 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4388 Type::getVoidTy(*getContext()),
4389 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004390 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004391 .setDiscardResult()
4392 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004393
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004394 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004395 return CallResult.second;
4396}
4397
Andrew Trickef9de2a2013-05-25 02:42:55 +00004398SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004399 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004400 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004401 MachinePointerInfo DstPtrInfo,
4402 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004403 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004404
Dan Gohman714663a2008-05-29 19:42:22 +00004405 // Check to see if we should lower the memmove to loads and stores first.
4406 // For cases within the target-specified limits, this is the best choice.
4407 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4408 if (ConstantSize) {
4409 // Memmove with size zero? Just return the original chain.
4410 if (ConstantSize->isNullValue())
4411 return Chain;
4412
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004413 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004414 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004415 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004416 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004417 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004418 return Result;
4419 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004420
4421 // Then check to see if we should lower the memmove with target-specific
4422 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004423 if (TSI) {
4424 SDValue Result = TSI->EmitTargetCodeForMemmove(
4425 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4426 if (Result.getNode())
4427 return Result;
4428 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004429
Mon P Wangbf862242010-04-06 08:27:51 +00004430 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4431 // not be safe. See memcpy above for more details.
4432
Dan Gohman544ab2c2008-04-12 04:36:06 +00004433 // Emit a library call.
4434 TargetLowering::ArgListTy Args;
4435 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004436 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004437 Entry.Node = Dst; Args.push_back(Entry);
4438 Entry.Node = Src; Args.push_back(Entry);
4439 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004440 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004441 TargetLowering::CallLoweringInfo CLI(*this);
4442 CLI.setDebugLoc(dl).setChain(Chain)
4443 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4444 Type::getVoidTy(*getContext()),
4445 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004446 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004447 .setDiscardResult()
4448 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004449
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004450 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004451 return CallResult.second;
4452}
4453
Andrew Trickef9de2a2013-05-25 02:42:55 +00004454SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004455 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004456 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004457 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004458 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004459
4460 // Check to see if we should lower the memset to stores first.
4461 // For cases within the target-specified limits, this is the best choice.
4462 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4463 if (ConstantSize) {
4464 // Memset with size zero? Just return the original chain.
4465 if (ConstantSize->isNullValue())
4466 return Chain;
4467
Mon P Wangc576ee92010-04-04 03:10:48 +00004468 SDValue Result =
4469 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004470 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004471
Gabor Greiff304a7a2008-08-28 21:40:38 +00004472 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004473 return Result;
4474 }
4475
4476 // Then check to see if we should lower the memset with target-specific
4477 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004478 if (TSI) {
4479 SDValue Result = TSI->EmitTargetCodeForMemset(
4480 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4481 if (Result.getNode())
4482 return Result;
4483 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004484
Wesley Peck527da1b2010-11-23 03:31:01 +00004485 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004486 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004487 TargetLowering::ArgListTy Args;
4488 TargetLowering::ArgListEntry Entry;
4489 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4490 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004491 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004492 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004493 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004494 Entry.Node = Size;
4495 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004496 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004497
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004498 // FIXME: pass in SDLoc
4499 TargetLowering::CallLoweringInfo CLI(*this);
4500 CLI.setDebugLoc(dl).setChain(Chain)
4501 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4502 Type::getVoidTy(*getContext()),
4503 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004504 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004505 .setDiscardResult()
4506 .setTailCall(isTailCall);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004507
4508 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004509 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004510}
4511
Andrew Trickef9de2a2013-05-25 02:42:55 +00004512SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004513 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004514 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004515 AtomicOrdering SuccessOrdering,
4516 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004517 SynchronizationScope SynchScope) {
4518 FoldingSetNodeID ID;
4519 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004520 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004521 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004522 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004523 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4524 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4525 return SDValue(E, 0);
4526 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004527
4528 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4529 // SDNode doesn't have access to it. This memory will be "leaked" when
4530 // the node is deallocated, but recovered when the allocator is released.
4531 // If the number of operands is less than 5 we use AtomicSDNode's internal
4532 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004533 unsigned NumOps = Ops.size();
4534 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4535 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004536
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004537 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4538 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004539 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004540 SuccessOrdering, FailureOrdering,
4541 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004542 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004543 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004544 return SDValue(N, 0);
4545}
4546
4547SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004548 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004549 MachineMemOperand *MMO,
4550 AtomicOrdering Ordering,
4551 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004552 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004553 Ordering, SynchScope);
4554}
4555
Tim Northover420a2162014-06-13 14:24:07 +00004556SDValue SelectionDAG::getAtomicCmpSwap(
4557 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4558 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4559 unsigned Alignment, AtomicOrdering SuccessOrdering,
4560 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4561 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4562 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4563 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4564
Dan Gohman48b185d2009-09-25 20:36:54 +00004565 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4566 Alignment = getEVTAlignment(MemVT);
4567
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004568 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004569
Eli Friedmane978d2f2011-09-07 02:23:42 +00004570 // FIXME: Volatile isn't really correct; we should keep track of atomic
4571 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004572 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004573 Flags |= MachineMemOperand::MOLoad;
4574 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004575
4576 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004577 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004578
Tim Northover420a2162014-06-13 14:24:07 +00004579 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4580 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004581}
4582
Tim Northover420a2162014-06-13 14:24:07 +00004583SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4584 SDVTList VTs, SDValue Chain, SDValue Ptr,
4585 SDValue Cmp, SDValue Swp,
4586 MachineMemOperand *MMO,
4587 AtomicOrdering SuccessOrdering,
4588 AtomicOrdering FailureOrdering,
4589 SynchronizationScope SynchScope) {
4590 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4591 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004592 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4593
Dale Johannesen839acbb2009-01-29 00:47:48 +00004594 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004595 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4596 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004597}
4598
Andrew Trickef9de2a2013-05-25 02:42:55 +00004599SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004600 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004601 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004602 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004603 unsigned Alignment,
4604 AtomicOrdering Ordering,
4605 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004606 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4607 Alignment = getEVTAlignment(MemVT);
4608
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004609 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004610 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004611 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004612 // For now, atomics are considered to be volatile always, and they are
4613 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004614 // FIXME: Volatile isn't really correct; we should keep track of atomic
4615 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004616 unsigned Flags = MachineMemOperand::MOVolatile;
4617 if (Opcode != ISD::ATOMIC_STORE)
4618 Flags |= MachineMemOperand::MOLoad;
4619 if (Opcode != ISD::ATOMIC_LOAD)
4620 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004621
4622 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004623 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004624 MemVT.getStoreSize(), Alignment);
4625
Eli Friedmanadec5872011-07-29 03:05:32 +00004626 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4627 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004628}
4629
Andrew Trickef9de2a2013-05-25 02:42:55 +00004630SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004631 SDValue Chain,
4632 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004633 MachineMemOperand *MMO,
4634 AtomicOrdering Ordering,
4635 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004636 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4637 Opcode == ISD::ATOMIC_LOAD_SUB ||
4638 Opcode == ISD::ATOMIC_LOAD_AND ||
4639 Opcode == ISD::ATOMIC_LOAD_OR ||
4640 Opcode == ISD::ATOMIC_LOAD_XOR ||
4641 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004642 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004643 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004644 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004645 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004646 Opcode == ISD::ATOMIC_SWAP ||
4647 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004648 "Invalid Atomic Op");
4649
Owen Anderson53aa7a92009-08-10 22:56:29 +00004650 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004651
Eli Friedman342e8df2011-08-24 20:50:09 +00004652 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4653 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004654 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004655 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004656}
4657
Andrew Trickef9de2a2013-05-25 02:42:55 +00004658SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004659 EVT VT, SDValue Chain,
4660 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004661 MachineMemOperand *MMO,
4662 AtomicOrdering Ordering,
4663 SynchronizationScope SynchScope) {
4664 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4665
4666 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004667 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004668 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004669}
4670
Duncan Sands739a0542008-07-02 17:40:58 +00004671/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004672SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4673 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004674 return Ops[0];
4675
Owen Anderson53aa7a92009-08-10 22:56:29 +00004676 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004677 VTs.reserve(Ops.size());
4678 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004679 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004680 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004681}
4682
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004683SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004684SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004685 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004686 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004687 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004688 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004689 if (Align == 0) // Ensure that codegen never sees alignment 0
4690 Align = getEVTAlignment(MemVT);
4691
4692 MachineFunction &MF = getMachineFunction();
4693 unsigned Flags = 0;
4694 if (WriteMem)
4695 Flags |= MachineMemOperand::MOStore;
4696 if (ReadMem)
4697 Flags |= MachineMemOperand::MOLoad;
4698 if (Vol)
4699 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004700 if (!Size)
4701 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004702 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004703 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004704
Craig Topper206fcd42014-04-26 19:29:41 +00004705 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004706}
4707
4708SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004709SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004710 ArrayRef<SDValue> Ops, EVT MemVT,
4711 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004712 assert((Opcode == ISD::INTRINSIC_VOID ||
4713 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004714 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004715 Opcode == ISD::LIFETIME_START ||
4716 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004717 (Opcode <= INT_MAX &&
4718 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4719 "Opcode is not a memory-accessing opcode!");
4720
Dale Johannesen839acbb2009-01-29 00:47:48 +00004721 // Memoize the node unless it returns a flag.
4722 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004723 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004724 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004725 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004726 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004727 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004728 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4729 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004730 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004731 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004732
Jack Carter170a5f22013-09-09 22:02:08 +00004733 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4734 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004735 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004736 CSEMap.InsertNode(N, IP);
4737 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004738 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4739 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004740 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004741 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004742 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004743 return SDValue(N, 0);
4744}
4745
Chris Lattnerea952f02010-09-21 17:24:05 +00004746/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4747/// MachinePointerInfo record from it. This is particularly useful because the
4748/// code generator has many cases where it doesn't bother passing in a
4749/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4750static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4751 // If this is FI+Offset, we can model it.
4752 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4753 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4754
4755 // If this is (FI+Offset1)+Offset2, we can model it.
4756 if (Ptr.getOpcode() != ISD::ADD ||
4757 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4758 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4759 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004760
Chris Lattnerea952f02010-09-21 17:24:05 +00004761 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4762 return MachinePointerInfo::getFixedStack(FI, Offset+
4763 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4764}
4765
4766/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4767/// MachinePointerInfo record from it. This is particularly useful because the
4768/// code generator has many cases where it doesn't bother passing in a
4769/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4770static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4771 // If the 'Offset' value isn't a constant, we can't handle this.
4772 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4773 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4774 if (OffsetOp.getOpcode() == ISD::UNDEF)
4775 return InferPointerInfo(Ptr);
4776 return MachinePointerInfo();
4777}
Wesley Peck527da1b2010-11-23 03:31:01 +00004778
Chris Lattnerea952f02010-09-21 17:24:05 +00004779
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004780SDValue
4781SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004782 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004783 SDValue Ptr, SDValue Offset,
4784 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004785 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004786 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004787 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004788 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004789 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004790 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4791 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004792
Dan Gohman48b185d2009-09-25 20:36:54 +00004793 unsigned Flags = MachineMemOperand::MOLoad;
4794 if (isVolatile)
4795 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004796 if (isNonTemporal)
4797 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004798 if (isInvariant)
4799 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004800
Chris Lattnerea952f02010-09-21 17:24:05 +00004801 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4802 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004803 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004804 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004805
Chris Lattnerea952f02010-09-21 17:24:05 +00004806 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004807 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004808 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004809 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004810 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004811}
4812
4813SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004814SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004815 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004816 SDValue Ptr, SDValue Offset, EVT MemVT,
4817 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004818 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004819 ExtType = ISD::NON_EXTLOAD;
4820 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004821 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004822 } else {
4823 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004824 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4825 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004826 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004827 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004828 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004829 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004830 assert((!VT.isVector() ||
4831 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004832 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004833 }
4834
4835 bool Indexed = AM != ISD::UNINDEXED;
4836 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4837 "Unindexed load with an offset!");
4838
4839 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004840 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004841 SDValue Ops[] = { Chain, Ptr, Offset };
4842 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004843 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004844 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004845 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004846 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004847 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004848 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004849 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004850 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4851 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004852 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004853 }
Jack Carter170a5f22013-09-09 22:02:08 +00004854 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4855 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004856 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004857 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004858 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004859 return SDValue(N, 0);
4860}
4861
Andrew Trickef9de2a2013-05-25 02:42:55 +00004862SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004863 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004864 MachinePointerInfo PtrInfo,
4865 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004866 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004867 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004868 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004869 SDValue Undef = getUNDEF(Ptr.getValueType());
4870 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004871 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004872 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004873}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004874
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004875SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4876 SDValue Chain, SDValue Ptr,
4877 MachineMemOperand *MMO) {
4878 SDValue Undef = getUNDEF(Ptr.getValueType());
4879 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4880 VT, MMO);
4881}
4882
Andrew Trickef9de2a2013-05-25 02:42:55 +00004883SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004884 SDValue Chain, SDValue Ptr,
4885 MachinePointerInfo PtrInfo, EVT MemVT,
4886 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004887 bool isInvariant, unsigned Alignment,
4888 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004889 SDValue Undef = getUNDEF(Ptr.getValueType());
4890 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004891 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4892 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004893}
4894
4895
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004896SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4897 SDValue Chain, SDValue Ptr, EVT MemVT,
4898 MachineMemOperand *MMO) {
4899 SDValue Undef = getUNDEF(Ptr.getValueType());
4900 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4901 MemVT, MMO);
4902}
4903
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004904SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004905SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004906 SDValue Offset, ISD::MemIndexedMode AM) {
4907 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4908 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4909 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004910 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004911 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004912 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004913 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004914}
4915
Andrew Trickef9de2a2013-05-25 02:42:55 +00004916SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004917 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004918 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004919 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004920 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004921 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004922 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004923 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004924
Dan Gohman48b185d2009-09-25 20:36:54 +00004925 unsigned Flags = MachineMemOperand::MOStore;
4926 if (isVolatile)
4927 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004928 if (isNonTemporal)
4929 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004930
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004931 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004932 PtrInfo = InferPointerInfo(Ptr);
4933
4934 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004935 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004936 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004937 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004938 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004939
4940 return getStore(Chain, dl, Val, Ptr, MMO);
4941}
4942
Andrew Trickef9de2a2013-05-25 02:42:55 +00004943SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004944 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004945 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004946 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004947 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004948 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004949 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004950 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4951 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004952 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004953 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004954 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004955 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004956 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004957 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004958 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4959 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004960 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004961 }
Jack Carter170a5f22013-09-09 22:02:08 +00004962 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4963 dl.getDebugLoc(), VTs,
4964 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004965 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004966 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004967 return SDValue(N, 0);
4968}
4969
Andrew Trickef9de2a2013-05-25 02:42:55 +00004970SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004971 SDValue Ptr, MachinePointerInfo PtrInfo,
4972 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004973 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004974 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004975 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004976 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004977 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4978 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004979
Dan Gohman48b185d2009-09-25 20:36:54 +00004980 unsigned Flags = MachineMemOperand::MOStore;
4981 if (isVolatile)
4982 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004983 if (isNonTemporal)
4984 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004985
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004986 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004987 PtrInfo = InferPointerInfo(Ptr);
4988
4989 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004990 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004991 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004992 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004993
4994 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4995}
4996
Andrew Trickef9de2a2013-05-25 02:42:55 +00004997SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004998 SDValue Ptr, EVT SVT,
4999 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005000 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00005001
Michael Ilsemand5f91512012-09-10 16:56:31 +00005002 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005003 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005004 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00005005 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005006
Dan Gohmancecad352009-12-14 23:40:38 +00005007 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
5008 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005009 assert(VT.isInteger() == SVT.isInteger() &&
5010 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00005011 assert(VT.isVector() == SVT.isVector() &&
5012 "Cannot use trunc store to convert to or from a vector!");
5013 assert((!VT.isVector() ||
5014 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
5015 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005016
Owen Anderson9f944592009-08-11 20:47:22 +00005017 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00005018 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005019 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5020 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005021 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005022 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00005023 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005024 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00005025 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005026 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00005027 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5028 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005029 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005030 }
Jack Carter170a5f22013-09-09 22:02:08 +00005031 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5032 dl.getDebugLoc(), VTs,
5033 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005034 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005035 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005036 return SDValue(N, 0);
5037}
5038
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005039SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005040SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00005041 SDValue Offset, ISD::MemIndexedMode AM) {
5042 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5043 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
5044 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00005045 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005046 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5047 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005048 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005049 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00005050 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00005051 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005052 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005053 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00005054 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005055
Jack Carter170a5f22013-09-09 22:02:08 +00005056 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5057 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00005058 ST->isTruncatingStore(),
5059 ST->getMemoryVT(),
5060 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005061 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005062 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005063 return SDValue(N, 0);
5064}
5065
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005066SDValue
5067SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005068 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5069 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005070
5071 SDVTList VTs = getVTList(VT, MVT::Other);
5072 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5073 FoldingSetNodeID ID;
5074 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5075 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005076 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005077 MMO->isVolatile(),
5078 MMO->isNonTemporal(),
5079 MMO->isInvariant()));
5080 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5081 void *IP = nullptr;
5082 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5083 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5084 return SDValue(E, 0);
5085 }
5086 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5087 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005088 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005089 CSEMap.InsertNode(N, IP);
5090 InsertNode(N);
5091 return SDValue(N, 0);
5092}
5093
5094SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005095 SDValue Ptr, SDValue Mask, EVT MemVT,
5096 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005097 assert(Chain.getValueType() == MVT::Other &&
5098 "Invalid chain type");
5099 EVT VT = Val.getValueType();
5100 SDVTList VTs = getVTList(MVT::Other);
5101 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5102 FoldingSetNodeID ID;
5103 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5104 ID.AddInteger(VT.getRawBits());
5105 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5106 MMO->isNonTemporal(), MMO->isInvariant()));
5107 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5108 void *IP = nullptr;
5109 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5110 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5111 return SDValue(E, 0);
5112 }
5113 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5114 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005115 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005116 CSEMap.InsertNode(N, IP);
5117 InsertNode(N);
5118 return SDValue(N, 0);
5119}
5120
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005121SDValue
5122SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl,
5123 ArrayRef<SDValue> Ops,
5124 MachineMemOperand *MMO) {
5125
5126 FoldingSetNodeID ID;
5127 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5128 ID.AddInteger(VT.getRawBits());
5129 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5130 MMO->isVolatile(),
5131 MMO->isNonTemporal(),
5132 MMO->isInvariant()));
5133 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5134 void *IP = nullptr;
5135 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5136 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5137 return SDValue(E, 0);
5138 }
5139 MaskedGatherSDNode *N =
5140 new (NodeAllocator) MaskedGatherSDNode(dl.getIROrder(), dl.getDebugLoc(),
5141 Ops, VTs, VT, MMO);
5142 CSEMap.InsertNode(N, IP);
5143 InsertNode(N);
5144 return SDValue(N, 0);
5145}
5146
5147SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl,
5148 ArrayRef<SDValue> Ops,
5149 MachineMemOperand *MMO) {
5150 FoldingSetNodeID ID;
5151 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5152 ID.AddInteger(VT.getRawBits());
5153 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5154 MMO->isNonTemporal(),
5155 MMO->isInvariant()));
5156 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5157 void *IP = nullptr;
5158 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5159 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5160 return SDValue(E, 0);
5161 }
5162 SDNode *N =
5163 new (NodeAllocator) MaskedScatterSDNode(dl.getIROrder(), dl.getDebugLoc(),
5164 Ops, VTs, VT, MMO);
5165 CSEMap.InsertNode(N, IP);
5166 InsertNode(N);
5167 return SDValue(N, 0);
5168}
5169
Andrew Trickef9de2a2013-05-25 02:42:55 +00005170SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005171 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005172 SDValue SV,
5173 unsigned Align) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005174 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005175 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005176}
5177
Andrew Trickef9de2a2013-05-25 02:42:55 +00005178SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005179 ArrayRef<SDUse> Ops) {
5180 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005181 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005182 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005183 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5184 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005185 default: break;
5186 }
5187
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005188 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005189 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005190 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005191 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005192}
5193
Andrew Trickef9de2a2013-05-25 02:42:55 +00005194SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005195 ArrayRef<SDValue> Ops) {
5196 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005197 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005198 case 0: return getNode(Opcode, DL, VT);
5199 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5200 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5201 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005202 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005203 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005204
Chris Lattnerb0713c72005-04-09 03:27:28 +00005205 switch (Opcode) {
5206 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005207 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005208 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005209 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5210 "LHS and RHS of condition must have same type!");
5211 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5212 "True and False arms of SelectCC must have same type!");
5213 assert(Ops[2].getValueType() == VT &&
5214 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005215 break;
5216 }
5217 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005218 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005219 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5220 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005221 break;
5222 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005223 }
5224
Chris Lattner566307f2005-05-14 07:42:29 +00005225 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005226 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005227 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005228
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005229 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005230 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005231 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005232 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005233
Bill Wendling022d18f2009-12-18 23:32:53 +00005234 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005235 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005236
Jack Carter170a5f22013-09-09 22:02:08 +00005237 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005238 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005239 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005240 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005241 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005242 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005243 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005244
Chandler Carruth41b20e72014-07-22 04:07:55 +00005245 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005246 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005247}
5248
Andrew Trickef9de2a2013-05-25 02:42:55 +00005249SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005250 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5251 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005252}
5253
Andrew Trickef9de2a2013-05-25 02:42:55 +00005254SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005255 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005256 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005257 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005258
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005259#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005260 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005261 // FIXME: figure out how to safely handle things like
5262 // int foo(int x) { return 1 << (x & 255); }
5263 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005264 case ISD::SRA_PARTS:
5265 case ISD::SRL_PARTS:
5266 case ISD::SHL_PARTS:
5267 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005268 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005269 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005270 else if (N3.getOpcode() == ISD::AND)
5271 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5272 // If the and is only masking out bits that cannot effect the shift,
5273 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005274 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005275 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005276 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005277 }
5278 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005279 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005280#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005281
Chris Lattnerf9c19152005-08-25 19:12:10 +00005282 // Memoize the node unless it returns a flag.
5283 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005284 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005285 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005286 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005287 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005288 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005289 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005290 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005291
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005292 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005293 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5294 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005295 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005296 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5297 DL.getDebugLoc(), VTList, Ops[0],
5298 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005299 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005300 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5301 DL.getDebugLoc(), VTList, Ops[0],
5302 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005303 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005304 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005305 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005306 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005307 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005308 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005309 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005310 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5311 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005312 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005313 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5314 DL.getDebugLoc(), VTList, Ops[0],
5315 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005316 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005317 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5318 DL.getDebugLoc(), VTList, Ops[0],
5319 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005320 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005321 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005322 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005323 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005324 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005325 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005326 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005327}
5328
Andrew Trickef9de2a2013-05-25 02:42:55 +00005329SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005330 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005331}
5332
Andrew Trickef9de2a2013-05-25 02:42:55 +00005333SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005334 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005335 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005336 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005337}
5338
Andrew Trickef9de2a2013-05-25 02:42:55 +00005339SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005340 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005341 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005342 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005343}
5344
Andrew Trickef9de2a2013-05-25 02:42:55 +00005345SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005346 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005347 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005348 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005349}
5350
Andrew Trickef9de2a2013-05-25 02:42:55 +00005351SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005352 SDValue N1, SDValue N2, SDValue N3,
5353 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005354 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005355 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005356}
5357
Andrew Trickef9de2a2013-05-25 02:42:55 +00005358SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005359 SDValue N1, SDValue N2, SDValue N3,
5360 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005361 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005362 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005363}
5364
Owen Anderson53aa7a92009-08-10 22:56:29 +00005365SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005366 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005367}
5368
Owen Anderson53aa7a92009-08-10 22:56:29 +00005369SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005370 FoldingSetNodeID ID;
5371 ID.AddInteger(2U);
5372 ID.AddInteger(VT1.getRawBits());
5373 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005374
Craig Topperc0196b12014-04-14 00:51:57 +00005375 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005376 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005377 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005378 EVT *Array = Allocator.Allocate<EVT>(2);
5379 Array[0] = VT1;
5380 Array[1] = VT2;
5381 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5382 VTListMap.InsertNode(Result, IP);
5383 }
5384 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005385}
Dan Gohman17059682008-07-17 19:10:17 +00005386
Owen Anderson53aa7a92009-08-10 22:56:29 +00005387SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005388 FoldingSetNodeID ID;
5389 ID.AddInteger(3U);
5390 ID.AddInteger(VT1.getRawBits());
5391 ID.AddInteger(VT2.getRawBits());
5392 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005393
Craig Topperc0196b12014-04-14 00:51:57 +00005394 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005395 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005396 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005397 EVT *Array = Allocator.Allocate<EVT>(3);
5398 Array[0] = VT1;
5399 Array[1] = VT2;
5400 Array[2] = VT3;
5401 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5402 VTListMap.InsertNode(Result, IP);
5403 }
5404 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005405}
5406
Owen Anderson53aa7a92009-08-10 22:56:29 +00005407SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005408 FoldingSetNodeID ID;
5409 ID.AddInteger(4U);
5410 ID.AddInteger(VT1.getRawBits());
5411 ID.AddInteger(VT2.getRawBits());
5412 ID.AddInteger(VT3.getRawBits());
5413 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005414
Craig Topperc0196b12014-04-14 00:51:57 +00005415 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005416 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005417 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005418 EVT *Array = Allocator.Allocate<EVT>(4);
5419 Array[0] = VT1;
5420 Array[1] = VT2;
5421 Array[2] = VT3;
5422 Array[3] = VT4;
5423 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5424 VTListMap.InsertNode(Result, IP);
5425 }
5426 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005427}
5428
Craig Topperabb4ac72014-04-16 06:10:51 +00005429SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5430 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005431 FoldingSetNodeID ID;
5432 ID.AddInteger(NumVTs);
5433 for (unsigned index = 0; index < NumVTs; index++) {
5434 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005435 }
5436
Craig Topperc0196b12014-04-14 00:51:57 +00005437 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005438 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005439 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005440 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005441 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005442 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5443 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005444 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005445 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005446}
5447
5448
Chris Lattnerf34156e2006-01-28 09:32:45 +00005449/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5450/// specified operands. If the resultant node already exists in the DAG,
5451/// this does not modify the specified node, instead it returns the node that
5452/// already exists. If the resultant node does not exist in the DAG, the
5453/// input node is returned. As a degenerate case, if you specify the same
5454/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005455SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005456 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005457
Chris Lattnerf34156e2006-01-28 09:32:45 +00005458 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005459 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005460
Chris Lattnerf34156e2006-01-28 09:32:45 +00005461 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005462 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005463 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005464 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005465
Dan Gohmanebeccb42008-07-21 22:38:59 +00005466 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005467 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005468 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005469 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005470
Chris Lattnerf34156e2006-01-28 09:32:45 +00005471 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005472 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005473
Chris Lattnerf34156e2006-01-28 09:32:45 +00005474 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005475 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005476 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005477}
5478
Dan Gohman92c11ac2010-06-18 15:30:29 +00005479SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005480 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005481
Chris Lattnerf34156e2006-01-28 09:32:45 +00005482 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005483 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005484 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005485
Chris Lattnerf34156e2006-01-28 09:32:45 +00005486 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005487 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005488 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005489 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005490
Dan Gohmanebeccb42008-07-21 22:38:59 +00005491 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005492 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005493 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005494 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005495
Chris Lattnerf34156e2006-01-28 09:32:45 +00005496 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005497 if (N->OperandList[0] != Op1)
5498 N->OperandList[0].set(Op1);
5499 if (N->OperandList[1] != Op2)
5500 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005501
Chris Lattnerf34156e2006-01-28 09:32:45 +00005502 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005503 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005504 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005505}
5506
Dan Gohman92c11ac2010-06-18 15:30:29 +00005507SDNode *SelectionDAG::
5508UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005509 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005510 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005511}
5512
Dan Gohman92c11ac2010-06-18 15:30:29 +00005513SDNode *SelectionDAG::
5514UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005515 SDValue Op3, SDValue Op4) {
5516 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005517 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005518}
5519
Dan Gohman92c11ac2010-06-18 15:30:29 +00005520SDNode *SelectionDAG::
5521UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005522 SDValue Op3, SDValue Op4, SDValue Op5) {
5523 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005524 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005525}
5526
Dan Gohman92c11ac2010-06-18 15:30:29 +00005527SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005528UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5529 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005530 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005531 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005532
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005533 // If no operands changed just return the input node.
Benjamin Kramer0b6742a2015-03-02 16:45:08 +00005534 if (Ops.empty() || std::equal(Ops.begin(), Ops.end(), N->op_begin()))
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005535 return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005536
Chris Lattnerf34156e2006-01-28 09:32:45 +00005537 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005538 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005539 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005540 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005541
Dan Gohman2f83b472008-05-02 00:05:03 +00005542 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005543 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005544 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005545 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005546
Chris Lattnerf34156e2006-01-28 09:32:45 +00005547 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005548 for (unsigned i = 0; i != NumOps; ++i)
5549 if (N->OperandList[i] != Ops[i])
5550 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005551
5552 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005553 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005554 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005555}
5556
Dan Gohman91697632008-07-07 20:57:48 +00005557/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005558/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005559void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005560 // Unlike the code in MorphNodeTo that does this, we don't need to
5561 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005562 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5563 SDUse &Use = *I++;
5564 Use.set(SDValue());
5565 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005566}
Chris Lattner19732782005-08-16 18:17:10 +00005567
Dan Gohman17059682008-07-17 19:10:17 +00005568/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5569/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005570///
Dan Gohman17059682008-07-17 19:10:17 +00005571SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005572 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005573 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005574 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005575}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005576
Dan Gohman17059682008-07-17 19:10:17 +00005577SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005578 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005579 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005580 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005581 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005582}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005583
Dan Gohman17059682008-07-17 19:10:17 +00005584SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005585 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005586 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005587 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005588 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005589 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005590}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005591
Dan Gohman17059682008-07-17 19:10:17 +00005592SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005593 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005594 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005595 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005596 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005597 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005598}
Chris Lattner466fece2005-08-21 22:30:30 +00005599
Dan Gohman17059682008-07-17 19:10:17 +00005600SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005601 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005602 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005603 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005604}
5605
Dan Gohman17059682008-07-17 19:10:17 +00005606SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005607 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005608 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005609 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005610}
5611
Dan Gohman17059682008-07-17 19:10:17 +00005612SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005613 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005614 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005615 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005616}
5617
Dan Gohman17059682008-07-17 19:10:17 +00005618SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005619 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005620 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005621 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005622 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005623}
5624
Bill Wendling2d598632008-12-01 23:28:22 +00005625SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005626 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005627 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005628 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005629 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005630}
5631
Scott Michelcf0da6c2009-02-17 22:15:04 +00005632SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005633 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005634 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005635 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005636 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005637 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005638}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005639
Scott Michelcf0da6c2009-02-17 22:15:04 +00005640SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005641 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005642 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005643 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005644 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005645 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005646}
5647
Dan Gohman17059682008-07-17 19:10:17 +00005648SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005649 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005650 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005651 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005652 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005653 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005654 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005655}
5656
Dan Gohman17059682008-07-17 19:10:17 +00005657SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005658 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005659 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005660 SDValue Op3) {
5661 SDVTList VTs = getVTList(VT1, VT2, VT3);
5662 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005663 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005664}
5665
5666SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005667 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005668 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005669 // Reset the NodeID to -1.
5670 N->setNodeId(-1);
5671 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005672}
5673
Andrew Trickef9de2a2013-05-25 02:42:55 +00005674/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005675/// the line number information on the merged node since it is not possible to
5676/// preserve the information that operation is associated with multiple lines.
5677/// This will make the debugger working better at -O0, were there is a higher
5678/// probability having other instructions associated with that line.
5679///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005680/// For IROrder, we keep the smaller of the two
5681SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005682 DebugLoc NLoc = N->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00005683 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005684 N->setDebugLoc(DebugLoc());
5685 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005686 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5687 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005688 return N;
5689}
5690
Chris Lattneraf197502010-02-28 21:36:14 +00005691/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005692/// return type, opcode, and operands.
5693///
5694/// Note that MorphNodeTo returns the resultant node. If there is already a
5695/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005696/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005697///
5698/// Using MorphNodeTo is faster than creating a new node and swapping it in
5699/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005700/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005701/// the node's users.
5702///
Chandler Carruth356665a2014-08-01 22:09:43 +00005703/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5704/// As a consequence it isn't appropriate to use from within the DAG combiner or
5705/// the legalizer which maintain worklists that would need to be updated when
5706/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005707SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005708 SDVTList VTs, ArrayRef<SDValue> Ops) {
5709 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005710 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005711 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005712 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005713 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005714 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005715 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005716 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005717 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005718
Dan Gohmand3fe1742008-09-13 01:54:27 +00005719 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005720 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005721
Dan Gohman17059682008-07-17 19:10:17 +00005722 // Start the morphing.
5723 N->NodeType = Opc;
5724 N->ValueList = VTs.VTs;
5725 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005726
Dan Gohman17059682008-07-17 19:10:17 +00005727 // Clear the operands list, updating used nodes to remove this from their
5728 // use list. Keep track of any operands that become dead as a result.
5729 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005730 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5731 SDUse &Use = *I++;
5732 SDNode *Used = Use.getNode();
5733 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005734 if (Used->use_empty())
5735 DeadNodeSet.insert(Used);
5736 }
5737
Dan Gohman48b185d2009-09-25 20:36:54 +00005738 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5739 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005740 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005741 // If NumOps is larger than the # of operands we can have in a
5742 // MachineSDNode, reallocate the operand list.
5743 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5744 if (MN->OperandsNeedDelete)
5745 delete[] MN->OperandList;
5746 if (NumOps > array_lengthof(MN->LocalOperands))
5747 // We're creating a final node that will live unmorphed for the
5748 // remainder of the current SelectionDAG iteration, so we can allocate
5749 // the operands directly out of a pool with no recycling metadata.
5750 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005751 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005752 else
Craig Topper131de822014-04-27 19:21:16 +00005753 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005754 MN->OperandsNeedDelete = false;
5755 } else
Craig Topper131de822014-04-27 19:21:16 +00005756 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005757 } else {
5758 // If NumOps is larger than the # of operands we currently have, reallocate
5759 // the operand list.
5760 if (NumOps > N->NumOperands) {
5761 if (N->OperandsNeedDelete)
5762 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005763 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005764 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005765 } else
Craig Topper131de822014-04-27 19:21:16 +00005766 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005767 }
5768
5769 // Delete any nodes that are still dead after adding the uses for the
5770 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005771 if (!DeadNodeSet.empty()) {
5772 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005773 for (SDNode *N : DeadNodeSet)
5774 if (N->use_empty())
5775 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005776 RemoveDeadNodes(DeadNodes);
5777 }
Dan Gohman91697632008-07-07 20:57:48 +00005778
Dan Gohman17059682008-07-17 19:10:17 +00005779 if (IP)
5780 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005781 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005782}
5783
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005784
Dan Gohman32f71d72009-09-25 18:54:59 +00005785/// getMachineNode - These are used for target selectors to create a new node
5786/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005787///
Dan Gohman32f71d72009-09-25 18:54:59 +00005788/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005789/// node of the specified opcode and operands, it returns that node instead of
5790/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005791MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005792SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005793 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005794 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005795}
Bill Wendlinga434d932009-01-29 09:01:55 +00005796
Dan Gohmana22f2d82009-10-10 01:29:16 +00005797MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005798SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005799 SDVTList VTs = getVTList(VT);
5800 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005801 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005802}
Bill Wendlinga434d932009-01-29 09:01:55 +00005803
Dan Gohmana22f2d82009-10-10 01:29:16 +00005804MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005805SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005806 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005807 SDVTList VTs = getVTList(VT);
5808 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005809 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005810}
Bill Wendlinga434d932009-01-29 09:01:55 +00005811
Dan Gohmana22f2d82009-10-10 01:29:16 +00005812MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005813SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005814 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005815 SDVTList VTs = getVTList(VT);
5816 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005817 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005818}
Bill Wendlinga434d932009-01-29 09:01:55 +00005819
Dan Gohmana22f2d82009-10-10 01:29:16 +00005820MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005821SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005822 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005823 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005824 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005825}
Bill Wendlinga434d932009-01-29 09:01:55 +00005826
Dan Gohmana22f2d82009-10-10 01:29:16 +00005827MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005828SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005829 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005830 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005831}
Bill Wendlinga434d932009-01-29 09:01:55 +00005832
Dan Gohmana22f2d82009-10-10 01:29:16 +00005833MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005834SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005835 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005836 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005837 SDValue Ops[] = { Op1 };
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,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005843 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005844 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005845 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005846 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005847}
5848
Dan Gohmana22f2d82009-10-10 01:29:16 +00005849MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005850SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005851 EVT VT1, EVT VT2, SDValue Op1,
5852 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005853 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005854 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005855 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005856}
5857
Dan Gohmana22f2d82009-10-10 01:29:16 +00005858MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005859SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005860 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005861 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005862 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005863 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005864}
Bill Wendlinga434d932009-01-29 09:01:55 +00005865
Dan Gohmana22f2d82009-10-10 01:29:16 +00005866MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005867SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005868 EVT VT1, EVT VT2, EVT VT3,
5869 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005870 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005871 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005872 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005873}
Bill Wendlinga434d932009-01-29 09:01:55 +00005874
Dan Gohmana22f2d82009-10-10 01:29:16 +00005875MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005876SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005877 EVT VT1, EVT VT2, EVT VT3,
5878 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005879 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005880 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005881 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005882}
Bill Wendlinga434d932009-01-29 09:01:55 +00005883
Dan Gohmana22f2d82009-10-10 01:29:16 +00005884MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005885SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005886 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005887 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005888 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005889 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005890}
Bill Wendlinga434d932009-01-29 09:01:55 +00005891
Dan Gohmana22f2d82009-10-10 01:29:16 +00005892MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005893SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005894 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005895 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005896 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005897 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005898}
5899
Dan Gohmana22f2d82009-10-10 01:29:16 +00005900MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005901SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005902 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005903 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005904 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005905 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005906}
5907
Dan Gohmana22f2d82009-10-10 01:29:16 +00005908MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005909SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005910 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005911 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005912 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005913 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005914 const SDValue *Ops = OpsArray.data();
5915 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005916
5917 if (DoCSE) {
5918 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005919 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005920 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005921 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005922 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005923 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005924 }
5925
5926 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005927 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5928 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005929
5930 // Initialize the operands list.
5931 if (NumOps > array_lengthof(N->LocalOperands))
5932 // We're creating a final node that will live unmorphed for the
5933 // remainder of the current SelectionDAG iteration, so we can allocate
5934 // the operands directly out of a pool with no recycling metadata.
5935 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5936 Ops, NumOps);
5937 else
5938 N->InitOperands(N->LocalOperands, Ops, NumOps);
5939 N->OperandsNeedDelete = false;
5940
5941 if (DoCSE)
5942 CSEMap.InsertNode(N, IP);
5943
Chandler Carruth41b20e72014-07-22 04:07:55 +00005944 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005945 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005946}
Evan Chengd3f1db92006-02-09 07:15:23 +00005947
Dan Gohmanac33a902009-08-19 18:16:17 +00005948/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005949/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005950SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005951SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005952 SDValue Operand) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005953 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005954 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005955 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005956 return SDValue(Subreg, 0);
5957}
5958
Bob Wilson2a45a652009-10-08 18:49:46 +00005959/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005960/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005961SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005962SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005963 SDValue Operand, SDValue Subreg) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005964 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005965 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005966 VT, Operand, Subreg, SRIdxVal);
5967 return SDValue(Result, 0);
5968}
5969
Evan Cheng31604a62008-03-22 01:55:50 +00005970/// getNodeIfExists - Get the specified node if it's already available, or
5971/// else return NULL.
5972SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Sanjay Patel801caff2015-05-05 21:40:38 +00005973 ArrayRef<SDValue> Ops,
5974 const SDNodeFlags *Flags) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005975 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005976 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005977 AddNodeIDNode(ID, Opcode, VTList, Ops);
Sanjay Patel801caff2015-05-05 21:40:38 +00005978 AddNodeIDFlags(ID, Opcode, Flags);
Craig Topperc0196b12014-04-14 00:51:57 +00005979 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005980 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005981 return E;
5982 }
Craig Topperc0196b12014-04-14 00:51:57 +00005983 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005984}
5985
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005986/// getDbgValue - Creates a SDDbgValue node.
5987///
Adrian Prantl32da8892014-04-25 20:49:25 +00005988/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005989SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5990 unsigned R, bool IsIndirect, uint64_t Off,
5991 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00005992 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00005993 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005994 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005995}
5996
Adrian Prantl32da8892014-04-25 20:49:25 +00005997/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005998SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5999 const Value *C, uint64_t Off,
6000 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006001 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006002 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006003 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006004}
6005
Adrian Prantl32da8892014-04-25 20:49:25 +00006006/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006007SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
6008 unsigned FI, uint64_t Off,
6009 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006010 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006011 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006012 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006013}
6014
Dan Gohman7d099f72010-03-03 21:33:37 +00006015namespace {
6016
Dan Gohman9cc886b2010-03-04 19:11:28 +00006017/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00006018/// pointed to by a use iterator is deleted, increment the use iterator
6019/// so that it doesn't dangle.
6020///
Dan Gohman7d099f72010-03-03 21:33:37 +00006021class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00006022 SDNode::use_iterator &UI;
6023 SDNode::use_iterator &UE;
6024
Craig Topper7b883b32014-03-08 06:31:39 +00006025 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00006026 // Increment the iterator as needed.
6027 while (UI != UE && N == *UI)
6028 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00006029 }
6030
6031public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006032 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00006033 SDNode::use_iterator &ui,
6034 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006035 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00006036};
6037
6038}
6039
Evan Cheng445b91a2006-08-07 22:13:29 +00006040/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006041/// This can cause recursive merging of nodes in the DAG.
6042///
Chris Lattner76858912008-02-03 03:35:22 +00006043/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00006044///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006045void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006046 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006047 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00006048 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00006049 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00006050
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006051 // Iterate over all the existing uses of From. New uses will be added
6052 // to the beginning of the use list, which we avoid visiting.
6053 // This specifically avoids visiting uses of From that arise while the
6054 // replacement is happening, because any such uses would be the result
6055 // of CSE: If an existing node looks like From after one of its operands
6056 // is replaced by To, we don't want to replace of all its users with To
6057 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006058 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006059 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006060 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006061 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006062
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006063 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006064 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006065
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006066 // A user can appear in a use list multiple times, and when this
6067 // happens the uses are usually next to each other in the list.
6068 // To help reduce the number of CSE recomputations, process all
6069 // the uses of this user that we can find this way.
6070 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006071 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006072 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006073 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006074 } while (UI != UE && *UI == User);
6075
6076 // Now that we have modified User, add it back to the CSE maps. If it
6077 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006078 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006079 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006080
6081 // If we just RAUW'd the root, take note.
6082 if (FromN == getRoot())
6083 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00006084}
6085
6086/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6087/// This can cause recursive merging of nodes in the DAG.
6088///
Dan Gohman8aa28b92009-04-15 20:06:30 +00006089/// This version assumes that for each value of From, there is a
6090/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00006091///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006092void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00006093#ifndef NDEBUG
6094 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6095 assert((!From->hasAnyUseOfValue(i) ||
6096 From->getValueType(i) == To->getValueType(i)) &&
6097 "Cannot use this version of ReplaceAllUsesWith!");
6098#endif
Dan Gohman17059682008-07-17 19:10:17 +00006099
6100 // Handle the trivial case.
6101 if (From == To)
6102 return;
6103
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006104 // Iterate over just the existing users of From. See the comments in
6105 // the ReplaceAllUsesWith above.
6106 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006107 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006108 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006109 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006110
Chris Lattner373f0482005-08-26 18:36:28 +00006111 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006112 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006113
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006114 // A user can appear in a use list multiple times, and when this
6115 // happens the uses are usually next to each other in the list.
6116 // To help reduce the number of CSE recomputations, process all
6117 // the uses of this user that we can find this way.
6118 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006119 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006120 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006121 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006122 } while (UI != UE && *UI == User);
6123
6124 // Now that we have modified User, add it back to the CSE maps. If it
6125 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006126 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006127 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006128
6129 // If we just RAUW'd the root, take note.
6130 if (From == getRoot().getNode())
6131 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006132}
6133
Chris Lattner373f0482005-08-26 18:36:28 +00006134/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6135/// This can cause recursive merging of nodes in the DAG.
6136///
6137/// This version can replace From with any result values. To must match the
6138/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006139void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006140 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006141 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006142
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006143 // Iterate over just the existing users of From. See the comments in
6144 // the ReplaceAllUsesWith above.
6145 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006146 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006147 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006148 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006149
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006150 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006151 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006152
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006153 // A user can appear in a use list multiple times, and when this
6154 // happens the uses are usually next to each other in the list.
6155 // To help reduce the number of CSE recomputations, process all
6156 // the uses of this user that we can find this way.
6157 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006158 SDUse &Use = UI.getUse();
6159 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006160 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006161 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006162 } while (UI != UE && *UI == User);
6163
6164 // Now that we have modified User, add it back to the CSE maps. If it
6165 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006166 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006167 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006168
6169 // If we just RAUW'd the root, take note.
6170 if (From == getRoot().getNode())
6171 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006172}
6173
Chris Lattner375e1a72006-02-17 21:58:01 +00006174/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006175/// uses of other values produced by From.getNode() alone. The Deleted
6176/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006177void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006178 // Handle the really simple, really trivial case efficiently.
6179 if (From == To) return;
6180
Chris Lattner375e1a72006-02-17 21:58:01 +00006181 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006182 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006183 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006184 return;
6185 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006186
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006187 // Iterate over just the existing users of From. See the comments in
6188 // the ReplaceAllUsesWith above.
6189 SDNode::use_iterator UI = From.getNode()->use_begin(),
6190 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006191 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006192 while (UI != UE) {
6193 SDNode *User = *UI;
6194 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006195
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006196 // A user can appear in a use list multiple times, and when this
6197 // happens the uses are usually next to each other in the list.
6198 // To help reduce the number of CSE recomputations, process all
6199 // the uses of this user that we can find this way.
6200 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006201 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006202
6203 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006204 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006205 ++UI;
6206 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006207 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006208
6209 // If this node hasn't been modified yet, it's still in the CSE maps,
6210 // so remove its old self from the CSE maps.
6211 if (!UserRemovedFromCSEMaps) {
6212 RemoveNodeFromCSEMaps(User);
6213 UserRemovedFromCSEMaps = true;
6214 }
6215
6216 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006217 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006218 } while (UI != UE && *UI == User);
6219
6220 // We are iterating over all uses of the From node, so if a use
6221 // doesn't use the specific value, no changes are made.
6222 if (!UserRemovedFromCSEMaps)
6223 continue;
6224
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006225 // Now that we have modified User, add it back to the CSE maps. If it
6226 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006227 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006228 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006229
6230 // If we just RAUW'd the root, take note.
6231 if (From == getRoot())
6232 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006233}
6234
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006235namespace {
6236 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6237 /// to record information about a use.
6238 struct UseMemo {
6239 SDNode *User;
6240 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006241 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006242 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006243
6244 /// operator< - Sort Memos by User.
6245 bool operator<(const UseMemo &L, const UseMemo &R) {
6246 return (intptr_t)L.User < (intptr_t)R.User;
6247 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006248}
6249
Dan Gohman17059682008-07-17 19:10:17 +00006250/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006251/// uses of other values produced by From.getNode() alone. The same value
6252/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006253/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006254void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6255 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006256 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006257 // Handle the simple, trivial case efficiently.
6258 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006259 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006260
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006261 // Read up all the uses and make records of them. This helps
6262 // processing new uses that are introduced during the
6263 // replacement process.
6264 SmallVector<UseMemo, 4> Uses;
6265 for (unsigned i = 0; i != Num; ++i) {
6266 unsigned FromResNo = From[i].getResNo();
6267 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006268 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006269 E = FromNode->use_end(); UI != E; ++UI) {
6270 SDUse &Use = UI.getUse();
6271 if (Use.getResNo() == FromResNo) {
6272 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006273 Uses.push_back(Memo);
6274 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006275 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006276 }
Dan Gohman17059682008-07-17 19:10:17 +00006277
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006278 // Sort the uses, so that all the uses from a given User are together.
6279 std::sort(Uses.begin(), Uses.end());
6280
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006281 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6282 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006283 // We know that this user uses some value of From. If it is the right
6284 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006285 SDNode *User = Uses[UseIndex].User;
6286
6287 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006288 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006289
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006290 // The Uses array is sorted, so all the uses for a given User
6291 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006292 // To help reduce the number of CSE recomputations, process all
6293 // the uses of this user that we can find this way.
6294 do {
6295 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006296 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006297 ++UseIndex;
6298
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006299 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006300 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6301
Dan Gohman17059682008-07-17 19:10:17 +00006302 // Now that we have modified User, add it back to the CSE maps. If it
6303 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006304 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006305 }
6306}
6307
Evan Cheng9631a602006-08-01 08:20:41 +00006308/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006309/// based on their topological order. It returns the maximum id and a vector
6310/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006311unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006312
Dan Gohman86aa16a2008-09-30 18:30:35 +00006313 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006314
Dan Gohman86aa16a2008-09-30 18:30:35 +00006315 // SortedPos tracks the progress of the algorithm. Nodes before it are
6316 // sorted, nodes after it are unsorted. When the algorithm completes
6317 // it is at the end of the list.
6318 allnodes_iterator SortedPos = allnodes_begin();
6319
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006320 // Visit all the nodes. Move nodes with no operands to the front of
6321 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006322 // operand count. Before we do this, the Node Id fields of the nodes
6323 // may contain arbitrary values. After, the Node Id fields for nodes
6324 // before SortedPos will contain the topological sort index, and the
6325 // Node Id fields for nodes At SortedPos and after will contain the
6326 // count of outstanding operands.
6327 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6328 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006329 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006330 unsigned Degree = N->getNumOperands();
6331 if (Degree == 0) {
6332 // A node with no uses, add it to the result array immediately.
6333 N->setNodeId(DAGSize++);
6334 allnodes_iterator Q = N;
6335 if (Q != SortedPos)
6336 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006337 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006338 ++SortedPos;
6339 } else {
6340 // Temporarily use the Node Id as scratch space for the degree count.
6341 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006342 }
6343 }
6344
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006345 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006346 // such that by the time the end is reached all nodes will be sorted.
6347 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6348 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006349 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006350 // N is in sorted position, so all its uses have one less operand
6351 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006352 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6353 UI != UE; ++UI) {
6354 SDNode *P = *UI;
6355 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006356 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006357 --Degree;
6358 if (Degree == 0) {
6359 // All of P's operands are sorted, so P may sorted now.
6360 P->setNodeId(DAGSize++);
6361 if (P != SortedPos)
6362 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006363 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006364 ++SortedPos;
6365 } else {
6366 // Update P's outstanding operand count.
6367 P->setNodeId(Degree);
6368 }
6369 }
David Greene3b2a68c2010-01-20 00:59:23 +00006370 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006371#ifndef NDEBUG
6372 SDNode *S = ++I;
6373 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006374 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006375 dbgs() << "Checking if this is due to cycles\n";
6376 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006377#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006378 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006379 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006380 }
6381
6382 assert(SortedPos == AllNodes.end() &&
6383 "Topological sort incomplete!");
6384 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6385 "First node in topological sort is not the entry token!");
6386 assert(AllNodes.front().getNodeId() == 0 &&
6387 "First node in topological sort has non-zero id!");
6388 assert(AllNodes.front().getNumOperands() == 0 &&
6389 "First node in topological sort has operands!");
6390 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6391 "Last node in topologic sort has unexpected id!");
6392 assert(AllNodes.back().use_empty() &&
6393 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006394 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006395 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006396}
6397
Evan Cheng563fe3c2010-03-25 01:38:16 +00006398/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6399/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006400void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006401 if (SD) {
6402 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006403 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006404 }
6405 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006406}
Evan Cheng29eefc12006-07-27 06:39:06 +00006407
Devang Patelefc6b162011-01-25 23:27:42 +00006408/// TransferDbgValues - Transfer SDDbgValues.
6409void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6410 if (From == To || !From.getNode()->getHasDebugValue())
6411 return;
6412 SDNode *FromNode = From.getNode();
6413 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006414 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006415 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006416 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006417 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006418 SDDbgValue *Dbg = *I;
6419 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006420 SDDbgValue *Clone =
6421 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6422 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6423 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006424 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006425 }
6426 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006427 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006428 E = ClonedDVs.end(); I != E; ++I)
6429 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006430}
6431
Jim Laskeyd66e6162005-08-17 20:08:02 +00006432//===----------------------------------------------------------------------===//
6433// SDNode Class
6434//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006435
Chris Lattner3bf17b62007-02-04 02:41:42 +00006436HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006437 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006438}
6439
Andrew Trickef9de2a2013-05-25 02:42:55 +00006440GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6441 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006442 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006443 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006444 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006445}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006446
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006447AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6448 SDValue X, unsigned SrcAS,
6449 unsigned DestAS)
6450 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6451 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6452
Andrew Trickef9de2a2013-05-25 02:42:55 +00006453MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6454 EVT memvt, MachineMemOperand *mmo)
6455 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006456 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006457 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006458 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006459 assert(isNonTemporal() == MMO->isNonTemporal() &&
6460 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006461 // We check here that the size of the memory operand fits within the size of
6462 // the MMO. This is because the MMO might indicate only a possible address
6463 // range instead of specifying the affected memory addresses precisely.
6464 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006465}
6466
Andrew Trickef9de2a2013-05-25 02:42:55 +00006467MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006468 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6469 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006470 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!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006474 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006475}
6476
Jim Laskeyf576b422006-10-27 23:46:08 +00006477/// Profile - Gather unique data for the node.
6478///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006479void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006480 AddNodeIDNode(ID, this);
6481}
6482
Owen Anderson3b1665e2009-08-25 22:27:22 +00006483namespace {
6484 struct EVTArray {
6485 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006486
Owen Anderson3b1665e2009-08-25 22:27:22 +00006487 EVTArray() {
6488 VTs.reserve(MVT::LAST_VALUETYPE);
6489 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6490 VTs.push_back(MVT((MVT::SimpleValueType)i));
6491 }
6492 };
6493}
6494
Owen Anderson53aa7a92009-08-10 22:56:29 +00006495static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006496static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006497static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006498
Chris Lattner88fa11c2005-11-08 23:30:28 +00006499/// getValueTypeList - Return a pointer to the specified value type.
6500///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006501const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006502 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006503 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006504 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006505 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006506 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006507 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006508 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006509 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006510}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006511
Chris Lattner40e79822005-01-12 18:37:47 +00006512/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6513/// indicated value. This method ignores uses of other values defined by this
6514/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006515bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006516 assert(Value < getNumValues() && "Bad value!");
6517
Roman Levenstein51f532f2008-04-07 10:06:32 +00006518 // TODO: Only iterate over uses of a given value of the node
6519 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006520 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006521 if (NUses == 0)
6522 return false;
6523 --NUses;
6524 }
Chris Lattner40e79822005-01-12 18:37:47 +00006525 }
6526
6527 // Found exactly the right number of uses?
6528 return NUses == 0;
6529}
6530
6531
Evan Cheng358c3d12007-08-02 05:29:38 +00006532/// hasAnyUseOfValue - Return true if there are any use of the indicated
6533/// value. This method ignores uses of other values defined by this operation.
6534bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6535 assert(Value < getNumValues() && "Bad value!");
6536
Dan Gohman7a510c22008-07-09 22:39:01 +00006537 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006538 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006539 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006540
6541 return false;
6542}
6543
6544
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006545/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006546///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006547bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006548 bool Seen = false;
6549 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006550 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006551 if (User == this)
6552 Seen = true;
6553 else
6554 return false;
6555 }
6556
6557 return Seen;
6558}
6559
Evan Cheng9456dd82006-11-03 07:31:32 +00006560/// isOperand - Return true if this node is an operand of N.
6561///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006562bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006563 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6564 if (*this == N->getOperand(i))
6565 return true;
6566 return false;
6567}
6568
Evan Cheng567d2e52008-03-04 00:41:45 +00006569bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006570 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006571 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006572 return true;
6573 return false;
6574}
Evan Chengd37645c2006-02-05 06:29:23 +00006575
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006576/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006577/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006578/// side-effecting instructions on any chain path. In practice, this looks
6579/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006580/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006581bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006582 unsigned Depth) const {
6583 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006584
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006585 // Don't search too deeply, we just want to be able to see through
6586 // TokenFactor's etc.
6587 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006588
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006589 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006590 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006591 if (getOpcode() == ISD::TokenFactor) {
6592 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006593 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6594 return false;
6595 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006596 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006597
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006598 // Loads don't have side effects, look through them.
6599 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6600 if (!Ld->isVolatile())
6601 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6602 }
6603 return false;
6604}
6605
Lang Hames5a004992011-07-07 04:31:51 +00006606/// hasPredecessor - Return true if N is a predecessor of this node.
6607/// N is either an operand of this node, or can be reached by recursively
6608/// traversing up the operands.
6609/// NOTE: This is an expensive method. Use it carefully.
6610bool SDNode::hasPredecessor(const SDNode *N) const {
6611 SmallPtrSet<const SDNode *, 32> Visited;
6612 SmallVector<const SDNode *, 16> Worklist;
6613 return hasPredecessorHelper(N, Visited, Worklist);
6614}
Dan Gohmancd139c02009-10-28 03:44:30 +00006615
Craig Topperb94011f2013-07-14 04:42:23 +00006616bool
6617SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006618 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006619 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006620 if (Visited.empty()) {
6621 Worklist.push_back(this);
6622 } else {
6623 // Take a look in the visited set. If we've already encountered this node
6624 // we needn't search further.
6625 if (Visited.count(N))
6626 return true;
6627 }
6628
6629 // Haven't visited N yet. Continue the search.
6630 while (!Worklist.empty()) {
6631 const SDNode *M = Worklist.pop_back_val();
6632 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6633 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006634 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006635 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006636 if (Op == N)
6637 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006638 }
Lang Hames5a004992011-07-07 04:31:51 +00006639 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006640
6641 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006642}
6643
Evan Cheng5d9fd972006-10-04 00:56:09 +00006644uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6645 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006646 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006647}
6648
Mon P Wang32f8bb92009-11-30 02:42:02 +00006649SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6650 assert(N->getNumValues() == 1 &&
6651 "Can't unroll a vector with multiple results!");
6652
6653 EVT VT = N->getValueType(0);
6654 unsigned NE = VT.getVectorNumElements();
6655 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006656 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006657
6658 SmallVector<SDValue, 8> Scalars;
6659 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6660
6661 // If ResNE is 0, fully unroll the vector op.
6662 if (ResNE == 0)
6663 ResNE = NE;
6664 else if (NE > ResNE)
6665 NE = ResNE;
6666
6667 unsigned i;
6668 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006669 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006670 SDValue Operand = N->getOperand(j);
6671 EVT OperandVT = Operand.getValueType();
6672 if (OperandVT.isVector()) {
6673 // A vector operand; extract a single element.
6674 EVT OperandEltVT = OperandVT.getVectorElementType();
6675 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6676 OperandEltVT,
6677 Operand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006678 getConstant(i, dl, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006679 } else {
6680 // A scalar operand; just use it as is.
6681 Operands[j] = Operand;
6682 }
6683 }
6684
6685 switch (N->getOpcode()) {
6686 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006687 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006688 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006689 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006690 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006691 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006692 case ISD::SHL:
6693 case ISD::SRA:
6694 case ISD::SRL:
6695 case ISD::ROTL:
6696 case ISD::ROTR:
6697 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006698 getShiftAmountOperand(Operands[0].getValueType(),
6699 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006700 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006701 case ISD::SIGN_EXTEND_INREG:
6702 case ISD::FP_ROUND_INREG: {
6703 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6704 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6705 Operands[0],
6706 getValueType(ExtVT)));
6707 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006708 }
6709 }
6710
6711 for (; i < ResNE; ++i)
6712 Scalars.push_back(getUNDEF(EltVT));
6713
6714 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006715 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006716}
6717
Evan Chengf5938d52009-12-09 01:36:00 +00006718
Wesley Peck527da1b2010-11-23 03:31:01 +00006719/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6720/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006721/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006722bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006723 unsigned Bytes, int Dist) const {
6724 if (LD->getChain() != Base->getChain())
6725 return false;
6726 EVT VT = LD->getValueType(0);
6727 if (VT.getSizeInBits() / 8 != Bytes)
6728 return false;
6729
6730 SDValue Loc = LD->getOperand(1);
6731 SDValue BaseLoc = Base->getOperand(1);
6732 if (Loc.getOpcode() == ISD::FrameIndex) {
6733 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6734 return false;
6735 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6736 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6737 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6738 int FS = MFI->getObjectSize(FI);
6739 int BFS = MFI->getObjectSize(BFI);
6740 if (FS != BFS || FS != (int)Bytes) return false;
6741 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6742 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006743
Sanjay Patel7129c102014-12-16 21:57:18 +00006744 // Handle X + C.
6745 if (isBaseWithConstantOffset(Loc)) {
6746 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6747 if (Loc.getOperand(0) == BaseLoc) {
6748 // If the base location is a simple address with no offset itself, then
6749 // the second load's first add operand should be the base address.
6750 if (LocOffset == Dist * (int)Bytes)
6751 return true;
6752 } else if (isBaseWithConstantOffset(BaseLoc)) {
6753 // The base location itself has an offset, so subtract that value from the
6754 // second load's offset before comparing to distance * size.
6755 int64_t BOffset =
6756 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6757 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6758 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6759 return true;
6760 }
6761 }
6762 }
Craig Topperc0196b12014-04-14 00:51:57 +00006763 const GlobalValue *GV1 = nullptr;
6764 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006765 int64_t Offset1 = 0;
6766 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006767 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6768 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006769 if (isGA1 && isGA2 && GV1 == GV2)
6770 return Offset1 == (Offset2 + Dist*Bytes);
6771 return false;
6772}
6773
6774
Evan Cheng34a23ea2009-12-09 01:04:59 +00006775/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6776/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006777unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006778 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006779 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006780 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006781 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006782 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006783 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00006784 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
6785 *TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006786 unsigned AlignBits = KnownZero.countTrailingOnes();
6787 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6788 if (Align)
6789 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006790 }
Evan Chengd938faf2009-12-09 01:53:58 +00006791
Evan Cheng34a23ea2009-12-09 01:04:59 +00006792 // If this is a direct reference to a stack slot, use information about the
6793 // stack slot's alignment.
6794 int FrameIdx = 1 << 31;
6795 int64_t FrameOffset = 0;
6796 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6797 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006798 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006799 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006800 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006801 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6802 FrameOffset = Ptr.getConstantOperandVal(1);
6803 }
6804
6805 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006806 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006807 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6808 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006809 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006810 }
6811
6812 return 0;
6813}
6814
Juergen Ributzkab3487102013-11-19 21:20:17 +00006815/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6816/// which is split (or expanded) into two not necessarily identical pieces.
6817std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6818 // Currently all types are split in half.
6819 EVT LoVT, HiVT;
6820 if (!VT.isVector()) {
6821 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6822 } else {
6823 unsigned NumElements = VT.getVectorNumElements();
6824 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6825 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6826 NumElements/2);
6827 }
6828 return std::make_pair(LoVT, HiVT);
6829}
6830
6831/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6832/// low/high part.
6833std::pair<SDValue, SDValue>
6834SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6835 const EVT &HiVT) {
6836 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6837 N.getValueType().getVectorNumElements() &&
6838 "More vector elements requested than available!");
6839 SDValue Lo, Hi;
6840 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006841 getConstant(0, DL, TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006842 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006843 getConstant(LoVT.getVectorNumElements(), DL,
6844 TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006845 return std::make_pair(Lo, Hi);
6846}
6847
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006848void SelectionDAG::ExtractVectorElements(SDValue Op,
6849 SmallVectorImpl<SDValue> &Args,
6850 unsigned Start, unsigned Count) {
6851 EVT VT = Op.getValueType();
6852 if (Count == 0)
6853 Count = VT.getVectorNumElements();
6854
6855 EVT EltVT = VT.getVectorElementType();
6856 EVT IdxTy = TLI->getVectorIdxTy();
6857 SDLoc SL(Op);
6858 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6859 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006860 Op, getConstant(i, SL, IdxTy)));
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006861 }
6862}
6863
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006864// getAddressSpace - Return the address space this GlobalAddress belongs to.
6865unsigned GlobalAddressSDNode::getAddressSpace() const {
6866 return getGlobal()->getType()->getAddressSpace();
6867}
6868
6869
Chris Lattner229907c2011-07-18 04:54:35 +00006870Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006871 if (isMachineConstantPoolEntry())
6872 return Val.MachineCPVal->getType();
6873 return Val.ConstVal->getType();
6874}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006875
Bob Wilson85cefe82009-03-02 23:24:16 +00006876bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6877 APInt &SplatUndef,
6878 unsigned &SplatBitSize,
6879 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006880 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006881 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006882 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006883 assert(VT.isVector() && "Expected a vector type");
6884 unsigned sz = VT.getSizeInBits();
6885 if (MinSplatBits > sz)
6886 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006887
Bob Wilson85cefe82009-03-02 23:24:16 +00006888 SplatValue = APInt(sz, 0);
6889 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006890
Bob Wilson85cefe82009-03-02 23:24:16 +00006891 // Get the bits. Bits with undefined values (when the corresponding element
6892 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6893 // in SplatValue. If any of the values are not constant, give up and return
6894 // false.
6895 unsigned int nOps = getNumOperands();
6896 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6897 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006898
6899 for (unsigned j = 0; j < nOps; ++j) {
6900 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006901 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006902 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006903
Bob Wilson85cefe82009-03-02 23:24:16 +00006904 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006905 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006906 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006907 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006908 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006909 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006910 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006911 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006912 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006913 }
6914
Bob Wilson85cefe82009-03-02 23:24:16 +00006915 // The build_vector is all constants or undefs. Find the smallest element
6916 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006917
Bob Wilson85cefe82009-03-02 23:24:16 +00006918 HasAnyUndefs = (SplatUndef != 0);
6919 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006920
Bob Wilson85cefe82009-03-02 23:24:16 +00006921 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006922 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6923 APInt LowValue = SplatValue.trunc(HalfSize);
6924 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6925 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006926
Bob Wilson85cefe82009-03-02 23:24:16 +00006927 // If the two halves do not match (ignoring undef bits), stop here.
6928 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6929 MinSplatBits > HalfSize)
6930 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006931
Bob Wilson85cefe82009-03-02 23:24:16 +00006932 SplatValue = HighValue | LowValue;
6933 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006934
Bob Wilson85cefe82009-03-02 23:24:16 +00006935 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006936 }
6937
Bob Wilson85cefe82009-03-02 23:24:16 +00006938 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006939 return true;
6940}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006941
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006942SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6943 if (UndefElements) {
6944 UndefElements->clear();
6945 UndefElements->resize(getNumOperands());
6946 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006947 SDValue Splatted;
6948 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6949 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006950 if (Op.getOpcode() == ISD::UNDEF) {
6951 if (UndefElements)
6952 (*UndefElements)[i] = true;
6953 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006954 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006955 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006956 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006957 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006958 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006959
Chandler Carruthb844e722014-07-08 07:19:55 +00006960 if (!Splatted) {
6961 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6962 "Can only have a splat without a constant for all undefs.");
6963 return getOperand(0);
6964 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006965
Chandler Carruthb844e722014-07-08 07:19:55 +00006966 return Splatted;
6967}
6968
6969ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006970BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006971 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006972 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006973}
6974
6975ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006976BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006977 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006978 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006979}
6980
Juergen Ributzka73844052014-01-13 20:51:35 +00006981bool BuildVectorSDNode::isConstant() const {
6982 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6983 unsigned Opc = getOperand(i).getOpcode();
6984 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6985 return false;
6986 }
6987 return true;
6988}
6989
Owen Anderson53aa7a92009-08-10 22:56:29 +00006990bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006991 // Find the first non-undef value in the shuffle mask.
6992 unsigned i, e;
6993 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6994 /* search */;
6995
Nate Begeman39b59db2009-04-29 18:13:31 +00006996 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006997
Nate Begeman5f829d82009-04-29 05:20:52 +00006998 // Make sure all remaining elements are either undef or the same as the first
6999 // non-undef value.
7000 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007001 if (Mask[i] >= 0 && Mask[i] != Idx)
7002 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007003 return true;
7004}
David Greene09851602010-01-20 20:13:31 +00007005
Adam Nemetb4690e32014-05-31 16:23:20 +00007006#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00007007static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00007008 SmallPtrSetImpl<const SDNode*> &Visited,
7009 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00007010 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007011 // If this node has already been checked, don't check it again.
7012 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00007013 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00007014
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007015 // If a node has already been visited on this depth-first walk, reject it as
7016 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00007017 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007018 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00007019 dbgs() << "Offending node:\n";
7020 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007021 abort();
David Greene09851602010-01-20 20:13:31 +00007022 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007023
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007024 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00007025 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00007026
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007027 Checked.insert(N);
7028 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00007029}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007030#endif
David Greene09851602010-01-20 20:13:31 +00007031
Adam Nemet7d394302014-05-31 16:23:17 +00007032void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00007033 const llvm::SelectionDAG *DAG,
7034 bool force) {
7035#ifndef NDEBUG
7036 bool check = force;
David Greene09851602010-01-20 20:13:31 +00007037#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00007038 check = true;
7039#endif // XDEBUG
7040 if (check) {
7041 assert(N && "Checking nonexistent SDNode");
7042 SmallPtrSet<const SDNode*, 32> visited;
7043 SmallPtrSet<const SDNode*, 32> checked;
7044 checkForCyclesHelper(N, visited, checked, DAG);
7045 }
7046#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00007047}
7048
Adam Nemetb4690e32014-05-31 16:23:20 +00007049void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7050 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00007051}