blob: 2afde5fbff6fab32abe20c54c1f68288492fa323 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendling6de55a02009-12-21 19:34:59 +000013
Chris Lattner600d3082003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng00fd0b62010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
Bill Wendlingbd092622008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetIntrinsicInfo.h"
44#include "llvm/Target/TargetLowering.h"
45#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetOptions.h"
47#include "llvm/Target/TargetRegisterInfo.h"
48#include "llvm/Target/TargetSelectionDAGInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000049#include "llvm/Target/TargetSubtargetInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000050#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000051#include <cmath>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000052
Chris Lattner560b5e42004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000054
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000065
Jim Laskeyd66e6162005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000076}
77
Owen Anderson53aa7a92009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Dale Johannesend246b2c2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000089}
90
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000094
Evan Chengc70e33c2006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000098 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +000099 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Evan Chengc70e33c2006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000105
Chris Lattnerc2d28112006-03-25 22:57:01 +0000106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000109
Chris Lattnerc2d28112006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000112
Chris Lattnerc2d28112006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000114 // elements. We have to be a bit careful here, as the type of the constant
115 // may not be the same as the type of the vector elements due to type
116 // legalization (the elements are promoted to a legal type for the target and
117 // a vector of a type may be legal when the base element type is not).
118 // We only want to check enough bits to cover the vector elements, because
119 // we care if the resultant vector is all ones, not whether the individual
120 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000125 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000128 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000129 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Chris Lattnerc2d28112006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000133 // undefs. Even with the above element type twiddling, this should be OK, as
134 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Chenga6789912006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000146 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +0000147 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chenga6789912006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Kevin Qin93d45ec2014-06-24 05:37:27 +0000152 bool IsAllUndef = true;
153 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
154 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
155 continue;
156 IsAllUndef = false;
157 // Do not accept build_vectors that aren't all constants or which have non-0
158 // elements. We have to be a bit careful here, as the type of the constant
159 // may not be the same as the type of the vector elements due to type
160 // legalization (the elements are promoted to a legal type for the target
161 // and a vector of a type may be legal when the base element type is not).
162 // We only want to check enough bits to cover the vector elements, because
163 // we care if the resultant vector is all zeros, not whether the individual
164 // constants are.
165 SDValue Zero = N->getOperand(i);
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
168 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
169 return false;
170 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
171 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
172 return false;
173 } else
174 return false;
175 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000176
Evan Chengc70e33c2006-03-27 06:58:47 +0000177 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000178 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000179 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000180 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000181}
182
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000183/// \brief Return true if the specified node is a BUILD_VECTOR node of
184/// all ConstantSDNode or undef.
185bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
186 if (N->getOpcode() != ISD::BUILD_VECTOR)
187 return false;
188
189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
190 SDValue Op = N->getOperand(i);
191 if (Op.getOpcode() == ISD::UNDEF)
192 continue;
193 if (!isa<ConstantSDNode>(Op))
194 return false;
195 }
196 return true;
197}
198
Simon Pilgrim09f3ff92015-03-25 22:30:31 +0000199/// \brief Return true if the specified node is a BUILD_VECTOR node of
200/// all ConstantFPSDNode or undef.
201bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
202 if (N->getOpcode() != ISD::BUILD_VECTOR)
203 return false;
204
205 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
206 SDValue Op = N->getOperand(i);
207 if (Op.getOpcode() == ISD::UNDEF)
208 continue;
209 if (!isa<ConstantFPSDNode>(Op))
210 return false;
211 }
212 return true;
213}
214
Evan Cheng6200c222008-02-18 23:04:32 +0000215/// isScalarToVector - Return true if the specified node is a
216/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
217/// element is not an undef.
218bool ISD::isScalarToVector(const SDNode *N) {
219 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
220 return true;
221
222 if (N->getOpcode() != ISD::BUILD_VECTOR)
223 return false;
224 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
225 return false;
226 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000227 if (NumElems == 1)
228 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000229 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000230 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000231 if (V.getOpcode() != ISD::UNDEF)
232 return false;
233 }
234 return true;
235}
236
Nadav Rotema62368c2012-07-15 08:38:23 +0000237/// allOperandsUndef - Return true if the node has at least one operand
238/// and all operands of the specified node are ISD::UNDEF.
239bool ISD::allOperandsUndef(const SDNode *N) {
240 // Return false if the node has no operands.
241 // This is "logically inconsistent" with the definition of "all" but
242 // is probably the desired behavior.
243 if (N->getNumOperands() == 0)
244 return false;
245
246 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
247 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
248 return false;
249
250 return true;
251}
252
Matt Arsenaultbd223422015-01-14 01:35:17 +0000253ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000254 switch (ExtType) {
255 case ISD::EXTLOAD:
Matt Arsenaultbd223422015-01-14 01:35:17 +0000256 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000257 case ISD::SEXTLOAD:
258 return ISD::SIGN_EXTEND;
259 case ISD::ZEXTLOAD:
260 return ISD::ZERO_EXTEND;
261 default:
262 break;
263 }
264
265 llvm_unreachable("Invalid LoadExtType");
266}
267
Chris Lattner061a1ea2005-01-07 07:46:32 +0000268/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
269/// when given the operation for (X op Y).
270ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
271 // To perform this operation, we just need to swap the L and G bits of the
272 // operation.
273 unsigned OldL = (Operation >> 2) & 1;
274 unsigned OldG = (Operation >> 1) & 1;
275 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
276 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000277 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000278}
279
280/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
281/// 'op' is a valid SetCC operation.
282ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
283 unsigned Operation = Op;
284 if (isInteger)
285 Operation ^= 7; // Flip L, G, E bits, but not U.
286 else
287 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000288
Chris Lattner061a1ea2005-01-07 07:46:32 +0000289 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000290 Operation &= ~8; // Don't let N and U bits get set.
291
Chris Lattner061a1ea2005-01-07 07:46:32 +0000292 return ISD::CondCode(Operation);
293}
294
295
296/// isSignedOp - For an integer comparison, return 1 if the comparison is a
297/// signed operation and 2 if the result is an unsigned comparison. Return zero
298/// if the operation does not depend on the sign of the input (setne and seteq).
299static int isSignedOp(ISD::CondCode Opcode) {
300 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000301 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000302 case ISD::SETEQ:
303 case ISD::SETNE: return 0;
304 case ISD::SETLT:
305 case ISD::SETLE:
306 case ISD::SETGT:
307 case ISD::SETGE: return 1;
308 case ISD::SETULT:
309 case ISD::SETULE:
310 case ISD::SETUGT:
311 case ISD::SETUGE: return 2;
312 }
313}
314
315/// getSetCCOrOperation - Return the result of a logical OR between different
316/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
317/// returns SETCC_INVALID if it is not possible to represent the resultant
318/// comparison.
319ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
320 bool isInteger) {
321 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
322 // Cannot fold a signed integer setcc with an unsigned integer setcc.
323 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000324
Chris Lattner061a1ea2005-01-07 07:46:32 +0000325 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000326
Chris Lattner061a1ea2005-01-07 07:46:32 +0000327 // If the N and U bits get set then the resultant comparison DOES suddenly
328 // care about orderedness, and is true when ordered.
329 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000330 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000331
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000332 // Canonicalize illegal integer setcc's.
333 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
334 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Chris Lattner061a1ea2005-01-07 07:46:32 +0000336 return ISD::CondCode(Op);
337}
338
339/// getSetCCAndOperation - Return the result of a logical AND between different
340/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
341/// function returns zero if it is not possible to represent the resultant
342/// comparison.
343ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
344 bool isInteger) {
345 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
346 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000347 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000348
349 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000350 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000351
Chris Lattner393d96a2006-04-27 05:01:07 +0000352 // Canonicalize illegal integer setcc's.
353 if (isInteger) {
354 switch (Result) {
355 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000356 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000357 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000358 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
359 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
360 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000361 }
362 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000363
Chris Lattner393d96a2006-04-27 05:01:07 +0000364 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000365}
366
Jim Laskeyd66e6162005-08-17 20:08:02 +0000367//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000368// SDNode Profile Support
369//===----------------------------------------------------------------------===//
370
Jim Laskeybd0f0882006-10-27 23:52:51 +0000371/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
372///
Jim Laskeyf576b422006-10-27 23:46:08 +0000373static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
374 ID.AddInteger(OpC);
375}
376
377/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
378/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000379static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000380 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000381}
382
Jim Laskeybd0f0882006-10-27 23:52:51 +0000383/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
384///
Jim Laskeyf576b422006-10-27 23:46:08 +0000385static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000386 ArrayRef<SDValue> Ops) {
387 for (auto& Op : Ops) {
388 ID.AddPointer(Op.getNode());
389 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000390 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000391}
392
Dan Gohman768f2c92008-07-07 18:26:29 +0000393/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
394///
395static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000396 ArrayRef<SDUse> Ops) {
397 for (auto& Op : Ops) {
398 ID.AddPointer(Op.getNode());
399 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000400 }
401}
402
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000403static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
404 bool exact) {
405 ID.AddBoolean(nuw);
406 ID.AddBoolean(nsw);
407 ID.AddBoolean(exact);
408}
409
410/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
411static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
412 bool nuw, bool nsw, bool exact) {
413 if (isBinOpWithFlags(Opcode))
414 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
415}
416
Craig Topper633d99b2014-04-27 23:22:43 +0000417static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
418 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000419 AddNodeIDOpcode(ID, OpC);
420 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000421 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000422}
423
Duncan Sands835bdca2008-10-27 15:30:53 +0000424/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
425/// the NodeID data.
426static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000427 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000428 case ISD::TargetExternalSymbol:
429 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000430 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000431 default: break; // Normal nodes don't need extra info.
432 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000433 case ISD::Constant: {
434 const ConstantSDNode *C = cast<ConstantSDNode>(N);
435 ID.AddPointer(C->getConstantIntValue());
436 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000438 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000439 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000440 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000441 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000442 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000443 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000444 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000445 case ISD::GlobalAddress:
446 case ISD::TargetGlobalTLSAddress:
447 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000448 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000449 ID.AddPointer(GA->getGlobal());
450 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000451 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000452 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000453 break;
454 }
455 case ISD::BasicBlock:
456 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
457 break;
458 case ISD::Register:
459 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
460 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000461 case ISD::RegisterMask:
462 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
463 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000464 case ISD::SRCVALUE:
465 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
466 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000467 case ISD::FrameIndex:
468 case ISD::TargetFrameIndex:
469 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
470 break;
471 case ISD::JumpTable:
472 case ISD::TargetJumpTable:
473 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000474 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000475 break;
476 case ISD::ConstantPool:
477 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000478 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000479 ID.AddInteger(CP->getAlignment());
480 ID.AddInteger(CP->getOffset());
481 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000482 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000483 else
484 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000485 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000486 break;
487 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000488 case ISD::TargetIndex: {
489 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
490 ID.AddInteger(TI->getIndex());
491 ID.AddInteger(TI->getOffset());
492 ID.AddInteger(TI->getTargetFlags());
493 break;
494 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000495 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000496 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000497 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000498 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000499 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000500 break;
501 }
502 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000503 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000504 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000505 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000506 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000507 break;
508 }
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000509 case ISD::SDIV:
510 case ISD::UDIV:
511 case ISD::SRA:
512 case ISD::SRL:
513 case ISD::MUL:
514 case ISD::ADD:
515 case ISD::SUB:
516 case ISD::SHL: {
517 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
Sanjay Patelba558042015-04-28 16:39:12 +0000518 AddBinaryNodeIDCustom(ID, N->getOpcode(),
519 BinNode->Flags.hasNoUnsignedWrap(),
520 BinNode->Flags.hasNoSignedWrap(),
521 BinNode->Flags.hasExact());
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000522 break;
523 }
Dan Gohman12f24902008-12-23 21:37:04 +0000524 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000525 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000526 case ISD::ATOMIC_SWAP:
527 case ISD::ATOMIC_LOAD_ADD:
528 case ISD::ATOMIC_LOAD_SUB:
529 case ISD::ATOMIC_LOAD_AND:
530 case ISD::ATOMIC_LOAD_OR:
531 case ISD::ATOMIC_LOAD_XOR:
532 case ISD::ATOMIC_LOAD_NAND:
533 case ISD::ATOMIC_LOAD_MIN:
534 case ISD::ATOMIC_LOAD_MAX:
535 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000536 case ISD::ATOMIC_LOAD_UMAX:
537 case ISD::ATOMIC_LOAD:
538 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000539 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000540 ID.AddInteger(AT->getMemoryVT().getRawBits());
541 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000542 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
543 break;
544 }
545 case ISD::PREFETCH: {
546 const MemSDNode *PF = cast<MemSDNode>(N);
547 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000548 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000549 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000550 case ISD::VECTOR_SHUFFLE: {
551 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000552 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000553 i != e; ++i)
554 ID.AddInteger(SVN->getMaskElt(i));
555 break;
556 }
Dan Gohman6c938802009-10-30 01:27:03 +0000557 case ISD::TargetBlockAddress:
558 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000559 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
560 ID.AddPointer(BA->getBlockAddress());
561 ID.AddInteger(BA->getOffset());
562 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000563 break;
564 }
Mon P Wang6a490372008-06-25 08:15:39 +0000565 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000566
567 // Target specific memory nodes could also have address spaces to check.
568 if (N->isTargetMemoryOpcode())
569 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000570}
571
Duncan Sands835bdca2008-10-27 15:30:53 +0000572/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
573/// data.
574static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
575 AddNodeIDOpcode(ID, N->getOpcode());
576 // Add the return value info.
577 AddNodeIDValueTypes(ID, N->getVTList());
578 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000579 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000580
581 // Handle SDNode leafs with special info.
582 AddNodeIDCustom(ID, N);
583}
584
Dan Gohman2da2bed2008-08-20 15:58:01 +0000585/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000586/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000587/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000588///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000589static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000590encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000591 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000592 assert((ConvType & 3) == ConvType &&
593 "ConvType may not require more than 2 bits!");
594 assert((AM & 7) == AM &&
595 "AM may not require more than 3 bits!");
596 return ConvType |
597 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000598 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000599 (isNonTemporal << 6) |
600 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000601}
602
Jim Laskeyf576b422006-10-27 23:46:08 +0000603//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000604// SelectionDAG Class
605//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000606
Duncan Sands835bdca2008-10-27 15:30:53 +0000607/// doNotCSE - Return true if CSE should not be performed for this node.
608static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000609 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000610 return true; // Never CSE anything that produces a flag.
611
612 switch (N->getOpcode()) {
613 default: break;
614 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000615 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000616 return true; // Never CSE these nodes.
617 }
618
619 // Check that remaining values produced are not flags.
620 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000621 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000622 return true; // Never CSE anything that produces a flag.
623
624 return false;
625}
626
Chris Lattner9c667932005-01-07 21:09:16 +0000627/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000628/// SelectionDAG.
629void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000630 // Create a dummy node (which is not added to allnodes), that adds a reference
631 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000632 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000633
Chris Lattner8927c872006-08-04 17:45:20 +0000634 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000635
Chris Lattner8927c872006-08-04 17:45:20 +0000636 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000637 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000638 if (I->use_empty())
639 DeadNodes.push_back(I);
640
Dan Gohman91697632008-07-07 20:57:48 +0000641 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000642
Dan Gohman91697632008-07-07 20:57:48 +0000643 // If the root changed (e.g. it was a dead load, update the root).
644 setRoot(Dummy.getValue());
645}
646
647/// RemoveDeadNodes - This method deletes the unreachable nodes in the
648/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000649void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000650
Chris Lattner8927c872006-08-04 17:45:20 +0000651 // Process the worklist, deleting the nodes and adding their uses to the
652 // worklist.
653 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000654 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000655
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000656 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000657 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000658
Chris Lattner8927c872006-08-04 17:45:20 +0000659 // Take the node out of the appropriate CSE map.
660 RemoveNodeFromCSEMaps(N);
661
662 // Next, brutally remove the operand list. This is safe to do, as there are
663 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000664 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
665 SDUse &Use = *I++;
666 SDNode *Operand = Use.getNode();
667 Use.set(SDValue());
668
Chris Lattner8927c872006-08-04 17:45:20 +0000669 // Now that we removed this operand, see if there are no uses of it left.
670 if (Operand->use_empty())
671 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000672 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000673
Dan Gohman534c8a22009-01-19 22:39:36 +0000674 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000675 }
Chris Lattner9c667932005-01-07 21:09:16 +0000676}
677
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000678void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000679 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000680
681 // Create a dummy node that adds a reference to the root node, preventing
682 // it from being deleted. (This matters if the root is an operand of the
683 // dead node.)
684 HandleSDNode Dummy(getRoot());
685
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000686 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000687}
688
Chris Lattnerc738d002005-08-29 21:59:31 +0000689void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000690 // First take this out of the appropriate CSE map.
691 RemoveNodeFromCSEMaps(N);
692
Scott Michelcf0da6c2009-02-17 22:15:04 +0000693 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000694 // AllNodes list, and delete the node.
695 DeleteNodeNotInCSEMaps(N);
696}
697
698void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000699 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
700 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000701
Dan Gohman86aa16a2008-09-30 18:30:35 +0000702 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000703 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000704
Dan Gohman534c8a22009-01-19 22:39:36 +0000705 DeallocateNode(N);
706}
707
Frederic Riss8ad4f492014-11-11 21:21:08 +0000708void SDDbgInfo::erase(const SDNode *Node) {
709 DbgValMapType::iterator I = DbgValMap.find(Node);
710 if (I == DbgValMap.end())
711 return;
712 for (auto &Val: I->second)
713 Val->setIsInvalidated();
714 DbgValMap.erase(I);
715}
716
Dan Gohman534c8a22009-01-19 22:39:36 +0000717void SelectionDAG::DeallocateNode(SDNode *N) {
718 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000719 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000720
Dan Gohman534c8a22009-01-19 22:39:36 +0000721 // Set the opcode to DELETED_NODE to help catch bugs when node
722 // memory is reallocated.
723 N->NodeType = ISD::DELETED_NODE;
724
Dan Gohman2e834902008-08-26 01:44:34 +0000725 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000726
Frederic Riss8ad4f492014-11-11 21:21:08 +0000727 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
728 // them and forget about that node.
729 DbgInfo->erase(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000730}
731
Chandler Carruth41b20e72014-07-22 04:07:55 +0000732#ifndef NDEBUG
733/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
734static void VerifySDNode(SDNode *N) {
735 switch (N->getOpcode()) {
736 default:
737 break;
738 case ISD::BUILD_PAIR: {
739 EVT VT = N->getValueType(0);
740 assert(N->getNumValues() == 1 && "Too many results!");
741 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
742 "Wrong return type!");
743 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
744 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
745 "Mismatched operand types!");
746 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
747 "Wrong operand type!");
748 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
749 "Wrong return type size");
750 break;
751 }
752 case ISD::BUILD_VECTOR: {
753 assert(N->getNumValues() == 1 && "Too many results!");
754 assert(N->getValueType(0).isVector() && "Wrong return type!");
755 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
756 "Wrong number of operands!");
757 EVT EltVT = N->getValueType(0).getVectorElementType();
758 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
759 assert((I->getValueType() == EltVT ||
760 (EltVT.isInteger() && I->getValueType().isInteger() &&
761 EltVT.bitsLE(I->getValueType()))) &&
762 "Wrong operand type!");
763 assert(I->getValueType() == N->getOperand(0).getValueType() &&
764 "Operands must all have the same type");
765 }
766 break;
767 }
768 }
769}
770#endif // NDEBUG
771
772/// \brief Insert a newly allocated node into the DAG.
773///
774/// Handles insertion into the all nodes list and CSE map, as well as
775/// verification and other common operations when a new node is allocated.
776void SelectionDAG::InsertNode(SDNode *N) {
777 AllNodes.push_back(N);
778#ifndef NDEBUG
779 VerifySDNode(N);
780#endif
781}
782
Chris Lattner19732782005-08-16 18:17:10 +0000783/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
784/// correspond to it. This is useful when we're about to delete or repurpose
785/// the node. We don't want future request for structurally identical nodes
786/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000787bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000788 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000789 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000790 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000791 case ISD::CONDCODE:
792 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
793 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000794 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
795 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000796 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000797 case ISD::ExternalSymbol:
798 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000799 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000800 case ISD::TargetExternalSymbol: {
801 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
802 Erased = TargetExternalSymbols.erase(
803 std::pair<std::string,unsigned char>(ESN->getSymbol(),
804 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000805 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000806 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000807 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000808 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000809 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000810 Erased = ExtendedValueTypeNodes.erase(VT);
811 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000812 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
813 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000814 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000815 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000816 }
Chris Lattner9c667932005-01-07 21:09:16 +0000817 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000818 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000819 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
820 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000821 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000822 break;
823 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000824#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000825 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000826 // flag result (which cannot be CSE'd) or is one of the special cases that are
827 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000828 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000829 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000830 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000831 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000832 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000833 }
834#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000835 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000836}
837
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000838/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
839/// maps and modified in place. Add it back to the CSE maps, unless an identical
840/// node already exists, in which case transfer all its users to the existing
841/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000842///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000843void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000844SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000845 // For node types that aren't CSE'd, just act as if no identical node
846 // already exists.
847 if (!doNotCSE(N)) {
848 SDNode *Existing = CSEMap.GetOrInsertNode(N);
849 if (Existing != N) {
850 // If there was already an existing matching node, use ReplaceAllUsesWith
851 // to replace the dead one with the existing one. This can cause
852 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000853 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000854
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000855 // N is now dead. Inform the listeners and delete it.
856 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
857 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000858 DeleteNodeNotInCSEMaps(N);
859 return;
860 }
861 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000862
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000863 // If the node doesn't already exist, we updated it. Inform listeners.
864 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
865 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000866}
867
Chris Lattnerf34156e2006-01-28 09:32:45 +0000868/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000869/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000870/// return null, otherwise return a pointer to the slot it would take. If a
871/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000872SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000873 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000874 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000875 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000876
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000877 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000878 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000879 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000880 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000881 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000882 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000883}
884
885/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000886/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000887/// return null, otherwise return a pointer to the slot it would take. If a
888/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000889SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000890 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000891 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000892 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000893 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000894
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000895 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000896 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000897 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000898 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000899 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000900 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000901}
902
903
904/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000905/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000906/// return null, otherwise return a pointer to the slot it would take. If a
907/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000908SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000909 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000910 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000911 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000912
Jim Laskeyf576b422006-10-27 23:46:08 +0000913 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000914 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000915 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000916 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000917 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000918}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000919
Owen Anderson53aa7a92009-08-10 22:56:29 +0000920/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000921/// given type.
922///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000923unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000924 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000925 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000926 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000927
Eric Christopher1e845f22014-10-08 21:08:32 +0000928 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000929}
Chris Lattner9c667932005-01-07 21:09:16 +0000930
Dale Johannesen8ba713212009-02-07 02:15:05 +0000931// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000932SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000933 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000934 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
935 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
936 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000937 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000938 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000939}
940
Eric Christopher8d07f442014-10-08 01:57:58 +0000941void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000942 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000943 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000944 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000945 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000946}
947
Chris Lattner600d3082003-08-11 14:57:33 +0000948SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000949 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000950 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000951 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000952}
953
954void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000955 assert(&*AllNodes.begin() == &EntryNode);
956 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000957 while (!AllNodes.empty())
958 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000959}
960
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000961BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
962 SDVTList VTs, SDValue N1,
963 SDValue N2, bool nuw, bool nsw,
964 bool exact) {
965 if (isBinOpWithFlags(Opcode)) {
966 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
967 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
Sanjay Patelba558042015-04-28 16:39:12 +0000968 FN->Flags.setNoUnsignedWrap(nuw);
969 FN->Flags.setNoSignedWrap(nsw);
970 FN->Flags.setExact(exact);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000971
972 return FN;
973 }
974
975 BinarySDNode *N = new (NodeAllocator)
976 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
977 return N;
978}
979
Dan Gohmane1a9a782008-08-27 23:52:12 +0000980void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000981 allnodes_clear();
982 OperandAllocator.Reset();
983 CSEMap.clear();
984
985 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000986 ExternalSymbols.clear();
987 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000988 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000989 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000990 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000991 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000992
Craig Topperc0196b12014-04-14 00:51:57 +0000993 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000994 AllNodes.push_back(&EntryNode);
995 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000996 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000997}
998
Andrew Trickef9de2a2013-05-25 02:42:55 +0000999SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +00001000 return VT.bitsGT(Op.getValueType()) ?
1001 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1002 getNode(ISD::TRUNCATE, DL, VT, Op);
1003}
1004
Andrew Trickef9de2a2013-05-25 02:42:55 +00001005SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001006 return VT.bitsGT(Op.getValueType()) ?
1007 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1008 getNode(ISD::TRUNCATE, DL, VT, Op);
1009}
1010
Andrew Trickef9de2a2013-05-25 02:42:55 +00001011SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001012 return VT.bitsGT(Op.getValueType()) ?
1013 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1014 getNode(ISD::TRUNCATE, DL, VT, Op);
1015}
1016
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001017SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1018 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001019 if (VT.bitsLE(Op.getValueType()))
1020 return getNode(ISD::TRUNCATE, SL, VT, Op);
1021
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001022 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001023 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1024}
1025
Andrew Trickef9de2a2013-05-25 02:42:55 +00001026SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001027 assert(!VT.isVector() &&
1028 "getZeroExtendInReg should use the vector element type instead of "
1029 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001030 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001031 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1032 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001033 VT.getSizeInBits());
1034 return getNode(ISD::AND, DL, Op.getValueType(), Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001035 getConstant(Imm, DL, Op.getValueType()));
Bill Wendlingc4093182009-01-30 22:23:15 +00001036}
1037
Chandler Carruth0b666e02014-07-10 12:32:32 +00001038SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1039 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1040 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1041 "The sizes of the input and result must match in order to perform the "
1042 "extend in-register.");
1043 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1044 "The destination vector type must have fewer lanes than the input.");
1045 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1046}
1047
1048SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1049 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1050 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1051 "The sizes of the input and result must match in order to perform the "
1052 "extend in-register.");
1053 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1054 "The destination vector type must have fewer lanes than the input.");
1055 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1056}
1057
Chandler Carruthafe4b252014-07-09 10:58:18 +00001058SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1059 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001060 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1061 "The sizes of the input and result must match in order to perform the "
1062 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001063 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1064 "The destination vector type must have fewer lanes than the input.");
1065 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1066}
1067
Bob Wilsonc5890052009-01-22 17:39:32 +00001068/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1069///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001070SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001071 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001072 SDValue NegOne =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001073 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001074 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1075}
1076
Pete Cooper7fd1d722014-05-12 23:26:58 +00001077SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1078 EVT EltVT = VT.getScalarType();
1079 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001080 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001081 case TargetLowering::ZeroOrOneBooleanContent:
1082 case TargetLowering::UndefinedBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001083 TrueValue = getConstant(1, DL, VT);
Pete Cooper7fd1d722014-05-12 23:26:58 +00001084 break;
1085 case TargetLowering::ZeroOrNegativeOneBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001086 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
Pete Cooper7fd1d722014-05-12 23:26:58 +00001087 VT);
1088 break;
1089 }
1090 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1091}
1092
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001093SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT,
1094 bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001095 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001096 assert((EltVT.getSizeInBits() >= 64 ||
1097 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1098 "getConstant with a uint64_t value that doesn't fit in the type!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001099 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001100}
1101
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001102SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT,
1103 bool isO)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001104{
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001105 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001106}
1107
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001108SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT,
1109 bool isT, bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001110 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001111
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001112 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001113 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001114
Duncan Sandsf2641e12011-09-06 19:07:46 +00001115 // In some cases the vector type is legal but the element type is illegal and
1116 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1117 // inserted value (the type does not need to match the vector element type).
1118 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001119 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001120 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001121 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001122 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1123 Elt = ConstantInt::get(*getContext(), NewVal);
1124 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001125 // In other cases the element type is illegal and needs to be expanded, for
1126 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1127 // the value into n parts and use a vector type with n-times the elements.
1128 // Then bitcast to the type requested.
1129 // Legalizing constants too early makes the DAGCombiner's job harder so we
1130 // only legalize if the DAG tells us we must produce legal types.
1131 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1132 TLI->getTypeAction(*getContext(), EltVT) ==
1133 TargetLowering::TypeExpandInteger) {
1134 APInt NewVal = Elt->getValue();
1135 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1136 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1137 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1138 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1139
1140 // Check the temporary vector is the correct size. If this fails then
1141 // getTypeToTransformTo() probably returned a type whose size (in bits)
1142 // isn't a power-of-2 factor of the requested type size.
1143 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1144
1145 SmallVector<SDValue, 2> EltParts;
1146 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1147 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001148 .trunc(ViaEltSizeInBits), DL,
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001149 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001150 }
1151
1152 // EltParts is currently in little endian order. If we actually want
1153 // big-endian order then reverse it now.
1154 if (TLI->isBigEndian())
1155 std::reverse(EltParts.begin(), EltParts.end());
1156
1157 // The elements must be reversed when the element order is different
1158 // to the endianness of the elements (because the BITCAST is itself a
1159 // vector shuffle in this situation). However, we do not need any code to
1160 // perform this reversal because getConstant() is producing a vector
1161 // splat.
1162 // This situation occurs in MIPS MSA.
1163
1164 SmallVector<SDValue, 8> Ops;
1165 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1166 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1167
1168 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1169 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001170 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001171 return Result;
1172 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001173
1174 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1175 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001176 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001177 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001178 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001179 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001180 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001181 void *IP = nullptr;
1182 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001183 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001184 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001185 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001186
Dan Gohman7a7742c2007-12-12 22:21:26 +00001187 if (!N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001188 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, DL.getDebugLoc(),
1189 EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001190 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001191 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001192 }
1193
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001194 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001195 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001196 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001197 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001198 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001199 }
1200 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001201}
1202
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001203SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) {
1204 return getConstant(Val, DL, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001205}
1206
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001207SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT,
1208 bool isTarget) {
1209 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001210}
1211
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001212SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT,
1213 bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001214 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001215
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001216 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001217
Chris Lattner381dddc2005-02-17 20:17:32 +00001218 // Do the map lookup using the actual bit pattern for the floating point
1219 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1220 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001221 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001222 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001223 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001224 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001225 void *IP = nullptr;
1226 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001227 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001228 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001229 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001230
Evan Cheng9458e6a2007-06-29 21:36:04 +00001231 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001232 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001233 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001234 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001235 }
1236
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001237 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001238 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001239 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001240 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001241 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001242 }
1243 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001244}
1245
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001246SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT,
1247 bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001248 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001249 if (EltVT==MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001250 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001251 else if (EltVT==MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001252 return getConstantFP(APFloat(Val), DL, VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001253 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1254 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001255 bool ignored;
1256 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001257 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001258 &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001259 return getConstantFP(apf, DL, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001260 } else
1261 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001262}
1263
Andrew Trickef9de2a2013-05-25 02:42:55 +00001264SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001265 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001266 bool isTargetGA,
1267 unsigned char TargetFlags) {
1268 assert((TargetFlags == 0 || isTargetGA) &&
1269 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001270
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001271 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001272 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001273 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001274 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001275
Chris Lattner8e34f982009-06-25 21:21:14 +00001276 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001277 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001278 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1279 else
1280 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001281
Jim Laskeyf576b422006-10-27 23:46:08 +00001282 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001283 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001284 ID.AddPointer(GV);
1285 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001286 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001287 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001288 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001289 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001290 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001291
Andrew Trickef9de2a2013-05-25 02:42:55 +00001292 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1293 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001294 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001295 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001296 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001297 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001298}
1299
Owen Anderson53aa7a92009-08-10 22:56:29 +00001300SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001301 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001302 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001303 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001304 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001305 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001306 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001307 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001308
Dan Gohman01c65a22010-03-18 18:49:47 +00001309 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001310 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001311 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001312 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001313}
1314
Owen Anderson53aa7a92009-08-10 22:56:29 +00001315SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001316 unsigned char TargetFlags) {
1317 assert((TargetFlags == 0 || isTarget) &&
1318 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001319 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001320 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001321 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001322 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001323 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001324 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001325 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001326 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001327
Dan Gohman01c65a22010-03-18 18:49:47 +00001328 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1329 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001330 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001331 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001332 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001333}
1334
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001335SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001336 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001337 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001338 unsigned char TargetFlags) {
1339 assert((TargetFlags == 0 || isTarget) &&
1340 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001341 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001342 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001343 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001344 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001345 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001346 ID.AddInteger(Alignment);
1347 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001348 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001349 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001350 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001351 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001352 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001353
Dan Gohman01c65a22010-03-18 18:49:47 +00001354 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1355 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001356 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001357 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001358 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001359}
1360
Chris Lattner061a1ea2005-01-07 07:46:32 +00001361
Owen Anderson53aa7a92009-08-10 22:56:29 +00001362SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001363 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001364 bool isTarget,
1365 unsigned char TargetFlags) {
1366 assert((TargetFlags == 0 || isTarget) &&
1367 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001368 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001369 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001370 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001371 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001372 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001373 ID.AddInteger(Alignment);
1374 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001375 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001376 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001377 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001378 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001379 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001380
Dan Gohman01c65a22010-03-18 18:49:47 +00001381 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1382 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001383 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001384 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001385 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001386}
1387
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001388SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1389 unsigned char TargetFlags) {
1390 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001391 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001392 ID.AddInteger(Index);
1393 ID.AddInteger(Offset);
1394 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001395 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001396 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1397 return SDValue(E, 0);
1398
1399 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1400 TargetFlags);
1401 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001402 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001403 return SDValue(N, 0);
1404}
1405
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001406SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001407 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001408 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001409 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001410 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001411 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001412 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001413
Dan Gohman01c65a22010-03-18 18:49:47 +00001414 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001415 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001416 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001417 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001418}
1419
Owen Anderson53aa7a92009-08-10 22:56:29 +00001420SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001421 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1422 ValueTypeNodes.size())
1423 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001424
Duncan Sands13237ac2008-06-06 12:08:01 +00001425 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001426 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001427
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001428 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001429 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001430 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001431 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001432}
1433
Owen Anderson53aa7a92009-08-10 22:56:29 +00001434SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001435 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001436 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001437 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001438 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001439 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001440}
1441
Owen Anderson53aa7a92009-08-10 22:56:29 +00001442SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001443 unsigned char TargetFlags) {
1444 SDNode *&N =
1445 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1446 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001447 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001448 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001449 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001450 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001451}
1452
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001453SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001454 if ((unsigned)Cond >= CondCodeNodes.size())
1455 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001456
Craig Topperc0196b12014-04-14 00:51:57 +00001457 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001458 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001459 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001460 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001461 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001462
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001463 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001464}
1465
Nate Begeman5f829d82009-04-29 05:20:52 +00001466// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1467// the shuffle mask M that point at N1 to point at N2, and indices that point
1468// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001469static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1470 std::swap(N1, N2);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001471 ShuffleVectorSDNode::commuteMask(M);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001472}
1473
Andrew Trickef9de2a2013-05-25 02:42:55 +00001474SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001475 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001476 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1477 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001478
1479 // Canonicalize shuffle undef, undef -> undef
1480 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001481 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001482
Eric Christopherdfda92b2009-08-22 00:40:45 +00001483 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001484 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001485 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001486 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001487 for (unsigned i = 0; i != NElts; ++i) {
1488 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001489 MaskVec.push_back(Mask[i]);
1490 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001491
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001492 // Canonicalize shuffle v, v -> v, undef
1493 if (N1 == N2) {
1494 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001495 for (unsigned i = 0; i != NElts; ++i)
1496 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001497 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001498
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001499 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1500 if (N1.getOpcode() == ISD::UNDEF)
1501 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001502
Chandler Carruth1b5285d2015-02-15 12:18:12 +00001503 // If shuffling a splat, try to blend the splat instead. We do this here so
1504 // that even when this arises during lowering we don't have to re-handle it.
1505 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1506 BitVector UndefElements;
1507 SDValue Splat = BV->getSplatValue(&UndefElements);
1508 if (!Splat)
1509 return;
1510
1511 for (int i = 0; i < (int)NElts; ++i) {
1512 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1513 continue;
1514
1515 // If this input comes from undef, mark it as such.
1516 if (UndefElements[MaskVec[i] - Offset]) {
1517 MaskVec[i] = -1;
1518 continue;
1519 }
1520
1521 // If we can blend a non-undef lane, use that instead.
1522 if (!UndefElements[i])
1523 MaskVec[i] = i + Offset;
1524 }
1525 };
1526 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1527 BlendSplat(N1BV, 0);
1528 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1529 BlendSplat(N2BV, NElts);
1530
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001531 // Canonicalize all index into lhs, -> shuffle lhs, undef
1532 // Canonicalize all index into rhs, -> shuffle rhs, undef
1533 bool AllLHS = true, AllRHS = true;
1534 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001535 for (unsigned i = 0; i != NElts; ++i) {
1536 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001537 if (N2Undef)
1538 MaskVec[i] = -1;
1539 else
1540 AllLHS = false;
1541 } else if (MaskVec[i] >= 0) {
1542 AllRHS = false;
1543 }
1544 }
1545 if (AllLHS && AllRHS)
1546 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001547 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001548 N2 = getUNDEF(VT);
1549 if (AllRHS) {
1550 N1 = getUNDEF(VT);
1551 commuteShuffle(N1, N2, MaskVec);
1552 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001553 // Reset our undef status after accounting for the mask.
1554 N2Undef = N2.getOpcode() == ISD::UNDEF;
1555 // Re-check whether both sides ended up undef.
1556 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1557 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001558
Craig Topper9a39b072013-08-08 08:03:12 +00001559 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001560 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001561 for (unsigned i = 0; i != NElts; ++i) {
1562 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001563 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001564 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001565 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001566 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001567
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001568 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001569 if (N2Undef) {
1570 SDValue V = N1;
1571
1572 // Look through any bitcasts. We check that these don't change the number
1573 // (and size) of elements and just changes their types.
1574 while (V.getOpcode() == ISD::BITCAST)
1575 V = V->getOperand(0);
1576
1577 // A splat should always show up as a build vector node.
1578 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001579 BitVector UndefElements;
1580 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001581 // If this is a splat of an undef, shuffling it is also undef.
1582 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1583 return getUNDEF(VT);
1584
Andrea Di Biagio83814752015-01-24 11:54:29 +00001585 bool SameNumElts =
1586 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1587
Chandler Carruth142e9662014-07-08 08:45:38 +00001588 // We only have a splat which can skip shuffles if there is a splatted
1589 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001590 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001591 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1592 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001593 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001594 return N1;
1595 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1596 if (C->isNullValue())
1597 return N1;
1598 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001599
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001600 // If the shuffle itself creates a splat, build the vector directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001601 if (AllSame && SameNumElts) {
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001602 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1603 SmallVector<SDValue, 8> Ops(NElts, Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001604
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001605 EVT BuildVT = BV->getValueType(0);
1606 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001607
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001608 // We may have jumped through bitcasts, so the type of the
1609 // BUILD_VECTOR may not match the type of the shuffle.
1610 if (BuildVT != VT)
1611 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1612 return NewBV;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001613 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001614 }
1615 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001616
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001617 FoldingSetNodeID ID;
1618 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001619 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001620 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001621 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001622
Craig Topperc0196b12014-04-14 00:51:57 +00001623 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001625 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001626
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001627 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1628 // SDNode doesn't have access to it. This memory will be "leaked" when
1629 // the node is deallocated, but recovered when the NodeAllocator is released.
1630 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1631 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001632
Dan Gohman01c65a22010-03-18 18:49:47 +00001633 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001634 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1635 dl.getDebugLoc(), N1, N2,
1636 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001637 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001638 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001639 return SDValue(N, 0);
1640}
1641
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001642SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1643 MVT VT = SV.getSimpleValueType(0);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001644 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1645 ShuffleVectorSDNode::commuteMask(MaskVec);
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001646
1647 SDValue Op0 = SV.getOperand(0);
1648 SDValue Op1 = SV.getOperand(1);
1649 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1650}
1651
Andrew Trickef9de2a2013-05-25 02:42:55 +00001652SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001653 SDValue Val, SDValue DTy,
1654 SDValue STy, SDValue Rnd, SDValue Sat,
1655 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001656 // If the src and dest types are the same and the conversion is between
1657 // integer types of the same sign or two floats, no conversion is necessary.
1658 if (DTy == STy &&
1659 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001660 return Val;
1661
1662 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001663 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001664 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001665 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001666 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001667 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001668
Jack Carter170a5f22013-09-09 22:02:08 +00001669 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1670 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001671 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001672 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001673 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001674 return SDValue(N, 0);
1675}
1676
Owen Anderson53aa7a92009-08-10 22:56:29 +00001677SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001678 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001679 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001680 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001681 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001682 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001683 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001684
Dan Gohman01c65a22010-03-18 18:49:47 +00001685 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001686 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001687 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001688 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001689}
1690
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001691SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1692 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001693 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001694 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001695 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001696 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1697 return SDValue(E, 0);
1698
1699 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1700 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001701 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001702 return SDValue(N, 0);
1703}
1704
Andrew Trickef9de2a2013-05-25 02:42:55 +00001705SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001706 FoldingSetNodeID ID;
1707 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001708 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001709 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001710 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001711 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001712 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001713
Jack Carter170a5f22013-09-09 22:02:08 +00001714 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1715 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001716 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001717 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001718 return SDValue(N, 0);
1719}
1720
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001721
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001722SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001723 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001724 bool isTarget,
1725 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001726 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1727
1728 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001729 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001730 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001731 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001732 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001733 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001734 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001735 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001736
Michael Liaoabb87d42012-09-12 21:43:09 +00001737 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1738 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001739 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001740 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001741 return SDValue(N, 0);
1742}
1743
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001744SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001745 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001746 "SrcValue is not a pointer?");
1747
Jim Laskeyf576b422006-10-27 23:46:08 +00001748 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001749 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001750 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001751
Craig Topperc0196b12014-04-14 00:51:57 +00001752 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001753 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001754 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001755
Dan Gohman01c65a22010-03-18 18:49:47 +00001756 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001757 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001758 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001759 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001760}
1761
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001762/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1763SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1764 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001765 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001766 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001767
Craig Topperc0196b12014-04-14 00:51:57 +00001768 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001769 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1770 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001771
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001772 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1773 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001774 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001775 return SDValue(N, 0);
1776}
1777
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001778/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1779SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1780 unsigned SrcAS, unsigned DestAS) {
1781 SDValue Ops[] = {Ptr};
1782 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001783 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001784 ID.AddInteger(SrcAS);
1785 ID.AddInteger(DestAS);
1786
Craig Topperc0196b12014-04-14 00:51:57 +00001787 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001788 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1789 return SDValue(E, 0);
1790
1791 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1792 dl.getDebugLoc(),
1793 VT, Ptr, SrcAS, DestAS);
1794 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001795 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001796 return SDValue(N, 0);
1797}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001798
Duncan Sands41826032009-01-31 15:50:11 +00001799/// getShiftAmountOperand - Return the specified value casted to
1800/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001801SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001802 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001803 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001804 if (OpTy == ShTy || OpTy.isVector()) return Op;
1805
1806 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001807 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001808}
1809
Chris Lattner9eb7a822007-10-15 17:47:20 +00001810/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1811/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001812SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001813 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001814 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001815 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001816 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001817 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001818
David Greene1fbe0542009-11-12 20:49:22 +00001819 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001820 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001821}
1822
Duncan Sands445071c2008-12-09 21:33:20 +00001823/// CreateStackTemporary - Create a stack temporary suitable for holding
1824/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001825SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001826 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1827 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001828 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1829 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001830 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001831 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1832 TD->getPrefTypeAlignment(Ty2));
1833
1834 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001835 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001836 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001837}
1838
Owen Anderson53aa7a92009-08-10 22:56:29 +00001839SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001840 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001841 // These setcc operations always fold.
1842 switch (Cond) {
1843 default: break;
1844 case ISD::SETFALSE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001845 case ISD::SETFALSE2: return getConstant(0, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001846 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001847 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001848 TargetLowering::BooleanContent Cnt =
1849 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001850 return getConstant(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001851 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1852 VT);
Tim Northover950fcc02013-09-06 12:38:12 +00001853 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001854
Chris Lattner393d96a2006-04-27 05:01:07 +00001855 case ISD::SETOEQ:
1856 case ISD::SETOGT:
1857 case ISD::SETOGE:
1858 case ISD::SETOLT:
1859 case ISD::SETOLE:
1860 case ISD::SETONE:
1861 case ISD::SETO:
1862 case ISD::SETUO:
1863 case ISD::SETUEQ:
1864 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001865 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001866 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001867 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001868
Gabor Greiff304a7a2008-08-28 21:40:38 +00001869 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001870 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001871 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001872 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001873
Chris Lattner061a1ea2005-01-07 07:46:32 +00001874 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001875 default: llvm_unreachable("Unknown integer setcc!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001876 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT);
1877 case ISD::SETNE: return getConstant(C1 != C2, dl, VT);
1878 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1879 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1880 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1881 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1882 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT);
1883 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT);
1884 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT);
1885 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001886 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001887 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001888 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001889 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1890 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001891 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001892 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001893 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001894 case ISD::SETEQ: 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::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001898 case ISD::SETNE: 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
1901 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001902 R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001903 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001904 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001905 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001906 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001907 case ISD::SETGT: 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
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001910 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001911 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001912 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001913 // fall through
1914 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001915 R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001916 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001917 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001918 // fall through
1919 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001920 R==APFloat::cmpEqual, dl, VT);
1921 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1922 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001923 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001924 R==APFloat::cmpEqual, dl, VT);
1925 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001926 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001927 R==APFloat::cmpLessThan, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001928 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001929 R==APFloat::cmpUnordered, dl, VT);
1930 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1931 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001932 }
1933 } else {
1934 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001935 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1936 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001937 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001938 return SDValue();
1939
1940 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001941 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001942 }
1943
Chris Lattnerd47675e2005-08-09 20:20:18 +00001944 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001945 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001946}
1947
Dan Gohman1f372ed2008-02-25 21:11:39 +00001948/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1949/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001950bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001951 // This predicate is not safe for vector operations.
1952 if (Op.getValueType().isVector())
1953 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001954
Dan Gohman1d459e42009-12-11 21:31:27 +00001955 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001956 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1957}
1958
Dan Gohman309d3d52007-06-22 14:59:07 +00001959/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1960/// this predicate to simplify operations downstream. Mask is known to be zero
1961/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001962bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001963 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001964 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001965 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001966 return (KnownZero & Mask) == Mask;
1967}
1968
Jay Foada0653a32014-05-14 21:14:37 +00001969/// Determine which bits of Op are known to be either zero or one and return
1970/// them in the KnownZero/KnownOne bitsets.
1971void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1972 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001973 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001974
Dan Gohmanf990faf2008-02-13 00:35:47 +00001975 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001976 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001977 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001978
Dan Gohmanf990faf2008-02-13 00:35:47 +00001979 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001980
1981 switch (Op.getOpcode()) {
1982 case ISD::Constant:
1983 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001984 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1985 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001986 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001987 case ISD::AND:
1988 // If either the LHS or the RHS are Zero, the result is zero.
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);
Dan Gohman309d3d52007-06-22 14:59:07 +00001991
1992 // Output known-1 bits are only known if set in both the LHS & RHS.
1993 KnownOne &= KnownOne2;
1994 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1995 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001996 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001997 case ISD::OR:
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 only known if clear in both the LHS & RHS.
2002 KnownZero &= KnownZero2;
2003 // Output known-1 are known to be set if set in either the LHS | RHS.
2004 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00002005 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002006 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00002007 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2008 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002009
Dan Gohman309d3d52007-06-22 14:59:07 +00002010 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002011 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00002012 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2013 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2014 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00002015 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002016 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002017 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00002018 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2019 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002020
2021 // If low bits are zero in either operand, output low known-0 bits.
2022 // Also compute a conserative estimate for high known-0 bits.
2023 // More trickiness is possible, but this is sufficient for the
2024 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00002025 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00002026 unsigned TrailZ = KnownZero.countTrailingOnes() +
2027 KnownZero2.countTrailingOnes();
2028 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002029 KnownZero2.countLeadingOnes(),
2030 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002031
2032 TrailZ = std::min(TrailZ, BitWidth);
2033 LeadZ = std::min(LeadZ, BitWidth);
2034 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2035 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002036 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002037 }
2038 case ISD::UDIV: {
2039 // For the purposes of computing leading zeros we can conservatively
2040 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002041 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002042 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002043 unsigned LeadZ = KnownZero2.countLeadingOnes();
2044
Jay Foad25a5e4c2010-12-01 08:53:58 +00002045 KnownOne2.clearAllBits();
2046 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002047 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002048 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2049 if (RHSUnknownLeadingOnes != BitWidth)
2050 LeadZ = std::min(BitWidth,
2051 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002052
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002053 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002054 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002055 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002056 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002057 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2058 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002059
Dan Gohman309d3d52007-06-22 14:59:07 +00002060 // Only known if known in both the LHS and RHS.
2061 KnownOne &= KnownOne2;
2062 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002063 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002064 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002065 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2066 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002067
Dan Gohman309d3d52007-06-22 14:59:07 +00002068 // Only known if known in both the LHS and RHS.
2069 KnownOne &= KnownOne2;
2070 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002071 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002072 case ISD::SADDO:
2073 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002074 case ISD::SSUBO:
2075 case ISD::USUBO:
2076 case ISD::SMULO:
2077 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002078 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002079 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002080 // The boolean result conforms to getBooleanContents.
2081 // If we know the result of a setcc has the top bits zero, use this info.
2082 // We know that we have an integer-based boolean since these operations
2083 // are only available for integer.
2084 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2085 TargetLowering::ZeroOrOneBooleanContent &&
2086 BitWidth > 1)
2087 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2088 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002089 case ISD::SETCC:
2090 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002091 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2092 TargetLowering::ZeroOrOneBooleanContent &&
2093 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002094 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002095 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002096 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002097 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002098 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002099 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002100
2101 // If the shift count is an invalid immediate, don't do anything.
2102 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002103 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002104
Jay Foada0653a32014-05-14 21:14:37 +00002105 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002106 KnownZero <<= ShAmt;
2107 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002108 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002109 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002110 }
Jay Foad5a29c362014-05-15 12:12:55 +00002111 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002112 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002113 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002114 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002115 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002116
Dan Gohman9db0aa82008-02-26 18:50:50 +00002117 // If the shift count is an invalid immediate, don't do anything.
2118 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002119 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002120
Jay Foada0653a32014-05-14 21:14:37 +00002121 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002122 KnownZero = KnownZero.lshr(ShAmt);
2123 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002124
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002125 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002126 KnownZero |= HighBits; // High bits known zero.
2127 }
Jay Foad5a29c362014-05-15 12:12:55 +00002128 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002129 case ISD::SRA:
2130 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002131 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002132
Dan Gohman9db0aa82008-02-26 18:50:50 +00002133 // If the shift count is an invalid immediate, don't do anything.
2134 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002135 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002136
Dan Gohman309d3d52007-06-22 14:59:07 +00002137 // If any of the demanded bits are produced by the sign extension, we also
2138 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002139 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002140
Jay Foada0653a32014-05-14 21:14:37 +00002141 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002142 KnownZero = KnownZero.lshr(ShAmt);
2143 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002144
Dan Gohman309d3d52007-06-22 14:59:07 +00002145 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002146 APInt SignBit = APInt::getSignBit(BitWidth);
2147 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002148
Dan Gohmanb717fda2008-02-20 16:30:17 +00002149 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002150 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002151 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002152 KnownOne |= HighBits; // New bits are known one.
2153 }
2154 }
Jay Foad5a29c362014-05-15 12:12:55 +00002155 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002156 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002157 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002158 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002159
2160 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002161 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002162 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002163
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002164 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002165 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002166
Dan Gohman309d3d52007-06-22 14:59:07 +00002167 // If the sign extended bits are demanded, we know that the sign
2168 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002169 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002170 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002171 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002172
Jay Foada0653a32014-05-14 21:14:37 +00002173 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002174 KnownOne &= InputDemandedBits;
2175 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002176
Dan Gohman309d3d52007-06-22 14:59:07 +00002177 // If the sign bit of the input is known set or clear, then we know the
2178 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002179 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002180 KnownZero |= NewBits;
2181 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002182 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002183 KnownOne |= NewBits;
2184 KnownZero &= ~NewBits;
2185 } else { // Input sign bit unknown
2186 KnownZero &= ~NewBits;
2187 KnownOne &= ~NewBits;
2188 }
Jay Foad5a29c362014-05-15 12:12:55 +00002189 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002190 }
2191 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002192 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002193 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002194 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002195 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002196 unsigned LowBits = Log2_32(BitWidth)+1;
2197 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002198 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002199 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002200 }
2201 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002202 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002203 // If this is a ZEXTLoad and we are looking at the loaded value.
2204 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002205 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002206 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002207 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002208 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002209 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002210 }
Jay Foad5a29c362014-05-15 12:12:55 +00002211 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002212 }
2213 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002214 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002215 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002216 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002217 KnownZero = KnownZero.trunc(InBits);
2218 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002219 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002220 KnownZero = KnownZero.zext(BitWidth);
2221 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002222 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002223 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002224 }
2225 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002226 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002227 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002228 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002229
Jay Foad583abbc2010-12-07 08:25:19 +00002230 KnownZero = KnownZero.trunc(InBits);
2231 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002232 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002233
2234 // Note if the sign bit is known to be zero or one.
2235 bool SignBitKnownZero = KnownZero.isNegative();
2236 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002237
Jay Foad583abbc2010-12-07 08:25:19 +00002238 KnownZero = KnownZero.zext(BitWidth);
2239 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002240
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002241 // If the sign bit is known zero or one, the top bits match.
2242 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002243 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002244 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002245 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002246 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002247 }
2248 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002249 EVT InVT = Op.getOperand(0).getValueType();
2250 unsigned InBits = InVT.getScalarType().getSizeInBits();
2251 KnownZero = KnownZero.trunc(InBits);
2252 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002253 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002254 KnownZero = KnownZero.zext(BitWidth);
2255 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002256 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002257 }
2258 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002259 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002260 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002261 KnownZero = KnownZero.zext(InBits);
2262 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002263 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002264 KnownZero = KnownZero.trunc(BitWidth);
2265 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002266 break;
2267 }
2268 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002269 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002270 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002271 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002272 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002273 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002274 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002275 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002276 case ISD::FGETSIGN:
2277 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002278 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002279 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002280
Dan Gohman72ec3f42008-04-28 17:02:21 +00002281 case ISD::SUB: {
2282 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2283 // We know that the top bits of C-X are clear if X contains less bits
2284 // than C (i.e. no wrap-around can happen). For example, 20-X is
2285 // positive if we can prove that X is >= 0 and < 16.
2286 if (CLHS->getAPIntValue().isNonNegative()) {
2287 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2288 // NLZ can't be BitWidth with no sign bit
2289 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002290 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002291
2292 // If all of the MaskV bits are known to be zero, then we know the
2293 // output top bits are zero, because we now know that the output is
2294 // from [0-C].
2295 if ((KnownZero2 & MaskV) == MaskV) {
2296 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2297 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002298 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002299 }
2300 }
2301 }
2302 }
2303 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002304 case ISD::ADD:
2305 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002306 // Output known-0 bits are known if clear or set in both the low clear bits
2307 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2308 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002309 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002310 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2311
Jay Foada0653a32014-05-14 21:14:37 +00002312 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002313 KnownZeroOut = std::min(KnownZeroOut,
2314 KnownZero2.countTrailingOnes());
2315
Chris Lattner440b2802010-12-19 20:38:28 +00002316 if (Op.getOpcode() == ISD::ADD) {
2317 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002318 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002319 }
2320
2321 // With ADDE, a carry bit may be added in, so we can only use this
2322 // information if we know (at least) that the low two bits are clear. We
2323 // then return to the caller that the low bit is unknown but that other bits
2324 // are known zero.
2325 if (KnownZeroOut >= 2) // ADDE
2326 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002327 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002328 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002329 case ISD::SREM:
2330 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002331 const APInt &RA = Rem->getAPIntValue().abs();
2332 if (RA.isPowerOf2()) {
2333 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002334 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002335
Duncan Sands33274982010-01-29 09:45:26 +00002336 // The low bits of the first operand are unchanged by the srem.
2337 KnownZero = KnownZero2 & LowBits;
2338 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002339
Duncan Sands33274982010-01-29 09:45:26 +00002340 // If the first operand is non-negative or has all low bits zero, then
2341 // the upper bits are all zero.
2342 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2343 KnownZero |= ~LowBits;
2344
2345 // If the first operand is negative and not all low bits are zero, then
2346 // the upper bits are all one.
2347 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2348 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002349 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002350 }
2351 }
Jay Foad5a29c362014-05-15 12:12:55 +00002352 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002353 case ISD::UREM: {
2354 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002355 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002356 if (RA.isPowerOf2()) {
2357 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002358 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2359
2360 // The upper bits are all zero, the lower ones are unchanged.
2361 KnownZero = KnownZero2 | ~LowBits;
2362 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002363 break;
2364 }
2365 }
2366
2367 // Since the result is less than or equal to either operand, any leading
2368 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002369 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2370 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002371
2372 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2373 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002374 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002375 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002376 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002378 case ISD::EXTRACT_ELEMENT: {
2379 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2380 const unsigned Index =
2381 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2382 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2383
2384 // Remove low part of known bits mask
2385 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2386 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2387
2388 // Remove high part of known bit mask
2389 KnownZero = KnownZero.trunc(BitWidth);
2390 KnownOne = KnownOne.trunc(BitWidth);
2391 break;
2392 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002393 case ISD::FrameIndex:
2394 case ISD::TargetFrameIndex:
2395 if (unsigned Align = InferPtrAlignment(Op)) {
2396 // The low bits are known zero if the pointer is aligned.
2397 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002398 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002399 }
2400 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002401
Dan Gohman309d3d52007-06-22 14:59:07 +00002402 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002403 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2404 break;
2405 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002406 case ISD::INTRINSIC_WO_CHAIN:
2407 case ISD::INTRINSIC_W_CHAIN:
2408 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002409 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002410 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002411 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002412 }
Jay Foad5a29c362014-05-15 12:12:55 +00002413
2414 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002415}
2416
2417/// ComputeNumSignBits - Return the number of times the sign bit of the
2418/// register is replicated into the other bits. We know that at least 1 bit
2419/// is always equal to the sign bit (itself), but other cases can give us
2420/// information. For example, immediately after an "SRA X, 2", we know that
2421/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002422unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002423 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002424 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002425 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002427 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002428
Dan Gohman309d3d52007-06-22 14:59:07 +00002429 if (Depth == 6)
2430 return 1; // Limit search depth.
2431
2432 switch (Op.getOpcode()) {
2433 default: break;
2434 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002435 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002436 return VTBits-Tmp+1;
2437 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002438 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002439 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002440
Dan Gohman309d3d52007-06-22 14:59:07 +00002441 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002442 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002443 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002444 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002445
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002447 Tmp =
2448 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002449 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002450
Dan Gohman309d3d52007-06-22 14:59:07 +00002451 case ISD::SIGN_EXTEND_INREG:
2452 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002453 Tmp =
2454 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002455 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002456
Dan Gohman309d3d52007-06-22 14:59:07 +00002457 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2458 return std::max(Tmp, Tmp2);
2459
2460 case ISD::SRA:
2461 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2462 // SRA X, C -> adds C sign bits.
2463 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002464 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002465 if (Tmp > VTBits) Tmp = VTBits;
2466 }
2467 return Tmp;
2468 case ISD::SHL:
2469 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2470 // shl destroys sign bits.
2471 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002472 if (C->getZExtValue() >= VTBits || // Bad shift.
2473 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2474 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002475 }
2476 break;
2477 case ISD::AND:
2478 case ISD::OR:
2479 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002480 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002481 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002482 if (Tmp != 1) {
2483 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2484 FirstAnswer = std::min(Tmp, Tmp2);
2485 // We computed what we know about the sign bits as our first
2486 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002487 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002488 }
2489 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002490
2491 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002492 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002493 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002494 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002495 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002496
2497 case ISD::SADDO:
2498 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002499 case ISD::SSUBO:
2500 case ISD::USUBO:
2501 case ISD::SMULO:
2502 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002503 if (Op.getResNo() != 1)
2504 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002505 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002506 // If setcc returns 0/-1, all bits are sign bits.
2507 // We know that we have an integer-based boolean since these operations
2508 // are only available for integer.
2509 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2510 TargetLowering::ZeroOrNegativeOneBooleanContent)
2511 return VTBits;
2512 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002513 case ISD::SETCC:
2514 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002515 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002516 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002517 return VTBits;
2518 break;
2519 case ISD::ROTL:
2520 case ISD::ROTR:
2521 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002522 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002523
Dan Gohman309d3d52007-06-22 14:59:07 +00002524 // Handle rotate right by N like a rotate left by 32-N.
2525 if (Op.getOpcode() == ISD::ROTR)
2526 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2527
2528 // If we aren't rotating out all of the known-in sign bits, return the
2529 // number that are left. This handles rotl(sext(x), 1) for example.
2530 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2531 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2532 }
2533 break;
2534 case ISD::ADD:
2535 // Add can have at most one carry bit. Thus we know that the output
2536 // is, at worst, one more bit than the inputs.
2537 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2538 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002539
Dan Gohman309d3d52007-06-22 14:59:07 +00002540 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002541 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002542 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002543 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002544 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002545
Dan Gohman309d3d52007-06-22 14:59:07 +00002546 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2547 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002548 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002549 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002550
Dan Gohman309d3d52007-06-22 14:59:07 +00002551 // If we are subtracting one from a positive number, there is no carry
2552 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002553 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002554 return Tmp;
2555 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002556
Dan Gohman309d3d52007-06-22 14:59:07 +00002557 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2558 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002559 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002560
Dan Gohman309d3d52007-06-22 14:59:07 +00002561 case ISD::SUB:
2562 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2563 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002564
Dan Gohman309d3d52007-06-22 14:59:07 +00002565 // Handle NEG.
2566 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002567 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002568 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002569 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002570 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2571 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002572 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002573 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002574
Dan Gohman309d3d52007-06-22 14:59:07 +00002575 // If the input is known to be positive (the sign bit is known clear),
2576 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002577 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002578 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002579
Dan Gohman309d3d52007-06-22 14:59:07 +00002580 // Otherwise, we treat this like a SUB.
2581 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002582
Dan Gohman309d3d52007-06-22 14:59:07 +00002583 // Sub can have at most one carry bit. Thus we know that the output
2584 // is, at worst, one more bit than the inputs.
2585 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2586 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002587 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002588 case ISD::TRUNCATE:
2589 // FIXME: it's tricky to do anything useful for this, but it is an important
2590 // case for targets like X86.
2591 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002592 case ISD::EXTRACT_ELEMENT: {
2593 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2594 const int BitWidth = Op.getValueType().getSizeInBits();
2595 const int Items =
2596 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2597
2598 // Get reverse index (starting from 1), Op1 value indexes elements from
2599 // little end. Sign starts at big end.
2600 const int rIndex = Items - 1 -
2601 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2602
2603 // If the sign portion ends in our element the substraction gives correct
2604 // result. Otherwise it gives either negative or > bitwidth result
2605 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2606 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002607 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002608
Nadav Rotem4536d582013-03-20 22:53:44 +00002609 // If we are looking at the loaded value of the SDNode.
2610 if (Op.getResNo() == 0) {
2611 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2612 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2613 unsigned ExtType = LD->getExtensionType();
2614 switch (ExtType) {
2615 default: break;
2616 case ISD::SEXTLOAD: // '17' bits known
2617 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2618 return VTBits-Tmp+1;
2619 case ISD::ZEXTLOAD: // '16' bits known
2620 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2621 return VTBits-Tmp;
2622 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002623 }
2624 }
2625
2626 // Allow the target to implement this method for its nodes.
2627 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002628 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002629 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2630 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002631 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002632 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002633 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002634
Dan Gohman309d3d52007-06-22 14:59:07 +00002635 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2636 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002637 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002638 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002639
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002640 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002641 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002642 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002643 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002644 Mask = KnownOne;
2645 } else {
2646 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002647 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002648 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002649
Dan Gohman309d3d52007-06-22 14:59:07 +00002650 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2651 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002652 Mask = ~Mask;
2653 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002654 // Return # leading zeros. We use 'min' here in case Val was zero before
2655 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002656 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002657}
2658
Chris Lattner46c01a32011-02-13 22:25:43 +00002659/// isBaseWithConstantOffset - Return true if the specified operand is an
2660/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2661/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2662/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002663/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002664bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2665 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2666 !isa<ConstantSDNode>(Op.getOperand(1)))
2667 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002668
2669 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002670 !MaskedValueIsZero(Op.getOperand(0),
2671 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2672 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002673
Chris Lattner46c01a32011-02-13 22:25:43 +00002674 return true;
2675}
2676
2677
Dan Gohmand0d5e682009-09-03 20:34:31 +00002678bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2679 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002680 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002681 return true;
2682
2683 // If the value is a constant, we can obviously see if it is a NaN or not.
2684 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2685 return !C->getValueAPF().isNaN();
2686
2687 // TODO: Recognize more cases here.
2688
2689 return false;
2690}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002691
Dan Gohman38605212010-02-24 06:52:40 +00002692bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2693 // If the value is a constant, we can obviously see if it is a zero or not.
2694 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2695 return !C->isZero();
2696
2697 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002698 switch (Op.getOpcode()) {
2699 default: break;
2700 case ISD::OR:
2701 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2702 return !C->isNullValue();
2703 break;
2704 }
Dan Gohman38605212010-02-24 06:52:40 +00002705
2706 return false;
2707}
2708
2709bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2710 // Check the obvious case.
2711 if (A == B) return true;
2712
2713 // For for negative and positive zero.
2714 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2715 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2716 if (CA->isZero() && CB->isZero()) return true;
2717
2718 // Otherwise they may not be equal.
2719 return false;
2720}
2721
Chris Lattner061a1ea2005-01-07 07:46:32 +00002722/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002723///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002724SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002725 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002726 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002727 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002728 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002729 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002730
Jack Carter170a5f22013-09-09 22:02:08 +00002731 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2732 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002733 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002734
Chandler Carruth41b20e72014-07-22 04:07:55 +00002735 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002736 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002737}
2738
Andrew Trickef9de2a2013-05-25 02:42:55 +00002739SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002740 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002741 // Constant fold unary operations with an integer constant operand. Even
2742 // opaque constant will be folded, because the folding of unary operations
2743 // doesn't create new constants with different values. Nevertheless, the
2744 // opaque flag is preserved during folding to prevent future folding with
2745 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002746 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002747 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002748 switch (Opcode) {
2749 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002750 case ISD::SIGN_EXTEND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002751 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002752 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002753 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002754 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002755 case ISD::TRUNCATE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002756 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002757 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002758 case ISD::UINT_TO_FP:
2759 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002760 APFloat apf(EVTToAPFloatSemantics(VT),
2761 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002762 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002763 Opcode==ISD::SINT_TO_FP,
2764 APFloat::rmNearestTiesToEven);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002765 return getConstantFP(apf, DL, VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002766 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002767 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002768 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002769 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002770 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002771 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002772 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002773 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002774 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002775 case ISD::BSWAP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002776 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002777 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002778 case ISD::CTPOP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002779 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002780 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002781 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002782 case ISD::CTLZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002783 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002784 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002785 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002786 case ISD::CTTZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002787 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002788 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002789 }
2790 }
2791
Dale Johannesen446b9002007-08-31 23:34:27 +00002792 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002793 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002794 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002795 switch (Opcode) {
2796 case ISD::FNEG:
2797 V.changeSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002798 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002799 case ISD::FABS:
2800 V.clearSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002801 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002802 case ISD::FCEIL: {
2803 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2804 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002805 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002806 break;
2807 }
2808 case ISD::FTRUNC: {
2809 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2810 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002811 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002812 break;
2813 }
2814 case ISD::FFLOOR: {
2815 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2816 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002817 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002818 break;
2819 }
2820 case ISD::FP_EXTEND: {
2821 bool ignored;
2822 // This can return overflow, underflow, or inexact; we don't care.
2823 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002824 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002825 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002826 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002827 }
2828 case ISD::FP_TO_SINT:
2829 case ISD::FP_TO_UINT: {
2830 integerPart x[2];
2831 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002832 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002833 // FIXME need to be more flexible about rounding mode.
2834 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2835 Opcode==ISD::FP_TO_SINT,
2836 APFloat::rmTowardZero, &ignored);
2837 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002838 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002839 APInt api(VT.getSizeInBits(), x);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002840 return getConstant(api, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002841 }
2842 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002843 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002844 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Owen Anderson558012a2014-12-09 06:50:39 +00002845 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002846 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002847 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002848 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002849 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002850 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002851 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002852
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002853 // Constant fold unary operations with a vector integer or float operand.
Jim Grosbachf7502c42014-07-18 00:40:52 +00002854 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002855 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002856 switch (Opcode) {
2857 default:
2858 // FIXME: Entirely reasonable to perform folding of other unary
2859 // operations here as the need arises.
2860 break;
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002861 case ISD::FNEG:
2862 case ISD::FABS:
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002863 case ISD::FCEIL:
2864 case ISD::FTRUNC:
2865 case ISD::FFLOOR:
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002866 case ISD::FP_EXTEND:
Simon Pilgrim017ca192015-05-02 13:04:07 +00002867 case ISD::FP_TO_SINT:
2868 case ISD::FP_TO_UINT:
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002869 case ISD::TRUNCATE:
Jim Grosbachf7502c42014-07-18 00:40:52 +00002870 case ISD::UINT_TO_FP:
2871 case ISD::SINT_TO_FP: {
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002872 EVT SVT = VT.getScalarType();
2873 EVT InVT = BV->getValueType(0);
2874 EVT InSVT = InVT.getScalarType();
2875
2876 // Find legal integer scalar type for constant promotion.
2877 EVT LegalSVT = SVT;
2878 if (SVT.isInteger()) {
2879 LegalSVT = TLI->getTypeToTransformTo(*getContext(), SVT);
2880 assert(LegalSVT.bitsGE(SVT) && "Unexpected legal scalar type size");
2881 }
2882
Simon Pilgrim481f4142015-03-23 22:44:55 +00002883 // Let the above scalar folding handle the folding of each element.
Jim Grosbach19dd3082014-07-23 20:41:31 +00002884 SmallVector<SDValue, 8> Ops;
2885 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2886 SDValue OpN = BV->getOperand(i);
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002887 EVT OpVT = OpN.getValueType();
2888
2889 // Build vector (integer) scalar operands may need implicit
2890 // truncation - do this before constant folding.
2891 if (OpVT.isInteger() && OpVT.bitsGT(InSVT))
2892 OpN = getNode(ISD::TRUNCATE, DL, InSVT, OpN);
2893
2894 OpN = getNode(Opcode, DL, SVT, OpN);
2895
2896 // Legalize the (integer) scalar constant if necessary.
2897 if (LegalSVT != SVT)
2898 OpN = getNode(ISD::ANY_EXTEND, DL, LegalSVT, OpN);
2899
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002900 if (OpN.getOpcode() != ISD::UNDEF &&
2901 OpN.getOpcode() != ISD::Constant &&
2902 OpN.getOpcode() != ISD::ConstantFP)
2903 break;
Jim Grosbach19dd3082014-07-23 20:41:31 +00002904 Ops.push_back(OpN);
2905 }
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002906 if (Ops.size() == VT.getVectorNumElements())
2907 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002908 break;
Jim Grosbachf7502c42014-07-18 00:40:52 +00002909 }
2910 }
2911 }
2912 }
2913
Gabor Greiff304a7a2008-08-28 21:40:38 +00002914 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002915 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002916 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002917 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002918 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002919 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002920 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002921 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002922 assert(VT.isFloatingPoint() &&
2923 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002924 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002925 assert((!VT.isVector() ||
2926 VT.getVectorNumElements() ==
2927 Operand.getValueType().getVectorNumElements()) &&
2928 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002929 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002930 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002931 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002932 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002933 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002934 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002935 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002936 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2937 "Invalid sext node, dst < src!");
2938 assert((!VT.isVector() ||
2939 VT.getVectorNumElements() ==
2940 Operand.getValueType().getVectorNumElements()) &&
2941 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002942 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2943 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2944 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002945 // sext(undef) = 0, because the top bits will all be the same.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002946 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002947 break;
2948 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002949 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002950 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002951 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002952 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2953 "Invalid zext node, dst < src!");
2954 assert((!VT.isVector() ||
2955 VT.getVectorNumElements() ==
2956 Operand.getValueType().getVectorNumElements()) &&
2957 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002958 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002959 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002960 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002961 else if (OpOpcode == ISD::UNDEF)
2962 // zext(undef) = 0, because the top bits will be zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002963 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002964 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002965 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002966 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002967 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002968 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002969 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2970 "Invalid anyext node, dst < src!");
2971 assert((!VT.isVector() ||
2972 VT.getVectorNumElements() ==
2973 Operand.getValueType().getVectorNumElements()) &&
2974 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002975
Dan Gohman08837892010-06-18 00:08:30 +00002976 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2977 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002978 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002979 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002980 else if (OpOpcode == ISD::UNDEF)
2981 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002982
2983 // (ext (trunx x)) -> x
2984 if (OpOpcode == ISD::TRUNCATE) {
2985 SDValue OpOp = Operand.getNode()->getOperand(0);
2986 if (OpOp.getValueType() == VT)
2987 return OpOp;
2988 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002989 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002990 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002991 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002992 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002993 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002994 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2995 "Invalid truncate node, src < dst!");
2996 assert((!VT.isVector() ||
2997 VT.getVectorNumElements() ==
2998 Operand.getValueType().getVectorNumElements()) &&
2999 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003000 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00003001 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003002 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3003 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00003004 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00003005 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
3006 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00003007 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003008 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00003009 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003010 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00003011 }
Craig Topper201c1a32012-01-15 01:05:11 +00003012 if (OpOpcode == ISD::UNDEF)
3013 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003014 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003015 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00003016 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00003017 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00003018 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00003019 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00003020 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
3021 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00003022 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003023 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003024 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003025 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003026 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00003027 (VT.getVectorElementType() == Operand.getValueType() ||
3028 (VT.getVectorElementType().isInteger() &&
3029 Operand.getValueType().isInteger() &&
3030 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003031 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00003032 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003033 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00003034 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3035 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3036 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3037 Operand.getConstantOperandVal(1) == 0 &&
3038 Operand.getOperand(0).getValueType() == VT)
3039 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003040 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00003041 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00003042 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003043 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00003044 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00003045 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003046 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00003047 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00003048 break;
3049 case ISD::FABS:
3050 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00003051 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003052 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003053 }
3054
Chris Lattnerf9c19152005-08-25 19:12:10 +00003055 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003056 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003057 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00003058 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003059 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00003060 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003061 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003062 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003063 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003064
Jack Carter170a5f22013-09-09 22:02:08 +00003065 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3066 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003067 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003068 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003069 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3070 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003071 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003072
Chandler Carruth41b20e72014-07-22 04:07:55 +00003073 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003074 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003075}
3076
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003077SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003078 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003079 // If the opcode is a target-specific ISD node, there's nothing we can
3080 // do here and the operand rules may not line up with the below, so
3081 // bail early.
3082 if (Opcode >= ISD::BUILTIN_OP_END)
3083 return SDValue();
3084
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003085 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3086 SmallVector<SDValue, 4> Outputs;
3087 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00003088
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003089 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3090 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003091 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3092 return SDValue();
3093
3094 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003095 // Scalar instruction.
3096 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003097 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003098 // For vectors extract each constant element into Inputs so we can constant
3099 // fold them individually.
3100 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3101 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3102 if (!BV1 || !BV2)
3103 return SDValue();
3104
3105 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3106
3107 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3108 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3109 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3110 if (!V1 || !V2) // Not a constant, bail.
3111 return SDValue();
3112
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003113 if (V1->isOpaque() || V2->isOpaque())
3114 return SDValue();
3115
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003116 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3117 // FIXME: This is valid and could be handled by truncating the APInts.
3118 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3119 return SDValue();
3120
3121 Inputs.push_back(std::make_pair(V1, V2));
3122 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003123 }
3124
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003125 // We have a number of constant values, constant fold them element by element.
3126 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3127 const APInt &C1 = Inputs[I].first->getAPIntValue();
3128 const APInt &C2 = Inputs[I].second->getAPIntValue();
3129
3130 switch (Opcode) {
3131 case ISD::ADD:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003132 Outputs.push_back(getConstant(C1 + C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003133 break;
3134 case ISD::SUB:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003135 Outputs.push_back(getConstant(C1 - C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003136 break;
3137 case ISD::MUL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003138 Outputs.push_back(getConstant(C1 * C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003139 break;
3140 case ISD::UDIV:
3141 if (!C2.getBoolValue())
3142 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003143 Outputs.push_back(getConstant(C1.udiv(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003144 break;
3145 case ISD::UREM:
3146 if (!C2.getBoolValue())
3147 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003148 Outputs.push_back(getConstant(C1.urem(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003149 break;
3150 case ISD::SDIV:
3151 if (!C2.getBoolValue())
3152 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003153 Outputs.push_back(getConstant(C1.sdiv(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003154 break;
3155 case ISD::SREM:
3156 if (!C2.getBoolValue())
3157 return SDValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003158 Outputs.push_back(getConstant(C1.srem(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003159 break;
3160 case ISD::AND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003161 Outputs.push_back(getConstant(C1 & C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003162 break;
3163 case ISD::OR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003164 Outputs.push_back(getConstant(C1 | C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003165 break;
3166 case ISD::XOR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003167 Outputs.push_back(getConstant(C1 ^ C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003168 break;
3169 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003170 Outputs.push_back(getConstant(C1 << C2, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003171 break;
3172 case ISD::SRL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003173 Outputs.push_back(getConstant(C1.lshr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003174 break;
3175 case ISD::SRA:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003176 Outputs.push_back(getConstant(C1.ashr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003177 break;
3178 case ISD::ROTL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003179 Outputs.push_back(getConstant(C1.rotl(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003180 break;
3181 case ISD::ROTR:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003182 Outputs.push_back(getConstant(C1.rotr(C2), DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003183 break;
3184 default:
3185 return SDValue();
3186 }
3187 }
3188
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003189 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3190 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003191
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003192 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003193 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003194 return Outputs.back();
3195
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003196 // We may have a vector type but a scalar result. Create a splat.
3197 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3198
3199 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003200 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003201}
3202
Andrew Trickef9de2a2013-05-25 02:42:55 +00003203SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003204 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003205 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3206 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003207 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003208 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003209 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003210 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3211 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003212 // Fold trivial token factors.
3213 if (N1.getOpcode() == ISD::EntryToken) return N2;
3214 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003215 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003216 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003217 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003218 // Concat of UNDEFs is UNDEF.
3219 if (N1.getOpcode() == ISD::UNDEF &&
3220 N2.getOpcode() == ISD::UNDEF)
3221 return getUNDEF(VT);
3222
Dan Gohman550c9af2008-08-14 20:04:46 +00003223 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3224 // one big BUILD_VECTOR.
3225 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3226 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003227 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3228 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003229 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Simon Pilgrim860f0872015-04-21 08:05:43 +00003230
3231 // BUILD_VECTOR requires all inputs to be of the same type, find the
3232 // maximum type and extend them all.
3233 EVT SVT = VT.getScalarType();
3234 for (SDValue Op : Elts)
3235 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
3236 if (SVT.bitsGT(VT.getScalarType()))
3237 for (SDValue &Op : Elts)
3238 Op = TLI->isZExtFree(Op.getValueType(), SVT)
3239 ? getZExtOrTrunc(Op, DL, SVT)
3240 : getSExtOrTrunc(Op, DL, SVT);
3241
Craig Topper48d114b2014-04-26 18:35:24 +00003242 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003243 }
3244 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003245 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003246 assert(VT.isInteger() && "This operator does not apply to FP types!");
3247 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003248 N1.getValueType() == VT && "Binary operator types must match!");
3249 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3250 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003251 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003252 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003253 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3254 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003255 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003256 case ISD::OR:
3257 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003258 case ISD::ADD:
3259 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003260 assert(VT.isInteger() && "This operator does not apply to FP types!");
3261 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003262 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003263 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3264 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003265 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003266 return N1;
3267 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003268 case ISD::UDIV:
3269 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003270 case ISD::MULHU:
3271 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003272 case ISD::MUL:
3273 case ISD::SDIV:
3274 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003275 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003276 assert(N1.getValueType() == N2.getValueType() &&
3277 N1.getValueType() == VT && "Binary operator types must match!");
3278 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003279 case ISD::FADD:
3280 case ISD::FSUB:
3281 case ISD::FMUL:
3282 case ISD::FDIV:
3283 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003284 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003285 if (Opcode == ISD::FADD) {
3286 // 0+x --> x
3287 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3288 if (CFP->getValueAPF().isZero())
3289 return N2;
3290 // x+0 --> x
3291 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3292 if (CFP->getValueAPF().isZero())
3293 return N1;
3294 } else if (Opcode == ISD::FSUB) {
3295 // x-0 --> x
3296 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3297 if (CFP->getValueAPF().isZero())
3298 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003299 } else if (Opcode == ISD::FMUL) {
3300 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3301 SDValue V = N2;
3302
3303 // If the first operand isn't the constant, try the second
3304 if (!CFP) {
3305 CFP = dyn_cast<ConstantFPSDNode>(N2);
3306 V = N1;
3307 }
3308
3309 if (CFP) {
3310 // 0*x --> 0
3311 if (CFP->isZero())
3312 return SDValue(CFP,0);
3313 // 1*x --> x
3314 if (CFP->isExactlyValue(1.0))
3315 return V;
3316 }
Dan Gohman1275e282009-01-23 19:10:37 +00003317 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003318 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003319 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003320 assert(N1.getValueType() == N2.getValueType() &&
3321 N1.getValueType() == VT && "Binary operator types must match!");
3322 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003323 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3324 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003325 N1.getValueType().isFloatingPoint() &&
3326 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003327 "Invalid FCOPYSIGN!");
3328 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003329 case ISD::SHL:
3330 case ISD::SRA:
3331 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003332 case ISD::ROTL:
3333 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003334 assert(VT == N1.getValueType() &&
3335 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003336 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003337 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003338 assert((!VT.isVector() || VT == N2.getValueType()) &&
3339 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003340 // Verify that the shift amount VT is bit enough to hold valid shift
3341 // amounts. This catches things like trying to shift an i1024 value by an
3342 // i8, which is easy to fall into in generic code that uses
3343 // TLI.getShiftAmount().
3344 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003345 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003346 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003347
3348 // Always fold shifts of i1 values so the code generator doesn't need to
3349 // handle them. Since we know the size of the shift has to be less than the
3350 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003351 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003352 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003353 if (N2C && N2C->isNullValue())
3354 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003355 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003356 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003357 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003358 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003359 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003360 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003361 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003362 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003363 "type is vector!");
3364 assert((!EVT.isVector() ||
3365 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3366 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003367 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003368 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003369 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003370 break;
3371 }
Chris Lattner72733e52008-01-17 07:00:52 +00003372 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003373 assert(VT.isFloatingPoint() &&
3374 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003375 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003376 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003377 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003378 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003379 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003380 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003381 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003382 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003383 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003384 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003385 assert(!EVT.isVector() &&
3386 "AssertSExt/AssertZExt type should be the vector element type "
3387 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003388 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003389 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003390 break;
3391 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003392 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003393 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003394 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003395 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003396 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003397 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003398 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003399 "type is vector!");
3400 assert((!EVT.isVector() ||
3401 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3402 "Vector element counts must match in SIGN_EXTEND_INREG");
3403 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003404 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003405
Chris Lattner16713612008-01-22 19:09:33 +00003406 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003407 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003408 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003409 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003410 Val = Val.ashr(Val.getBitWidth()-FromBits);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003411 return getConstant(Val, DL, VT);
Chris Lattner751817c2006-05-06 23:05:41 +00003412 }
Chris Lattner16713612008-01-22 19:09:33 +00003413 break;
3414 }
3415 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003416 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3417 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003418 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003419
Chris Lattner16713612008-01-22 19:09:33 +00003420 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3421 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003422 if (N2C &&
3423 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003424 N1.getNumOperands() > 0) {
3425 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003426 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003427 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003428 N1.getOperand(N2C->getZExtValue() / Factor),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003429 getConstant(N2C->getZExtValue() % Factor, DL,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003430 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003431 }
3432
3433 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3434 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003435 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3436 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003437
3438 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003439 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003440 // are promoted and implicitly truncated, and the result implicitly
3441 // extended. Make that explicit here.
3442 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003443
Bob Wilson59dbbb22009-04-13 22:05:19 +00003444 return Elt;
3445 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003446
Chris Lattner16713612008-01-22 19:09:33 +00003447 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3448 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003449 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003450 // If the indices are the same, return the inserted element else
3451 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003452 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003453 SDValue N1Op2 = N1.getOperand(2);
3454 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3455
3456 if (N1Op2C && N2C) {
3457 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3458 if (VT == N1.getOperand(1).getValueType())
3459 return N1.getOperand(1);
3460 else
3461 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3462 }
3463
Dale Johannesendb393622009-02-03 01:55:44 +00003464 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003465 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003466 }
Chris Lattner16713612008-01-22 19:09:33 +00003467 break;
3468 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003469 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003470 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3471 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003472 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003473 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003474
Chris Lattner16713612008-01-22 19:09:33 +00003475 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3476 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003477 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003478 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003479 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003480
Chris Lattner16713612008-01-22 19:09:33 +00003481 // EXTRACT_ELEMENT of a constant int is also very common.
3482 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003483 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003484 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003485 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003486 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
Chris Lattner16713612008-01-22 19:09:33 +00003487 }
3488 break;
David Greeneb6f16112011-01-26 15:38:49 +00003489 case ISD::EXTRACT_SUBVECTOR: {
3490 SDValue Index = N2;
3491 if (VT.isSimple() && N1.getValueType().isSimple()) {
3492 assert(VT.isVector() && N1.getValueType().isVector() &&
3493 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003494 assert(VT.getVectorElementType() ==
3495 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003496 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003497 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003498 "Extract subvector must be from larger vector to smaller vector!");
3499
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003500 if (isa<ConstantSDNode>(Index.getNode())) {
3501 assert((VT.getVectorNumElements() +
3502 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003503 <= N1.getValueType().getVectorNumElements())
3504 && "Extract subvector overflow!");
3505 }
3506
3507 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003508 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003509 return N1;
3510 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003511 break;
Chris Lattner16713612008-01-22 19:09:33 +00003512 }
David Greeneb6f16112011-01-26 15:38:49 +00003513 }
Chris Lattner16713612008-01-22 19:09:33 +00003514
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003515 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003516 if (SDValue SV =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003517 FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
Matthias Braunf50ab432015-01-13 22:17:46 +00003518 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003519
3520 // Canonicalize constant to RHS if commutative.
3521 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3522 std::swap(N1C, N2C);
3523 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003524 }
3525
Chris Lattner16713612008-01-22 19:09:33 +00003526 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003527 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003528 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3529 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003530 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003531 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003532 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003533 std::swap(N1CFP, N2CFP);
3534 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003535 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003536 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3537 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003538 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003539 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003540 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003541 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003542 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003543 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003544 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003545 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003546 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003547 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003548 break;
3549 case ISD::FMUL:
3550 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003551 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003552 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003553 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003554 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003555 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003556 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3557 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003558 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003559 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003560 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003561 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003562 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003563 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3564 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003565 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003566 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003567 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003568 case ISD::FCOPYSIGN:
3569 V1.copySign(V2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003570 return getConstantFP(V1, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003571 default: break;
3572 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003573 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003574
3575 if (Opcode == ISD::FP_ROUND) {
3576 APFloat V = N1CFP->getValueAPF(); // make copy
3577 bool ignored;
3578 // This can return overflow, underflow, or inexact; we don't care.
3579 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003580 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003581 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003582 return getConstantFP(V, DL, VT);
Owen Anderson6f1ee162012-04-10 22:46:53 +00003583 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003584 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003585
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003586 // Canonicalize an UNDEF to the RHS, even over a constant.
3587 if (N1.getOpcode() == ISD::UNDEF) {
3588 if (isCommutativeBinOp(Opcode)) {
3589 std::swap(N1, N2);
3590 } else {
3591 switch (Opcode) {
3592 case ISD::FP_ROUND_INREG:
3593 case ISD::SIGN_EXTEND_INREG:
3594 case ISD::SUB:
3595 case ISD::FSUB:
3596 case ISD::FDIV:
3597 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003598 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003599 return N1; // fold op(undef, arg2) -> undef
3600 case ISD::UDIV:
3601 case ISD::SDIV:
3602 case ISD::UREM:
3603 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003604 case ISD::SRL:
3605 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003606 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003607 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003608 // For vectors, we can't easily build an all zero vector, just return
3609 // the LHS.
3610 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003611 }
3612 }
3613 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003614
3615 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003616 if (N2.getOpcode() == ISD::UNDEF) {
3617 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003618 case ISD::XOR:
3619 if (N1.getOpcode() == ISD::UNDEF)
3620 // Handle undef ^ undef -> 0 special case. This is a common
3621 // idiom (misuse).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003622 return getConstant(0, DL, VT);
Evan Chengdf1690d2008-03-25 20:08:07 +00003623 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003624 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003625 case ISD::ADDC:
3626 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003627 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003628 case ISD::UDIV:
3629 case ISD::SDIV:
3630 case ISD::UREM:
3631 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003632 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003633 case ISD::FADD:
3634 case ISD::FSUB:
3635 case ISD::FMUL:
3636 case ISD::FDIV:
3637 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003638 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003639 return N2;
3640 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003641 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003642 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003643 case ISD::SRL:
3644 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003645 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003646 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003647 // For vectors, we can't easily build an all zero vector, just return
3648 // the LHS.
3649 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003650 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003651 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003652 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003653 // For vectors, we can't easily build an all one vector, just return
3654 // the LHS.
3655 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003656 case ISD::SRA:
3657 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003658 }
3659 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003660
Chris Lattner724f7ee2005-05-11 18:57:39 +00003661 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003662 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003663 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003664 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003665 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003666 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003667 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003668 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003669 if (BinOpHasFlags)
3670 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003671 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003672 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003673 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003674
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003675 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3676
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003677 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003678 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003679 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003680 }
3681
Chandler Carruth41b20e72014-07-22 04:07:55 +00003682 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003683 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003684}
3685
Andrew Trickef9de2a2013-05-25 02:42:55 +00003686SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003687 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003688 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003689 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003690 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003691 case ISD::FMA: {
3692 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3693 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3694 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3695 if (N1CFP && N2CFP && N3CFP) {
3696 APFloat V1 = N1CFP->getValueAPF();
3697 const APFloat &V2 = N2CFP->getValueAPF();
3698 const APFloat &V3 = N3CFP->getValueAPF();
3699 APFloat::opStatus s =
3700 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003701 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003702 return getConstantFP(V1, DL, VT);
Owen Anderson32baf992013-05-09 22:27:13 +00003703 }
3704 break;
3705 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003706 case ISD::CONCAT_VECTORS:
3707 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3708 // one big BUILD_VECTOR.
3709 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3710 N2.getOpcode() == ISD::BUILD_VECTOR &&
3711 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003712 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3713 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003714 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3715 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003716 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003717 }
3718 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003719 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003720 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003721 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003722 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003723 break;
3724 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003725 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003726 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003727 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003728 return N2; // select true, X, Y -> X
3729 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003730 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003731
3732 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003733 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003734 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003735 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003736 case ISD::INSERT_SUBVECTOR: {
3737 SDValue Index = N3;
3738 if (VT.isSimple() && N1.getValueType().isSimple()
3739 && N2.getValueType().isSimple()) {
3740 assert(VT.isVector() && N1.getValueType().isVector() &&
3741 N2.getValueType().isVector() &&
3742 "Insert subvector VTs must be a vectors");
3743 assert(VT == N1.getValueType() &&
3744 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003745 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003746 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003747 if (isa<ConstantSDNode>(Index.getNode())) {
3748 assert((N2.getValueType().getVectorNumElements() +
3749 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003750 <= VT.getVectorNumElements())
3751 && "Insert subvector overflow!");
3752 }
3753
3754 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003755 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003756 return N2;
3757 }
3758 break;
3759 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003760 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003761 // Fold bit_convert nodes from a type to themselves.
3762 if (N1.getValueType() == VT)
3763 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003764 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003765 }
3766
Chris Lattnerf9c19152005-08-25 19:12:10 +00003767 // Memoize node if it doesn't produce a flag.
3768 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003769 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003770 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003771 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003772 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003773 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003774 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003775 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003776 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003777
Jack Carter170a5f22013-09-09 22:02:08 +00003778 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3779 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003780 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003781 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003782 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3783 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003784 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003785
Chandler Carruth41b20e72014-07-22 04:07:55 +00003786 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003787 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003788}
3789
Andrew Trickef9de2a2013-05-25 02:42:55 +00003790SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003791 SDValue N1, SDValue N2, SDValue N3,
3792 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003793 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003794 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003795}
3796
Andrew Trickef9de2a2013-05-25 02:42:55 +00003797SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003798 SDValue N1, SDValue N2, SDValue N3,
3799 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003800 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003801 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003802}
3803
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003804/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3805/// the incoming stack arguments to be loaded from the stack.
3806SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3807 SmallVector<SDValue, 8> ArgChains;
3808
3809 // Include the original chain at the beginning of the list. When this is
3810 // used by target LowerCall hooks, this helps legalize find the
3811 // CALLSEQ_BEGIN node.
3812 ArgChains.push_back(Chain);
3813
3814 // Add a chain value for each stack argument.
3815 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3816 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3817 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3818 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3819 if (FI->getIndex() < 0)
3820 ArgChains.push_back(SDValue(L, 1));
3821
3822 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003823 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003824}
3825
Dan Gohman544ab2c2008-04-12 04:36:06 +00003826/// getMemsetValue - Vectorized representation of the memset value
3827/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003828static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003829 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003830 assert(Value.getOpcode() != ISD::UNDEF);
3831
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003832 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003833 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003834 assert(C->getAPIntValue().getBitWidth() == 8);
3835 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003836 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003837 return DAG.getConstant(Val, dl, VT);
3838 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
3839 VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003840 }
Evan Chengef377ad2008-05-15 08:39:06 +00003841
Hal Finkel17b6d772015-03-31 20:35:26 +00003842 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
3843 EVT IntVT = VT.getScalarType();
3844 if (!IntVT.isInteger())
3845 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
3846
3847 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003848 if (NumBits > 8) {
3849 // Use a multiplication with 0x010101... to extend the input to the
3850 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003851 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Hal Finkel17b6d772015-03-31 20:35:26 +00003852 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003853 DAG.getConstant(Magic, dl, IntVT));
Hal Finkel17b6d772015-03-31 20:35:26 +00003854 }
3855
3856 if (VT != Value.getValueType() && !VT.isInteger())
3857 Value = DAG.getNode(ISD::BITCAST, dl, VT.getScalarType(), Value);
3858 if (VT != Value.getValueType()) {
3859 assert(VT.getVectorElementType() == Value.getValueType() &&
3860 "value type should be one vector element here");
3861 SmallVector<SDValue, 8> BVOps(VT.getVectorNumElements(), Value);
3862 Value = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, BVOps);
Evan Chengef377ad2008-05-15 08:39:06 +00003863 }
3864
3865 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003866}
3867
Dan Gohman544ab2c2008-04-12 04:36:06 +00003868/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3869/// used when a memcpy is turned into a memset when the source is a constant
3870/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003871static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003872 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003873 // Handle vector with all elements zero.
3874 if (Str.empty()) {
3875 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003876 return DAG.getConstant(0, dl, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003877 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003878 return DAG.getConstantFP(0.0, dl, VT);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003879 else if (VT.isVector()) {
3880 unsigned NumElts = VT.getVectorNumElements();
3881 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003882 return DAG.getNode(ISD::BITCAST, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003883 DAG.getConstant(0, dl,
3884 EVT::getVectorVT(*DAG.getContext(),
3885 EltVT, NumElts)));
Evan Cheng43cd9e32010-04-01 06:04:33 +00003886 } else
3887 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003888 }
3889
Duncan Sands13237ac2008-06-06 12:08:01 +00003890 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003891 unsigned NumVTBits = VT.getSizeInBits();
3892 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003893 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3894
Evan Chengc8444b12013-01-10 22:13:27 +00003895 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003896 if (TLI.isLittleEndian()) {
3897 for (unsigned i = 0; i != NumBytes; ++i)
3898 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3899 } else {
3900 for (unsigned i = 0; i != NumBytes; ++i)
3901 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003902 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003903
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003904 // If the "cost" of materializing the integer immediate is less than the cost
3905 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003906 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3907 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003908 return DAG.getConstant(Val, dl, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003909 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003910}
3911
Scott Michelcf0da6c2009-02-17 22:15:04 +00003912/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003913///
Andrew Tricke2431c62013-05-25 03:08:10 +00003914static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003915 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003916 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003917 return DAG.getNode(ISD::ADD, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003918 VT, Base, DAG.getConstant(Offset, dl, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003919}
3920
Evan Chengef377ad2008-05-15 08:39:06 +00003921/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3922///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003923static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003924 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003925 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003926 if (Src.getOpcode() == ISD::GlobalAddress)
3927 G = cast<GlobalAddressSDNode>(Src);
3928 else if (Src.getOpcode() == ISD::ADD &&
3929 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3930 Src.getOperand(1).getOpcode() == ISD::Constant) {
3931 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003932 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003933 }
3934 if (!G)
3935 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003936
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003937 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003938}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003939
Evan Cheng43cd9e32010-04-01 06:04:33 +00003940/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3941/// to replace the memset / memcpy. Return true if the number of memory ops
3942/// is below the threshold. It returns the types of the sequence of
3943/// memory ops to perform memset / memcpy by reference.
3944static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003945 unsigned Limit, uint64_t Size,
3946 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003947 bool IsMemset,
3948 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003949 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003950 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003951 SelectionDAG &DAG,
3952 const TargetLowering &TLI) {
3953 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3954 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003955 // If 'SrcAlign' is zero, that means the memory operation does not need to
3956 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3957 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3958 // is the specified alignment of the memory operation. If it is zero, that
3959 // means it's possible to change the alignment of the destination.
3960 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3961 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003962 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003963 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003964 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003965
Chris Lattner28dc6c12010-03-07 07:45:08 +00003966 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003967 unsigned AS = 0;
3968 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003969 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003970 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003971 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003972 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003973 case 0: VT = MVT::i64; break;
3974 case 4: VT = MVT::i32; break;
3975 case 2: VT = MVT::i16; break;
3976 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003977 }
3978 }
3979
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003980 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003981 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003982 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003983 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003984
Duncan Sands11dd4242008-06-08 20:54:56 +00003985 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003986 VT = LVT;
3987 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003988
Dan Gohman544ab2c2008-04-12 04:36:06 +00003989 unsigned NumMemOps = 0;
3990 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003991 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003992 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003993 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003994 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003995 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003996
3997 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003998 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003999 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00004000 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004001 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00004002 Found = true;
4003 else if (NewVT == MVT::i64 &&
4004 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004005 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00004006 // i64 is usually not legal on 32-bit targets, but f64 may be.
4007 NewVT = MVT::f64;
4008 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004009 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00004010 }
4011
Evan Cheng04e55182012-12-12 00:42:09 +00004012 if (!Found) {
4013 do {
4014 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
4015 if (NewVT == MVT::i8)
4016 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00004017 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00004018 }
4019 NewVTSize = NewVT.getSizeInBits() / 8;
4020
Evan Cheng79e2ca92012-12-10 23:21:26 +00004021 // If the new VT cannot cover all of the remaining bits, then consider
4022 // issuing a (or a pair of) unaligned and overlapping load / store.
4023 // FIXME: Only does this for 64-bit or more since we don't have proper
4024 // cost model for unaligned load / store.
4025 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00004026 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00004027 if (NumMemOps && AllowOverlap &&
4028 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00004029 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00004030 VTSize = Size;
4031 else {
4032 VT = NewVT;
4033 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00004034 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004035 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004036
Evan Chengb7d3d032012-12-12 20:43:23 +00004037 if (++NumMemOps > Limit)
4038 return false;
4039
Dan Gohman544ab2c2008-04-12 04:36:06 +00004040 MemOps.push_back(VT);
4041 Size -= VTSize;
4042 }
4043
4044 return true;
4045}
4046
Andrew Trickef9de2a2013-05-25 02:42:55 +00004047static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00004048 SDValue Chain, SDValue Dst,
4049 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004050 unsigned Align, bool isVol,
4051 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004052 MachinePointerInfo DstPtrInfo,
4053 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004054 // Turn a memcpy of undef to nop.
4055 if (Src.getOpcode() == ISD::UNDEF)
4056 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004057
Dan Gohman714663a2008-05-29 19:42:22 +00004058 // Expand memcpy to a series of load and store ops if the size operand falls
4059 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00004060 // TODO: In the AlwaysInline case, if the size is big then generate a loop
4061 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00004062 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004063 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004064 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004065 MachineFunction &MF = DAG.getMachineFunction();
4066 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004067 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004068 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4069 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4070 DstAlignCanChange = true;
4071 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4072 if (Align > SrcAlign)
4073 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004074 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004075 bool CopyFromStr = isMemSrcFromString(Src, Str);
4076 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004077 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00004078
Evan Cheng4c014c82010-04-01 18:19:11 +00004079 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004080 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00004081 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00004082 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004083 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004084
Evan Cheng43cd9e32010-04-01 06:04:33 +00004085 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004086 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004087 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00004088
4089 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00004090 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004091 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004092 if (!TRI->needsStackRealignment(MF))
4093 while (NewAlign > Align &&
4094 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4095 NewAlign /= 2;
4096
Evan Cheng43cd9e32010-04-01 06:04:33 +00004097 if (NewAlign > Align) {
4098 // Give the stack frame object a larger alignment if needed.
4099 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4100 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4101 Align = NewAlign;
4102 }
4103 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004104
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004105 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004106 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004107 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004108 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004109 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004110 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004111 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004112
Evan Cheng79e2ca92012-12-10 23:21:26 +00004113 if (VTSize > Size) {
4114 // Issuing an unaligned load / store pair that overlaps with the previous
4115 // pair. Adjust the offset accordingly.
4116 assert(i == NumMemOps-1 && i != 0);
4117 SrcOff -= VTSize - Size;
4118 DstOff -= VTSize - Size;
4119 }
4120
Evan Cheng43cd9e32010-04-01 06:04:33 +00004121 if (CopyFromStr &&
4122 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004123 // It's unlikely a store of a vector immediate can be done in a single
4124 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004125 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004126 // FIXME: Handle other cases where store of vector immediate is done in
4127 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004128 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004129 if (Value.getNode())
4130 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004131 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004132 DstPtrInfo.getWithOffset(DstOff), isVol,
4133 false, Align);
4134 }
4135
4136 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004137 // The type might not be legal for the target. This should only happen
4138 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004139 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4140 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004141 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004142 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004143 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004144 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004145 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004146 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004147 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004148 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004149 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004150 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4151 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004152 }
4153 OutChains.push_back(Store);
4154 SrcOff += VTSize;
4155 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004156 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004157 }
4158
Craig Topper48d114b2014-04-26 18:35:24 +00004159 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004160}
4161
Andrew Trickef9de2a2013-05-25 02:42:55 +00004162static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004163 SDValue Chain, SDValue Dst,
4164 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004165 unsigned Align, bool isVol,
4166 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004167 MachinePointerInfo DstPtrInfo,
4168 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004169 // Turn a memmove of undef to nop.
4170 if (Src.getOpcode() == ISD::UNDEF)
4171 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004172
4173 // Expand memmove to a series of load and store ops if the size operand falls
4174 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004175 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004176 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004177 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004178 MachineFunction &MF = DAG.getMachineFunction();
4179 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004180 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004181 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4182 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4183 DstAlignCanChange = true;
4184 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4185 if (Align > SrcAlign)
4186 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004187 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004188
Evan Cheng4c014c82010-04-01 18:19:11 +00004189 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004190 (DstAlignCanChange ? 0 : Align), SrcAlign,
4191 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004192 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004193
Evan Cheng43cd9e32010-04-01 06:04:33 +00004194 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004195 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004196 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004197 if (NewAlign > Align) {
4198 // Give the stack frame object a larger alignment if needed.
4199 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4200 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4201 Align = NewAlign;
4202 }
4203 }
Dan Gohman714663a2008-05-29 19:42:22 +00004204
Evan Cheng43cd9e32010-04-01 06:04:33 +00004205 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004206 SmallVector<SDValue, 8> LoadValues;
4207 SmallVector<SDValue, 8> LoadChains;
4208 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004209 unsigned NumMemOps = MemOps.size();
4210 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004211 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004212 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004213 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004214
Dale Johannesenabf66b82009-02-03 22:26:09 +00004215 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004216 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004217 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004218 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004219 LoadValues.push_back(Value);
4220 LoadChains.push_back(Value.getValue(1));
4221 SrcOff += VTSize;
4222 }
Craig Topper48d114b2014-04-26 18:35:24 +00004223 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004224 OutChains.clear();
4225 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004226 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004227 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004228 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004229
Dale Johannesenabf66b82009-02-03 22:26:09 +00004230 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004231 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004232 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004233 OutChains.push_back(Store);
4234 DstOff += VTSize;
4235 }
4236
Craig Topper48d114b2014-04-26 18:35:24 +00004237 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004238}
4239
Serge Pavlov8ec39992013-09-17 16:24:42 +00004240/// \brief Lower the call to 'memset' intrinsic function into a series of store
4241/// operations.
4242///
4243/// \param DAG Selection DAG where lowered code is placed.
4244/// \param dl Link to corresponding IR location.
4245/// \param Chain Control flow dependency.
4246/// \param Dst Pointer to destination memory location.
4247/// \param Src Value of byte to write into the memory.
4248/// \param Size Number of bytes to write.
4249/// \param Align Alignment of the destination in bytes.
4250/// \param isVol True if destination is volatile.
4251/// \param DstPtrInfo IR information on the memory pointer.
4252/// \returns New head in the control flow, if lowering was successful, empty
4253/// SDValue otherwise.
4254///
4255/// The function tries to replace 'llvm.memset' intrinsic with several store
4256/// operations and value calculation code. This is usually profitable for small
4257/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004258static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004259 SDValue Chain, SDValue Dst,
4260 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004261 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004262 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004263 // Turn a memset of undef to nop.
4264 if (Src.getOpcode() == ISD::UNDEF)
4265 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004266
4267 // Expand memset to a series of load/store ops if the size operand
4268 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004269 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004270 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004271 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004272 MachineFunction &MF = DAG.getMachineFunction();
4273 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004274 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004275 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4276 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4277 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004278 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004279 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004280 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004281 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004282 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004283 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004284
Evan Cheng43cd9e32010-04-01 06:04:33 +00004285 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004286 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004287 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004288 if (NewAlign > Align) {
4289 // Give the stack frame object a larger alignment if needed.
4290 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4291 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4292 Align = NewAlign;
4293 }
4294 }
4295
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004296 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004297 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004298 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004299
4300 // Find the largest store and generate the bit pattern for it.
4301 EVT LargestVT = MemOps[0];
4302 for (unsigned i = 1; i < NumMemOps; i++)
4303 if (MemOps[i].bitsGT(LargestVT))
4304 LargestVT = MemOps[i];
4305 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4306
Dan Gohman544ab2c2008-04-12 04:36:06 +00004307 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004308 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004309 unsigned VTSize = VT.getSizeInBits() / 8;
4310 if (VTSize > Size) {
4311 // Issuing an unaligned load / store pair that overlaps with the previous
4312 // pair. Adjust the offset accordingly.
4313 assert(i == NumMemOps-1 && i != 0);
4314 DstOff -= VTSize - Size;
4315 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004316
4317 // If this store is smaller than the largest store see whether we can get
4318 // the smaller value for free with a truncate.
4319 SDValue Value = MemSetValue;
4320 if (VT.bitsLT(LargestVT)) {
4321 if (!LargestVT.isVector() && !VT.isVector() &&
4322 TLI.isTruncateFree(LargestVT, VT))
4323 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4324 else
4325 Value = getMemsetValue(Src, VT, DAG, dl);
4326 }
4327 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004328 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004329 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004330 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004331 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004332 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004333 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004334 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004335 }
4336
Craig Topper48d114b2014-04-26 18:35:24 +00004337 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004338}
4339
Andrew Trickef9de2a2013-05-25 02:42:55 +00004340SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004341 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004342 unsigned Align, bool isVol, bool AlwaysInline,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004343 bool isTailCall, MachinePointerInfo DstPtrInfo,
Chris Lattner2510de22010-09-21 05:40:29 +00004344 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004345 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004346
4347 // Check to see if we should lower the memcpy to loads and stores first.
4348 // For cases within the target-specified limits, this is the best choice.
4349 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4350 if (ConstantSize) {
4351 // Memcpy with size zero? Just return the original chain.
4352 if (ConstantSize->isNullValue())
4353 return Chain;
4354
Evan Cheng43cd9e32010-04-01 06:04:33 +00004355 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4356 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004357 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004358 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004359 return Result;
4360 }
4361
4362 // Then check to see if we should lower the memcpy with target-specific
4363 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004364 if (TSI) {
4365 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4366 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4367 DstPtrInfo, SrcPtrInfo);
4368 if (Result.getNode())
4369 return Result;
4370 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004371
4372 // If we really need inline code and the target declined to provide it,
4373 // use a (potentially long) sequence of loads and stores.
4374 if (AlwaysInline) {
4375 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004376 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004377 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004378 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004379 }
4380
Dan Gohmanf38547c2010-04-05 20:24:08 +00004381 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4382 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4383 // respect volatile, so they may do things like read or write memory
4384 // beyond the given memory regions. But fixing this isn't easy, and most
4385 // people don't care.
4386
Dan Gohman544ab2c2008-04-12 04:36:06 +00004387 // Emit a library call.
4388 TargetLowering::ArgListTy Args;
4389 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004390 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004391 Entry.Node = Dst; Args.push_back(Entry);
4392 Entry.Node = Src; Args.push_back(Entry);
4393 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004394 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004395 TargetLowering::CallLoweringInfo CLI(*this);
4396 CLI.setDebugLoc(dl).setChain(Chain)
4397 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4398 Type::getVoidTy(*getContext()),
4399 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004400 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004401 .setDiscardResult()
4402 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004403
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004404 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004405 return CallResult.second;
4406}
4407
Andrew Trickef9de2a2013-05-25 02:42:55 +00004408SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004409 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004410 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004411 MachinePointerInfo DstPtrInfo,
4412 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004413 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004414
Dan Gohman714663a2008-05-29 19:42:22 +00004415 // Check to see if we should lower the memmove to loads and stores first.
4416 // For cases within the target-specified limits, this is the best choice.
4417 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4418 if (ConstantSize) {
4419 // Memmove with size zero? Just return the original chain.
4420 if (ConstantSize->isNullValue())
4421 return Chain;
4422
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004423 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004424 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004425 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004426 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004427 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004428 return Result;
4429 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004430
4431 // Then check to see if we should lower the memmove with target-specific
4432 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004433 if (TSI) {
4434 SDValue Result = TSI->EmitTargetCodeForMemmove(
4435 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4436 if (Result.getNode())
4437 return Result;
4438 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004439
Mon P Wangbf862242010-04-06 08:27:51 +00004440 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4441 // not be safe. See memcpy above for more details.
4442
Dan Gohman544ab2c2008-04-12 04:36:06 +00004443 // Emit a library call.
4444 TargetLowering::ArgListTy Args;
4445 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004446 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004447 Entry.Node = Dst; Args.push_back(Entry);
4448 Entry.Node = Src; Args.push_back(Entry);
4449 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004450 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004451 TargetLowering::CallLoweringInfo CLI(*this);
4452 CLI.setDebugLoc(dl).setChain(Chain)
4453 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4454 Type::getVoidTy(*getContext()),
4455 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004456 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004457 .setDiscardResult()
4458 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004459
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004460 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004461 return CallResult.second;
4462}
4463
Andrew Trickef9de2a2013-05-25 02:42:55 +00004464SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004465 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004466 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004467 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004468 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004469
4470 // Check to see if we should lower the memset to stores first.
4471 // For cases within the target-specified limits, this is the best choice.
4472 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4473 if (ConstantSize) {
4474 // Memset with size zero? Just return the original chain.
4475 if (ConstantSize->isNullValue())
4476 return Chain;
4477
Mon P Wangc576ee92010-04-04 03:10:48 +00004478 SDValue Result =
4479 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004480 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004481
Gabor Greiff304a7a2008-08-28 21:40:38 +00004482 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004483 return Result;
4484 }
4485
4486 // Then check to see if we should lower the memset with target-specific
4487 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004488 if (TSI) {
4489 SDValue Result = TSI->EmitTargetCodeForMemset(
4490 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4491 if (Result.getNode())
4492 return Result;
4493 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004494
Wesley Peck527da1b2010-11-23 03:31:01 +00004495 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004496 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004497 TargetLowering::ArgListTy Args;
4498 TargetLowering::ArgListEntry Entry;
4499 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4500 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004501 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004502 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004503 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004504 Entry.Node = Size;
4505 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004506 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004507
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004508 // FIXME: pass in SDLoc
4509 TargetLowering::CallLoweringInfo CLI(*this);
4510 CLI.setDebugLoc(dl).setChain(Chain)
4511 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4512 Type::getVoidTy(*getContext()),
4513 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004514 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004515 .setDiscardResult()
4516 .setTailCall(isTailCall);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004517
4518 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004519 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004520}
4521
Andrew Trickef9de2a2013-05-25 02:42:55 +00004522SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004523 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004524 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004525 AtomicOrdering SuccessOrdering,
4526 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004527 SynchronizationScope SynchScope) {
4528 FoldingSetNodeID ID;
4529 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004530 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004531 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004532 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004533 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4534 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4535 return SDValue(E, 0);
4536 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004537
4538 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4539 // SDNode doesn't have access to it. This memory will be "leaked" when
4540 // the node is deallocated, but recovered when the allocator is released.
4541 // If the number of operands is less than 5 we use AtomicSDNode's internal
4542 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004543 unsigned NumOps = Ops.size();
4544 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4545 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004546
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004547 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4548 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004549 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004550 SuccessOrdering, FailureOrdering,
4551 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004552 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004553 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004554 return SDValue(N, 0);
4555}
4556
4557SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004558 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004559 MachineMemOperand *MMO,
4560 AtomicOrdering Ordering,
4561 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004562 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004563 Ordering, SynchScope);
4564}
4565
Tim Northover420a2162014-06-13 14:24:07 +00004566SDValue SelectionDAG::getAtomicCmpSwap(
4567 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4568 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4569 unsigned Alignment, AtomicOrdering SuccessOrdering,
4570 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4571 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4572 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4573 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4574
Dan Gohman48b185d2009-09-25 20:36:54 +00004575 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4576 Alignment = getEVTAlignment(MemVT);
4577
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004578 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004579
Eli Friedmane978d2f2011-09-07 02:23:42 +00004580 // FIXME: Volatile isn't really correct; we should keep track of atomic
4581 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004582 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004583 Flags |= MachineMemOperand::MOLoad;
4584 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004585
4586 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004587 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004588
Tim Northover420a2162014-06-13 14:24:07 +00004589 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4590 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004591}
4592
Tim Northover420a2162014-06-13 14:24:07 +00004593SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4594 SDVTList VTs, SDValue Chain, SDValue Ptr,
4595 SDValue Cmp, SDValue Swp,
4596 MachineMemOperand *MMO,
4597 AtomicOrdering SuccessOrdering,
4598 AtomicOrdering FailureOrdering,
4599 SynchronizationScope SynchScope) {
4600 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4601 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004602 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4603
Dale Johannesen839acbb2009-01-29 00:47:48 +00004604 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004605 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4606 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004607}
4608
Andrew Trickef9de2a2013-05-25 02:42:55 +00004609SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004610 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004611 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004612 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004613 unsigned Alignment,
4614 AtomicOrdering Ordering,
4615 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004616 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4617 Alignment = getEVTAlignment(MemVT);
4618
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004619 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004620 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004621 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004622 // For now, atomics are considered to be volatile always, and they are
4623 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004624 // FIXME: Volatile isn't really correct; we should keep track of atomic
4625 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004626 unsigned Flags = MachineMemOperand::MOVolatile;
4627 if (Opcode != ISD::ATOMIC_STORE)
4628 Flags |= MachineMemOperand::MOLoad;
4629 if (Opcode != ISD::ATOMIC_LOAD)
4630 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004631
4632 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004633 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004634 MemVT.getStoreSize(), Alignment);
4635
Eli Friedmanadec5872011-07-29 03:05:32 +00004636 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4637 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004638}
4639
Andrew Trickef9de2a2013-05-25 02:42:55 +00004640SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004641 SDValue Chain,
4642 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004643 MachineMemOperand *MMO,
4644 AtomicOrdering Ordering,
4645 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004646 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4647 Opcode == ISD::ATOMIC_LOAD_SUB ||
4648 Opcode == ISD::ATOMIC_LOAD_AND ||
4649 Opcode == ISD::ATOMIC_LOAD_OR ||
4650 Opcode == ISD::ATOMIC_LOAD_XOR ||
4651 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004652 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004653 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004654 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004655 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004656 Opcode == ISD::ATOMIC_SWAP ||
4657 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004658 "Invalid Atomic Op");
4659
Owen Anderson53aa7a92009-08-10 22:56:29 +00004660 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004661
Eli Friedman342e8df2011-08-24 20:50:09 +00004662 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4663 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004664 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004665 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004666}
4667
Andrew Trickef9de2a2013-05-25 02:42:55 +00004668SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004669 EVT VT, SDValue Chain,
4670 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004671 MachineMemOperand *MMO,
4672 AtomicOrdering Ordering,
4673 SynchronizationScope SynchScope) {
4674 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4675
4676 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004677 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004678 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004679}
4680
Duncan Sands739a0542008-07-02 17:40:58 +00004681/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004682SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4683 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004684 return Ops[0];
4685
Owen Anderson53aa7a92009-08-10 22:56:29 +00004686 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004687 VTs.reserve(Ops.size());
4688 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004689 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004690 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004691}
4692
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004693SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004694SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004695 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004696 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004697 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004698 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004699 if (Align == 0) // Ensure that codegen never sees alignment 0
4700 Align = getEVTAlignment(MemVT);
4701
4702 MachineFunction &MF = getMachineFunction();
4703 unsigned Flags = 0;
4704 if (WriteMem)
4705 Flags |= MachineMemOperand::MOStore;
4706 if (ReadMem)
4707 Flags |= MachineMemOperand::MOLoad;
4708 if (Vol)
4709 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004710 if (!Size)
4711 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004712 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004713 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004714
Craig Topper206fcd42014-04-26 19:29:41 +00004715 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004716}
4717
4718SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004719SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004720 ArrayRef<SDValue> Ops, EVT MemVT,
4721 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004722 assert((Opcode == ISD::INTRINSIC_VOID ||
4723 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004724 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004725 Opcode == ISD::LIFETIME_START ||
4726 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004727 (Opcode <= INT_MAX &&
4728 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4729 "Opcode is not a memory-accessing opcode!");
4730
Dale Johannesen839acbb2009-01-29 00:47:48 +00004731 // Memoize the node unless it returns a flag.
4732 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004733 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004734 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004735 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004736 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004737 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004738 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4739 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004740 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004741 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004742
Jack Carter170a5f22013-09-09 22:02:08 +00004743 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4744 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004745 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004746 CSEMap.InsertNode(N, IP);
4747 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004748 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4749 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004750 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004751 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004752 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004753 return SDValue(N, 0);
4754}
4755
Chris Lattnerea952f02010-09-21 17:24:05 +00004756/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4757/// MachinePointerInfo record from it. This is particularly useful because the
4758/// code generator has many cases where it doesn't bother passing in a
4759/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4760static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4761 // If this is FI+Offset, we can model it.
4762 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4763 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4764
4765 // If this is (FI+Offset1)+Offset2, we can model it.
4766 if (Ptr.getOpcode() != ISD::ADD ||
4767 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4768 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4769 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004770
Chris Lattnerea952f02010-09-21 17:24:05 +00004771 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4772 return MachinePointerInfo::getFixedStack(FI, Offset+
4773 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4774}
4775
4776/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4777/// MachinePointerInfo record from it. This is particularly useful because the
4778/// code generator has many cases where it doesn't bother passing in a
4779/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4780static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4781 // If the 'Offset' value isn't a constant, we can't handle this.
4782 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4783 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4784 if (OffsetOp.getOpcode() == ISD::UNDEF)
4785 return InferPointerInfo(Ptr);
4786 return MachinePointerInfo();
4787}
Wesley Peck527da1b2010-11-23 03:31:01 +00004788
Chris Lattnerea952f02010-09-21 17:24:05 +00004789
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004790SDValue
4791SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004792 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004793 SDValue Ptr, SDValue Offset,
4794 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004795 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004796 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004797 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004798 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004799 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004800 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4801 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004802
Dan Gohman48b185d2009-09-25 20:36:54 +00004803 unsigned Flags = MachineMemOperand::MOLoad;
4804 if (isVolatile)
4805 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004806 if (isNonTemporal)
4807 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004808 if (isInvariant)
4809 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004810
Chris Lattnerea952f02010-09-21 17:24:05 +00004811 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4812 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004813 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004814 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004815
Chris Lattnerea952f02010-09-21 17:24:05 +00004816 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004817 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004818 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004819 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004820 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004821}
4822
4823SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004824SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004825 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004826 SDValue Ptr, SDValue Offset, EVT MemVT,
4827 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004828 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004829 ExtType = ISD::NON_EXTLOAD;
4830 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004831 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004832 } else {
4833 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004834 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4835 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004836 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004837 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004838 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004839 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004840 assert((!VT.isVector() ||
4841 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004842 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004843 }
4844
4845 bool Indexed = AM != ISD::UNINDEXED;
4846 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4847 "Unindexed load with an offset!");
4848
4849 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004850 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004851 SDValue Ops[] = { Chain, Ptr, Offset };
4852 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004853 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004854 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004855 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004856 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004857 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004858 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004859 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4861 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004862 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004863 }
Jack Carter170a5f22013-09-09 22:02:08 +00004864 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4865 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004866 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004867 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004868 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004869 return SDValue(N, 0);
4870}
4871
Andrew Trickef9de2a2013-05-25 02:42:55 +00004872SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004873 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004874 MachinePointerInfo PtrInfo,
4875 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004876 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004877 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004878 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004879 SDValue Undef = getUNDEF(Ptr.getValueType());
4880 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004881 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004882 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004883}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004884
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004885SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4886 SDValue Chain, SDValue Ptr,
4887 MachineMemOperand *MMO) {
4888 SDValue Undef = getUNDEF(Ptr.getValueType());
4889 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4890 VT, MMO);
4891}
4892
Andrew Trickef9de2a2013-05-25 02:42:55 +00004893SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004894 SDValue Chain, SDValue Ptr,
4895 MachinePointerInfo PtrInfo, EVT MemVT,
4896 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004897 bool isInvariant, unsigned Alignment,
4898 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004899 SDValue Undef = getUNDEF(Ptr.getValueType());
4900 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004901 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4902 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004903}
4904
4905
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004906SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4907 SDValue Chain, SDValue Ptr, EVT MemVT,
4908 MachineMemOperand *MMO) {
4909 SDValue Undef = getUNDEF(Ptr.getValueType());
4910 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4911 MemVT, MMO);
4912}
4913
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004914SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004915SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004916 SDValue Offset, ISD::MemIndexedMode AM) {
4917 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4918 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4919 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004920 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004921 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004922 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004923 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004924}
4925
Andrew Trickef9de2a2013-05-25 02:42:55 +00004926SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004927 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004928 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004929 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004930 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004931 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004932 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004933 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004934
Dan Gohman48b185d2009-09-25 20:36:54 +00004935 unsigned Flags = MachineMemOperand::MOStore;
4936 if (isVolatile)
4937 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004938 if (isNonTemporal)
4939 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004940
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004941 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004942 PtrInfo = InferPointerInfo(Ptr);
4943
4944 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004945 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004946 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004947 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004948 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004949
4950 return getStore(Chain, dl, Val, Ptr, MMO);
4951}
4952
Andrew Trickef9de2a2013-05-25 02:42:55 +00004953SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004954 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004955 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004956 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004957 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004958 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004959 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004960 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4961 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004962 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004963 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004964 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004965 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004966 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004967 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004968 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4969 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004970 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004971 }
Jack Carter170a5f22013-09-09 22:02:08 +00004972 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4973 dl.getDebugLoc(), VTs,
4974 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004975 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004976 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004977 return SDValue(N, 0);
4978}
4979
Andrew Trickef9de2a2013-05-25 02:42:55 +00004980SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004981 SDValue Ptr, MachinePointerInfo PtrInfo,
4982 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004983 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004984 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004985 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004986 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004987 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4988 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004989
Dan Gohman48b185d2009-09-25 20:36:54 +00004990 unsigned Flags = MachineMemOperand::MOStore;
4991 if (isVolatile)
4992 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004993 if (isNonTemporal)
4994 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004995
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004996 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004997 PtrInfo = InferPointerInfo(Ptr);
4998
4999 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00005000 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00005001 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00005002 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00005003
5004 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
5005}
5006
Andrew Trickef9de2a2013-05-25 02:42:55 +00005007SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00005008 SDValue Ptr, EVT SVT,
5009 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005010 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00005011
Michael Ilsemand5f91512012-09-10 16:56:31 +00005012 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005013 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005014 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00005015 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005016
Dan Gohmancecad352009-12-14 23:40:38 +00005017 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
5018 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005019 assert(VT.isInteger() == SVT.isInteger() &&
5020 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00005021 assert(VT.isVector() == SVT.isVector() &&
5022 "Cannot use trunc store to convert to or from a vector!");
5023 assert((!VT.isVector() ||
5024 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
5025 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005026
Owen Anderson9f944592009-08-11 20:47:22 +00005027 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00005028 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005029 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5030 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005031 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005032 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00005033 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005034 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00005035 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005036 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00005037 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5038 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005039 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005040 }
Jack Carter170a5f22013-09-09 22:02:08 +00005041 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5042 dl.getDebugLoc(), VTs,
5043 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005044 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005045 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005046 return SDValue(N, 0);
5047}
5048
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005049SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005050SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00005051 SDValue Offset, ISD::MemIndexedMode AM) {
5052 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5053 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
5054 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00005055 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005056 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5057 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005058 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005059 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00005060 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00005061 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005062 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005063 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00005064 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005065
Jack Carter170a5f22013-09-09 22:02:08 +00005066 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5067 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00005068 ST->isTruncatingStore(),
5069 ST->getMemoryVT(),
5070 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005071 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005072 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005073 return SDValue(N, 0);
5074}
5075
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005076SDValue
5077SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005078 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5079 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005080
5081 SDVTList VTs = getVTList(VT, MVT::Other);
5082 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5083 FoldingSetNodeID ID;
5084 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5085 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005086 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005087 MMO->isVolatile(),
5088 MMO->isNonTemporal(),
5089 MMO->isInvariant()));
5090 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5091 void *IP = nullptr;
5092 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5093 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5094 return SDValue(E, 0);
5095 }
5096 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5097 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005098 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005099 CSEMap.InsertNode(N, IP);
5100 InsertNode(N);
5101 return SDValue(N, 0);
5102}
5103
5104SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005105 SDValue Ptr, SDValue Mask, EVT MemVT,
5106 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005107 assert(Chain.getValueType() == MVT::Other &&
5108 "Invalid chain type");
5109 EVT VT = Val.getValueType();
5110 SDVTList VTs = getVTList(MVT::Other);
5111 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5112 FoldingSetNodeID ID;
5113 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5114 ID.AddInteger(VT.getRawBits());
5115 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5116 MMO->isNonTemporal(), MMO->isInvariant()));
5117 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5118 void *IP = nullptr;
5119 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5120 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5121 return SDValue(E, 0);
5122 }
5123 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5124 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005125 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005126 CSEMap.InsertNode(N, IP);
5127 InsertNode(N);
5128 return SDValue(N, 0);
5129}
5130
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005131SDValue
5132SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl,
5133 ArrayRef<SDValue> Ops,
5134 MachineMemOperand *MMO) {
5135
5136 FoldingSetNodeID ID;
5137 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5138 ID.AddInteger(VT.getRawBits());
5139 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5140 MMO->isVolatile(),
5141 MMO->isNonTemporal(),
5142 MMO->isInvariant()));
5143 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5144 void *IP = nullptr;
5145 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5146 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5147 return SDValue(E, 0);
5148 }
5149 MaskedGatherSDNode *N =
5150 new (NodeAllocator) MaskedGatherSDNode(dl.getIROrder(), dl.getDebugLoc(),
5151 Ops, VTs, VT, MMO);
5152 CSEMap.InsertNode(N, IP);
5153 InsertNode(N);
5154 return SDValue(N, 0);
5155}
5156
5157SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl,
5158 ArrayRef<SDValue> Ops,
5159 MachineMemOperand *MMO) {
5160 FoldingSetNodeID ID;
5161 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5162 ID.AddInteger(VT.getRawBits());
5163 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5164 MMO->isNonTemporal(),
5165 MMO->isInvariant()));
5166 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5167 void *IP = nullptr;
5168 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5169 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5170 return SDValue(E, 0);
5171 }
5172 SDNode *N =
5173 new (NodeAllocator) MaskedScatterSDNode(dl.getIROrder(), dl.getDebugLoc(),
5174 Ops, VTs, VT, MMO);
5175 CSEMap.InsertNode(N, IP);
5176 InsertNode(N);
5177 return SDValue(N, 0);
5178}
5179
Andrew Trickef9de2a2013-05-25 02:42:55 +00005180SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005181 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005182 SDValue SV,
5183 unsigned Align) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005184 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005185 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005186}
5187
Andrew Trickef9de2a2013-05-25 02:42:55 +00005188SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005189 ArrayRef<SDUse> Ops) {
5190 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005191 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005192 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005193 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5194 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005195 default: break;
5196 }
5197
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005198 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005199 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005200 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005201 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005202}
5203
Andrew Trickef9de2a2013-05-25 02:42:55 +00005204SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005205 ArrayRef<SDValue> Ops) {
5206 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005207 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005208 case 0: return getNode(Opcode, DL, VT);
5209 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5210 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5211 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005212 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005213 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005214
Chris Lattnerb0713c72005-04-09 03:27:28 +00005215 switch (Opcode) {
5216 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005217 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005218 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005219 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5220 "LHS and RHS of condition must have same type!");
5221 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5222 "True and False arms of SelectCC must have same type!");
5223 assert(Ops[2].getValueType() == VT &&
5224 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005225 break;
5226 }
5227 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005228 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005229 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5230 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005231 break;
5232 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005233 }
5234
Chris Lattner566307f2005-05-14 07:42:29 +00005235 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005236 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005237 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005238
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005239 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005240 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005241 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005242 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005243
Bill Wendling022d18f2009-12-18 23:32:53 +00005244 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005245 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005246
Jack Carter170a5f22013-09-09 22:02:08 +00005247 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005248 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005249 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005250 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005251 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005252 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005253 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005254
Chandler Carruth41b20e72014-07-22 04:07:55 +00005255 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005256 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005257}
5258
Andrew Trickef9de2a2013-05-25 02:42:55 +00005259SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005260 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5261 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005262}
5263
Andrew Trickef9de2a2013-05-25 02:42:55 +00005264SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005265 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005266 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005267 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005268
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005269#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005270 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005271 // FIXME: figure out how to safely handle things like
5272 // int foo(int x) { return 1 << (x & 255); }
5273 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005274 case ISD::SRA_PARTS:
5275 case ISD::SRL_PARTS:
5276 case ISD::SHL_PARTS:
5277 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005278 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005279 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005280 else if (N3.getOpcode() == ISD::AND)
5281 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5282 // If the and is only masking out bits that cannot effect the shift,
5283 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005284 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005285 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005286 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005287 }
5288 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005289 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005290#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005291
Chris Lattnerf9c19152005-08-25 19:12:10 +00005292 // Memoize the node unless it returns a flag.
5293 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005294 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005295 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005296 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005297 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005298 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005299 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005300 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005301
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005302 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005303 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5304 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005305 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005306 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5307 DL.getDebugLoc(), VTList, Ops[0],
5308 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005309 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005310 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5311 DL.getDebugLoc(), VTList, Ops[0],
5312 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005313 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005314 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005315 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005316 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005317 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005318 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005319 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005320 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5321 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005322 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005323 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5324 DL.getDebugLoc(), VTList, Ops[0],
5325 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005326 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005327 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5328 DL.getDebugLoc(), VTList, Ops[0],
5329 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005330 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005331 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005332 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005333 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005334 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005335 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005336 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005337}
5338
Andrew Trickef9de2a2013-05-25 02:42:55 +00005339SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005340 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005341}
5342
Andrew Trickef9de2a2013-05-25 02:42:55 +00005343SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005344 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005345 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005346 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005347}
5348
Andrew Trickef9de2a2013-05-25 02:42:55 +00005349SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005350 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005351 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005352 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005353}
5354
Andrew Trickef9de2a2013-05-25 02:42:55 +00005355SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005356 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005357 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005358 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005359}
5360
Andrew Trickef9de2a2013-05-25 02:42:55 +00005361SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005362 SDValue N1, SDValue N2, SDValue N3,
5363 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005364 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005365 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005366}
5367
Andrew Trickef9de2a2013-05-25 02:42:55 +00005368SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005369 SDValue N1, SDValue N2, SDValue N3,
5370 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005371 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005372 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005373}
5374
Owen Anderson53aa7a92009-08-10 22:56:29 +00005375SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005376 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005377}
5378
Owen Anderson53aa7a92009-08-10 22:56:29 +00005379SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005380 FoldingSetNodeID ID;
5381 ID.AddInteger(2U);
5382 ID.AddInteger(VT1.getRawBits());
5383 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005384
Craig Topperc0196b12014-04-14 00:51:57 +00005385 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005386 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005387 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005388 EVT *Array = Allocator.Allocate<EVT>(2);
5389 Array[0] = VT1;
5390 Array[1] = VT2;
5391 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5392 VTListMap.InsertNode(Result, IP);
5393 }
5394 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005395}
Dan Gohman17059682008-07-17 19:10:17 +00005396
Owen Anderson53aa7a92009-08-10 22:56:29 +00005397SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005398 FoldingSetNodeID ID;
5399 ID.AddInteger(3U);
5400 ID.AddInteger(VT1.getRawBits());
5401 ID.AddInteger(VT2.getRawBits());
5402 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005403
Craig Topperc0196b12014-04-14 00:51:57 +00005404 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005405 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005406 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005407 EVT *Array = Allocator.Allocate<EVT>(3);
5408 Array[0] = VT1;
5409 Array[1] = VT2;
5410 Array[2] = VT3;
5411 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5412 VTListMap.InsertNode(Result, IP);
5413 }
5414 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005415}
5416
Owen Anderson53aa7a92009-08-10 22:56:29 +00005417SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005418 FoldingSetNodeID ID;
5419 ID.AddInteger(4U);
5420 ID.AddInteger(VT1.getRawBits());
5421 ID.AddInteger(VT2.getRawBits());
5422 ID.AddInteger(VT3.getRawBits());
5423 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005424
Craig Topperc0196b12014-04-14 00:51:57 +00005425 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005426 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005427 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005428 EVT *Array = Allocator.Allocate<EVT>(4);
5429 Array[0] = VT1;
5430 Array[1] = VT2;
5431 Array[2] = VT3;
5432 Array[3] = VT4;
5433 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5434 VTListMap.InsertNode(Result, IP);
5435 }
5436 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005437}
5438
Craig Topperabb4ac72014-04-16 06:10:51 +00005439SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5440 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005441 FoldingSetNodeID ID;
5442 ID.AddInteger(NumVTs);
5443 for (unsigned index = 0; index < NumVTs; index++) {
5444 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005445 }
5446
Craig Topperc0196b12014-04-14 00:51:57 +00005447 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005448 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005449 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005450 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005451 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005452 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5453 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005454 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005455 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005456}
5457
5458
Chris Lattnerf34156e2006-01-28 09:32:45 +00005459/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5460/// specified operands. If the resultant node already exists in the DAG,
5461/// this does not modify the specified node, instead it returns the node that
5462/// already exists. If the resultant node does not exist in the DAG, the
5463/// input node is returned. As a degenerate case, if you specify the same
5464/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005465SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005466 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005467
Chris Lattnerf34156e2006-01-28 09:32:45 +00005468 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005469 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005470
Chris Lattnerf34156e2006-01-28 09:32:45 +00005471 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005472 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005473 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005474 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005475
Dan Gohmanebeccb42008-07-21 22:38:59 +00005476 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005477 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005478 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005479 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005480
Chris Lattnerf34156e2006-01-28 09:32:45 +00005481 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005482 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005483
Chris Lattnerf34156e2006-01-28 09:32:45 +00005484 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005485 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005486 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005487}
5488
Dan Gohman92c11ac2010-06-18 15:30:29 +00005489SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005490 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005491
Chris Lattnerf34156e2006-01-28 09:32:45 +00005492 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005493 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005494 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005495
Chris Lattnerf34156e2006-01-28 09:32:45 +00005496 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005497 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005498 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005499 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005500
Dan Gohmanebeccb42008-07-21 22:38:59 +00005501 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005502 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005503 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005504 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005505
Chris Lattnerf34156e2006-01-28 09:32:45 +00005506 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005507 if (N->OperandList[0] != Op1)
5508 N->OperandList[0].set(Op1);
5509 if (N->OperandList[1] != Op2)
5510 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005511
Chris Lattnerf34156e2006-01-28 09:32:45 +00005512 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005513 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005514 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005515}
5516
Dan Gohman92c11ac2010-06-18 15:30:29 +00005517SDNode *SelectionDAG::
5518UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005519 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005520 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005521}
5522
Dan Gohman92c11ac2010-06-18 15:30:29 +00005523SDNode *SelectionDAG::
5524UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005525 SDValue Op3, SDValue Op4) {
5526 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005527 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005528}
5529
Dan Gohman92c11ac2010-06-18 15:30:29 +00005530SDNode *SelectionDAG::
5531UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005532 SDValue Op3, SDValue Op4, SDValue Op5) {
5533 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005534 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005535}
5536
Dan Gohman92c11ac2010-06-18 15:30:29 +00005537SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005538UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5539 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005540 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005541 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005542
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005543 // If no operands changed just return the input node.
Benjamin Kramer0b6742a2015-03-02 16:45:08 +00005544 if (Ops.empty() || std::equal(Ops.begin(), Ops.end(), N->op_begin()))
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005545 return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005546
Chris Lattnerf34156e2006-01-28 09:32:45 +00005547 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005548 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005549 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005550 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005551
Dan Gohman2f83b472008-05-02 00:05:03 +00005552 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005553 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005554 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005555 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005556
Chris Lattnerf34156e2006-01-28 09:32:45 +00005557 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005558 for (unsigned i = 0; i != NumOps; ++i)
5559 if (N->OperandList[i] != Ops[i])
5560 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005561
5562 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005563 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005564 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005565}
5566
Dan Gohman91697632008-07-07 20:57:48 +00005567/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005568/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005569void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005570 // Unlike the code in MorphNodeTo that does this, we don't need to
5571 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005572 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5573 SDUse &Use = *I++;
5574 Use.set(SDValue());
5575 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005576}
Chris Lattner19732782005-08-16 18:17:10 +00005577
Dan Gohman17059682008-07-17 19:10:17 +00005578/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5579/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005580///
Dan Gohman17059682008-07-17 19:10:17 +00005581SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005582 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005583 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005584 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005585}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005586
Dan Gohman17059682008-07-17 19:10:17 +00005587SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005588 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005589 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005590 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005591 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005592}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005593
Dan Gohman17059682008-07-17 19:10:17 +00005594SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005595 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005596 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005597 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005598 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005599 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005600}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005601
Dan Gohman17059682008-07-17 19:10:17 +00005602SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005603 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005604 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005605 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005606 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005607 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005608}
Chris Lattner466fece2005-08-21 22:30:30 +00005609
Dan Gohman17059682008-07-17 19:10:17 +00005610SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005611 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005612 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005613 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005614}
5615
Dan Gohman17059682008-07-17 19:10:17 +00005616SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005617 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005618 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005619 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005620}
5621
Dan Gohman17059682008-07-17 19:10:17 +00005622SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005623 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005624 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005625 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005626}
5627
Dan Gohman17059682008-07-17 19:10:17 +00005628SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005629 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005630 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005631 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005632 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005633}
5634
Bill Wendling2d598632008-12-01 23:28:22 +00005635SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005636 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005637 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005638 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005639 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005640}
5641
Scott Michelcf0da6c2009-02-17 22:15:04 +00005642SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005643 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005644 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005645 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005646 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005647 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005648}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005649
Scott Michelcf0da6c2009-02-17 22:15:04 +00005650SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005651 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005652 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005653 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005654 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005655 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005656}
5657
Dan Gohman17059682008-07-17 19:10:17 +00005658SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005659 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005660 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005661 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005662 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005663 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005664 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005665}
5666
Dan Gohman17059682008-07-17 19:10:17 +00005667SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005668 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005669 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005670 SDValue Op3) {
5671 SDVTList VTs = getVTList(VT1, VT2, VT3);
5672 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005673 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005674}
5675
5676SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005677 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005678 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005679 // Reset the NodeID to -1.
5680 N->setNodeId(-1);
5681 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005682}
5683
Andrew Trickef9de2a2013-05-25 02:42:55 +00005684/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005685/// the line number information on the merged node since it is not possible to
5686/// preserve the information that operation is associated with multiple lines.
5687/// This will make the debugger working better at -O0, were there is a higher
5688/// probability having other instructions associated with that line.
5689///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005690/// For IROrder, we keep the smaller of the two
5691SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005692 DebugLoc NLoc = N->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00005693 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005694 N->setDebugLoc(DebugLoc());
5695 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005696 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5697 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005698 return N;
5699}
5700
Chris Lattneraf197502010-02-28 21:36:14 +00005701/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005702/// return type, opcode, and operands.
5703///
5704/// Note that MorphNodeTo returns the resultant node. If there is already a
5705/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005706/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005707///
5708/// Using MorphNodeTo is faster than creating a new node and swapping it in
5709/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005710/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005711/// the node's users.
5712///
Chandler Carruth356665a2014-08-01 22:09:43 +00005713/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5714/// As a consequence it isn't appropriate to use from within the DAG combiner or
5715/// the legalizer which maintain worklists that would need to be updated when
5716/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005717SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005718 SDVTList VTs, ArrayRef<SDValue> Ops) {
5719 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005720 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005721 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005722 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005723 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005724 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005725 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005726 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005727 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005728
Dan Gohmand3fe1742008-09-13 01:54:27 +00005729 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005730 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005731
Dan Gohman17059682008-07-17 19:10:17 +00005732 // Start the morphing.
5733 N->NodeType = Opc;
5734 N->ValueList = VTs.VTs;
5735 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005736
Dan Gohman17059682008-07-17 19:10:17 +00005737 // Clear the operands list, updating used nodes to remove this from their
5738 // use list. Keep track of any operands that become dead as a result.
5739 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005740 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5741 SDUse &Use = *I++;
5742 SDNode *Used = Use.getNode();
5743 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005744 if (Used->use_empty())
5745 DeadNodeSet.insert(Used);
5746 }
5747
Dan Gohman48b185d2009-09-25 20:36:54 +00005748 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5749 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005750 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005751 // If NumOps is larger than the # of operands we can have in a
5752 // MachineSDNode, reallocate the operand list.
5753 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5754 if (MN->OperandsNeedDelete)
5755 delete[] MN->OperandList;
5756 if (NumOps > array_lengthof(MN->LocalOperands))
5757 // We're creating a final node that will live unmorphed for the
5758 // remainder of the current SelectionDAG iteration, so we can allocate
5759 // the operands directly out of a pool with no recycling metadata.
5760 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005761 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005762 else
Craig Topper131de822014-04-27 19:21:16 +00005763 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005764 MN->OperandsNeedDelete = false;
5765 } else
Craig Topper131de822014-04-27 19:21:16 +00005766 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005767 } else {
5768 // If NumOps is larger than the # of operands we currently have, reallocate
5769 // the operand list.
5770 if (NumOps > N->NumOperands) {
5771 if (N->OperandsNeedDelete)
5772 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005773 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005774 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005775 } else
Craig Topper131de822014-04-27 19:21:16 +00005776 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005777 }
5778
5779 // Delete any nodes that are still dead after adding the uses for the
5780 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005781 if (!DeadNodeSet.empty()) {
5782 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005783 for (SDNode *N : DeadNodeSet)
5784 if (N->use_empty())
5785 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005786 RemoveDeadNodes(DeadNodes);
5787 }
Dan Gohman91697632008-07-07 20:57:48 +00005788
Dan Gohman17059682008-07-17 19:10:17 +00005789 if (IP)
5790 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005791 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005792}
5793
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005794
Dan Gohman32f71d72009-09-25 18:54:59 +00005795/// getMachineNode - These are used for target selectors to create a new node
5796/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005797///
Dan Gohman32f71d72009-09-25 18:54:59 +00005798/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005799/// node of the specified opcode and operands, it returns that node instead of
5800/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005801MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005802SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005803 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005804 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005805}
Bill Wendlinga434d932009-01-29 09:01:55 +00005806
Dan Gohmana22f2d82009-10-10 01:29:16 +00005807MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005808SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005809 SDVTList VTs = getVTList(VT);
5810 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005811 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005812}
Bill Wendlinga434d932009-01-29 09:01:55 +00005813
Dan Gohmana22f2d82009-10-10 01:29:16 +00005814MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005815SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005816 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005817 SDVTList VTs = getVTList(VT);
5818 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005819 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005820}
Bill Wendlinga434d932009-01-29 09:01:55 +00005821
Dan Gohmana22f2d82009-10-10 01:29:16 +00005822MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005823SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005824 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005825 SDVTList VTs = getVTList(VT);
5826 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005827 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005828}
Bill Wendlinga434d932009-01-29 09:01:55 +00005829
Dan Gohmana22f2d82009-10-10 01:29:16 +00005830MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005831SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005832 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005833 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005834 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005835}
Bill Wendlinga434d932009-01-29 09:01:55 +00005836
Dan Gohmana22f2d82009-10-10 01:29:16 +00005837MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005838SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005839 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005840 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005841}
Bill Wendlinga434d932009-01-29 09:01:55 +00005842
Dan Gohmana22f2d82009-10-10 01:29:16 +00005843MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005844SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005845 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005846 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005847 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005848 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005849}
Bill Wendlinga434d932009-01-29 09:01:55 +00005850
Dan Gohmana22f2d82009-10-10 01:29:16 +00005851MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005852SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005853 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005854 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005855 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005856 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005857}
5858
Dan Gohmana22f2d82009-10-10 01:29:16 +00005859MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005860SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005861 EVT VT1, EVT VT2, SDValue Op1,
5862 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005863 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005864 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005865 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005866}
5867
Dan Gohmana22f2d82009-10-10 01:29:16 +00005868MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005869SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005870 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005871 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005872 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005873 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005874}
Bill Wendlinga434d932009-01-29 09:01:55 +00005875
Dan Gohmana22f2d82009-10-10 01:29:16 +00005876MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005877SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005878 EVT VT1, EVT VT2, EVT VT3,
5879 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005880 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005881 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005882 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005883}
Bill Wendlinga434d932009-01-29 09:01:55 +00005884
Dan Gohmana22f2d82009-10-10 01:29:16 +00005885MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005886SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005887 EVT VT1, EVT VT2, EVT VT3,
5888 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005889 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005890 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005891 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005892}
Bill Wendlinga434d932009-01-29 09:01:55 +00005893
Dan Gohmana22f2d82009-10-10 01:29:16 +00005894MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005895SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005896 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005897 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005898 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005899 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005900}
Bill Wendlinga434d932009-01-29 09:01:55 +00005901
Dan Gohmana22f2d82009-10-10 01:29:16 +00005902MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005903SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005904 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005905 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005906 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005907 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005908}
5909
Dan Gohmana22f2d82009-10-10 01:29:16 +00005910MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005911SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005912 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005913 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005914 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005915 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005916}
5917
Dan Gohmana22f2d82009-10-10 01:29:16 +00005918MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005919SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005920 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005921 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005922 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005923 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005924 const SDValue *Ops = OpsArray.data();
5925 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005926
5927 if (DoCSE) {
5928 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005929 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005930 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005931 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005932 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005933 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005934 }
5935
5936 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005937 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5938 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005939
5940 // Initialize the operands list.
5941 if (NumOps > array_lengthof(N->LocalOperands))
5942 // We're creating a final node that will live unmorphed for the
5943 // remainder of the current SelectionDAG iteration, so we can allocate
5944 // the operands directly out of a pool with no recycling metadata.
5945 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5946 Ops, NumOps);
5947 else
5948 N->InitOperands(N->LocalOperands, Ops, NumOps);
5949 N->OperandsNeedDelete = false;
5950
5951 if (DoCSE)
5952 CSEMap.InsertNode(N, IP);
5953
Chandler Carruth41b20e72014-07-22 04:07:55 +00005954 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005955 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005956}
Evan Chengd3f1db92006-02-09 07:15:23 +00005957
Dan Gohmanac33a902009-08-19 18:16:17 +00005958/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005959/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005960SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005961SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005962 SDValue Operand) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005963 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005964 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005965 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005966 return SDValue(Subreg, 0);
5967}
5968
Bob Wilson2a45a652009-10-08 18:49:46 +00005969/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005970/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005971SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005972SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005973 SDValue Operand, SDValue Subreg) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005974 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005975 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005976 VT, Operand, Subreg, SRIdxVal);
5977 return SDValue(Result, 0);
5978}
5979
Evan Cheng31604a62008-03-22 01:55:50 +00005980/// getNodeIfExists - Get the specified node if it's already available, or
5981/// else return NULL.
5982SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005983 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5984 bool exact) {
5985 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005986 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005987 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005988 if (isBinOpWithFlags(Opcode))
5989 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005990 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005991 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005992 return E;
5993 }
Craig Topperc0196b12014-04-14 00:51:57 +00005994 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005995}
5996
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005997/// getDbgValue - Creates a SDDbgValue node.
5998///
Adrian Prantl32da8892014-04-25 20:49:25 +00005999/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006000SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
6001 unsigned R, bool IsIndirect, uint64_t Off,
6002 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006003 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006004 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006005 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006006}
6007
Adrian Prantl32da8892014-04-25 20:49:25 +00006008/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006009SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
6010 const Value *C, uint64_t Off,
6011 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006012 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006013 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006014 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006015}
6016
Adrian Prantl32da8892014-04-25 20:49:25 +00006017/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006018SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
6019 unsigned FI, uint64_t Off,
6020 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006021 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006022 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006023 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006024}
6025
Dan Gohman7d099f72010-03-03 21:33:37 +00006026namespace {
6027
Dan Gohman9cc886b2010-03-04 19:11:28 +00006028/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00006029/// pointed to by a use iterator is deleted, increment the use iterator
6030/// so that it doesn't dangle.
6031///
Dan Gohman7d099f72010-03-03 21:33:37 +00006032class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00006033 SDNode::use_iterator &UI;
6034 SDNode::use_iterator &UE;
6035
Craig Topper7b883b32014-03-08 06:31:39 +00006036 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00006037 // Increment the iterator as needed.
6038 while (UI != UE && N == *UI)
6039 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00006040 }
6041
6042public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006043 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00006044 SDNode::use_iterator &ui,
6045 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006046 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00006047};
6048
6049}
6050
Evan Cheng445b91a2006-08-07 22:13:29 +00006051/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006052/// This can cause recursive merging of nodes in the DAG.
6053///
Chris Lattner76858912008-02-03 03:35:22 +00006054/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00006055///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006056void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006057 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006058 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00006059 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00006060 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00006061
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006062 // Iterate over all the existing uses of From. New uses will be added
6063 // to the beginning of the use list, which we avoid visiting.
6064 // This specifically avoids visiting uses of From that arise while the
6065 // replacement is happening, because any such uses would be the result
6066 // of CSE: If an existing node looks like From after one of its operands
6067 // is replaced by To, we don't want to replace of all its users with To
6068 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006069 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006070 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006071 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006072 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006073
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006074 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006075 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006076
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006077 // A user can appear in a use list multiple times, and when this
6078 // happens the uses are usually next to each other in the list.
6079 // To help reduce the number of CSE recomputations, process all
6080 // the uses of this user that we can find this way.
6081 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006082 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006083 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006084 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006085 } while (UI != UE && *UI == User);
6086
6087 // Now that we have modified User, add it back to the CSE maps. If it
6088 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006089 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006090 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006091
6092 // If we just RAUW'd the root, take note.
6093 if (FromN == getRoot())
6094 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00006095}
6096
6097/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6098/// This can cause recursive merging of nodes in the DAG.
6099///
Dan Gohman8aa28b92009-04-15 20:06:30 +00006100/// This version assumes that for each value of From, there is a
6101/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00006102///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006103void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00006104#ifndef NDEBUG
6105 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6106 assert((!From->hasAnyUseOfValue(i) ||
6107 From->getValueType(i) == To->getValueType(i)) &&
6108 "Cannot use this version of ReplaceAllUsesWith!");
6109#endif
Dan Gohman17059682008-07-17 19:10:17 +00006110
6111 // Handle the trivial case.
6112 if (From == To)
6113 return;
6114
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006115 // Iterate over just the existing users of From. See the comments in
6116 // the ReplaceAllUsesWith above.
6117 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006118 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006119 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006120 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006121
Chris Lattner373f0482005-08-26 18:36:28 +00006122 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006123 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006124
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006125 // A user can appear in a use list multiple times, and when this
6126 // happens the uses are usually next to each other in the list.
6127 // To help reduce the number of CSE recomputations, process all
6128 // the uses of this user that we can find this way.
6129 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006130 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006131 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006132 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006133 } while (UI != UE && *UI == User);
6134
6135 // Now that we have modified User, add it back to the CSE maps. If it
6136 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006137 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006138 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006139
6140 // If we just RAUW'd the root, take note.
6141 if (From == getRoot().getNode())
6142 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006143}
6144
Chris Lattner373f0482005-08-26 18:36:28 +00006145/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6146/// This can cause recursive merging of nodes in the DAG.
6147///
6148/// This version can replace From with any result values. To must match the
6149/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006150void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006151 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006152 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006153
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006154 // Iterate over just the existing users of From. See the comments in
6155 // the ReplaceAllUsesWith above.
6156 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006157 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006158 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006159 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006160
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006161 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006162 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006163
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006164 // A user can appear in a use list multiple times, and when this
6165 // happens the uses are usually next to each other in the list.
6166 // To help reduce the number of CSE recomputations, process all
6167 // the uses of this user that we can find this way.
6168 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006169 SDUse &Use = UI.getUse();
6170 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006171 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006172 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006173 } while (UI != UE && *UI == User);
6174
6175 // Now that we have modified User, add it back to the CSE maps. If it
6176 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006177 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006178 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006179
6180 // If we just RAUW'd the root, take note.
6181 if (From == getRoot().getNode())
6182 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006183}
6184
Chris Lattner375e1a72006-02-17 21:58:01 +00006185/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006186/// uses of other values produced by From.getNode() alone. The Deleted
6187/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006188void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006189 // Handle the really simple, really trivial case efficiently.
6190 if (From == To) return;
6191
Chris Lattner375e1a72006-02-17 21:58:01 +00006192 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006193 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006194 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006195 return;
6196 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006197
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006198 // Iterate over just the existing users of From. See the comments in
6199 // the ReplaceAllUsesWith above.
6200 SDNode::use_iterator UI = From.getNode()->use_begin(),
6201 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006202 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006203 while (UI != UE) {
6204 SDNode *User = *UI;
6205 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006206
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006207 // A user can appear in a use list multiple times, and when this
6208 // happens the uses are usually next to each other in the list.
6209 // To help reduce the number of CSE recomputations, process all
6210 // the uses of this user that we can find this way.
6211 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006212 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006213
6214 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006215 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006216 ++UI;
6217 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006218 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006219
6220 // If this node hasn't been modified yet, it's still in the CSE maps,
6221 // so remove its old self from the CSE maps.
6222 if (!UserRemovedFromCSEMaps) {
6223 RemoveNodeFromCSEMaps(User);
6224 UserRemovedFromCSEMaps = true;
6225 }
6226
6227 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006228 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006229 } while (UI != UE && *UI == User);
6230
6231 // We are iterating over all uses of the From node, so if a use
6232 // doesn't use the specific value, no changes are made.
6233 if (!UserRemovedFromCSEMaps)
6234 continue;
6235
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006236 // Now that we have modified User, add it back to the CSE maps. If it
6237 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006238 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006239 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006240
6241 // If we just RAUW'd the root, take note.
6242 if (From == getRoot())
6243 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006244}
6245
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006246namespace {
6247 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6248 /// to record information about a use.
6249 struct UseMemo {
6250 SDNode *User;
6251 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006252 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006253 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006254
6255 /// operator< - Sort Memos by User.
6256 bool operator<(const UseMemo &L, const UseMemo &R) {
6257 return (intptr_t)L.User < (intptr_t)R.User;
6258 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006259}
6260
Dan Gohman17059682008-07-17 19:10:17 +00006261/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006262/// uses of other values produced by From.getNode() alone. The same value
6263/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006264/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006265void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6266 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006267 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006268 // Handle the simple, trivial case efficiently.
6269 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006270 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006271
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006272 // Read up all the uses and make records of them. This helps
6273 // processing new uses that are introduced during the
6274 // replacement process.
6275 SmallVector<UseMemo, 4> Uses;
6276 for (unsigned i = 0; i != Num; ++i) {
6277 unsigned FromResNo = From[i].getResNo();
6278 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006279 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006280 E = FromNode->use_end(); UI != E; ++UI) {
6281 SDUse &Use = UI.getUse();
6282 if (Use.getResNo() == FromResNo) {
6283 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006284 Uses.push_back(Memo);
6285 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006286 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006287 }
Dan Gohman17059682008-07-17 19:10:17 +00006288
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006289 // Sort the uses, so that all the uses from a given User are together.
6290 std::sort(Uses.begin(), Uses.end());
6291
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006292 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6293 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006294 // We know that this user uses some value of From. If it is the right
6295 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006296 SDNode *User = Uses[UseIndex].User;
6297
6298 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006299 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006300
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006301 // The Uses array is sorted, so all the uses for a given User
6302 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006303 // To help reduce the number of CSE recomputations, process all
6304 // the uses of this user that we can find this way.
6305 do {
6306 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006307 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006308 ++UseIndex;
6309
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006310 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006311 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6312
Dan Gohman17059682008-07-17 19:10:17 +00006313 // Now that we have modified User, add it back to the CSE maps. If it
6314 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006315 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006316 }
6317}
6318
Evan Cheng9631a602006-08-01 08:20:41 +00006319/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006320/// based on their topological order. It returns the maximum id and a vector
6321/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006322unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006323
Dan Gohman86aa16a2008-09-30 18:30:35 +00006324 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006325
Dan Gohman86aa16a2008-09-30 18:30:35 +00006326 // SortedPos tracks the progress of the algorithm. Nodes before it are
6327 // sorted, nodes after it are unsorted. When the algorithm completes
6328 // it is at the end of the list.
6329 allnodes_iterator SortedPos = allnodes_begin();
6330
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006331 // Visit all the nodes. Move nodes with no operands to the front of
6332 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006333 // operand count. Before we do this, the Node Id fields of the nodes
6334 // may contain arbitrary values. After, the Node Id fields for nodes
6335 // before SortedPos will contain the topological sort index, and the
6336 // Node Id fields for nodes At SortedPos and after will contain the
6337 // count of outstanding operands.
6338 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6339 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006340 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006341 unsigned Degree = N->getNumOperands();
6342 if (Degree == 0) {
6343 // A node with no uses, add it to the result array immediately.
6344 N->setNodeId(DAGSize++);
6345 allnodes_iterator Q = N;
6346 if (Q != SortedPos)
6347 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006348 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006349 ++SortedPos;
6350 } else {
6351 // Temporarily use the Node Id as scratch space for the degree count.
6352 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006353 }
6354 }
6355
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006356 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006357 // such that by the time the end is reached all nodes will be sorted.
6358 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6359 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006360 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006361 // N is in sorted position, so all its uses have one less operand
6362 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006363 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6364 UI != UE; ++UI) {
6365 SDNode *P = *UI;
6366 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006367 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006368 --Degree;
6369 if (Degree == 0) {
6370 // All of P's operands are sorted, so P may sorted now.
6371 P->setNodeId(DAGSize++);
6372 if (P != SortedPos)
6373 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006374 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006375 ++SortedPos;
6376 } else {
6377 // Update P's outstanding operand count.
6378 P->setNodeId(Degree);
6379 }
6380 }
David Greene3b2a68c2010-01-20 00:59:23 +00006381 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006382#ifndef NDEBUG
6383 SDNode *S = ++I;
6384 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006385 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006386 dbgs() << "Checking if this is due to cycles\n";
6387 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006388#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006389 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006390 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006391 }
6392
6393 assert(SortedPos == AllNodes.end() &&
6394 "Topological sort incomplete!");
6395 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6396 "First node in topological sort is not the entry token!");
6397 assert(AllNodes.front().getNodeId() == 0 &&
6398 "First node in topological sort has non-zero id!");
6399 assert(AllNodes.front().getNumOperands() == 0 &&
6400 "First node in topological sort has operands!");
6401 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6402 "Last node in topologic sort has unexpected id!");
6403 assert(AllNodes.back().use_empty() &&
6404 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006405 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006406 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006407}
6408
Evan Cheng563fe3c2010-03-25 01:38:16 +00006409/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6410/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006411void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006412 if (SD) {
6413 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006414 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006415 }
6416 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006417}
Evan Cheng29eefc12006-07-27 06:39:06 +00006418
Devang Patelefc6b162011-01-25 23:27:42 +00006419/// TransferDbgValues - Transfer SDDbgValues.
6420void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6421 if (From == To || !From.getNode()->getHasDebugValue())
6422 return;
6423 SDNode *FromNode = From.getNode();
6424 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006425 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006426 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006427 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006428 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006429 SDDbgValue *Dbg = *I;
6430 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006431 SDDbgValue *Clone =
6432 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6433 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6434 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006435 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006436 }
6437 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006438 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006439 E = ClonedDVs.end(); I != E; ++I)
6440 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006441}
6442
Jim Laskeyd66e6162005-08-17 20:08:02 +00006443//===----------------------------------------------------------------------===//
6444// SDNode Class
6445//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006446
Chris Lattner3bf17b62007-02-04 02:41:42 +00006447HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006448 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006449}
6450
Andrew Trickef9de2a2013-05-25 02:42:55 +00006451GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6452 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006453 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006454 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006455 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006456}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006457
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006458AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6459 SDValue X, unsigned SrcAS,
6460 unsigned DestAS)
6461 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6462 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6463
Andrew Trickef9de2a2013-05-25 02:42:55 +00006464MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6465 EVT memvt, MachineMemOperand *mmo)
6466 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006467 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006468 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006469 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006470 assert(isNonTemporal() == MMO->isNonTemporal() &&
6471 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006472 // We check here that the size of the memory operand fits within the size of
6473 // the MMO. This is because the MMO might indicate only a possible address
6474 // range instead of specifying the affected memory addresses precisely.
6475 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006476}
6477
Andrew Trickef9de2a2013-05-25 02:42:55 +00006478MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006479 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6480 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006481 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006482 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006483 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006484 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006485 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006486}
6487
Jim Laskeyf576b422006-10-27 23:46:08 +00006488/// Profile - Gather unique data for the node.
6489///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006490void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006491 AddNodeIDNode(ID, this);
6492}
6493
Owen Anderson3b1665e2009-08-25 22:27:22 +00006494namespace {
6495 struct EVTArray {
6496 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006497
Owen Anderson3b1665e2009-08-25 22:27:22 +00006498 EVTArray() {
6499 VTs.reserve(MVT::LAST_VALUETYPE);
6500 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6501 VTs.push_back(MVT((MVT::SimpleValueType)i));
6502 }
6503 };
6504}
6505
Owen Anderson53aa7a92009-08-10 22:56:29 +00006506static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006507static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006508static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006509
Chris Lattner88fa11c2005-11-08 23:30:28 +00006510/// getValueTypeList - Return a pointer to the specified value type.
6511///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006512const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006513 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006514 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006515 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006516 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006517 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006518 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006519 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006520 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006521}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006522
Chris Lattner40e79822005-01-12 18:37:47 +00006523/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6524/// indicated value. This method ignores uses of other values defined by this
6525/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006526bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006527 assert(Value < getNumValues() && "Bad value!");
6528
Roman Levenstein51f532f2008-04-07 10:06:32 +00006529 // TODO: Only iterate over uses of a given value of the node
6530 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006531 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006532 if (NUses == 0)
6533 return false;
6534 --NUses;
6535 }
Chris Lattner40e79822005-01-12 18:37:47 +00006536 }
6537
6538 // Found exactly the right number of uses?
6539 return NUses == 0;
6540}
6541
6542
Evan Cheng358c3d12007-08-02 05:29:38 +00006543/// hasAnyUseOfValue - Return true if there are any use of the indicated
6544/// value. This method ignores uses of other values defined by this operation.
6545bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6546 assert(Value < getNumValues() && "Bad value!");
6547
Dan Gohman7a510c22008-07-09 22:39:01 +00006548 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006549 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006550 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006551
6552 return false;
6553}
6554
6555
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006556/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006557///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006558bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006559 bool Seen = false;
6560 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006561 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006562 if (User == this)
6563 Seen = true;
6564 else
6565 return false;
6566 }
6567
6568 return Seen;
6569}
6570
Evan Cheng9456dd82006-11-03 07:31:32 +00006571/// isOperand - Return true if this node is an operand of N.
6572///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006573bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006574 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6575 if (*this == N->getOperand(i))
6576 return true;
6577 return false;
6578}
6579
Evan Cheng567d2e52008-03-04 00:41:45 +00006580bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006581 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006582 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006583 return true;
6584 return false;
6585}
Evan Chengd37645c2006-02-05 06:29:23 +00006586
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006587/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006588/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006589/// side-effecting instructions on any chain path. In practice, this looks
6590/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006591/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006592bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006593 unsigned Depth) const {
6594 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006595
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006596 // Don't search too deeply, we just want to be able to see through
6597 // TokenFactor's etc.
6598 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006599
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006600 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006601 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006602 if (getOpcode() == ISD::TokenFactor) {
6603 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006604 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6605 return false;
6606 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006607 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006608
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006609 // Loads don't have side effects, look through them.
6610 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6611 if (!Ld->isVolatile())
6612 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6613 }
6614 return false;
6615}
6616
Lang Hames5a004992011-07-07 04:31:51 +00006617/// hasPredecessor - Return true if N is a predecessor of this node.
6618/// N is either an operand of this node, or can be reached by recursively
6619/// traversing up the operands.
6620/// NOTE: This is an expensive method. Use it carefully.
6621bool SDNode::hasPredecessor(const SDNode *N) const {
6622 SmallPtrSet<const SDNode *, 32> Visited;
6623 SmallVector<const SDNode *, 16> Worklist;
6624 return hasPredecessorHelper(N, Visited, Worklist);
6625}
Dan Gohmancd139c02009-10-28 03:44:30 +00006626
Craig Topperb94011f2013-07-14 04:42:23 +00006627bool
6628SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006629 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006630 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006631 if (Visited.empty()) {
6632 Worklist.push_back(this);
6633 } else {
6634 // Take a look in the visited set. If we've already encountered this node
6635 // we needn't search further.
6636 if (Visited.count(N))
6637 return true;
6638 }
6639
6640 // Haven't visited N yet. Continue the search.
6641 while (!Worklist.empty()) {
6642 const SDNode *M = Worklist.pop_back_val();
6643 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6644 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006645 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006646 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006647 if (Op == N)
6648 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006649 }
Lang Hames5a004992011-07-07 04:31:51 +00006650 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006651
6652 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006653}
6654
Evan Cheng5d9fd972006-10-04 00:56:09 +00006655uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6656 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006657 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006658}
6659
Mon P Wang32f8bb92009-11-30 02:42:02 +00006660SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6661 assert(N->getNumValues() == 1 &&
6662 "Can't unroll a vector with multiple results!");
6663
6664 EVT VT = N->getValueType(0);
6665 unsigned NE = VT.getVectorNumElements();
6666 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006667 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006668
6669 SmallVector<SDValue, 8> Scalars;
6670 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6671
6672 // If ResNE is 0, fully unroll the vector op.
6673 if (ResNE == 0)
6674 ResNE = NE;
6675 else if (NE > ResNE)
6676 NE = ResNE;
6677
6678 unsigned i;
6679 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006680 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006681 SDValue Operand = N->getOperand(j);
6682 EVT OperandVT = Operand.getValueType();
6683 if (OperandVT.isVector()) {
6684 // A vector operand; extract a single element.
6685 EVT OperandEltVT = OperandVT.getVectorElementType();
6686 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6687 OperandEltVT,
6688 Operand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006689 getConstant(i, dl, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006690 } else {
6691 // A scalar operand; just use it as is.
6692 Operands[j] = Operand;
6693 }
6694 }
6695
6696 switch (N->getOpcode()) {
6697 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006698 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006699 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006700 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006701 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006702 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006703 case ISD::SHL:
6704 case ISD::SRA:
6705 case ISD::SRL:
6706 case ISD::ROTL:
6707 case ISD::ROTR:
6708 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006709 getShiftAmountOperand(Operands[0].getValueType(),
6710 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006711 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006712 case ISD::SIGN_EXTEND_INREG:
6713 case ISD::FP_ROUND_INREG: {
6714 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6715 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6716 Operands[0],
6717 getValueType(ExtVT)));
6718 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006719 }
6720 }
6721
6722 for (; i < ResNE; ++i)
6723 Scalars.push_back(getUNDEF(EltVT));
6724
6725 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006726 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006727}
6728
Evan Chengf5938d52009-12-09 01:36:00 +00006729
Wesley Peck527da1b2010-11-23 03:31:01 +00006730/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6731/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006732/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006733bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006734 unsigned Bytes, int Dist) const {
6735 if (LD->getChain() != Base->getChain())
6736 return false;
6737 EVT VT = LD->getValueType(0);
6738 if (VT.getSizeInBits() / 8 != Bytes)
6739 return false;
6740
6741 SDValue Loc = LD->getOperand(1);
6742 SDValue BaseLoc = Base->getOperand(1);
6743 if (Loc.getOpcode() == ISD::FrameIndex) {
6744 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6745 return false;
6746 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6747 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6748 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6749 int FS = MFI->getObjectSize(FI);
6750 int BFS = MFI->getObjectSize(BFI);
6751 if (FS != BFS || FS != (int)Bytes) return false;
6752 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6753 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006754
Sanjay Patel7129c102014-12-16 21:57:18 +00006755 // Handle X + C.
6756 if (isBaseWithConstantOffset(Loc)) {
6757 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6758 if (Loc.getOperand(0) == BaseLoc) {
6759 // If the base location is a simple address with no offset itself, then
6760 // the second load's first add operand should be the base address.
6761 if (LocOffset == Dist * (int)Bytes)
6762 return true;
6763 } else if (isBaseWithConstantOffset(BaseLoc)) {
6764 // The base location itself has an offset, so subtract that value from the
6765 // second load's offset before comparing to distance * size.
6766 int64_t BOffset =
6767 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6768 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6769 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6770 return true;
6771 }
6772 }
6773 }
Craig Topperc0196b12014-04-14 00:51:57 +00006774 const GlobalValue *GV1 = nullptr;
6775 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006776 int64_t Offset1 = 0;
6777 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006778 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6779 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006780 if (isGA1 && isGA2 && GV1 == GV2)
6781 return Offset1 == (Offset2 + Dist*Bytes);
6782 return false;
6783}
6784
6785
Evan Cheng34a23ea2009-12-09 01:04:59 +00006786/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6787/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006788unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006789 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006790 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006791 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006792 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006793 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006794 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00006795 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
6796 *TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006797 unsigned AlignBits = KnownZero.countTrailingOnes();
6798 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6799 if (Align)
6800 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006801 }
Evan Chengd938faf2009-12-09 01:53:58 +00006802
Evan Cheng34a23ea2009-12-09 01:04:59 +00006803 // If this is a direct reference to a stack slot, use information about the
6804 // stack slot's alignment.
6805 int FrameIdx = 1 << 31;
6806 int64_t FrameOffset = 0;
6807 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6808 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006809 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006810 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006811 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006812 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6813 FrameOffset = Ptr.getConstantOperandVal(1);
6814 }
6815
6816 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006817 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006818 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6819 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006820 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006821 }
6822
6823 return 0;
6824}
6825
Juergen Ributzkab3487102013-11-19 21:20:17 +00006826/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6827/// which is split (or expanded) into two not necessarily identical pieces.
6828std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6829 // Currently all types are split in half.
6830 EVT LoVT, HiVT;
6831 if (!VT.isVector()) {
6832 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6833 } else {
6834 unsigned NumElements = VT.getVectorNumElements();
6835 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6836 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6837 NumElements/2);
6838 }
6839 return std::make_pair(LoVT, HiVT);
6840}
6841
6842/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6843/// low/high part.
6844std::pair<SDValue, SDValue>
6845SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6846 const EVT &HiVT) {
6847 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6848 N.getValueType().getVectorNumElements() &&
6849 "More vector elements requested than available!");
6850 SDValue Lo, Hi;
6851 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006852 getConstant(0, DL, TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006853 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006854 getConstant(LoVT.getVectorNumElements(), DL,
6855 TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006856 return std::make_pair(Lo, Hi);
6857}
6858
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006859void SelectionDAG::ExtractVectorElements(SDValue Op,
6860 SmallVectorImpl<SDValue> &Args,
6861 unsigned Start, unsigned Count) {
6862 EVT VT = Op.getValueType();
6863 if (Count == 0)
6864 Count = VT.getVectorNumElements();
6865
6866 EVT EltVT = VT.getVectorElementType();
6867 EVT IdxTy = TLI->getVectorIdxTy();
6868 SDLoc SL(Op);
6869 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6870 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006871 Op, getConstant(i, SL, IdxTy)));
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006872 }
6873}
6874
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006875// getAddressSpace - Return the address space this GlobalAddress belongs to.
6876unsigned GlobalAddressSDNode::getAddressSpace() const {
6877 return getGlobal()->getType()->getAddressSpace();
6878}
6879
6880
Chris Lattner229907c2011-07-18 04:54:35 +00006881Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006882 if (isMachineConstantPoolEntry())
6883 return Val.MachineCPVal->getType();
6884 return Val.ConstVal->getType();
6885}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006886
Bob Wilson85cefe82009-03-02 23:24:16 +00006887bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6888 APInt &SplatUndef,
6889 unsigned &SplatBitSize,
6890 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006891 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006892 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006893 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006894 assert(VT.isVector() && "Expected a vector type");
6895 unsigned sz = VT.getSizeInBits();
6896 if (MinSplatBits > sz)
6897 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006898
Bob Wilson85cefe82009-03-02 23:24:16 +00006899 SplatValue = APInt(sz, 0);
6900 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006901
Bob Wilson85cefe82009-03-02 23:24:16 +00006902 // Get the bits. Bits with undefined values (when the corresponding element
6903 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6904 // in SplatValue. If any of the values are not constant, give up and return
6905 // false.
6906 unsigned int nOps = getNumOperands();
6907 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6908 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006909
6910 for (unsigned j = 0; j < nOps; ++j) {
6911 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006912 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006913 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006914
Bob Wilson85cefe82009-03-02 23:24:16 +00006915 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006916 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006917 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006918 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006919 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006920 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006921 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006922 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006923 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006924 }
6925
Bob Wilson85cefe82009-03-02 23:24:16 +00006926 // The build_vector is all constants or undefs. Find the smallest element
6927 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006928
Bob Wilson85cefe82009-03-02 23:24:16 +00006929 HasAnyUndefs = (SplatUndef != 0);
6930 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006931
Bob Wilson85cefe82009-03-02 23:24:16 +00006932 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006933 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6934 APInt LowValue = SplatValue.trunc(HalfSize);
6935 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6936 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006937
Bob Wilson85cefe82009-03-02 23:24:16 +00006938 // If the two halves do not match (ignoring undef bits), stop here.
6939 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6940 MinSplatBits > HalfSize)
6941 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006942
Bob Wilson85cefe82009-03-02 23:24:16 +00006943 SplatValue = HighValue | LowValue;
6944 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006945
Bob Wilson85cefe82009-03-02 23:24:16 +00006946 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006947 }
6948
Bob Wilson85cefe82009-03-02 23:24:16 +00006949 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006950 return true;
6951}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006952
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006953SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6954 if (UndefElements) {
6955 UndefElements->clear();
6956 UndefElements->resize(getNumOperands());
6957 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006958 SDValue Splatted;
6959 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6960 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006961 if (Op.getOpcode() == ISD::UNDEF) {
6962 if (UndefElements)
6963 (*UndefElements)[i] = true;
6964 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006965 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006966 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006967 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006968 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006969 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006970
Chandler Carruthb844e722014-07-08 07:19:55 +00006971 if (!Splatted) {
6972 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6973 "Can only have a splat without a constant for all undefs.");
6974 return getOperand(0);
6975 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006976
Chandler Carruthb844e722014-07-08 07:19:55 +00006977 return Splatted;
6978}
6979
6980ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006981BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006982 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006983 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006984}
6985
6986ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006987BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006988 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006989 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006990}
6991
Juergen Ributzka73844052014-01-13 20:51:35 +00006992bool BuildVectorSDNode::isConstant() const {
6993 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6994 unsigned Opc = getOperand(i).getOpcode();
6995 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6996 return false;
6997 }
6998 return true;
6999}
7000
Owen Anderson53aa7a92009-08-10 22:56:29 +00007001bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00007002 // Find the first non-undef value in the shuffle mask.
7003 unsigned i, e;
7004 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
7005 /* search */;
7006
Nate Begeman39b59db2009-04-29 18:13:31 +00007007 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00007008
Nate Begeman5f829d82009-04-29 05:20:52 +00007009 // Make sure all remaining elements are either undef or the same as the first
7010 // non-undef value.
7011 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007012 if (Mask[i] >= 0 && Mask[i] != Idx)
7013 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007014 return true;
7015}
David Greene09851602010-01-20 20:13:31 +00007016
Adam Nemetb4690e32014-05-31 16:23:20 +00007017#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00007018static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00007019 SmallPtrSetImpl<const SDNode*> &Visited,
7020 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00007021 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007022 // If this node has already been checked, don't check it again.
7023 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00007024 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00007025
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007026 // If a node has already been visited on this depth-first walk, reject it as
7027 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00007028 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007029 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00007030 dbgs() << "Offending node:\n";
7031 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007032 abort();
David Greene09851602010-01-20 20:13:31 +00007033 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007034
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007035 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00007036 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00007037
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007038 Checked.insert(N);
7039 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00007040}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007041#endif
David Greene09851602010-01-20 20:13:31 +00007042
Adam Nemet7d394302014-05-31 16:23:17 +00007043void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00007044 const llvm::SelectionDAG *DAG,
7045 bool force) {
7046#ifndef NDEBUG
7047 bool check = force;
David Greene09851602010-01-20 20:13:31 +00007048#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00007049 check = true;
7050#endif // XDEBUG
7051 if (check) {
7052 assert(N && "Checking nonexistent SDNode");
7053 SmallPtrSet<const SDNode*, 32> visited;
7054 SmallPtrSet<const SDNode*, 32> checked;
7055 checkForCyclesHelper(N, visited, checked, DAG);
7056 }
7057#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00007058}
7059
Adam Nemetb4690e32014-05-31 16:23:20 +00007060void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7061 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00007062}