blob: 6d75a7c95333013be1493244b4645f58390b7cac [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>
Matthias Braun20683ef2015-05-19 01:40:21 +000052#include <utility>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000053
Chris Lattner560b5e42004-06-02 04:28:06 +000054using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000055
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000056/// makeVTList - Return an instance of the SDVTList struct initialized with the
57/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000058static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000059 SDVTList Res = {VTs, NumVTs};
60 return Res;
61}
62
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000063// Default null implementations of the callbacks.
64void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
65void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000066
Jim Laskeyd66e6162005-08-17 20:08:02 +000067//===----------------------------------------------------------------------===//
68// ConstantFPSDNode Class
69//===----------------------------------------------------------------------===//
70
71/// isExactlyValue - We don't rely on operator== working on double values, as
72/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
73/// As such, this method can be used to do an exact bit-for-bit comparison of
74/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000075bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000076 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000077}
78
Owen Anderson53aa7a92009-08-10 22:56:29 +000079bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000080 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000081 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000082
Dale Johannesend246b2c2007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000085 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000086 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000088 &losesInfo);
89 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000090}
91
Jim Laskeyd66e6162005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000093// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000094//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000095
Evan Chengc70e33c2006-03-27 06:58:47 +000096/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000097/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000098bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000099 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +0000100 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000101 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000102
Evan Chengc70e33c2006-03-27 06:58:47 +0000103 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000104
Chris Lattnerc2d28112006-03-25 22:57:01 +0000105 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000106
Chris Lattnerc2d28112006-03-25 22:57:01 +0000107 // Skip over all of the undef values.
108 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
109 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000110
Chris Lattnerc2d28112006-03-25 22:57:01 +0000111 // Do not accept an all-undef vector.
112 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000113
Chris Lattnerc2d28112006-03-25 22:57:01 +0000114 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000115 // elements. We have to be a bit careful here, as the type of the constant
116 // may not be the same as the type of the vector elements due to type
117 // legalization (the elements are promoted to a legal type for the target and
118 // a vector of a type may be legal when the base element type is not).
119 // We only want to check enough bits to cover the vector elements, because
120 // we care if the resultant vector is all ones, not whether the individual
121 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000122 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000123 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000124 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
125 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000126 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000127 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
128 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000129 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000130 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000131 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000132
Chris Lattnerc2d28112006-03-25 22:57:01 +0000133 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000134 // undefs. Even with the above element type twiddling, this should be OK, as
135 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000136 for (++i; i != e; ++i)
137 if (N->getOperand(i) != NotZero &&
138 N->getOperand(i).getOpcode() != ISD::UNDEF)
139 return false;
140 return true;
141}
142
143
Evan Chenga6789912006-03-26 09:50:58 +0000144/// isBuildVectorAllZeros - Return true if the specified node is a
145/// BUILD_VECTOR where all of the elements are 0 or undef.
146bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000147 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +0000148 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000149 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000150
Evan Chenga6789912006-03-26 09:50:58 +0000151 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000152
Kevin Qin93d45ec2014-06-24 05:37:27 +0000153 bool IsAllUndef = true;
154 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
155 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
156 continue;
157 IsAllUndef = false;
158 // Do not accept build_vectors that aren't all constants or which have non-0
159 // elements. We have to be a bit careful here, as the type of the constant
160 // may not be the same as the type of the vector elements due to type
161 // legalization (the elements are promoted to a legal type for the target
162 // and a vector of a type may be legal when the base element type is not).
163 // We only want to check enough bits to cover the vector elements, because
164 // we care if the resultant vector is all zeros, not whether the individual
165 // constants are.
166 SDValue Zero = N->getOperand(i);
167 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
168 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
169 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
170 return false;
171 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
172 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
173 return false;
174 } else
175 return false;
176 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000177
Evan Chengc70e33c2006-03-27 06:58:47 +0000178 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000179 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000180 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000181 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000182}
183
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000184/// \brief Return true if the specified node is a BUILD_VECTOR node of
185/// all ConstantSDNode or undef.
186bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
187 if (N->getOpcode() != ISD::BUILD_VECTOR)
188 return false;
189
190 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
191 SDValue Op = N->getOperand(i);
192 if (Op.getOpcode() == ISD::UNDEF)
193 continue;
194 if (!isa<ConstantSDNode>(Op))
195 return false;
196 }
197 return true;
198}
199
Simon Pilgrim09f3ff92015-03-25 22:30:31 +0000200/// \brief Return true if the specified node is a BUILD_VECTOR node of
201/// all ConstantFPSDNode or undef.
202bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
203 if (N->getOpcode() != ISD::BUILD_VECTOR)
204 return false;
205
206 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
207 SDValue Op = N->getOperand(i);
208 if (Op.getOpcode() == ISD::UNDEF)
209 continue;
210 if (!isa<ConstantFPSDNode>(Op))
211 return false;
212 }
213 return true;
214}
215
Evan Cheng6200c222008-02-18 23:04:32 +0000216/// isScalarToVector - Return true if the specified node is a
217/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
218/// element is not an undef.
219bool ISD::isScalarToVector(const SDNode *N) {
220 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
221 return true;
222
223 if (N->getOpcode() != ISD::BUILD_VECTOR)
224 return false;
225 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
226 return false;
227 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000228 if (NumElems == 1)
229 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000230 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000231 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000232 if (V.getOpcode() != ISD::UNDEF)
233 return false;
234 }
235 return true;
236}
237
Nadav Rotema62368c2012-07-15 08:38:23 +0000238/// allOperandsUndef - Return true if the node has at least one operand
239/// and all operands of the specified node are ISD::UNDEF.
240bool ISD::allOperandsUndef(const SDNode *N) {
241 // Return false if the node has no operands.
242 // This is "logically inconsistent" with the definition of "all" but
243 // is probably the desired behavior.
244 if (N->getNumOperands() == 0)
245 return false;
246
247 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
248 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
249 return false;
250
251 return true;
252}
253
Matt Arsenaultbd223422015-01-14 01:35:17 +0000254ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000255 switch (ExtType) {
256 case ISD::EXTLOAD:
Matt Arsenaultbd223422015-01-14 01:35:17 +0000257 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000258 case ISD::SEXTLOAD:
259 return ISD::SIGN_EXTEND;
260 case ISD::ZEXTLOAD:
261 return ISD::ZERO_EXTEND;
262 default:
263 break;
264 }
265
266 llvm_unreachable("Invalid LoadExtType");
267}
268
Chris Lattner061a1ea2005-01-07 07:46:32 +0000269/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
270/// when given the operation for (X op Y).
271ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
272 // To perform this operation, we just need to swap the L and G bits of the
273 // operation.
274 unsigned OldL = (Operation >> 2) & 1;
275 unsigned OldG = (Operation >> 1) & 1;
276 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
277 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000278 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000279}
280
281/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
282/// 'op' is a valid SetCC operation.
283ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
284 unsigned Operation = Op;
285 if (isInteger)
286 Operation ^= 7; // Flip L, G, E bits, but not U.
287 else
288 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000289
Chris Lattner061a1ea2005-01-07 07:46:32 +0000290 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000291 Operation &= ~8; // Don't let N and U bits get set.
292
Chris Lattner061a1ea2005-01-07 07:46:32 +0000293 return ISD::CondCode(Operation);
294}
295
296
297/// isSignedOp - For an integer comparison, return 1 if the comparison is a
298/// signed operation and 2 if the result is an unsigned comparison. Return zero
299/// if the operation does not depend on the sign of the input (setne and seteq).
300static int isSignedOp(ISD::CondCode Opcode) {
301 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000302 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000303 case ISD::SETEQ:
304 case ISD::SETNE: return 0;
305 case ISD::SETLT:
306 case ISD::SETLE:
307 case ISD::SETGT:
308 case ISD::SETGE: return 1;
309 case ISD::SETULT:
310 case ISD::SETULE:
311 case ISD::SETUGT:
312 case ISD::SETUGE: return 2;
313 }
314}
315
316/// getSetCCOrOperation - Return the result of a logical OR between different
317/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
318/// returns SETCC_INVALID if it is not possible to represent the resultant
319/// comparison.
320ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
321 bool isInteger) {
322 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
323 // Cannot fold a signed integer setcc with an unsigned integer setcc.
324 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000325
Chris Lattner061a1ea2005-01-07 07:46:32 +0000326 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000327
Chris Lattner061a1ea2005-01-07 07:46:32 +0000328 // If the N and U bits get set then the resultant comparison DOES suddenly
329 // care about orderedness, and is true when ordered.
330 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000331 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000332
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000333 // Canonicalize illegal integer setcc's.
334 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
335 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000336
Chris Lattner061a1ea2005-01-07 07:46:32 +0000337 return ISD::CondCode(Op);
338}
339
340/// getSetCCAndOperation - Return the result of a logical AND between different
341/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
342/// function returns zero if it is not possible to represent the resultant
343/// comparison.
344ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
345 bool isInteger) {
346 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
347 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000348 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000349
350 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000351 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000352
Chris Lattner393d96a2006-04-27 05:01:07 +0000353 // Canonicalize illegal integer setcc's.
354 if (isInteger) {
355 switch (Result) {
356 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000357 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000358 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000359 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
360 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
361 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000362 }
363 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000364
Chris Lattner393d96a2006-04-27 05:01:07 +0000365 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000366}
367
Jim Laskeyd66e6162005-08-17 20:08:02 +0000368//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000369// SDNode Profile Support
370//===----------------------------------------------------------------------===//
371
Jim Laskeybd0f0882006-10-27 23:52:51 +0000372/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
373///
Jim Laskeyf576b422006-10-27 23:46:08 +0000374static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
375 ID.AddInteger(OpC);
376}
377
378/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
379/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000380static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000381 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000382}
383
Jim Laskeybd0f0882006-10-27 23:52:51 +0000384/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
385///
Jim Laskeyf576b422006-10-27 23:46:08 +0000386static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000387 ArrayRef<SDValue> Ops) {
388 for (auto& Op : Ops) {
389 ID.AddPointer(Op.getNode());
390 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000391 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000392}
393
Dan Gohman768f2c92008-07-07 18:26:29 +0000394/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
395///
396static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000397 ArrayRef<SDUse> Ops) {
398 for (auto& Op : Ops) {
399 ID.AddPointer(Op.getNode());
400 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000401 }
402}
Nick Lewycky37a17502015-05-13 23:41:47 +0000403
404static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
405 bool exact) {
406 ID.AddBoolean(nuw);
407 ID.AddBoolean(nsw);
408 ID.AddBoolean(exact);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000409}
410
Nick Lewycky37a17502015-05-13 23:41:47 +0000411/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
412static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
413 bool nuw, bool nsw, bool exact) {
414 if (isBinOpWithFlags(Opcode))
415 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000416}
417
Craig Topper633d99b2014-04-27 23:22:43 +0000418static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
419 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000420 AddNodeIDOpcode(ID, OpC);
421 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000422 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000423}
424
Duncan Sands835bdca2008-10-27 15:30:53 +0000425/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
426/// the NodeID data.
427static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000428 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000429 case ISD::TargetExternalSymbol:
430 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000431 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000432 default: break; // Normal nodes don't need extra info.
433 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000434 case ISD::Constant: {
435 const ConstantSDNode *C = cast<ConstantSDNode>(N);
436 ID.AddPointer(C->getConstantIntValue());
437 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000438 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000439 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000440 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000441 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000442 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000443 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000444 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000445 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000446 case ISD::GlobalAddress:
447 case ISD::TargetGlobalTLSAddress:
448 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000449 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000450 ID.AddPointer(GA->getGlobal());
451 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000452 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000453 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000454 break;
455 }
456 case ISD::BasicBlock:
457 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
458 break;
459 case ISD::Register:
460 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
461 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000462 case ISD::RegisterMask:
463 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
464 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000465 case ISD::SRCVALUE:
466 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
467 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000468 case ISD::FrameIndex:
469 case ISD::TargetFrameIndex:
470 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
471 break;
472 case ISD::JumpTable:
473 case ISD::TargetJumpTable:
474 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000475 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000476 break;
477 case ISD::ConstantPool:
478 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000479 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000480 ID.AddInteger(CP->getAlignment());
481 ID.AddInteger(CP->getOffset());
482 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000483 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000484 else
485 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000486 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000487 break;
488 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000489 case ISD::TargetIndex: {
490 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
491 ID.AddInteger(TI->getIndex());
492 ID.AddInteger(TI->getOffset());
493 ID.AddInteger(TI->getTargetFlags());
494 break;
495 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000496 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000497 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000498 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000499 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000500 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000501 break;
502 }
503 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000504 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000505 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000506 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000507 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000508 break;
509 }
Nick Lewycky37a17502015-05-13 23:41:47 +0000510 case ISD::SDIV:
511 case ISD::UDIV:
512 case ISD::SRA:
513 case ISD::SRL:
514 case ISD::MUL:
515 case ISD::ADD:
516 case ISD::SUB:
517 case ISD::SHL: {
518 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
519 AddBinaryNodeIDCustom(
520 ID, N->getOpcode(), BinNode->Flags.hasNoUnsignedWrap(),
521 BinNode->Flags.hasNoSignedWrap(), BinNode->Flags.hasExact());
522 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);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +0000881 SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), 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);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +0000899 SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), 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);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +0000916 SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), 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
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000961BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
962 SDVTList VTs, SDValue N1,
Nick Lewycky37a17502015-05-13 23:41:47 +0000963 SDValue N2, bool nuw, bool nsw,
964 bool exact) {
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000965 if (isBinOpWithFlags(Opcode)) {
966 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
Nick Lewycky37a17502015-05-13 23:41:47 +0000967 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
968 FN->Flags.setNoUnsignedWrap(nuw);
969 FN->Flags.setNoSignedWrap(nsw);
970 FN->Flags.setExact(exact);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000971
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000972 return FN;
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000973 }
974
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000975 BinarySDNode *N = new (NodeAllocator)
976 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000977 return N;
978}
979
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +0000980SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
981 void *&InsertPos) {
982 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
983 if (N) {
984 switch (N->getOpcode()) {
985 default: break;
986 case ISD::Constant:
987 case ISD::ConstantFP:
988 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
989 "debug location. Use another overload.");
990 }
991 }
992 return N;
993}
994
995SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
996 DebugLoc DL, void *&InsertPos) {
997 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
998 if (N) {
999 switch (N->getOpcode()) {
1000 default: break; // Process only regular (non-target) constant nodes.
1001 case ISD::Constant:
1002 case ISD::ConstantFP:
1003 // Erase debug location from the node if the node is used at several
1004 // different places to do not propagate one location to all uses as it
1005 // leads to incorrect debug info.
1006 if (N->getDebugLoc() != DL)
1007 N->setDebugLoc(DebugLoc());
1008 break;
1009 }
1010 }
1011 return N;
1012}
1013
Dan Gohmane1a9a782008-08-27 23:52:12 +00001014void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001015 allnodes_clear();
1016 OperandAllocator.Reset();
1017 CSEMap.clear();
1018
1019 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +00001020 ExternalSymbols.clear();
1021 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001022 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +00001023 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001024 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +00001025 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001026
Craig Topperc0196b12014-04-14 00:51:57 +00001027 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001028 AllNodes.push_back(&EntryNode);
1029 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00001030 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001031}
1032
Andrew Trickef9de2a2013-05-25 02:42:55 +00001033SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +00001034 return VT.bitsGT(Op.getValueType()) ?
1035 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1036 getNode(ISD::TRUNCATE, DL, VT, Op);
1037}
1038
Andrew Trickef9de2a2013-05-25 02:42:55 +00001039SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001040 return VT.bitsGT(Op.getValueType()) ?
1041 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1042 getNode(ISD::TRUNCATE, DL, VT, Op);
1043}
1044
Andrew Trickef9de2a2013-05-25 02:42:55 +00001045SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001046 return VT.bitsGT(Op.getValueType()) ?
1047 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1048 getNode(ISD::TRUNCATE, DL, VT, Op);
1049}
1050
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001051SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1052 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001053 if (VT.bitsLE(Op.getValueType()))
1054 return getNode(ISD::TRUNCATE, SL, VT, Op);
1055
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001056 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001057 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1058}
1059
Andrew Trickef9de2a2013-05-25 02:42:55 +00001060SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001061 assert(!VT.isVector() &&
1062 "getZeroExtendInReg should use the vector element type instead of "
1063 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001064 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001065 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1066 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001067 VT.getSizeInBits());
1068 return getNode(ISD::AND, DL, Op.getValueType(), Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001069 getConstant(Imm, DL, Op.getValueType()));
Bill Wendlingc4093182009-01-30 22:23:15 +00001070}
1071
Chandler Carruth0b666e02014-07-10 12:32:32 +00001072SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1073 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1074 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1075 "The sizes of the input and result must match in order to perform the "
1076 "extend in-register.");
1077 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1078 "The destination vector type must have fewer lanes than the input.");
1079 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1080}
1081
1082SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1083 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1084 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1085 "The sizes of the input and result must match in order to perform the "
1086 "extend in-register.");
1087 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1088 "The destination vector type must have fewer lanes than the input.");
1089 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1090}
1091
Chandler Carruthafe4b252014-07-09 10:58:18 +00001092SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1093 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001094 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1095 "The sizes of the input and result must match in order to perform the "
1096 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001097 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1098 "The destination vector type must have fewer lanes than the input.");
1099 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1100}
1101
Bob Wilsonc5890052009-01-22 17:39:32 +00001102/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1103///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001104SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001105 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001106 SDValue NegOne =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001107 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001108 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1109}
1110
Pete Cooper7fd1d722014-05-12 23:26:58 +00001111SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1112 EVT EltVT = VT.getScalarType();
1113 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001114 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001115 case TargetLowering::ZeroOrOneBooleanContent:
1116 case TargetLowering::UndefinedBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001117 TrueValue = getConstant(1, DL, VT);
Pete Cooper7fd1d722014-05-12 23:26:58 +00001118 break;
1119 case TargetLowering::ZeroOrNegativeOneBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001120 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
Pete Cooper7fd1d722014-05-12 23:26:58 +00001121 VT);
1122 break;
1123 }
1124 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1125}
1126
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001127SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT,
1128 bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001129 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001130 assert((EltVT.getSizeInBits() >= 64 ||
1131 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1132 "getConstant with a uint64_t value that doesn't fit in the type!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001133 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001134}
1135
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001136SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT,
1137 bool isO)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001138{
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001139 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001140}
1141
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001142SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT,
1143 bool isT, bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001144 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001145
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001146 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001147 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001148
Duncan Sandsf2641e12011-09-06 19:07:46 +00001149 // In some cases the vector type is legal but the element type is illegal and
1150 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1151 // inserted value (the type does not need to match the vector element type).
1152 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001153 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001154 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001155 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001156 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1157 Elt = ConstantInt::get(*getContext(), NewVal);
1158 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001159 // In other cases the element type is illegal and needs to be expanded, for
1160 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1161 // the value into n parts and use a vector type with n-times the elements.
1162 // Then bitcast to the type requested.
1163 // Legalizing constants too early makes the DAGCombiner's job harder so we
1164 // only legalize if the DAG tells us we must produce legal types.
1165 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1166 TLI->getTypeAction(*getContext(), EltVT) ==
1167 TargetLowering::TypeExpandInteger) {
1168 APInt NewVal = Elt->getValue();
1169 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1170 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1171 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1172 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1173
1174 // Check the temporary vector is the correct size. If this fails then
1175 // getTypeToTransformTo() probably returned a type whose size (in bits)
1176 // isn't a power-of-2 factor of the requested type size.
1177 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1178
1179 SmallVector<SDValue, 2> EltParts;
1180 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1181 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001182 .trunc(ViaEltSizeInBits), DL,
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001183 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001184 }
1185
1186 // EltParts is currently in little endian order. If we actually want
1187 // big-endian order then reverse it now.
1188 if (TLI->isBigEndian())
1189 std::reverse(EltParts.begin(), EltParts.end());
1190
1191 // The elements must be reversed when the element order is different
1192 // to the endianness of the elements (because the BITCAST is itself a
1193 // vector shuffle in this situation). However, we do not need any code to
1194 // perform this reversal because getConstant() is producing a vector
1195 // splat.
1196 // This situation occurs in MIPS MSA.
1197
1198 SmallVector<SDValue, 8> Ops;
1199 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1200 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1201
1202 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1203 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001204 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001205 return Result;
1206 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001207
1208 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1209 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001210 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001211 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001212 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001213 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001214 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001215 void *IP = nullptr;
1216 SDNode *N = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001217 if ((N = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001218 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001219 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001220
Dan Gohman7a7742c2007-12-12 22:21:26 +00001221 if (!N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001222 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, DL.getDebugLoc(),
1223 EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001224 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001225 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001226 }
1227
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001228 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001229 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001230 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001231 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001232 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001233 }
1234 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001235}
1236
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001237SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) {
1238 return getConstant(Val, DL, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001239}
1240
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001241SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT,
1242 bool isTarget) {
1243 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001244}
1245
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001246SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT,
1247 bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001248 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001249
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001250 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001251
Chris Lattner381dddc2005-02-17 20:17:32 +00001252 // Do the map lookup using the actual bit pattern for the floating point
1253 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1254 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001255 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001256 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001257 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001258 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001259 void *IP = nullptr;
1260 SDNode *N = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001261 if ((N = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001262 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001263 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001264
Evan Cheng9458e6a2007-06-29 21:36:04 +00001265 if (!N) {
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001266 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, DL.getDebugLoc(),
1267 EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001268 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001269 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001270 }
1271
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001272 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001273 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001274 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001275 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001276 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001277 }
1278 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001279}
1280
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001281SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT,
1282 bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001283 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001284 if (EltVT==MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001285 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001286 else if (EltVT==MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001287 return getConstantFP(APFloat(Val), DL, VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001288 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1289 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001290 bool ignored;
1291 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001292 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001293 &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001294 return getConstantFP(apf, DL, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001295 } else
1296 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001297}
1298
Andrew Trickef9de2a2013-05-25 02:42:55 +00001299SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001300 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001301 bool isTargetGA,
1302 unsigned char TargetFlags) {
1303 assert((TargetFlags == 0 || isTargetGA) &&
1304 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001305
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001306 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001307 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001308 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001309 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001310
Chris Lattner8e34f982009-06-25 21:21:14 +00001311 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001312 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001313 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1314 else
1315 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001316
Jim Laskeyf576b422006-10-27 23:46:08 +00001317 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001318 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001319 ID.AddPointer(GV);
1320 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001321 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001322 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001323 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001324 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001325 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001326
Andrew Trickef9de2a2013-05-25 02:42:55 +00001327 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1328 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001329 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +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);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001333}
1334
Owen Anderson53aa7a92009-08-10 22:56:29 +00001335SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001336 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001337 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001338 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001339 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001340 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001341 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001342 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001343
Dan Gohman01c65a22010-03-18 18:49:47 +00001344 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001345 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001346 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001347 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001348}
1349
Owen Anderson53aa7a92009-08-10 22:56:29 +00001350SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001351 unsigned char TargetFlags) {
1352 assert((TargetFlags == 0 || isTarget) &&
1353 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001354 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001355 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001356 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001357 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001358 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001359 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001360 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001361 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001362
Dan Gohman01c65a22010-03-18 18:49:47 +00001363 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1364 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001365 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001366 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001367 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001368}
1369
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001370SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001371 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001372 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001373 unsigned char TargetFlags) {
1374 assert((TargetFlags == 0 || isTarget) &&
1375 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001376 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001377 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001378 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001379 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001380 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001381 ID.AddInteger(Alignment);
1382 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001383 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001384 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001385 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001386 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001387 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001388
Dan Gohman01c65a22010-03-18 18:49:47 +00001389 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1390 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001391 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001392 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001393 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001394}
1395
Chris Lattner061a1ea2005-01-07 07:46:32 +00001396
Owen Anderson53aa7a92009-08-10 22:56:29 +00001397SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001398 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001399 bool isTarget,
1400 unsigned char TargetFlags) {
1401 assert((TargetFlags == 0 || isTarget) &&
1402 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001403 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001404 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001405 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001406 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001407 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001408 ID.AddInteger(Alignment);
1409 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001410 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001411 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001412 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001413 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001414 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001415
Dan Gohman01c65a22010-03-18 18:49:47 +00001416 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1417 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001418 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001419 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001420 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001421}
1422
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001423SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1424 unsigned char TargetFlags) {
1425 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001426 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001427 ID.AddInteger(Index);
1428 ID.AddInteger(Offset);
1429 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001430 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001431 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001432 return SDValue(E, 0);
1433
1434 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1435 TargetFlags);
1436 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001437 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001438 return SDValue(N, 0);
1439}
1440
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001441SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001442 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001443 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001444 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001445 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001446 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001447 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001448
Dan Gohman01c65a22010-03-18 18:49:47 +00001449 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001450 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001451 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001452 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001453}
1454
Owen Anderson53aa7a92009-08-10 22:56:29 +00001455SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001456 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1457 ValueTypeNodes.size())
1458 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001459
Duncan Sands13237ac2008-06-06 12:08:01 +00001460 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001461 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001462
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001463 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001464 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001465 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001466 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001467}
1468
Owen Anderson53aa7a92009-08-10 22:56:29 +00001469SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001470 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001471 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001472 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001473 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001474 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001475}
1476
Owen Anderson53aa7a92009-08-10 22:56:29 +00001477SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001478 unsigned char TargetFlags) {
1479 SDNode *&N =
1480 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1481 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001482 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001483 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001484 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001485 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001486}
1487
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001488SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001489 if ((unsigned)Cond >= CondCodeNodes.size())
1490 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001491
Craig Topperc0196b12014-04-14 00:51:57 +00001492 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001493 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001494 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001495 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001496 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001497
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001498 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001499}
1500
Nate Begeman5f829d82009-04-29 05:20:52 +00001501// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1502// the shuffle mask M that point at N1 to point at N2, and indices that point
1503// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001504static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1505 std::swap(N1, N2);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001506 ShuffleVectorSDNode::commuteMask(M);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001507}
1508
Andrew Trickef9de2a2013-05-25 02:42:55 +00001509SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001510 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001511 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1512 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001513
1514 // Canonicalize shuffle undef, undef -> undef
1515 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001516 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001517
Eric Christopherdfda92b2009-08-22 00:40:45 +00001518 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001519 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001520 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001521 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001522 for (unsigned i = 0; i != NElts; ++i) {
1523 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001524 MaskVec.push_back(Mask[i]);
1525 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001526
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001527 // Canonicalize shuffle v, v -> v, undef
1528 if (N1 == N2) {
1529 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001530 for (unsigned i = 0; i != NElts; ++i)
1531 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001532 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001533
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001534 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1535 if (N1.getOpcode() == ISD::UNDEF)
1536 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001537
Chandler Carruth1b5285d2015-02-15 12:18:12 +00001538 // If shuffling a splat, try to blend the splat instead. We do this here so
1539 // that even when this arises during lowering we don't have to re-handle it.
1540 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1541 BitVector UndefElements;
1542 SDValue Splat = BV->getSplatValue(&UndefElements);
1543 if (!Splat)
1544 return;
1545
1546 for (int i = 0; i < (int)NElts; ++i) {
1547 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1548 continue;
1549
1550 // If this input comes from undef, mark it as such.
1551 if (UndefElements[MaskVec[i] - Offset]) {
1552 MaskVec[i] = -1;
1553 continue;
1554 }
1555
1556 // If we can blend a non-undef lane, use that instead.
1557 if (!UndefElements[i])
1558 MaskVec[i] = i + Offset;
1559 }
1560 };
1561 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1562 BlendSplat(N1BV, 0);
1563 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1564 BlendSplat(N2BV, NElts);
1565
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001566 // Canonicalize all index into lhs, -> shuffle lhs, undef
1567 // Canonicalize all index into rhs, -> shuffle rhs, undef
1568 bool AllLHS = true, AllRHS = true;
1569 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001570 for (unsigned i = 0; i != NElts; ++i) {
1571 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001572 if (N2Undef)
1573 MaskVec[i] = -1;
1574 else
1575 AllLHS = false;
1576 } else if (MaskVec[i] >= 0) {
1577 AllRHS = false;
1578 }
1579 }
1580 if (AllLHS && AllRHS)
1581 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001582 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001583 N2 = getUNDEF(VT);
1584 if (AllRHS) {
1585 N1 = getUNDEF(VT);
1586 commuteShuffle(N1, N2, MaskVec);
1587 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001588 // Reset our undef status after accounting for the mask.
1589 N2Undef = N2.getOpcode() == ISD::UNDEF;
1590 // Re-check whether both sides ended up undef.
1591 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1592 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001593
Craig Topper9a39b072013-08-08 08:03:12 +00001594 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001595 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001596 for (unsigned i = 0; i != NElts; ++i) {
1597 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001598 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001599 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001600 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001601 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001602
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001603 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001604 if (N2Undef) {
1605 SDValue V = N1;
1606
1607 // Look through any bitcasts. We check that these don't change the number
1608 // (and size) of elements and just changes their types.
1609 while (V.getOpcode() == ISD::BITCAST)
1610 V = V->getOperand(0);
1611
1612 // A splat should always show up as a build vector node.
1613 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001614 BitVector UndefElements;
1615 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001616 // If this is a splat of an undef, shuffling it is also undef.
1617 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1618 return getUNDEF(VT);
1619
Andrea Di Biagio83814752015-01-24 11:54:29 +00001620 bool SameNumElts =
1621 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1622
Chandler Carruth142e9662014-07-08 08:45:38 +00001623 // We only have a splat which can skip shuffles if there is a splatted
1624 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001625 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001626 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1627 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001628 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001629 return N1;
1630 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1631 if (C->isNullValue())
1632 return N1;
1633 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001634
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001635 // If the shuffle itself creates a splat, build the vector directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001636 if (AllSame && SameNumElts) {
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001637 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1638 SmallVector<SDValue, 8> Ops(NElts, Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001639
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001640 EVT BuildVT = BV->getValueType(0);
1641 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001642
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001643 // We may have jumped through bitcasts, so the type of the
1644 // BUILD_VECTOR may not match the type of the shuffle.
1645 if (BuildVT != VT)
1646 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1647 return NewBV;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001648 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001649 }
1650 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001651
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001652 FoldingSetNodeID ID;
1653 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001654 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001655 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001656 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001657
Craig Topperc0196b12014-04-14 00:51:57 +00001658 void* IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001659 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001660 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001661
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001662 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1663 // SDNode doesn't have access to it. This memory will be "leaked" when
1664 // the node is deallocated, but recovered when the NodeAllocator is released.
1665 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1666 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001667
Dan Gohman01c65a22010-03-18 18:49:47 +00001668 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001669 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1670 dl.getDebugLoc(), N1, N2,
1671 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001672 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001673 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001674 return SDValue(N, 0);
1675}
1676
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001677SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1678 MVT VT = SV.getSimpleValueType(0);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001679 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1680 ShuffleVectorSDNode::commuteMask(MaskVec);
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001681
1682 SDValue Op0 = SV.getOperand(0);
1683 SDValue Op1 = SV.getOperand(1);
1684 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1685}
1686
Andrew Trickef9de2a2013-05-25 02:42:55 +00001687SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001688 SDValue Val, SDValue DTy,
1689 SDValue STy, SDValue Rnd, SDValue Sat,
1690 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001691 // If the src and dest types are the same and the conversion is between
1692 // integer types of the same sign or two floats, no conversion is necessary.
1693 if (DTy == STy &&
1694 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001695 return Val;
1696
1697 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001698 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001699 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001700 void* IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001701 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001702 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001703
Jack Carter170a5f22013-09-09 22:02:08 +00001704 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1705 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001706 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001707 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001708 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001709 return SDValue(N, 0);
1710}
1711
Owen Anderson53aa7a92009-08-10 22:56:29 +00001712SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001713 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001714 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001715 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001716 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001717 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001718 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001719
Dan Gohman01c65a22010-03-18 18:49:47 +00001720 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001721 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001722 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001723 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001724}
1725
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001726SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1727 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001728 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001729 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001730 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001731 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001732 return SDValue(E, 0);
1733
1734 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1735 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001736 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001737 return SDValue(N, 0);
1738}
1739
Andrew Trickef9de2a2013-05-25 02:42:55 +00001740SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001741 FoldingSetNodeID ID;
1742 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001743 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001744 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001745 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001746 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001747 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001748
Jack Carter170a5f22013-09-09 22:02:08 +00001749 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1750 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001751 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001752 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001753 return SDValue(N, 0);
1754}
1755
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001756
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001757SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001758 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001759 bool isTarget,
1760 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001761 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1762
1763 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001764 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001765 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001766 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001767 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001768 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001769 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001770 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001771
Michael Liaoabb87d42012-09-12 21:43:09 +00001772 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1773 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001774 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001775 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001776 return SDValue(N, 0);
1777}
1778
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001779SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001780 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001781 "SrcValue is not a pointer?");
1782
Jim Laskeyf576b422006-10-27 23:46:08 +00001783 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001784 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001785 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001786
Craig Topperc0196b12014-04-14 00:51:57 +00001787 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001788 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001789 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001790
Dan Gohman01c65a22010-03-18 18:49:47 +00001791 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001792 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001793 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001794 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001795}
1796
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001797/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1798SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1799 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001800 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001801 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001802
Craig Topperc0196b12014-04-14 00:51:57 +00001803 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001804 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001805 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001806
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001807 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1808 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001809 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001810 return SDValue(N, 0);
1811}
1812
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001813/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1814SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1815 unsigned SrcAS, unsigned DestAS) {
1816 SDValue Ops[] = {Ptr};
1817 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001818 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001819 ID.AddInteger(SrcAS);
1820 ID.AddInteger(DestAS);
1821
Craig Topperc0196b12014-04-14 00:51:57 +00001822 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001823 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001824 return SDValue(E, 0);
1825
1826 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1827 dl.getDebugLoc(),
1828 VT, Ptr, SrcAS, DestAS);
1829 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001830 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001831 return SDValue(N, 0);
1832}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001833
Duncan Sands41826032009-01-31 15:50:11 +00001834/// getShiftAmountOperand - Return the specified value casted to
1835/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001836SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001837 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001838 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001839 if (OpTy == ShTy || OpTy.isVector()) return Op;
1840
1841 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001842 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001843}
1844
Chris Lattner9eb7a822007-10-15 17:47:20 +00001845/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1846/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001847SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001848 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001849 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001850 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001851 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001852 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001853
David Greene1fbe0542009-11-12 20:49:22 +00001854 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001855 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001856}
1857
Duncan Sands445071c2008-12-09 21:33:20 +00001858/// CreateStackTemporary - Create a stack temporary suitable for holding
1859/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001860SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001861 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1862 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001863 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1864 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001865 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001866 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1867 TD->getPrefTypeAlignment(Ty2));
1868
1869 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001870 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001871 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001872}
1873
Owen Anderson53aa7a92009-08-10 22:56:29 +00001874SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001875 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001876 // These setcc operations always fold.
1877 switch (Cond) {
1878 default: break;
1879 case ISD::SETFALSE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001880 case ISD::SETFALSE2: return getConstant(0, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001881 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001882 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001883 TargetLowering::BooleanContent Cnt =
1884 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001885 return getConstant(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001886 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1887 VT);
Tim Northover950fcc02013-09-06 12:38:12 +00001888 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001889
Chris Lattner393d96a2006-04-27 05:01:07 +00001890 case ISD::SETOEQ:
1891 case ISD::SETOGT:
1892 case ISD::SETOGE:
1893 case ISD::SETOLT:
1894 case ISD::SETOLE:
1895 case ISD::SETONE:
1896 case ISD::SETO:
1897 case ISD::SETUO:
1898 case ISD::SETUEQ:
1899 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001900 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001901 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001902 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001903
Gabor Greiff304a7a2008-08-28 21:40:38 +00001904 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001905 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001906 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001907 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001908
Chris Lattner061a1ea2005-01-07 07:46:32 +00001909 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001910 default: llvm_unreachable("Unknown integer setcc!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001911 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT);
1912 case ISD::SETNE: return getConstant(C1 != C2, dl, VT);
1913 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1914 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1915 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1916 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1917 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT);
1918 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT);
1919 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT);
1920 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001921 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001922 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001923 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001924 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1925 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001926 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001927 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001928 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001929 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001930 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001931 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001932 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001933 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001934 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001935 // fall through
1936 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001937 R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001938 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001939 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001940 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001941 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001942 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001943 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001944 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001945 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001946 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001947 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001948 // fall through
1949 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001950 R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001951 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001952 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001953 // fall through
1954 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001955 R==APFloat::cmpEqual, dl, VT);
1956 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1957 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001958 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001959 R==APFloat::cmpEqual, dl, VT);
1960 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001961 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001962 R==APFloat::cmpLessThan, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001963 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001964 R==APFloat::cmpUnordered, dl, VT);
1965 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1966 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001967 }
1968 } else {
1969 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001970 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1971 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001972 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001973 return SDValue();
1974
1975 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001976 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001977 }
1978
Chris Lattnerd47675e2005-08-09 20:20:18 +00001979 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001980 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001981}
1982
Dan Gohman1f372ed2008-02-25 21:11:39 +00001983/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1984/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001985bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001986 // This predicate is not safe for vector operations.
1987 if (Op.getValueType().isVector())
1988 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001989
Dan Gohman1d459e42009-12-11 21:31:27 +00001990 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001991 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1992}
1993
Dan Gohman309d3d52007-06-22 14:59:07 +00001994/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1995/// this predicate to simplify operations downstream. Mask is known to be zero
1996/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001997bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001998 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001999 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002000 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002001 return (KnownZero & Mask) == Mask;
2002}
2003
Jay Foada0653a32014-05-14 21:14:37 +00002004/// Determine which bits of Op are known to be either zero or one and return
2005/// them in the KnownZero/KnownOne bitsets.
2006void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
2007 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002008 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00002009
Dan Gohmanf990faf2008-02-13 00:35:47 +00002010 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002011 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00002012 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002013
Dan Gohmanf990faf2008-02-13 00:35:47 +00002014 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00002015
2016 switch (Op.getOpcode()) {
2017 case ISD::Constant:
2018 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002019 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
2020 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00002021 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002022 case ISD::AND:
2023 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00002024 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2025 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002026
2027 // Output known-1 bits are only known if set in both the LHS & RHS.
2028 KnownOne &= KnownOne2;
2029 // Output known-0 are known to be clear if zero in either the LHS | RHS.
2030 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002031 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002032 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00002033 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2034 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002035
Dan Gohman309d3d52007-06-22 14:59:07 +00002036 // Output known-0 bits are only known if clear in both the LHS & RHS.
2037 KnownZero &= KnownZero2;
2038 // Output known-1 are known to be set if set in either the LHS | RHS.
2039 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00002040 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002041 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00002042 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2043 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002044
Dan Gohman309d3d52007-06-22 14:59:07 +00002045 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002046 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00002047 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2048 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2049 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00002050 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002051 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002052 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00002053 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2054 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002055
2056 // If low bits are zero in either operand, output low known-0 bits.
2057 // Also compute a conserative estimate for high known-0 bits.
2058 // More trickiness is possible, but this is sufficient for the
2059 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00002060 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00002061 unsigned TrailZ = KnownZero.countTrailingOnes() +
2062 KnownZero2.countTrailingOnes();
2063 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002064 KnownZero2.countLeadingOnes(),
2065 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002066
2067 TrailZ = std::min(TrailZ, BitWidth);
2068 LeadZ = std::min(LeadZ, BitWidth);
2069 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2070 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002071 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002072 }
2073 case ISD::UDIV: {
2074 // For the purposes of computing leading zeros we can conservatively
2075 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002076 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002077 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002078 unsigned LeadZ = KnownZero2.countLeadingOnes();
2079
Jay Foad25a5e4c2010-12-01 08:53:58 +00002080 KnownOne2.clearAllBits();
2081 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002082 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002083 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2084 if (RHSUnknownLeadingOnes != BitWidth)
2085 LeadZ = std::min(BitWidth,
2086 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002087
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002088 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002089 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002090 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002091 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002092 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2093 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002094
Dan Gohman309d3d52007-06-22 14:59:07 +00002095 // Only known if known in both the LHS and RHS.
2096 KnownOne &= KnownOne2;
2097 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002098 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002099 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002100 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2101 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002102
Dan Gohman309d3d52007-06-22 14:59:07 +00002103 // Only known if known in both the LHS and RHS.
2104 KnownOne &= KnownOne2;
2105 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002106 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002107 case ISD::SADDO:
2108 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002109 case ISD::SSUBO:
2110 case ISD::USUBO:
2111 case ISD::SMULO:
2112 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002113 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002114 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002115 // The boolean result conforms to getBooleanContents.
2116 // If we know the result of a setcc has the top bits zero, use this info.
2117 // We know that we have an integer-based boolean since these operations
2118 // are only available for integer.
2119 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2120 TargetLowering::ZeroOrOneBooleanContent &&
2121 BitWidth > 1)
2122 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2123 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002124 case ISD::SETCC:
2125 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002126 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2127 TargetLowering::ZeroOrOneBooleanContent &&
2128 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002129 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002130 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002131 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002132 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002133 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002134 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002135
2136 // If the shift count is an invalid immediate, don't do anything.
2137 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002138 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002139
Jay Foada0653a32014-05-14 21:14:37 +00002140 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002141 KnownZero <<= ShAmt;
2142 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002143 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002144 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002145 }
Jay Foad5a29c362014-05-15 12:12:55 +00002146 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002147 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002148 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002149 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002150 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002151
Dan Gohman9db0aa82008-02-26 18:50:50 +00002152 // If the shift count is an invalid immediate, don't do anything.
2153 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002154 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002155
Jay Foada0653a32014-05-14 21:14:37 +00002156 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002157 KnownZero = KnownZero.lshr(ShAmt);
2158 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002159
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002160 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002161 KnownZero |= HighBits; // High bits known zero.
2162 }
Jay Foad5a29c362014-05-15 12:12:55 +00002163 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002164 case ISD::SRA:
2165 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002166 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002167
Dan Gohman9db0aa82008-02-26 18:50:50 +00002168 // If the shift count is an invalid immediate, don't do anything.
2169 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002170 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002171
Dan Gohman309d3d52007-06-22 14:59:07 +00002172 // If any of the demanded bits are produced by the sign extension, we also
2173 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002174 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002175
Jay Foada0653a32014-05-14 21:14:37 +00002176 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002177 KnownZero = KnownZero.lshr(ShAmt);
2178 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002179
Dan Gohman309d3d52007-06-22 14:59:07 +00002180 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002181 APInt SignBit = APInt::getSignBit(BitWidth);
2182 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002183
Dan Gohmanb717fda2008-02-20 16:30:17 +00002184 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002185 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002186 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002187 KnownOne |= HighBits; // New bits are known one.
2188 }
2189 }
Jay Foad5a29c362014-05-15 12:12:55 +00002190 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002191 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002192 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002193 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002194
2195 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002196 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002197 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002198
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002199 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002200 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002201
Dan Gohman309d3d52007-06-22 14:59:07 +00002202 // If the sign extended bits are demanded, we know that the sign
2203 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002204 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002205 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002206 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002207
Jay Foada0653a32014-05-14 21:14:37 +00002208 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002209 KnownOne &= InputDemandedBits;
2210 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002211
Dan Gohman309d3d52007-06-22 14:59:07 +00002212 // If the sign bit of the input is known set or clear, then we know the
2213 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002214 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002215 KnownZero |= NewBits;
2216 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002217 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002218 KnownOne |= NewBits;
2219 KnownZero &= ~NewBits;
2220 } else { // Input sign bit unknown
2221 KnownZero &= ~NewBits;
2222 KnownOne &= ~NewBits;
2223 }
Jay Foad5a29c362014-05-15 12:12:55 +00002224 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002225 }
2226 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002227 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002228 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002229 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002230 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002231 unsigned LowBits = Log2_32(BitWidth)+1;
2232 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002233 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002234 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002235 }
2236 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002237 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002238 // If this is a ZEXTLoad and we are looking at the loaded value.
2239 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002240 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002241 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002242 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002243 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002244 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002245 }
Jay Foad5a29c362014-05-15 12:12:55 +00002246 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002247 }
2248 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002249 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002250 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002251 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002252 KnownZero = KnownZero.trunc(InBits);
2253 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002254 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002255 KnownZero = KnownZero.zext(BitWidth);
2256 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002257 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002258 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002259 }
2260 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002261 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002262 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002263 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002264
Jay Foad583abbc2010-12-07 08:25:19 +00002265 KnownZero = KnownZero.trunc(InBits);
2266 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002267 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002268
2269 // Note if the sign bit is known to be zero or one.
2270 bool SignBitKnownZero = KnownZero.isNegative();
2271 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002272
Jay Foad583abbc2010-12-07 08:25:19 +00002273 KnownZero = KnownZero.zext(BitWidth);
2274 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002275
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002276 // If the sign bit is known zero or one, the top bits match.
2277 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002278 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002279 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002280 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002281 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002282 }
2283 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002284 EVT InVT = Op.getOperand(0).getValueType();
2285 unsigned InBits = InVT.getScalarType().getSizeInBits();
2286 KnownZero = KnownZero.trunc(InBits);
2287 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002288 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002289 KnownZero = KnownZero.zext(BitWidth);
2290 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002291 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002292 }
2293 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002294 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002295 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002296 KnownZero = KnownZero.zext(InBits);
2297 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002298 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002299 KnownZero = KnownZero.trunc(BitWidth);
2300 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002301 break;
2302 }
2303 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002304 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002305 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002306 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002307 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002308 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002309 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002310 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002311 case ISD::FGETSIGN:
2312 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002313 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002314 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002315
Dan Gohman72ec3f42008-04-28 17:02:21 +00002316 case ISD::SUB: {
2317 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2318 // We know that the top bits of C-X are clear if X contains less bits
2319 // than C (i.e. no wrap-around can happen). For example, 20-X is
2320 // positive if we can prove that X is >= 0 and < 16.
2321 if (CLHS->getAPIntValue().isNonNegative()) {
2322 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2323 // NLZ can't be BitWidth with no sign bit
2324 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002325 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002326
2327 // If all of the MaskV bits are known to be zero, then we know the
2328 // output top bits are zero, because we now know that the output is
2329 // from [0-C].
2330 if ((KnownZero2 & MaskV) == MaskV) {
2331 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2332 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002333 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002334 }
2335 }
2336 }
2337 }
2338 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002339 case ISD::ADD:
2340 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002341 // Output known-0 bits are known if clear or set in both the low clear bits
2342 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2343 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002344 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002345 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2346
Jay Foada0653a32014-05-14 21:14:37 +00002347 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002348 KnownZeroOut = std::min(KnownZeroOut,
2349 KnownZero2.countTrailingOnes());
2350
Chris Lattner440b2802010-12-19 20:38:28 +00002351 if (Op.getOpcode() == ISD::ADD) {
2352 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002353 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002354 }
2355
2356 // With ADDE, a carry bit may be added in, so we can only use this
2357 // information if we know (at least) that the low two bits are clear. We
2358 // then return to the caller that the low bit is unknown but that other bits
2359 // are known zero.
2360 if (KnownZeroOut >= 2) // ADDE
2361 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002362 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002363 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002364 case ISD::SREM:
2365 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002366 const APInt &RA = Rem->getAPIntValue().abs();
2367 if (RA.isPowerOf2()) {
2368 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002369 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002370
Duncan Sands33274982010-01-29 09:45:26 +00002371 // The low bits of the first operand are unchanged by the srem.
2372 KnownZero = KnownZero2 & LowBits;
2373 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002374
Duncan Sands33274982010-01-29 09:45:26 +00002375 // If the first operand is non-negative or has all low bits zero, then
2376 // the upper bits are all zero.
2377 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2378 KnownZero |= ~LowBits;
2379
2380 // If the first operand is negative and not all low bits are zero, then
2381 // the upper bits are all one.
2382 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2383 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002384 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002385 }
2386 }
Jay Foad5a29c362014-05-15 12:12:55 +00002387 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002388 case ISD::UREM: {
2389 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002390 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002391 if (RA.isPowerOf2()) {
2392 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002393 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2394
2395 // The upper bits are all zero, the lower ones are unchanged.
2396 KnownZero = KnownZero2 | ~LowBits;
2397 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002398 break;
2399 }
2400 }
2401
2402 // Since the result is less than or equal to either operand, any leading
2403 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002404 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2405 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002406
2407 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2408 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002409 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002410 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002411 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002412 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002413 case ISD::EXTRACT_ELEMENT: {
2414 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2415 const unsigned Index =
2416 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2417 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2418
2419 // Remove low part of known bits mask
2420 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2421 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2422
2423 // Remove high part of known bit mask
2424 KnownZero = KnownZero.trunc(BitWidth);
2425 KnownOne = KnownOne.trunc(BitWidth);
2426 break;
2427 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002428 case ISD::FrameIndex:
2429 case ISD::TargetFrameIndex:
2430 if (unsigned Align = InferPtrAlignment(Op)) {
2431 // The low bits are known zero if the pointer is aligned.
2432 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002433 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002434 }
2435 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002436
Dan Gohman309d3d52007-06-22 14:59:07 +00002437 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002438 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2439 break;
2440 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002441 case ISD::INTRINSIC_WO_CHAIN:
2442 case ISD::INTRINSIC_W_CHAIN:
2443 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002444 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002445 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002446 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002447 }
Jay Foad5a29c362014-05-15 12:12:55 +00002448
2449 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002450}
2451
2452/// ComputeNumSignBits - Return the number of times the sign bit of the
2453/// register is replicated into the other bits. We know that at least 1 bit
2454/// is always equal to the sign bit (itself), but other cases can give us
2455/// information. For example, immediately after an "SRA X, 2", we know that
2456/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002457unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002458 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002459 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002460 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002461 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002462 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002463
Dan Gohman309d3d52007-06-22 14:59:07 +00002464 if (Depth == 6)
2465 return 1; // Limit search depth.
2466
2467 switch (Op.getOpcode()) {
2468 default: break;
2469 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002470 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002471 return VTBits-Tmp+1;
2472 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002473 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002474 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002475
Dan Gohman309d3d52007-06-22 14:59:07 +00002476 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002477 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002478 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002479 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002480
Dan Gohman309d3d52007-06-22 14:59:07 +00002481 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002482 Tmp =
2483 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002484 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002485
Dan Gohman309d3d52007-06-22 14:59:07 +00002486 case ISD::SIGN_EXTEND_INREG:
2487 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002488 Tmp =
2489 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002490 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002491
Dan Gohman309d3d52007-06-22 14:59:07 +00002492 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2493 return std::max(Tmp, Tmp2);
2494
2495 case ISD::SRA:
2496 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2497 // SRA X, C -> adds C sign bits.
2498 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002499 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002500 if (Tmp > VTBits) Tmp = VTBits;
2501 }
2502 return Tmp;
2503 case ISD::SHL:
2504 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2505 // shl destroys sign bits.
2506 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002507 if (C->getZExtValue() >= VTBits || // Bad shift.
2508 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2509 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002510 }
2511 break;
2512 case ISD::AND:
2513 case ISD::OR:
2514 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002515 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002516 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002517 if (Tmp != 1) {
2518 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2519 FirstAnswer = std::min(Tmp, Tmp2);
2520 // We computed what we know about the sign bits as our first
2521 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002522 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002523 }
2524 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002525
2526 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002527 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002528 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002529 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002530 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002531
2532 case ISD::SADDO:
2533 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002534 case ISD::SSUBO:
2535 case ISD::USUBO:
2536 case ISD::SMULO:
2537 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002538 if (Op.getResNo() != 1)
2539 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002540 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002541 // If setcc returns 0/-1, all bits are sign bits.
2542 // We know that we have an integer-based boolean since these operations
2543 // are only available for integer.
2544 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2545 TargetLowering::ZeroOrNegativeOneBooleanContent)
2546 return VTBits;
2547 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002548 case ISD::SETCC:
2549 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002550 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002551 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002552 return VTBits;
2553 break;
2554 case ISD::ROTL:
2555 case ISD::ROTR:
2556 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002557 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002558
Dan Gohman309d3d52007-06-22 14:59:07 +00002559 // Handle rotate right by N like a rotate left by 32-N.
2560 if (Op.getOpcode() == ISD::ROTR)
2561 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2562
2563 // If we aren't rotating out all of the known-in sign bits, return the
2564 // number that are left. This handles rotl(sext(x), 1) for example.
2565 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2566 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2567 }
2568 break;
2569 case ISD::ADD:
2570 // Add can have at most one carry bit. Thus we know that the output
2571 // is, at worst, one more bit than the inputs.
2572 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2573 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002574
Dan Gohman309d3d52007-06-22 14:59:07 +00002575 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002576 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002577 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002578 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002579 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002580
Dan Gohman309d3d52007-06-22 14:59:07 +00002581 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2582 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002583 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002584 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002585
Dan Gohman309d3d52007-06-22 14:59:07 +00002586 // If we are subtracting one from a positive number, there is no carry
2587 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002588 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002589 return Tmp;
2590 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002591
Dan Gohman309d3d52007-06-22 14:59:07 +00002592 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2593 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002594 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002595
Dan Gohman309d3d52007-06-22 14:59:07 +00002596 case ISD::SUB:
2597 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2598 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002599
Dan Gohman309d3d52007-06-22 14:59:07 +00002600 // Handle NEG.
2601 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002602 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002603 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002604 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002605 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2606 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002607 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002608 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002609
Dan Gohman309d3d52007-06-22 14:59:07 +00002610 // If the input is known to be positive (the sign bit is known clear),
2611 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002612 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002613 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002614
Dan Gohman309d3d52007-06-22 14:59:07 +00002615 // Otherwise, we treat this like a SUB.
2616 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002617
Dan Gohman309d3d52007-06-22 14:59:07 +00002618 // Sub can have at most one carry bit. Thus we know that the output
2619 // is, at worst, one more bit than the inputs.
2620 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2621 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002622 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002623 case ISD::TRUNCATE:
2624 // FIXME: it's tricky to do anything useful for this, but it is an important
2625 // case for targets like X86.
2626 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002627 case ISD::EXTRACT_ELEMENT: {
2628 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2629 const int BitWidth = Op.getValueType().getSizeInBits();
2630 const int Items =
2631 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2632
2633 // Get reverse index (starting from 1), Op1 value indexes elements from
2634 // little end. Sign starts at big end.
2635 const int rIndex = Items - 1 -
2636 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2637
2638 // If the sign portion ends in our element the substraction gives correct
2639 // result. Otherwise it gives either negative or > bitwidth result
2640 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2641 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002642 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002643
Nadav Rotem4536d582013-03-20 22:53:44 +00002644 // If we are looking at the loaded value of the SDNode.
2645 if (Op.getResNo() == 0) {
2646 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2647 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2648 unsigned ExtType = LD->getExtensionType();
2649 switch (ExtType) {
2650 default: break;
2651 case ISD::SEXTLOAD: // '17' bits known
2652 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2653 return VTBits-Tmp+1;
2654 case ISD::ZEXTLOAD: // '16' bits known
2655 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2656 return VTBits-Tmp;
2657 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002658 }
2659 }
2660
2661 // Allow the target to implement this method for its nodes.
2662 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002663 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002664 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2665 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002666 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002667 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002668 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002669
Dan Gohman309d3d52007-06-22 14:59:07 +00002670 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2671 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002672 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002673 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002674
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002675 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002676 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002677 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002678 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002679 Mask = KnownOne;
2680 } else {
2681 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002682 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002683 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002684
Dan Gohman309d3d52007-06-22 14:59:07 +00002685 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2686 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002687 Mask = ~Mask;
2688 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002689 // Return # leading zeros. We use 'min' here in case Val was zero before
2690 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002691 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002692}
2693
Chris Lattner46c01a32011-02-13 22:25:43 +00002694/// isBaseWithConstantOffset - Return true if the specified operand is an
2695/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2696/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2697/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002698/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002699bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2700 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2701 !isa<ConstantSDNode>(Op.getOperand(1)))
2702 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002703
2704 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002705 !MaskedValueIsZero(Op.getOperand(0),
2706 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2707 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002708
Chris Lattner46c01a32011-02-13 22:25:43 +00002709 return true;
2710}
2711
2712
Dan Gohmand0d5e682009-09-03 20:34:31 +00002713bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2714 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002715 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002716 return true;
2717
2718 // If the value is a constant, we can obviously see if it is a NaN or not.
2719 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2720 return !C->getValueAPF().isNaN();
2721
2722 // TODO: Recognize more cases here.
2723
2724 return false;
2725}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002726
Dan Gohman38605212010-02-24 06:52:40 +00002727bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2728 // If the value is a constant, we can obviously see if it is a zero or not.
2729 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2730 return !C->isZero();
2731
2732 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002733 switch (Op.getOpcode()) {
2734 default: break;
2735 case ISD::OR:
2736 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2737 return !C->isNullValue();
2738 break;
2739 }
Dan Gohman38605212010-02-24 06:52:40 +00002740
2741 return false;
2742}
2743
2744bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2745 // Check the obvious case.
2746 if (A == B) return true;
2747
2748 // For for negative and positive zero.
2749 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2750 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2751 if (CA->isZero() && CB->isZero()) return true;
2752
2753 // Otherwise they may not be equal.
2754 return false;
2755}
2756
Chris Lattner061a1ea2005-01-07 07:46:32 +00002757/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002758///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002759SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002760 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002761 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002762 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00002763 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002764 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002765
Jack Carter170a5f22013-09-09 22:02:08 +00002766 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2767 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002768 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002769
Chandler Carruth41b20e72014-07-22 04:07:55 +00002770 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002771 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002772}
2773
Andrew Trickef9de2a2013-05-25 02:42:55 +00002774SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002775 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002776 // Constant fold unary operations with an integer constant operand. Even
2777 // opaque constant will be folded, because the folding of unary operations
2778 // doesn't create new constants with different values. Nevertheless, the
2779 // opaque flag is preserved during folding to prevent future folding with
2780 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002781 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002782 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002783 switch (Opcode) {
2784 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002785 case ISD::SIGN_EXTEND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002786 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002787 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002788 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002789 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002790 case ISD::TRUNCATE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002791 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002792 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002793 case ISD::UINT_TO_FP:
2794 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002795 APFloat apf(EVTToAPFloatSemantics(VT),
2796 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002797 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002798 Opcode==ISD::SINT_TO_FP,
2799 APFloat::rmNearestTiesToEven);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002800 return getConstantFP(apf, DL, VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002801 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002802 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002803 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002804 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002805 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002806 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002807 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002808 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002809 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002810 case ISD::BSWAP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002811 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002812 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002813 case ISD::CTPOP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002814 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002815 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002816 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002817 case ISD::CTLZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002818 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002819 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002820 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002821 case ISD::CTTZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002822 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002823 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002824 }
2825 }
2826
Dale Johannesen446b9002007-08-31 23:34:27 +00002827 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002828 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002829 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002830 switch (Opcode) {
2831 case ISD::FNEG:
2832 V.changeSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002833 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002834 case ISD::FABS:
2835 V.clearSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002836 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002837 case ISD::FCEIL: {
2838 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2839 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002840 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002841 break;
2842 }
2843 case ISD::FTRUNC: {
2844 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2845 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002846 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002847 break;
2848 }
2849 case ISD::FFLOOR: {
2850 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2851 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002852 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002853 break;
2854 }
2855 case ISD::FP_EXTEND: {
2856 bool ignored;
2857 // This can return overflow, underflow, or inexact; we don't care.
2858 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002859 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002860 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002861 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002862 }
2863 case ISD::FP_TO_SINT:
2864 case ISD::FP_TO_UINT: {
2865 integerPart x[2];
2866 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002867 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002868 // FIXME need to be more flexible about rounding mode.
2869 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2870 Opcode==ISD::FP_TO_SINT,
2871 APFloat::rmTowardZero, &ignored);
2872 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002873 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002874 APInt api(VT.getSizeInBits(), x);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002875 return getConstant(api, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002876 }
2877 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002878 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002879 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Owen Anderson558012a2014-12-09 06:50:39 +00002880 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002881 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002882 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002883 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002884 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002885 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002886 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002887
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002888 // Constant fold unary operations with a vector integer or float operand.
Jim Grosbachf7502c42014-07-18 00:40:52 +00002889 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002890 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002891 switch (Opcode) {
2892 default:
2893 // FIXME: Entirely reasonable to perform folding of other unary
2894 // operations here as the need arises.
2895 break;
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002896 case ISD::FNEG:
2897 case ISD::FABS:
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002898 case ISD::FCEIL:
2899 case ISD::FTRUNC:
2900 case ISD::FFLOOR:
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002901 case ISD::FP_EXTEND:
Simon Pilgrim017ca192015-05-02 13:04:07 +00002902 case ISD::FP_TO_SINT:
2903 case ISD::FP_TO_UINT:
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002904 case ISD::TRUNCATE:
Jim Grosbachf7502c42014-07-18 00:40:52 +00002905 case ISD::UINT_TO_FP:
2906 case ISD::SINT_TO_FP: {
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002907 EVT SVT = VT.getScalarType();
2908 EVT InVT = BV->getValueType(0);
2909 EVT InSVT = InVT.getScalarType();
2910
Simon Pilgrime09584c2015-05-10 14:14:51 +00002911 // Find legal integer scalar type for constant promotion and
2912 // ensure that its scalar size is at least as large as source.
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002913 EVT LegalSVT = SVT;
2914 if (SVT.isInteger()) {
2915 LegalSVT = TLI->getTypeToTransformTo(*getContext(), SVT);
Simon Pilgrime09584c2015-05-10 14:14:51 +00002916 if (LegalSVT.bitsLT(SVT)) break;
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002917 }
2918
Simon Pilgrim481f4142015-03-23 22:44:55 +00002919 // Let the above scalar folding handle the folding of each element.
Jim Grosbach19dd3082014-07-23 20:41:31 +00002920 SmallVector<SDValue, 8> Ops;
2921 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2922 SDValue OpN = BV->getOperand(i);
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002923 EVT OpVT = OpN.getValueType();
2924
2925 // Build vector (integer) scalar operands may need implicit
2926 // truncation - do this before constant folding.
2927 if (OpVT.isInteger() && OpVT.bitsGT(InSVT))
2928 OpN = getNode(ISD::TRUNCATE, DL, InSVT, OpN);
2929
2930 OpN = getNode(Opcode, DL, SVT, OpN);
2931
2932 // Legalize the (integer) scalar constant if necessary.
2933 if (LegalSVT != SVT)
2934 OpN = getNode(ISD::ANY_EXTEND, DL, LegalSVT, OpN);
2935
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002936 if (OpN.getOpcode() != ISD::UNDEF &&
2937 OpN.getOpcode() != ISD::Constant &&
2938 OpN.getOpcode() != ISD::ConstantFP)
2939 break;
Jim Grosbach19dd3082014-07-23 20:41:31 +00002940 Ops.push_back(OpN);
2941 }
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002942 if (Ops.size() == VT.getVectorNumElements())
2943 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002944 break;
Jim Grosbachf7502c42014-07-18 00:40:52 +00002945 }
2946 }
2947 }
2948 }
2949
Gabor Greiff304a7a2008-08-28 21:40:38 +00002950 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002951 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002952 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002953 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002954 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002955 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002956 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002957 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002958 assert(VT.isFloatingPoint() &&
2959 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002960 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002961 assert((!VT.isVector() ||
2962 VT.getVectorNumElements() ==
2963 Operand.getValueType().getVectorNumElements()) &&
2964 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002965 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002966 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002967 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002968 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002969 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002970 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002971 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002972 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2973 "Invalid sext node, dst < src!");
2974 assert((!VT.isVector() ||
2975 VT.getVectorNumElements() ==
2976 Operand.getValueType().getVectorNumElements()) &&
2977 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002978 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2979 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2980 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002981 // sext(undef) = 0, because the top bits will all be the same.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002982 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002983 break;
2984 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002985 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002986 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002987 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002988 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2989 "Invalid zext node, dst < src!");
2990 assert((!VT.isVector() ||
2991 VT.getVectorNumElements() ==
2992 Operand.getValueType().getVectorNumElements()) &&
2993 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002994 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002995 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002996 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002997 else if (OpOpcode == ISD::UNDEF)
2998 // zext(undef) = 0, because the top bits will be zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002999 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003000 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00003001 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003002 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00003003 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00003004 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00003005 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
3006 "Invalid anyext node, dst < src!");
3007 assert((!VT.isVector() ||
3008 VT.getVectorNumElements() ==
3009 Operand.getValueType().getVectorNumElements()) &&
3010 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00003011
Dan Gohman08837892010-06-18 00:08:30 +00003012 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3013 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00003014 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00003015 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00003016 else if (OpOpcode == ISD::UNDEF)
3017 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00003018
3019 // (ext (trunx x)) -> x
3020 if (OpOpcode == ISD::TRUNCATE) {
3021 SDValue OpOp = Operand.getNode()->getOperand(0);
3022 if (OpOp.getValueType() == VT)
3023 return OpOp;
3024 }
Chris Lattner8c393c22005-09-02 00:17:32 +00003025 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003026 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00003027 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00003028 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003029 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00003030 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
3031 "Invalid truncate node, src < dst!");
3032 assert((!VT.isVector() ||
3033 VT.getVectorNumElements() ==
3034 Operand.getValueType().getVectorNumElements()) &&
3035 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003036 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00003037 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003038 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3039 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00003040 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00003041 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
3042 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00003043 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003044 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00003045 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003046 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00003047 }
Craig Topper201c1a32012-01-15 01:05:11 +00003048 if (OpOpcode == ISD::UNDEF)
3049 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003050 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003051 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00003052 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00003053 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00003054 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00003055 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00003056 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
3057 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00003058 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003059 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003060 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003061 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003062 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00003063 (VT.getVectorElementType() == Operand.getValueType() ||
3064 (VT.getVectorElementType().isInteger() &&
3065 Operand.getValueType().isInteger() &&
3066 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003067 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00003068 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003069 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00003070 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3071 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3072 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3073 Operand.getConstantOperandVal(1) == 0 &&
3074 Operand.getOperand(0).getValueType() == VT)
3075 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003076 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00003077 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00003078 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003079 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00003080 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00003081 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003082 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00003083 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00003084 break;
3085 case ISD::FABS:
3086 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00003087 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003088 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003089 }
3090
Chris Lattnerf9c19152005-08-25 19:12:10 +00003091 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003092 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003093 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00003094 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003095 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00003096 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003097 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00003098 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003099 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003100
Jack Carter170a5f22013-09-09 22:02:08 +00003101 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3102 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003103 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003104 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003105 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3106 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003107 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003108
Chandler Carruth41b20e72014-07-22 04:07:55 +00003109 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003110 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003111}
3112
Matthias Braun20683ef2015-05-19 01:40:21 +00003113static std::pair<APInt, bool> FoldValue(unsigned Opcode, const APInt &C1,
3114 const APInt &C2) {
3115 switch (Opcode) {
3116 case ISD::ADD: return std::make_pair(C1 + C2, true);
3117 case ISD::SUB: return std::make_pair(C1 - C2, true);
3118 case ISD::MUL: return std::make_pair(C1 * C2, true);
3119 case ISD::AND: return std::make_pair(C1 & C2, true);
3120 case ISD::OR: return std::make_pair(C1 | C2, true);
3121 case ISD::XOR: return std::make_pair(C1 ^ C2, true);
3122 case ISD::SHL: return std::make_pair(C1 << C2, true);
3123 case ISD::SRL: return std::make_pair(C1.lshr(C2), true);
3124 case ISD::SRA: return std::make_pair(C1.ashr(C2), true);
3125 case ISD::ROTL: return std::make_pair(C1.rotl(C2), true);
3126 case ISD::ROTR: return std::make_pair(C1.rotr(C2), true);
3127 case ISD::UDIV:
3128 if (!C2.getBoolValue())
3129 break;
3130 return std::make_pair(C1.udiv(C2), true);
3131 case ISD::UREM:
3132 if (!C2.getBoolValue())
3133 break;
3134 return std::make_pair(C1.urem(C2), true);
3135 case ISD::SDIV:
3136 if (!C2.getBoolValue())
3137 break;
3138 return std::make_pair(C1.sdiv(C2), true);
3139 case ISD::SREM:
3140 if (!C2.getBoolValue())
3141 break;
3142 return std::make_pair(C1.srem(C2), true);
3143 }
3144 return std::make_pair(APInt(1, 0), false);
3145}
3146
3147SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
3148 const ConstantSDNode *Cst1,
3149 const ConstantSDNode *Cst2) {
3150 if (Cst1->isOpaque() || Cst2->isOpaque())
3151 return SDValue();
3152
3153 std::pair<APInt, bool> Folded = FoldValue(Opcode, Cst1->getAPIntValue(),
3154 Cst2->getAPIntValue());
3155 if (!Folded.second)
3156 return SDValue();
3157 return getConstant(Folded.first, DL, VT);
3158}
3159
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003160SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003161 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003162 // If the opcode is a target-specific ISD node, there's nothing we can
3163 // do here and the operand rules may not line up with the below, so
3164 // bail early.
3165 if (Opcode >= ISD::BUILTIN_OP_END)
3166 return SDValue();
3167
Matthias Braun20683ef2015-05-19 01:40:21 +00003168 // Handle the case of two scalars.
3169 if (const ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1)) {
3170 if (const ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2)) {
3171 if (SDValue Folded =
3172 FoldConstantArithmetic(Opcode, DL, VT, Scalar1, Scalar2)) {
3173 if (!VT.isVector())
3174 return Folded;
3175 SmallVector<SDValue, 4> Outputs;
3176 // We may have a vector type but a scalar result. Create a splat.
3177 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3178 // Build a big vector out of the scalar elements we generated.
3179 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
3180 } else {
3181 return SDValue();
3182 }
3183 }
3184 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003185
Matthias Braun20683ef2015-05-19 01:40:21 +00003186 // For vectors extract each constant element into Inputs so we can constant
3187 // fold them individually.
3188 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3189 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3190 if (!BV1 || !BV2)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003191 return SDValue();
3192
Matthias Braun20683ef2015-05-19 01:40:21 +00003193 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3194
3195 EVT SVT = VT.getScalarType();
3196 SmallVector<SDValue, 4> Outputs;
3197 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3198 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3199 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3200 if (!V1 || !V2) // Not a constant, bail.
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003201 return SDValue();
3202
Matthias Braun20683ef2015-05-19 01:40:21 +00003203 if (V1->isOpaque() || V2->isOpaque())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003204 return SDValue();
Matthias Braun20683ef2015-05-19 01:40:21 +00003205
3206 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3207 // FIXME: This is valid and could be handled by truncating the APInts.
3208 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3209 return SDValue();
3210
3211 // Fold one vector element.
3212 std::pair<APInt, bool> Folded = FoldValue(Opcode, V1->getAPIntValue(),
3213 V2->getAPIntValue());
3214 if (!Folded.second)
3215 return SDValue();
3216 Outputs.push_back(getConstant(Folded.first, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003217 }
3218
Matthias Braun20683ef2015-05-19 01:40:21 +00003219 assert(VT.getVectorNumElements() == Outputs.size() &&
3220 "Vector size mismatch!");
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003221
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003222 // We may have a vector type but a scalar result. Create a splat.
3223 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3224
3225 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003226 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003227}
3228
Andrew Trickef9de2a2013-05-25 02:42:55 +00003229SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Nick Lewycky37a17502015-05-13 23:41:47 +00003230 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003231 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3232 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003233 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003234 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003235 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003236 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3237 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003238 // Fold trivial token factors.
3239 if (N1.getOpcode() == ISD::EntryToken) return N2;
3240 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003241 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003242 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003243 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003244 // Concat of UNDEFs is UNDEF.
3245 if (N1.getOpcode() == ISD::UNDEF &&
3246 N2.getOpcode() == ISD::UNDEF)
3247 return getUNDEF(VT);
3248
Dan Gohman550c9af2008-08-14 20:04:46 +00003249 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3250 // one big BUILD_VECTOR.
3251 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3252 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003253 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3254 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003255 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Simon Pilgrim860f0872015-04-21 08:05:43 +00003256
3257 // BUILD_VECTOR requires all inputs to be of the same type, find the
3258 // maximum type and extend them all.
3259 EVT SVT = VT.getScalarType();
3260 for (SDValue Op : Elts)
3261 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
3262 if (SVT.bitsGT(VT.getScalarType()))
3263 for (SDValue &Op : Elts)
3264 Op = TLI->isZExtFree(Op.getValueType(), SVT)
3265 ? getZExtOrTrunc(Op, DL, SVT)
3266 : getSExtOrTrunc(Op, DL, SVT);
3267
Craig Topper48d114b2014-04-26 18:35:24 +00003268 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003269 }
3270 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003271 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003272 assert(VT.isInteger() && "This operator does not apply to FP types!");
3273 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003274 N1.getValueType() == VT && "Binary operator types must match!");
3275 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3276 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003277 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003278 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003279 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3280 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003281 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003282 case ISD::OR:
3283 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003284 case ISD::ADD:
3285 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003286 assert(VT.isInteger() && "This operator does not apply to FP types!");
3287 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003288 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003289 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3290 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003291 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003292 return N1;
3293 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003294 case ISD::UDIV:
3295 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003296 case ISD::MULHU:
3297 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003298 case ISD::MUL:
3299 case ISD::SDIV:
3300 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003301 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003302 assert(N1.getValueType() == N2.getValueType() &&
3303 N1.getValueType() == VT && "Binary operator types must match!");
3304 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003305 case ISD::FADD:
3306 case ISD::FSUB:
3307 case ISD::FMUL:
3308 case ISD::FDIV:
3309 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003310 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003311 if (Opcode == ISD::FADD) {
3312 // 0+x --> x
3313 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3314 if (CFP->getValueAPF().isZero())
3315 return N2;
3316 // x+0 --> x
3317 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3318 if (CFP->getValueAPF().isZero())
3319 return N1;
3320 } else if (Opcode == ISD::FSUB) {
3321 // x-0 --> x
3322 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3323 if (CFP->getValueAPF().isZero())
3324 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003325 } else if (Opcode == ISD::FMUL) {
3326 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3327 SDValue V = N2;
3328
3329 // If the first operand isn't the constant, try the second
3330 if (!CFP) {
3331 CFP = dyn_cast<ConstantFPSDNode>(N2);
3332 V = N1;
3333 }
3334
3335 if (CFP) {
3336 // 0*x --> 0
3337 if (CFP->isZero())
3338 return SDValue(CFP,0);
3339 // 1*x --> x
3340 if (CFP->isExactlyValue(1.0))
3341 return V;
3342 }
Dan Gohman1275e282009-01-23 19:10:37 +00003343 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003344 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003345 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003346 assert(N1.getValueType() == N2.getValueType() &&
3347 N1.getValueType() == VT && "Binary operator types must match!");
3348 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003349 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3350 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003351 N1.getValueType().isFloatingPoint() &&
3352 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003353 "Invalid FCOPYSIGN!");
3354 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003355 case ISD::SHL:
3356 case ISD::SRA:
3357 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003358 case ISD::ROTL:
3359 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003360 assert(VT == N1.getValueType() &&
3361 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003362 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003363 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003364 assert((!VT.isVector() || VT == N2.getValueType()) &&
3365 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003366 // Verify that the shift amount VT is bit enough to hold valid shift
3367 // amounts. This catches things like trying to shift an i1024 value by an
3368 // i8, which is easy to fall into in generic code that uses
3369 // TLI.getShiftAmount().
3370 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003371 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003372 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003373
3374 // Always fold shifts of i1 values so the code generator doesn't need to
3375 // handle them. Since we know the size of the shift has to be less than the
3376 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003377 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003378 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003379 if (N2C && N2C->isNullValue())
3380 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003381 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003382 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003383 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003384 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003385 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003386 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003387 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003388 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003389 "type is vector!");
3390 assert((!EVT.isVector() ||
3391 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3392 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003393 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003394 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003395 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003396 break;
3397 }
Chris Lattner72733e52008-01-17 07:00:52 +00003398 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003399 assert(VT.isFloatingPoint() &&
3400 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003401 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003402 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003403 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003404 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003405 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003406 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003407 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003408 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003409 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003410 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003411 assert(!EVT.isVector() &&
3412 "AssertSExt/AssertZExt type should be the vector element type "
3413 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003414 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003415 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003416 break;
3417 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003418 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003419 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003420 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003421 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003422 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003423 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003424 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003425 "type is vector!");
3426 assert((!EVT.isVector() ||
3427 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3428 "Vector element counts must match in SIGN_EXTEND_INREG");
3429 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003430 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003431
Chris Lattner16713612008-01-22 19:09:33 +00003432 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003433 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003434 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003435 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003436 Val = Val.ashr(Val.getBitWidth()-FromBits);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003437 return getConstant(Val, DL, VT);
Chris Lattner751817c2006-05-06 23:05:41 +00003438 }
Chris Lattner16713612008-01-22 19:09:33 +00003439 break;
3440 }
3441 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003442 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3443 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003444 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003445
Pawel Bylica9f1fb9d2015-05-06 10:19:14 +00003446 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF
3447 if (N2C && N2C->getZExtValue() >= N1.getValueType().getVectorNumElements())
3448 return getUNDEF(VT);
3449
Chris Lattner16713612008-01-22 19:09:33 +00003450 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3451 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003452 if (N2C &&
3453 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003454 N1.getNumOperands() > 0) {
3455 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003456 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003457 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003458 N1.getOperand(N2C->getZExtValue() / Factor),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003459 getConstant(N2C->getZExtValue() % Factor, DL,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003460 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003461 }
3462
3463 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3464 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003465 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3466 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003467
3468 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003469 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003470 // are promoted and implicitly truncated, and the result implicitly
3471 // extended. Make that explicit here.
3472 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003473
Bob Wilson59dbbb22009-04-13 22:05:19 +00003474 return Elt;
3475 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003476
Chris Lattner16713612008-01-22 19:09:33 +00003477 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3478 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003479 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003480 // If the indices are the same, return the inserted element else
3481 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003482 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003483 SDValue N1Op2 = N1.getOperand(2);
3484 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3485
3486 if (N1Op2C && N2C) {
3487 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3488 if (VT == N1.getOperand(1).getValueType())
3489 return N1.getOperand(1);
3490 else
3491 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3492 }
3493
Dale Johannesendb393622009-02-03 01:55:44 +00003494 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003495 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003496 }
Chris Lattner16713612008-01-22 19:09:33 +00003497 break;
3498 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003499 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003500 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3501 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003502 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003503 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003504
Chris Lattner16713612008-01-22 19:09:33 +00003505 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3506 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003507 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003508 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003509 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003510
Chris Lattner16713612008-01-22 19:09:33 +00003511 // EXTRACT_ELEMENT of a constant int is also very common.
3512 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003513 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003514 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003515 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003516 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
Chris Lattner16713612008-01-22 19:09:33 +00003517 }
3518 break;
David Greeneb6f16112011-01-26 15:38:49 +00003519 case ISD::EXTRACT_SUBVECTOR: {
3520 SDValue Index = N2;
3521 if (VT.isSimple() && N1.getValueType().isSimple()) {
3522 assert(VT.isVector() && N1.getValueType().isVector() &&
3523 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003524 assert(VT.getVectorElementType() ==
3525 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003526 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003527 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003528 "Extract subvector must be from larger vector to smaller vector!");
3529
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003530 if (isa<ConstantSDNode>(Index.getNode())) {
3531 assert((VT.getVectorNumElements() +
3532 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003533 <= N1.getValueType().getVectorNumElements())
3534 && "Extract subvector overflow!");
3535 }
3536
3537 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003538 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003539 return N1;
3540 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003541 break;
Chris Lattner16713612008-01-22 19:09:33 +00003542 }
David Greeneb6f16112011-01-26 15:38:49 +00003543 }
Chris Lattner16713612008-01-22 19:09:33 +00003544
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003545 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003546 if (SDValue SV =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003547 FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
Matthias Braunf50ab432015-01-13 22:17:46 +00003548 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003549
3550 // Canonicalize constant to RHS if commutative.
3551 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3552 std::swap(N1C, N2C);
3553 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003554 }
3555
Chris Lattner16713612008-01-22 19:09:33 +00003556 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003557 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003558 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3559 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003560 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003561 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003562 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003563 std::swap(N1CFP, N2CFP);
3564 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003565 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003566 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3567 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003568 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003569 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003570 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003571 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003572 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003573 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003574 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003575 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003576 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003577 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003578 break;
3579 case ISD::FMUL:
3580 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003581 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003582 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003583 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003584 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003585 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003586 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3587 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003588 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003589 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003590 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003591 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003592 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003593 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3594 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003595 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003596 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003597 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003598 case ISD::FCOPYSIGN:
3599 V1.copySign(V2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003600 return getConstantFP(V1, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003601 default: break;
3602 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003603 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003604
3605 if (Opcode == ISD::FP_ROUND) {
3606 APFloat V = N1CFP->getValueAPF(); // make copy
3607 bool ignored;
3608 // This can return overflow, underflow, or inexact; we don't care.
3609 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003610 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003611 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003612 return getConstantFP(V, DL, VT);
Owen Anderson6f1ee162012-04-10 22:46:53 +00003613 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003614 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003615
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003616 // Canonicalize an UNDEF to the RHS, even over a constant.
3617 if (N1.getOpcode() == ISD::UNDEF) {
3618 if (isCommutativeBinOp(Opcode)) {
3619 std::swap(N1, N2);
3620 } else {
3621 switch (Opcode) {
3622 case ISD::FP_ROUND_INREG:
3623 case ISD::SIGN_EXTEND_INREG:
3624 case ISD::SUB:
3625 case ISD::FSUB:
3626 case ISD::FDIV:
3627 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003628 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003629 return N1; // fold op(undef, arg2) -> undef
3630 case ISD::UDIV:
3631 case ISD::SDIV:
3632 case ISD::UREM:
3633 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003634 case ISD::SRL:
3635 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003636 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003637 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003638 // For vectors, we can't easily build an all zero vector, just return
3639 // the LHS.
3640 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003641 }
3642 }
3643 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003644
3645 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003646 if (N2.getOpcode() == ISD::UNDEF) {
3647 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003648 case ISD::XOR:
3649 if (N1.getOpcode() == ISD::UNDEF)
3650 // Handle undef ^ undef -> 0 special case. This is a common
3651 // idiom (misuse).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003652 return getConstant(0, DL, VT);
Evan Chengdf1690d2008-03-25 20:08:07 +00003653 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003654 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003655 case ISD::ADDC:
3656 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003657 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003658 case ISD::UDIV:
3659 case ISD::SDIV:
3660 case ISD::UREM:
3661 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003662 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003663 case ISD::FADD:
3664 case ISD::FSUB:
3665 case ISD::FMUL:
3666 case ISD::FDIV:
3667 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003668 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003669 return N2;
3670 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003671 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003672 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003673 case ISD::SRL:
3674 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003675 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003676 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003677 // For vectors, we can't easily build an all zero vector, just return
3678 // the LHS.
3679 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003680 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003681 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003682 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003683 // For vectors, we can't easily build an all one vector, just return
3684 // the LHS.
3685 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003686 case ISD::SRA:
3687 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003688 }
3689 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003690
Chris Lattner724f7ee2005-05-11 18:57:39 +00003691 // Memoize this node if possible.
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003692 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003693 SDVTList VTs = getVTList(VT);
Nick Lewycky37a17502015-05-13 23:41:47 +00003694 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003695 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003696 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003697 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003698 AddNodeIDNode(ID, Opcode, VTs, Ops);
Nick Lewycky37a17502015-05-13 23:41:47 +00003699 if (BinOpHasFlags)
3700 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003701 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00003702 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003703 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003704
Nick Lewycky37a17502015-05-13 23:41:47 +00003705 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003706
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003707 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003708 } else {
Nick Lewycky37a17502015-05-13 23:41:47 +00003709 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003710 }
3711
Chandler Carruth41b20e72014-07-22 04:07:55 +00003712 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003713 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003714}
3715
Andrew Trickef9de2a2013-05-25 02:42:55 +00003716SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003717 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003718 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003719 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003720 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003721 case ISD::FMA: {
3722 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3723 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3724 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3725 if (N1CFP && N2CFP && N3CFP) {
3726 APFloat V1 = N1CFP->getValueAPF();
3727 const APFloat &V2 = N2CFP->getValueAPF();
3728 const APFloat &V3 = N3CFP->getValueAPF();
3729 APFloat::opStatus s =
3730 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003731 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003732 return getConstantFP(V1, DL, VT);
Owen Anderson32baf992013-05-09 22:27:13 +00003733 }
3734 break;
3735 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003736 case ISD::CONCAT_VECTORS:
3737 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3738 // one big BUILD_VECTOR.
3739 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3740 N2.getOpcode() == ISD::BUILD_VECTOR &&
3741 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003742 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3743 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003744 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3745 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003746 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003747 }
3748 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003749 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003750 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003751 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003752 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003753 break;
3754 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003755 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003756 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003757 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003758 return N2; // select true, X, Y -> X
3759 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003760 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003761
3762 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003763 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003764 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003765 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003766 case ISD::INSERT_SUBVECTOR: {
3767 SDValue Index = N3;
3768 if (VT.isSimple() && N1.getValueType().isSimple()
3769 && N2.getValueType().isSimple()) {
3770 assert(VT.isVector() && N1.getValueType().isVector() &&
3771 N2.getValueType().isVector() &&
3772 "Insert subvector VTs must be a vectors");
3773 assert(VT == N1.getValueType() &&
3774 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003775 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003776 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003777 if (isa<ConstantSDNode>(Index.getNode())) {
3778 assert((N2.getValueType().getVectorNumElements() +
3779 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003780 <= VT.getVectorNumElements())
3781 && "Insert subvector overflow!");
3782 }
3783
3784 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003785 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003786 return N2;
3787 }
3788 break;
3789 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003790 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003791 // Fold bit_convert nodes from a type to themselves.
3792 if (N1.getValueType() == VT)
3793 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003794 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003795 }
3796
Chris Lattnerf9c19152005-08-25 19:12:10 +00003797 // Memoize node if it doesn't produce a flag.
3798 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003799 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003800 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003801 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003802 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003803 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003804 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00003805 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003806 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003807
Jack Carter170a5f22013-09-09 22:02:08 +00003808 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3809 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003810 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003811 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003812 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3813 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003814 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003815
Chandler Carruth41b20e72014-07-22 04:07:55 +00003816 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003817 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003818}
3819
Andrew Trickef9de2a2013-05-25 02:42:55 +00003820SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003821 SDValue N1, SDValue N2, SDValue N3,
3822 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003823 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003824 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003825}
3826
Andrew Trickef9de2a2013-05-25 02:42:55 +00003827SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003828 SDValue N1, SDValue N2, SDValue N3,
3829 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003830 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003831 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003832}
3833
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003834/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3835/// the incoming stack arguments to be loaded from the stack.
3836SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3837 SmallVector<SDValue, 8> ArgChains;
3838
3839 // Include the original chain at the beginning of the list. When this is
3840 // used by target LowerCall hooks, this helps legalize find the
3841 // CALLSEQ_BEGIN node.
3842 ArgChains.push_back(Chain);
3843
3844 // Add a chain value for each stack argument.
3845 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3846 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3847 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3848 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3849 if (FI->getIndex() < 0)
3850 ArgChains.push_back(SDValue(L, 1));
3851
3852 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003853 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003854}
3855
Dan Gohman544ab2c2008-04-12 04:36:06 +00003856/// getMemsetValue - Vectorized representation of the memset value
3857/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003858static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003859 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003860 assert(Value.getOpcode() != ISD::UNDEF);
3861
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003862 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003863 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003864 assert(C->getAPIntValue().getBitWidth() == 8);
3865 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003866 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003867 return DAG.getConstant(Val, dl, VT);
3868 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
3869 VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003870 }
Evan Chengef377ad2008-05-15 08:39:06 +00003871
Hal Finkel17b6d772015-03-31 20:35:26 +00003872 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
3873 EVT IntVT = VT.getScalarType();
3874 if (!IntVT.isInteger())
3875 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
3876
3877 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003878 if (NumBits > 8) {
3879 // Use a multiplication with 0x010101... to extend the input to the
3880 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003881 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Hal Finkel17b6d772015-03-31 20:35:26 +00003882 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003883 DAG.getConstant(Magic, dl, IntVT));
Hal Finkel17b6d772015-03-31 20:35:26 +00003884 }
3885
3886 if (VT != Value.getValueType() && !VT.isInteger())
3887 Value = DAG.getNode(ISD::BITCAST, dl, VT.getScalarType(), Value);
3888 if (VT != Value.getValueType()) {
3889 assert(VT.getVectorElementType() == Value.getValueType() &&
3890 "value type should be one vector element here");
3891 SmallVector<SDValue, 8> BVOps(VT.getVectorNumElements(), Value);
3892 Value = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, BVOps);
Evan Chengef377ad2008-05-15 08:39:06 +00003893 }
3894
3895 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003896}
3897
Dan Gohman544ab2c2008-04-12 04:36:06 +00003898/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3899/// used when a memcpy is turned into a memset when the source is a constant
3900/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003901static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003902 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003903 // Handle vector with all elements zero.
3904 if (Str.empty()) {
3905 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003906 return DAG.getConstant(0, dl, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003907 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003908 return DAG.getConstantFP(0.0, dl, VT);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003909 else if (VT.isVector()) {
3910 unsigned NumElts = VT.getVectorNumElements();
3911 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003912 return DAG.getNode(ISD::BITCAST, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003913 DAG.getConstant(0, dl,
3914 EVT::getVectorVT(*DAG.getContext(),
3915 EltVT, NumElts)));
Evan Cheng43cd9e32010-04-01 06:04:33 +00003916 } else
3917 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003918 }
3919
Duncan Sands13237ac2008-06-06 12:08:01 +00003920 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003921 unsigned NumVTBits = VT.getSizeInBits();
3922 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003923 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3924
Evan Chengc8444b12013-01-10 22:13:27 +00003925 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003926 if (TLI.isLittleEndian()) {
3927 for (unsigned i = 0; i != NumBytes; ++i)
3928 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3929 } else {
3930 for (unsigned i = 0; i != NumBytes; ++i)
3931 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003932 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003933
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003934 // If the "cost" of materializing the integer immediate is less than the cost
3935 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003936 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3937 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003938 return DAG.getConstant(Val, dl, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003939 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003940}
3941
Scott Michelcf0da6c2009-02-17 22:15:04 +00003942/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003943///
Andrew Tricke2431c62013-05-25 03:08:10 +00003944static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003945 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003946 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003947 return DAG.getNode(ISD::ADD, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003948 VT, Base, DAG.getConstant(Offset, dl, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003949}
3950
Evan Chengef377ad2008-05-15 08:39:06 +00003951/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3952///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003953static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003954 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003955 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003956 if (Src.getOpcode() == ISD::GlobalAddress)
3957 G = cast<GlobalAddressSDNode>(Src);
3958 else if (Src.getOpcode() == ISD::ADD &&
3959 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3960 Src.getOperand(1).getOpcode() == ISD::Constant) {
3961 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003962 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003963 }
3964 if (!G)
3965 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003966
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003967 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003968}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003969
Evan Cheng43cd9e32010-04-01 06:04:33 +00003970/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3971/// to replace the memset / memcpy. Return true if the number of memory ops
3972/// is below the threshold. It returns the types of the sequence of
3973/// memory ops to perform memset / memcpy by reference.
3974static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003975 unsigned Limit, uint64_t Size,
3976 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003977 bool IsMemset,
3978 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003979 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003980 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003981 SelectionDAG &DAG,
3982 const TargetLowering &TLI) {
3983 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3984 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003985 // If 'SrcAlign' is zero, that means the memory operation does not need to
3986 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3987 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3988 // is the specified alignment of the memory operation. If it is zero, that
3989 // means it's possible to change the alignment of the destination.
3990 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3991 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003992 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003993 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003994 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003995
Chris Lattner28dc6c12010-03-07 07:45:08 +00003996 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003997 unsigned AS = 0;
3998 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003999 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00004000 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00004001 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00004002 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00004003 case 0: VT = MVT::i64; break;
4004 case 4: VT = MVT::i32; break;
4005 case 2: VT = MVT::i16; break;
4006 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00004007 }
4008 }
4009
Owen Andersonc6daf8f2009-08-11 21:59:30 +00004010 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00004011 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00004012 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00004013 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00004014
Duncan Sands11dd4242008-06-08 20:54:56 +00004015 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00004016 VT = LVT;
4017 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004018
Dan Gohman544ab2c2008-04-12 04:36:06 +00004019 unsigned NumMemOps = 0;
4020 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00004021 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004022 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00004023 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00004024 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004025 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00004026
4027 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004028 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00004029 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00004030 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004031 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00004032 Found = true;
4033 else if (NewVT == MVT::i64 &&
4034 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004035 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00004036 // i64 is usually not legal on 32-bit targets, but f64 may be.
4037 NewVT = MVT::f64;
4038 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004039 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00004040 }
4041
Evan Cheng04e55182012-12-12 00:42:09 +00004042 if (!Found) {
4043 do {
4044 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
4045 if (NewVT == MVT::i8)
4046 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00004047 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00004048 }
4049 NewVTSize = NewVT.getSizeInBits() / 8;
4050
Evan Cheng79e2ca92012-12-10 23:21:26 +00004051 // If the new VT cannot cover all of the remaining bits, then consider
4052 // issuing a (or a pair of) unaligned and overlapping load / store.
4053 // FIXME: Only does this for 64-bit or more since we don't have proper
4054 // cost model for unaligned load / store.
4055 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00004056 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00004057 if (NumMemOps && AllowOverlap &&
4058 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00004059 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00004060 VTSize = Size;
4061 else {
4062 VT = NewVT;
4063 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00004064 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004065 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004066
Evan Chengb7d3d032012-12-12 20:43:23 +00004067 if (++NumMemOps > Limit)
4068 return false;
4069
Dan Gohman544ab2c2008-04-12 04:36:06 +00004070 MemOps.push_back(VT);
4071 Size -= VTSize;
4072 }
4073
4074 return true;
4075}
4076
Andrew Trickef9de2a2013-05-25 02:42:55 +00004077static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00004078 SDValue Chain, SDValue Dst,
4079 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004080 unsigned Align, bool isVol,
4081 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004082 MachinePointerInfo DstPtrInfo,
4083 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004084 // Turn a memcpy of undef to nop.
4085 if (Src.getOpcode() == ISD::UNDEF)
4086 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004087
Dan Gohman714663a2008-05-29 19:42:22 +00004088 // Expand memcpy to a series of load and store ops if the size operand falls
4089 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00004090 // TODO: In the AlwaysInline case, if the size is big then generate a loop
4091 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00004092 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004093 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004094 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004095 MachineFunction &MF = DAG.getMachineFunction();
4096 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004097 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004098 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4099 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4100 DstAlignCanChange = true;
4101 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4102 if (Align > SrcAlign)
4103 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004104 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004105 bool CopyFromStr = isMemSrcFromString(Src, Str);
4106 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004107 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00004108
Evan Cheng4c014c82010-04-01 18:19:11 +00004109 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004110 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00004111 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00004112 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004113 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004114
Evan Cheng43cd9e32010-04-01 06:04:33 +00004115 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004116 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004117 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00004118
4119 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00004120 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004121 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004122 if (!TRI->needsStackRealignment(MF))
4123 while (NewAlign > Align &&
4124 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4125 NewAlign /= 2;
4126
Evan Cheng43cd9e32010-04-01 06:04:33 +00004127 if (NewAlign > Align) {
4128 // Give the stack frame object a larger alignment if needed.
4129 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4130 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4131 Align = NewAlign;
4132 }
4133 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004134
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004135 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004136 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004137 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004138 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004139 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004140 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004141 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004142
Evan Cheng79e2ca92012-12-10 23:21:26 +00004143 if (VTSize > Size) {
4144 // Issuing an unaligned load / store pair that overlaps with the previous
4145 // pair. Adjust the offset accordingly.
4146 assert(i == NumMemOps-1 && i != 0);
4147 SrcOff -= VTSize - Size;
4148 DstOff -= VTSize - Size;
4149 }
4150
Evan Cheng43cd9e32010-04-01 06:04:33 +00004151 if (CopyFromStr &&
4152 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004153 // It's unlikely a store of a vector immediate can be done in a single
4154 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004155 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004156 // FIXME: Handle other cases where store of vector immediate is done in
4157 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004158 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004159 if (Value.getNode())
4160 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004161 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004162 DstPtrInfo.getWithOffset(DstOff), isVol,
4163 false, Align);
4164 }
4165
4166 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004167 // The type might not be legal for the target. This should only happen
4168 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004169 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4170 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004171 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004172 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004173 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004174 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004175 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004176 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004177 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004178 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004179 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004180 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4181 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004182 }
4183 OutChains.push_back(Store);
4184 SrcOff += VTSize;
4185 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004186 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004187 }
4188
Craig Topper48d114b2014-04-26 18:35:24 +00004189 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004190}
4191
Andrew Trickef9de2a2013-05-25 02:42:55 +00004192static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004193 SDValue Chain, SDValue Dst,
4194 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004195 unsigned Align, bool isVol,
4196 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004197 MachinePointerInfo DstPtrInfo,
4198 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004199 // Turn a memmove of undef to nop.
4200 if (Src.getOpcode() == ISD::UNDEF)
4201 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004202
4203 // Expand memmove to a series of load and store ops if the size operand falls
4204 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004205 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004206 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004207 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004208 MachineFunction &MF = DAG.getMachineFunction();
4209 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004210 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004211 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4212 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4213 DstAlignCanChange = true;
4214 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4215 if (Align > SrcAlign)
4216 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004217 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004218
Evan Cheng4c014c82010-04-01 18:19:11 +00004219 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004220 (DstAlignCanChange ? 0 : Align), SrcAlign,
4221 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004222 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004223
Evan Cheng43cd9e32010-04-01 06:04:33 +00004224 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004225 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004226 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004227 if (NewAlign > Align) {
4228 // Give the stack frame object a larger alignment if needed.
4229 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4230 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4231 Align = NewAlign;
4232 }
4233 }
Dan Gohman714663a2008-05-29 19:42:22 +00004234
Evan Cheng43cd9e32010-04-01 06:04:33 +00004235 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004236 SmallVector<SDValue, 8> LoadValues;
4237 SmallVector<SDValue, 8> LoadChains;
4238 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004239 unsigned NumMemOps = MemOps.size();
4240 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004241 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004242 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004243 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004244
Dale Johannesenabf66b82009-02-03 22:26:09 +00004245 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004246 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004247 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004248 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004249 LoadValues.push_back(Value);
4250 LoadChains.push_back(Value.getValue(1));
4251 SrcOff += VTSize;
4252 }
Craig Topper48d114b2014-04-26 18:35:24 +00004253 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004254 OutChains.clear();
4255 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004256 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004257 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004258 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004259
Dale Johannesenabf66b82009-02-03 22:26:09 +00004260 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004261 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004262 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004263 OutChains.push_back(Store);
4264 DstOff += VTSize;
4265 }
4266
Craig Topper48d114b2014-04-26 18:35:24 +00004267 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004268}
4269
Serge Pavlov8ec39992013-09-17 16:24:42 +00004270/// \brief Lower the call to 'memset' intrinsic function into a series of store
4271/// operations.
4272///
4273/// \param DAG Selection DAG where lowered code is placed.
4274/// \param dl Link to corresponding IR location.
4275/// \param Chain Control flow dependency.
4276/// \param Dst Pointer to destination memory location.
4277/// \param Src Value of byte to write into the memory.
4278/// \param Size Number of bytes to write.
4279/// \param Align Alignment of the destination in bytes.
4280/// \param isVol True if destination is volatile.
4281/// \param DstPtrInfo IR information on the memory pointer.
4282/// \returns New head in the control flow, if lowering was successful, empty
4283/// SDValue otherwise.
4284///
4285/// The function tries to replace 'llvm.memset' intrinsic with several store
4286/// operations and value calculation code. This is usually profitable for small
4287/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004288static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004289 SDValue Chain, SDValue Dst,
4290 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004291 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004292 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004293 // Turn a memset of undef to nop.
4294 if (Src.getOpcode() == ISD::UNDEF)
4295 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004296
4297 // Expand memset to a series of load/store ops if the size operand
4298 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004299 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004300 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004301 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004302 MachineFunction &MF = DAG.getMachineFunction();
4303 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004304 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004305 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4306 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4307 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004308 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004309 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004310 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004311 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004312 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004313 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004314
Evan Cheng43cd9e32010-04-01 06:04:33 +00004315 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004316 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004317 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004318 if (NewAlign > Align) {
4319 // Give the stack frame object a larger alignment if needed.
4320 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4321 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4322 Align = NewAlign;
4323 }
4324 }
4325
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004326 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004327 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004328 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004329
4330 // Find the largest store and generate the bit pattern for it.
4331 EVT LargestVT = MemOps[0];
4332 for (unsigned i = 1; i < NumMemOps; i++)
4333 if (MemOps[i].bitsGT(LargestVT))
4334 LargestVT = MemOps[i];
4335 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4336
Dan Gohman544ab2c2008-04-12 04:36:06 +00004337 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004338 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004339 unsigned VTSize = VT.getSizeInBits() / 8;
4340 if (VTSize > Size) {
4341 // Issuing an unaligned load / store pair that overlaps with the previous
4342 // pair. Adjust the offset accordingly.
4343 assert(i == NumMemOps-1 && i != 0);
4344 DstOff -= VTSize - Size;
4345 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004346
4347 // If this store is smaller than the largest store see whether we can get
4348 // the smaller value for free with a truncate.
4349 SDValue Value = MemSetValue;
4350 if (VT.bitsLT(LargestVT)) {
4351 if (!LargestVT.isVector() && !VT.isVector() &&
4352 TLI.isTruncateFree(LargestVT, VT))
4353 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4354 else
4355 Value = getMemsetValue(Src, VT, DAG, dl);
4356 }
4357 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004358 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004359 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004360 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004361 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004362 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004363 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004364 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004365 }
4366
Craig Topper48d114b2014-04-26 18:35:24 +00004367 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004368}
4369
Andrew Trickef9de2a2013-05-25 02:42:55 +00004370SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004371 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004372 unsigned Align, bool isVol, bool AlwaysInline,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004373 bool isTailCall, MachinePointerInfo DstPtrInfo,
Chris Lattner2510de22010-09-21 05:40:29 +00004374 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004375 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004376
4377 // Check to see if we should lower the memcpy to loads and stores first.
4378 // For cases within the target-specified limits, this is the best choice.
4379 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4380 if (ConstantSize) {
4381 // Memcpy with size zero? Just return the original chain.
4382 if (ConstantSize->isNullValue())
4383 return Chain;
4384
Evan Cheng43cd9e32010-04-01 06:04:33 +00004385 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4386 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004387 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004388 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004389 return Result;
4390 }
4391
4392 // Then check to see if we should lower the memcpy with target-specific
4393 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004394 if (TSI) {
4395 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4396 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4397 DstPtrInfo, SrcPtrInfo);
4398 if (Result.getNode())
4399 return Result;
4400 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004401
4402 // If we really need inline code and the target declined to provide it,
4403 // use a (potentially long) sequence of loads and stores.
4404 if (AlwaysInline) {
4405 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004406 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004407 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004408 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004409 }
4410
Dan Gohmanf38547c2010-04-05 20:24:08 +00004411 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4412 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4413 // respect volatile, so they may do things like read or write memory
4414 // beyond the given memory regions. But fixing this isn't easy, and most
4415 // people don't care.
4416
Dan Gohman544ab2c2008-04-12 04:36:06 +00004417 // Emit a library call.
4418 TargetLowering::ArgListTy Args;
4419 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004420 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004421 Entry.Node = Dst; Args.push_back(Entry);
4422 Entry.Node = Src; Args.push_back(Entry);
4423 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004424 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004425 TargetLowering::CallLoweringInfo CLI(*this);
4426 CLI.setDebugLoc(dl).setChain(Chain)
4427 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4428 Type::getVoidTy(*getContext()),
4429 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004430 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004431 .setDiscardResult()
4432 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004433
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004434 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004435 return CallResult.second;
4436}
4437
Andrew Trickef9de2a2013-05-25 02:42:55 +00004438SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004439 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004440 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004441 MachinePointerInfo DstPtrInfo,
4442 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004443 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004444
Dan Gohman714663a2008-05-29 19:42:22 +00004445 // Check to see if we should lower the memmove to loads and stores first.
4446 // For cases within the target-specified limits, this is the best choice.
4447 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4448 if (ConstantSize) {
4449 // Memmove with size zero? Just return the original chain.
4450 if (ConstantSize->isNullValue())
4451 return Chain;
4452
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004453 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004454 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004455 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004456 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004457 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004458 return Result;
4459 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004460
4461 // Then check to see if we should lower the memmove with target-specific
4462 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004463 if (TSI) {
4464 SDValue Result = TSI->EmitTargetCodeForMemmove(
4465 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4466 if (Result.getNode())
4467 return Result;
4468 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004469
Mon P Wangbf862242010-04-06 08:27:51 +00004470 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4471 // not be safe. See memcpy above for more details.
4472
Dan Gohman544ab2c2008-04-12 04:36:06 +00004473 // Emit a library call.
4474 TargetLowering::ArgListTy Args;
4475 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004476 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004477 Entry.Node = Dst; Args.push_back(Entry);
4478 Entry.Node = Src; Args.push_back(Entry);
4479 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004480 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004481 TargetLowering::CallLoweringInfo CLI(*this);
4482 CLI.setDebugLoc(dl).setChain(Chain)
4483 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4484 Type::getVoidTy(*getContext()),
4485 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004486 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004487 .setDiscardResult()
4488 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004489
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004490 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004491 return CallResult.second;
4492}
4493
Andrew Trickef9de2a2013-05-25 02:42:55 +00004494SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004495 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004496 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004497 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004498 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004499
4500 // Check to see if we should lower the memset to stores first.
4501 // For cases within the target-specified limits, this is the best choice.
4502 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4503 if (ConstantSize) {
4504 // Memset with size zero? Just return the original chain.
4505 if (ConstantSize->isNullValue())
4506 return Chain;
4507
Mon P Wangc576ee92010-04-04 03:10:48 +00004508 SDValue Result =
4509 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004510 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004511
Gabor Greiff304a7a2008-08-28 21:40:38 +00004512 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004513 return Result;
4514 }
4515
4516 // Then check to see if we should lower the memset with target-specific
4517 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004518 if (TSI) {
4519 SDValue Result = TSI->EmitTargetCodeForMemset(
4520 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4521 if (Result.getNode())
4522 return Result;
4523 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004524
Wesley Peck527da1b2010-11-23 03:31:01 +00004525 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004526 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004527 TargetLowering::ArgListTy Args;
4528 TargetLowering::ArgListEntry Entry;
4529 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4530 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004531 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004532 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004533 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004534 Entry.Node = Size;
4535 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004536 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004537
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004538 // FIXME: pass in SDLoc
4539 TargetLowering::CallLoweringInfo CLI(*this);
4540 CLI.setDebugLoc(dl).setChain(Chain)
4541 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4542 Type::getVoidTy(*getContext()),
4543 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004544 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004545 .setDiscardResult()
4546 .setTailCall(isTailCall);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004547
4548 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004549 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004550}
4551
Andrew Trickef9de2a2013-05-25 02:42:55 +00004552SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004553 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004554 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004555 AtomicOrdering SuccessOrdering,
4556 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004557 SynchronizationScope SynchScope) {
4558 FoldingSetNodeID ID;
4559 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004560 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004561 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004562 void* IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00004563 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004564 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4565 return SDValue(E, 0);
4566 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004567
4568 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4569 // SDNode doesn't have access to it. This memory will be "leaked" when
4570 // the node is deallocated, but recovered when the allocator is released.
4571 // If the number of operands is less than 5 we use AtomicSDNode's internal
4572 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004573 unsigned NumOps = Ops.size();
4574 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4575 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004576
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004577 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4578 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004579 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004580 SuccessOrdering, FailureOrdering,
4581 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004582 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004583 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004584 return SDValue(N, 0);
4585}
4586
4587SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004588 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004589 MachineMemOperand *MMO,
4590 AtomicOrdering Ordering,
4591 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004592 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004593 Ordering, SynchScope);
4594}
4595
Tim Northover420a2162014-06-13 14:24:07 +00004596SDValue SelectionDAG::getAtomicCmpSwap(
4597 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4598 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4599 unsigned Alignment, AtomicOrdering SuccessOrdering,
4600 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4601 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4602 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4603 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4604
Dan Gohman48b185d2009-09-25 20:36:54 +00004605 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4606 Alignment = getEVTAlignment(MemVT);
4607
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004608 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004609
Eli Friedmane978d2f2011-09-07 02:23:42 +00004610 // FIXME: Volatile isn't really correct; we should keep track of atomic
4611 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004612 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004613 Flags |= MachineMemOperand::MOLoad;
4614 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004615
4616 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004617 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004618
Tim Northover420a2162014-06-13 14:24:07 +00004619 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4620 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004621}
4622
Tim Northover420a2162014-06-13 14:24:07 +00004623SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4624 SDVTList VTs, SDValue Chain, SDValue Ptr,
4625 SDValue Cmp, SDValue Swp,
4626 MachineMemOperand *MMO,
4627 AtomicOrdering SuccessOrdering,
4628 AtomicOrdering FailureOrdering,
4629 SynchronizationScope SynchScope) {
4630 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4631 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004632 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4633
Dale Johannesen839acbb2009-01-29 00:47:48 +00004634 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004635 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4636 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004637}
4638
Andrew Trickef9de2a2013-05-25 02:42:55 +00004639SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004640 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004641 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004642 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004643 unsigned Alignment,
4644 AtomicOrdering Ordering,
4645 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004646 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4647 Alignment = getEVTAlignment(MemVT);
4648
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004649 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004650 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004651 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004652 // For now, atomics are considered to be volatile always, and they are
4653 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004654 // FIXME: Volatile isn't really correct; we should keep track of atomic
4655 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004656 unsigned Flags = MachineMemOperand::MOVolatile;
4657 if (Opcode != ISD::ATOMIC_STORE)
4658 Flags |= MachineMemOperand::MOLoad;
4659 if (Opcode != ISD::ATOMIC_LOAD)
4660 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004661
4662 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004663 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004664 MemVT.getStoreSize(), Alignment);
4665
Eli Friedmanadec5872011-07-29 03:05:32 +00004666 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4667 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004668}
4669
Andrew Trickef9de2a2013-05-25 02:42:55 +00004670SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004671 SDValue Chain,
4672 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004673 MachineMemOperand *MMO,
4674 AtomicOrdering Ordering,
4675 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004676 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4677 Opcode == ISD::ATOMIC_LOAD_SUB ||
4678 Opcode == ISD::ATOMIC_LOAD_AND ||
4679 Opcode == ISD::ATOMIC_LOAD_OR ||
4680 Opcode == ISD::ATOMIC_LOAD_XOR ||
4681 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004682 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004683 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004684 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004685 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004686 Opcode == ISD::ATOMIC_SWAP ||
4687 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004688 "Invalid Atomic Op");
4689
Owen Anderson53aa7a92009-08-10 22:56:29 +00004690 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004691
Eli Friedman342e8df2011-08-24 20:50:09 +00004692 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4693 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004694 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004695 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004696}
4697
Andrew Trickef9de2a2013-05-25 02:42:55 +00004698SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004699 EVT VT, SDValue Chain,
4700 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004701 MachineMemOperand *MMO,
4702 AtomicOrdering Ordering,
4703 SynchronizationScope SynchScope) {
4704 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4705
4706 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004707 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004708 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004709}
4710
Duncan Sands739a0542008-07-02 17:40:58 +00004711/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004712SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4713 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004714 return Ops[0];
4715
Owen Anderson53aa7a92009-08-10 22:56:29 +00004716 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004717 VTs.reserve(Ops.size());
4718 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004719 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004720 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004721}
4722
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004723SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004724SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004725 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004726 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004727 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004728 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004729 if (Align == 0) // Ensure that codegen never sees alignment 0
4730 Align = getEVTAlignment(MemVT);
4731
4732 MachineFunction &MF = getMachineFunction();
4733 unsigned Flags = 0;
4734 if (WriteMem)
4735 Flags |= MachineMemOperand::MOStore;
4736 if (ReadMem)
4737 Flags |= MachineMemOperand::MOLoad;
4738 if (Vol)
4739 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004740 if (!Size)
4741 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004742 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004743 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004744
Craig Topper206fcd42014-04-26 19:29:41 +00004745 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004746}
4747
4748SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004749SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004750 ArrayRef<SDValue> Ops, EVT MemVT,
4751 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004752 assert((Opcode == ISD::INTRINSIC_VOID ||
4753 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004754 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004755 Opcode == ISD::LIFETIME_START ||
4756 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004757 (Opcode <= INT_MAX &&
4758 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4759 "Opcode is not a memory-accessing opcode!");
4760
Dale Johannesen839acbb2009-01-29 00:47:48 +00004761 // Memoize the node unless it returns a flag.
4762 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004763 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004764 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004765 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004766 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004767 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00004768 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004769 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004770 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004771 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004772
Jack Carter170a5f22013-09-09 22:02:08 +00004773 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4774 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004775 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004776 CSEMap.InsertNode(N, IP);
4777 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004778 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4779 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004780 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004781 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004782 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004783 return SDValue(N, 0);
4784}
4785
Chris Lattnerea952f02010-09-21 17:24:05 +00004786/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4787/// MachinePointerInfo record from it. This is particularly useful because the
4788/// code generator has many cases where it doesn't bother passing in a
4789/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4790static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4791 // If this is FI+Offset, we can model it.
4792 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4793 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4794
4795 // If this is (FI+Offset1)+Offset2, we can model it.
4796 if (Ptr.getOpcode() != ISD::ADD ||
4797 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4798 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4799 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004800
Chris Lattnerea952f02010-09-21 17:24:05 +00004801 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4802 return MachinePointerInfo::getFixedStack(FI, Offset+
4803 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4804}
4805
4806/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4807/// MachinePointerInfo record from it. This is particularly useful because the
4808/// code generator has many cases where it doesn't bother passing in a
4809/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4810static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4811 // If the 'Offset' value isn't a constant, we can't handle this.
4812 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4813 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4814 if (OffsetOp.getOpcode() == ISD::UNDEF)
4815 return InferPointerInfo(Ptr);
4816 return MachinePointerInfo();
4817}
Wesley Peck527da1b2010-11-23 03:31:01 +00004818
Chris Lattnerea952f02010-09-21 17:24:05 +00004819
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004820SDValue
4821SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004822 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004823 SDValue Ptr, SDValue Offset,
4824 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004825 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004826 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004827 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004828 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004829 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004830 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4831 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004832
Dan Gohman48b185d2009-09-25 20:36:54 +00004833 unsigned Flags = MachineMemOperand::MOLoad;
4834 if (isVolatile)
4835 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004836 if (isNonTemporal)
4837 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004838 if (isInvariant)
4839 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004840
Chris Lattnerea952f02010-09-21 17:24:05 +00004841 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4842 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004843 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004844 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004845
Chris Lattnerea952f02010-09-21 17:24:05 +00004846 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004847 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004848 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004849 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004850 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004851}
4852
4853SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004854SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004855 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004856 SDValue Ptr, SDValue Offset, EVT MemVT,
4857 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004858 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004859 ExtType = ISD::NON_EXTLOAD;
4860 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004861 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004862 } else {
4863 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004864 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4865 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004866 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004867 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004868 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004869 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004870 assert((!VT.isVector() ||
4871 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004872 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004873 }
4874
4875 bool Indexed = AM != ISD::UNINDEXED;
4876 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4877 "Unindexed load with an offset!");
4878
4879 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004880 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004881 SDValue Ops[] = { Chain, Ptr, Offset };
4882 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004883 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004884 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004885 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004886 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004887 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004888 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004889 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00004890 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004891 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004892 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004893 }
Jack Carter170a5f22013-09-09 22:02:08 +00004894 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4895 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004896 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004897 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004898 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004899 return SDValue(N, 0);
4900}
4901
Andrew Trickef9de2a2013-05-25 02:42:55 +00004902SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004903 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004904 MachinePointerInfo PtrInfo,
4905 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004906 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004907 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004908 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004909 SDValue Undef = getUNDEF(Ptr.getValueType());
4910 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004911 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004912 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004913}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004914
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004915SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4916 SDValue Chain, SDValue Ptr,
4917 MachineMemOperand *MMO) {
4918 SDValue Undef = getUNDEF(Ptr.getValueType());
4919 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4920 VT, MMO);
4921}
4922
Andrew Trickef9de2a2013-05-25 02:42:55 +00004923SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004924 SDValue Chain, SDValue Ptr,
4925 MachinePointerInfo PtrInfo, EVT MemVT,
4926 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004927 bool isInvariant, unsigned Alignment,
4928 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004929 SDValue Undef = getUNDEF(Ptr.getValueType());
4930 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004931 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4932 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004933}
4934
4935
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004936SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4937 SDValue Chain, SDValue Ptr, EVT MemVT,
4938 MachineMemOperand *MMO) {
4939 SDValue Undef = getUNDEF(Ptr.getValueType());
4940 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4941 MemVT, MMO);
4942}
4943
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004944SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004945SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004946 SDValue Offset, ISD::MemIndexedMode AM) {
4947 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4948 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4949 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004950 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004951 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004952 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004953 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004954}
4955
Andrew Trickef9de2a2013-05-25 02:42:55 +00004956SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004957 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004958 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004959 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004960 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004961 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004962 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004963 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004964
Dan Gohman48b185d2009-09-25 20:36:54 +00004965 unsigned Flags = MachineMemOperand::MOStore;
4966 if (isVolatile)
4967 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004968 if (isNonTemporal)
4969 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004970
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004971 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004972 PtrInfo = InferPointerInfo(Ptr);
4973
4974 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004975 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004976 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004977 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004978 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004979
4980 return getStore(Chain, dl, Val, Ptr, MMO);
4981}
4982
Andrew Trickef9de2a2013-05-25 02:42:55 +00004983SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004984 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004985 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004986 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004987 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004988 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004989 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004990 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4991 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004992 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004993 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004994 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004995 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004996 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004997 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00004998 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004999 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005000 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005001 }
Jack Carter170a5f22013-09-09 22:02:08 +00005002 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5003 dl.getDebugLoc(), VTs,
5004 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005005 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005006 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005007 return SDValue(N, 0);
5008}
5009
Andrew Trickef9de2a2013-05-25 02:42:55 +00005010SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00005011 SDValue Ptr, MachinePointerInfo PtrInfo,
5012 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00005013 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00005014 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00005015 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005016 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00005017 if (Alignment == 0) // Ensure that codegen never sees alignment 0
5018 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00005019
Dan Gohman48b185d2009-09-25 20:36:54 +00005020 unsigned Flags = MachineMemOperand::MOStore;
5021 if (isVolatile)
5022 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00005023 if (isNonTemporal)
5024 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00005025
Nick Lewyckyaad475b2014-04-15 07:22:52 +00005026 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00005027 PtrInfo = InferPointerInfo(Ptr);
5028
5029 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00005030 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00005031 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00005032 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00005033
5034 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
5035}
5036
Andrew Trickef9de2a2013-05-25 02:42:55 +00005037SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00005038 SDValue Ptr, EVT SVT,
5039 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005040 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00005041
Michael Ilsemand5f91512012-09-10 16:56:31 +00005042 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005043 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005044 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00005045 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005046
Dan Gohmancecad352009-12-14 23:40:38 +00005047 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
5048 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005049 assert(VT.isInteger() == SVT.isInteger() &&
5050 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00005051 assert(VT.isVector() == SVT.isVector() &&
5052 "Cannot use trunc store to convert to or from a vector!");
5053 assert((!VT.isVector() ||
5054 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
5055 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005056
Owen Anderson9f944592009-08-11 20:47:22 +00005057 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00005058 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005059 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5060 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005061 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005062 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00005063 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005064 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00005065 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005066 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005067 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005068 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005069 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005070 }
Jack Carter170a5f22013-09-09 22:02:08 +00005071 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5072 dl.getDebugLoc(), VTs,
5073 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005074 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005075 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005076 return SDValue(N, 0);
5077}
5078
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005079SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005080SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00005081 SDValue Offset, ISD::MemIndexedMode AM) {
5082 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5083 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
5084 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00005085 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005086 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5087 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005088 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005089 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00005090 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00005091 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005092 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005093 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00005094 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005095
Jack Carter170a5f22013-09-09 22:02:08 +00005096 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5097 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00005098 ST->isTruncatingStore(),
5099 ST->getMemoryVT(),
5100 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005101 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005102 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005103 return SDValue(N, 0);
5104}
5105
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005106SDValue
5107SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005108 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5109 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005110
5111 SDVTList VTs = getVTList(VT, MVT::Other);
5112 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5113 FoldingSetNodeID ID;
5114 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5115 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005116 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005117 MMO->isVolatile(),
5118 MMO->isNonTemporal(),
5119 MMO->isInvariant()));
5120 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5121 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005122 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005123 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5124 return SDValue(E, 0);
5125 }
5126 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5127 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005128 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005129 CSEMap.InsertNode(N, IP);
5130 InsertNode(N);
5131 return SDValue(N, 0);
5132}
5133
5134SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005135 SDValue Ptr, SDValue Mask, EVT MemVT,
5136 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005137 assert(Chain.getValueType() == MVT::Other &&
5138 "Invalid chain type");
5139 EVT VT = Val.getValueType();
5140 SDVTList VTs = getVTList(MVT::Other);
5141 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5142 FoldingSetNodeID ID;
5143 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5144 ID.AddInteger(VT.getRawBits());
5145 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5146 MMO->isNonTemporal(), MMO->isInvariant()));
5147 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5148 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005149 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005150 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5151 return SDValue(E, 0);
5152 }
5153 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5154 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005155 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005156 CSEMap.InsertNode(N, IP);
5157 InsertNode(N);
5158 return SDValue(N, 0);
5159}
5160
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005161SDValue
5162SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl,
5163 ArrayRef<SDValue> Ops,
5164 MachineMemOperand *MMO) {
5165
5166 FoldingSetNodeID ID;
5167 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5168 ID.AddInteger(VT.getRawBits());
5169 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5170 MMO->isVolatile(),
5171 MMO->isNonTemporal(),
5172 MMO->isInvariant()));
5173 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5174 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005175 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005176 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5177 return SDValue(E, 0);
5178 }
5179 MaskedGatherSDNode *N =
5180 new (NodeAllocator) MaskedGatherSDNode(dl.getIROrder(), dl.getDebugLoc(),
5181 Ops, VTs, VT, MMO);
5182 CSEMap.InsertNode(N, IP);
5183 InsertNode(N);
5184 return SDValue(N, 0);
5185}
5186
5187SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl,
5188 ArrayRef<SDValue> Ops,
5189 MachineMemOperand *MMO) {
5190 FoldingSetNodeID ID;
5191 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5192 ID.AddInteger(VT.getRawBits());
5193 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5194 MMO->isNonTemporal(),
5195 MMO->isInvariant()));
5196 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5197 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005198 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005199 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5200 return SDValue(E, 0);
5201 }
5202 SDNode *N =
5203 new (NodeAllocator) MaskedScatterSDNode(dl.getIROrder(), dl.getDebugLoc(),
5204 Ops, VTs, VT, MMO);
5205 CSEMap.InsertNode(N, IP);
5206 InsertNode(N);
5207 return SDValue(N, 0);
5208}
5209
Andrew Trickef9de2a2013-05-25 02:42:55 +00005210SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005211 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005212 SDValue SV,
5213 unsigned Align) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005214 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005215 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005216}
5217
Andrew Trickef9de2a2013-05-25 02:42:55 +00005218SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005219 ArrayRef<SDUse> Ops) {
5220 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005221 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005222 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005223 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5224 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005225 default: break;
5226 }
5227
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005228 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005229 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005230 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005231 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005232}
5233
Andrew Trickef9de2a2013-05-25 02:42:55 +00005234SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005235 ArrayRef<SDValue> Ops) {
5236 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005237 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005238 case 0: return getNode(Opcode, DL, VT);
5239 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5240 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5241 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005242 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005243 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005244
Chris Lattnerb0713c72005-04-09 03:27:28 +00005245 switch (Opcode) {
5246 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005247 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005248 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005249 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5250 "LHS and RHS of condition must have same type!");
5251 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5252 "True and False arms of SelectCC must have same type!");
5253 assert(Ops[2].getValueType() == VT &&
5254 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005255 break;
5256 }
5257 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005258 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005259 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5260 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005261 break;
5262 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005263 }
5264
Chris Lattner566307f2005-05-14 07:42:29 +00005265 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005266 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005267 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005268
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005269 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005270 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005271 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005272 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005273
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005274 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005275 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005276
Jack Carter170a5f22013-09-09 22:02:08 +00005277 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005278 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005279 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005280 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005281 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005282 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005283 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005284
Chandler Carruth41b20e72014-07-22 04:07:55 +00005285 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005286 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005287}
5288
Andrew Trickef9de2a2013-05-25 02:42:55 +00005289SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005290 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5291 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005292}
5293
Andrew Trickef9de2a2013-05-25 02:42:55 +00005294SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005295 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005296 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005297 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005298
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005299#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005300 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005301 // FIXME: figure out how to safely handle things like
5302 // int foo(int x) { return 1 << (x & 255); }
5303 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005304 case ISD::SRA_PARTS:
5305 case ISD::SRL_PARTS:
5306 case ISD::SHL_PARTS:
5307 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005308 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005309 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005310 else if (N3.getOpcode() == ISD::AND)
5311 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5312 // If the and is only masking out bits that cannot effect the shift,
5313 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005314 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005315 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005316 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005317 }
5318 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005319 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005320#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005321
Chris Lattnerf9c19152005-08-25 19:12:10 +00005322 // Memoize the node unless it returns a flag.
5323 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005324 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005325 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005326 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005327 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005328 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005329 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005330 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005331
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005332 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005333 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5334 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005335 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005336 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5337 DL.getDebugLoc(), VTList, Ops[0],
5338 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005339 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005340 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5341 DL.getDebugLoc(), VTList, Ops[0],
5342 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005343 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005344 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005345 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005346 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005347 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005348 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005349 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005350 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5351 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005352 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005353 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5354 DL.getDebugLoc(), VTList, Ops[0],
5355 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005356 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005357 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5358 DL.getDebugLoc(), VTList, Ops[0],
5359 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005360 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005361 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005362 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005363 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005364 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005365 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005366 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005367}
5368
Andrew Trickef9de2a2013-05-25 02:42:55 +00005369SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005370 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005371}
5372
Andrew Trickef9de2a2013-05-25 02:42:55 +00005373SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005374 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005375 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005376 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005377}
5378
Andrew Trickef9de2a2013-05-25 02:42:55 +00005379SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005380 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005381 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005382 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005383}
5384
Andrew Trickef9de2a2013-05-25 02:42:55 +00005385SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005386 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005387 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005388 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005389}
5390
Andrew Trickef9de2a2013-05-25 02:42:55 +00005391SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005392 SDValue N1, SDValue N2, SDValue N3,
5393 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005394 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005395 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005396}
5397
Andrew Trickef9de2a2013-05-25 02:42:55 +00005398SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005399 SDValue N1, SDValue N2, SDValue N3,
5400 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005401 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005402 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005403}
5404
Owen Anderson53aa7a92009-08-10 22:56:29 +00005405SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005406 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005407}
5408
Owen Anderson53aa7a92009-08-10 22:56:29 +00005409SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005410 FoldingSetNodeID ID;
5411 ID.AddInteger(2U);
5412 ID.AddInteger(VT1.getRawBits());
5413 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005414
Craig Topperc0196b12014-04-14 00:51:57 +00005415 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005416 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005417 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005418 EVT *Array = Allocator.Allocate<EVT>(2);
5419 Array[0] = VT1;
5420 Array[1] = VT2;
5421 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5422 VTListMap.InsertNode(Result, IP);
5423 }
5424 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005425}
Dan Gohman17059682008-07-17 19:10:17 +00005426
Owen Anderson53aa7a92009-08-10 22:56:29 +00005427SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005428 FoldingSetNodeID ID;
5429 ID.AddInteger(3U);
5430 ID.AddInteger(VT1.getRawBits());
5431 ID.AddInteger(VT2.getRawBits());
5432 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005433
Craig Topperc0196b12014-04-14 00:51:57 +00005434 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005435 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005436 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005437 EVT *Array = Allocator.Allocate<EVT>(3);
5438 Array[0] = VT1;
5439 Array[1] = VT2;
5440 Array[2] = VT3;
5441 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5442 VTListMap.InsertNode(Result, IP);
5443 }
5444 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005445}
5446
Owen Anderson53aa7a92009-08-10 22:56:29 +00005447SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005448 FoldingSetNodeID ID;
5449 ID.AddInteger(4U);
5450 ID.AddInteger(VT1.getRawBits());
5451 ID.AddInteger(VT2.getRawBits());
5452 ID.AddInteger(VT3.getRawBits());
5453 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005454
Craig Topperc0196b12014-04-14 00:51:57 +00005455 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005456 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005457 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005458 EVT *Array = Allocator.Allocate<EVT>(4);
5459 Array[0] = VT1;
5460 Array[1] = VT2;
5461 Array[2] = VT3;
5462 Array[3] = VT4;
5463 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5464 VTListMap.InsertNode(Result, IP);
5465 }
5466 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005467}
5468
Craig Topperabb4ac72014-04-16 06:10:51 +00005469SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5470 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005471 FoldingSetNodeID ID;
5472 ID.AddInteger(NumVTs);
5473 for (unsigned index = 0; index < NumVTs; index++) {
5474 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005475 }
5476
Craig Topperc0196b12014-04-14 00:51:57 +00005477 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005478 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005479 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005480 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005481 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005482 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5483 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005484 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005485 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005486}
5487
5488
Chris Lattnerf34156e2006-01-28 09:32:45 +00005489/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5490/// specified operands. If the resultant node already exists in the DAG,
5491/// this does not modify the specified node, instead it returns the node that
5492/// already exists. If the resultant node does not exist in the DAG, the
5493/// input node is returned. As a degenerate case, if you specify the same
5494/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005495SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005496 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005497
Chris Lattnerf34156e2006-01-28 09:32:45 +00005498 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005499 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005500
Chris Lattnerf34156e2006-01-28 09:32:45 +00005501 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005502 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005503 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005504 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005505
Dan Gohmanebeccb42008-07-21 22:38:59 +00005506 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005507 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005508 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005509 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005510
Chris Lattnerf34156e2006-01-28 09:32:45 +00005511 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005512 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005513
Chris Lattnerf34156e2006-01-28 09:32:45 +00005514 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005515 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005516 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005517}
5518
Dan Gohman92c11ac2010-06-18 15:30:29 +00005519SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005520 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005521
Chris Lattnerf34156e2006-01-28 09:32:45 +00005522 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005523 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005524 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005525
Chris Lattnerf34156e2006-01-28 09:32:45 +00005526 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005527 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005528 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005529 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005530
Dan Gohmanebeccb42008-07-21 22:38:59 +00005531 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005532 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005533 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005534 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005535
Chris Lattnerf34156e2006-01-28 09:32:45 +00005536 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005537 if (N->OperandList[0] != Op1)
5538 N->OperandList[0].set(Op1);
5539 if (N->OperandList[1] != Op2)
5540 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005541
Chris Lattnerf34156e2006-01-28 09:32:45 +00005542 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005543 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005544 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005545}
5546
Dan Gohman92c11ac2010-06-18 15:30:29 +00005547SDNode *SelectionDAG::
5548UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005549 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005550 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005551}
5552
Dan Gohman92c11ac2010-06-18 15:30:29 +00005553SDNode *SelectionDAG::
5554UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005555 SDValue Op3, SDValue Op4) {
5556 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005557 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005558}
5559
Dan Gohman92c11ac2010-06-18 15:30:29 +00005560SDNode *SelectionDAG::
5561UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005562 SDValue Op3, SDValue Op4, SDValue Op5) {
5563 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005564 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005565}
5566
Dan Gohman92c11ac2010-06-18 15:30:29 +00005567SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005568UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5569 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005570 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005571 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005572
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005573 // If no operands changed just return the input node.
Benjamin Kramer0b6742a2015-03-02 16:45:08 +00005574 if (Ops.empty() || std::equal(Ops.begin(), Ops.end(), N->op_begin()))
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005575 return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005576
Chris Lattnerf34156e2006-01-28 09:32:45 +00005577 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005578 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005579 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005580 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005581
Dan Gohman2f83b472008-05-02 00:05:03 +00005582 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005583 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005584 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005585 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005586
Chris Lattnerf34156e2006-01-28 09:32:45 +00005587 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005588 for (unsigned i = 0; i != NumOps; ++i)
5589 if (N->OperandList[i] != Ops[i])
5590 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005591
5592 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005593 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005594 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005595}
5596
Dan Gohman91697632008-07-07 20:57:48 +00005597/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005598/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005599void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005600 // Unlike the code in MorphNodeTo that does this, we don't need to
5601 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005602 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5603 SDUse &Use = *I++;
5604 Use.set(SDValue());
5605 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005606}
Chris Lattner19732782005-08-16 18:17:10 +00005607
Dan Gohman17059682008-07-17 19:10:17 +00005608/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5609/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005610///
Dan Gohman17059682008-07-17 19:10:17 +00005611SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005612 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005613 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005614 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005615}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005616
Dan Gohman17059682008-07-17 19:10:17 +00005617SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005618 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005619 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005620 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005621 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005622}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005623
Dan Gohman17059682008-07-17 19:10:17 +00005624SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005625 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005626 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005627 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005628 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005629 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005630}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005631
Dan Gohman17059682008-07-17 19:10:17 +00005632SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005633 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005634 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005635 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005636 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005637 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005638}
Chris Lattner466fece2005-08-21 22:30:30 +00005639
Dan Gohman17059682008-07-17 19:10:17 +00005640SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005641 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005642 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005643 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005644}
5645
Dan Gohman17059682008-07-17 19:10:17 +00005646SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005647 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005648 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005649 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005650}
5651
Dan Gohman17059682008-07-17 19:10:17 +00005652SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005653 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005654 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005655 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +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, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005660 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005661 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005662 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005663}
5664
Bill Wendling2d598632008-12-01 23:28:22 +00005665SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005666 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005667 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005668 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005669 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005670}
5671
Scott Michelcf0da6c2009-02-17 22:15:04 +00005672SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005673 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005674 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005675 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005676 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005677 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005678}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005679
Scott Michelcf0da6c2009-02-17 22:15:04 +00005680SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005681 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005682 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005683 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005684 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005685 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005686}
5687
Dan Gohman17059682008-07-17 19:10:17 +00005688SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005689 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005690 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005691 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005692 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005693 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005694 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005695}
5696
Dan Gohman17059682008-07-17 19:10:17 +00005697SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005698 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005699 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005700 SDValue Op3) {
5701 SDVTList VTs = getVTList(VT1, VT2, VT3);
5702 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005703 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005704}
5705
5706SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005707 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005708 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005709 // Reset the NodeID to -1.
5710 N->setNodeId(-1);
5711 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005712}
5713
Andrew Trickef9de2a2013-05-25 02:42:55 +00005714/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005715/// the line number information on the merged node since it is not possible to
5716/// preserve the information that operation is associated with multiple lines.
5717/// This will make the debugger working better at -O0, were there is a higher
5718/// probability having other instructions associated with that line.
5719///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005720/// For IROrder, we keep the smaller of the two
5721SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005722 DebugLoc NLoc = N->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00005723 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005724 N->setDebugLoc(DebugLoc());
5725 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005726 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5727 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005728 return N;
5729}
5730
Chris Lattneraf197502010-02-28 21:36:14 +00005731/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005732/// return type, opcode, and operands.
5733///
5734/// Note that MorphNodeTo returns the resultant node. If there is already a
5735/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005736/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005737///
5738/// Using MorphNodeTo is faster than creating a new node and swapping it in
5739/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005740/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005741/// the node's users.
5742///
Chandler Carruth356665a2014-08-01 22:09:43 +00005743/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5744/// As a consequence it isn't appropriate to use from within the DAG combiner or
5745/// the legalizer which maintain worklists that would need to be updated when
5746/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005747SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005748 SDVTList VTs, ArrayRef<SDValue> Ops) {
5749 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005750 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005751 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005752 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005753 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005754 AddNodeIDNode(ID, Opc, VTs, Ops);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005755 if (SDNode *ON = FindNodeOrInsertPos(ID, N->getDebugLoc(), IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005756 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005757 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005758
Dan Gohmand3fe1742008-09-13 01:54:27 +00005759 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005760 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005761
Dan Gohman17059682008-07-17 19:10:17 +00005762 // Start the morphing.
5763 N->NodeType = Opc;
5764 N->ValueList = VTs.VTs;
5765 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005766
Dan Gohman17059682008-07-17 19:10:17 +00005767 // Clear the operands list, updating used nodes to remove this from their
5768 // use list. Keep track of any operands that become dead as a result.
5769 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005770 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5771 SDUse &Use = *I++;
5772 SDNode *Used = Use.getNode();
5773 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005774 if (Used->use_empty())
5775 DeadNodeSet.insert(Used);
5776 }
5777
Dan Gohman48b185d2009-09-25 20:36:54 +00005778 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5779 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005780 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005781 // If NumOps is larger than the # of operands we can have in a
5782 // MachineSDNode, reallocate the operand list.
5783 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5784 if (MN->OperandsNeedDelete)
5785 delete[] MN->OperandList;
5786 if (NumOps > array_lengthof(MN->LocalOperands))
5787 // We're creating a final node that will live unmorphed for the
5788 // remainder of the current SelectionDAG iteration, so we can allocate
5789 // the operands directly out of a pool with no recycling metadata.
5790 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005791 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005792 else
Craig Topper131de822014-04-27 19:21:16 +00005793 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005794 MN->OperandsNeedDelete = false;
5795 } else
Craig Topper131de822014-04-27 19:21:16 +00005796 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005797 } else {
5798 // If NumOps is larger than the # of operands we currently have, reallocate
5799 // the operand list.
5800 if (NumOps > N->NumOperands) {
5801 if (N->OperandsNeedDelete)
5802 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005803 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005804 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005805 } else
Craig Topper131de822014-04-27 19:21:16 +00005806 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005807 }
5808
5809 // Delete any nodes that are still dead after adding the uses for the
5810 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005811 if (!DeadNodeSet.empty()) {
5812 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005813 for (SDNode *N : DeadNodeSet)
5814 if (N->use_empty())
5815 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005816 RemoveDeadNodes(DeadNodes);
5817 }
Dan Gohman91697632008-07-07 20:57:48 +00005818
Dan Gohman17059682008-07-17 19:10:17 +00005819 if (IP)
5820 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005821 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005822}
5823
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005824
Dan Gohman32f71d72009-09-25 18:54:59 +00005825/// getMachineNode - These are used for target selectors to create a new node
5826/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005827///
Dan Gohman32f71d72009-09-25 18:54:59 +00005828/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005829/// node of the specified opcode and operands, it returns that node instead of
5830/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005831MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005832SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005833 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005834 return getMachineNode(Opcode, dl, VTs, None);
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 VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005839 SDVTList VTs = getVTList(VT);
5840 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005841 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005842}
Bill Wendlinga434d932009-01-29 09:01:55 +00005843
Dan Gohmana22f2d82009-10-10 01:29:16 +00005844MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005845SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005846 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005847 SDVTList VTs = getVTList(VT);
5848 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005849 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005850}
Bill Wendlinga434d932009-01-29 09:01:55 +00005851
Dan Gohmana22f2d82009-10-10 01:29:16 +00005852MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005853SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005854 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005855 SDVTList VTs = getVTList(VT);
5856 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005857 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005858}
Bill Wendlinga434d932009-01-29 09:01:55 +00005859
Dan Gohmana22f2d82009-10-10 01:29:16 +00005860MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005861SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005862 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005863 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005864 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005865}
Bill Wendlinga434d932009-01-29 09:01:55 +00005866
Dan Gohmana22f2d82009-10-10 01:29:16 +00005867MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005868SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005869 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005870 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005871}
Bill Wendlinga434d932009-01-29 09:01:55 +00005872
Dan Gohmana22f2d82009-10-10 01:29:16 +00005873MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005874SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005875 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005876 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005877 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005878 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005879}
Bill Wendlinga434d932009-01-29 09:01:55 +00005880
Dan Gohmana22f2d82009-10-10 01:29:16 +00005881MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005882SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005883 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005884 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005885 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005886 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005887}
5888
Dan Gohmana22f2d82009-10-10 01:29:16 +00005889MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005890SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005891 EVT VT1, EVT VT2, SDValue Op1,
5892 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005893 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005894 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005895 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005896}
5897
Dan Gohmana22f2d82009-10-10 01:29:16 +00005898MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005899SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005900 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005901 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005902 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005903 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005904}
Bill Wendlinga434d932009-01-29 09:01:55 +00005905
Dan Gohmana22f2d82009-10-10 01:29:16 +00005906MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005907SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005908 EVT VT1, EVT VT2, EVT VT3,
5909 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005910 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005911 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005912 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005913}
Bill Wendlinga434d932009-01-29 09:01:55 +00005914
Dan Gohmana22f2d82009-10-10 01:29:16 +00005915MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005916SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005917 EVT VT1, EVT VT2, EVT VT3,
5918 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005919 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005920 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005921 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005922}
Bill Wendlinga434d932009-01-29 09:01:55 +00005923
Dan Gohmana22f2d82009-10-10 01:29:16 +00005924MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005925SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005926 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005927 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005928 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005929 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005930}
Bill Wendlinga434d932009-01-29 09:01:55 +00005931
Dan Gohmana22f2d82009-10-10 01:29:16 +00005932MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005933SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005934 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005935 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005936 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005937 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005938}
5939
Dan Gohmana22f2d82009-10-10 01:29:16 +00005940MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005941SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005942 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005943 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005944 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005945 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005946}
5947
Dan Gohmana22f2d82009-10-10 01:29:16 +00005948MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005949SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005950 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005951 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005952 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005953 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005954 const SDValue *Ops = OpsArray.data();
5955 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005956
5957 if (DoCSE) {
5958 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005959 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005960 IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005961 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005962 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005963 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005964 }
5965
5966 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005967 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5968 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005969
5970 // Initialize the operands list.
5971 if (NumOps > array_lengthof(N->LocalOperands))
5972 // We're creating a final node that will live unmorphed for the
5973 // remainder of the current SelectionDAG iteration, so we can allocate
5974 // the operands directly out of a pool with no recycling metadata.
5975 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5976 Ops, NumOps);
5977 else
5978 N->InitOperands(N->LocalOperands, Ops, NumOps);
5979 N->OperandsNeedDelete = false;
5980
5981 if (DoCSE)
5982 CSEMap.InsertNode(N, IP);
5983
Chandler Carruth41b20e72014-07-22 04:07:55 +00005984 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005985 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005986}
Evan Chengd3f1db92006-02-09 07:15:23 +00005987
Dan Gohmanac33a902009-08-19 18:16:17 +00005988/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005989/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005990SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005991SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005992 SDValue Operand) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005993 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005994 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005995 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005996 return SDValue(Subreg, 0);
5997}
5998
Bob Wilson2a45a652009-10-08 18:49:46 +00005999/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00006000/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00006001SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006002SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00006003 SDValue Operand, SDValue Subreg) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006004 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00006005 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00006006 VT, Operand, Subreg, SRIdxVal);
6007 return SDValue(Result, 0);
6008}
6009
Evan Cheng31604a62008-03-22 01:55:50 +00006010/// getNodeIfExists - Get the specified node if it's already available, or
6011/// else return NULL.
6012SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Nick Lewycky37a17502015-05-13 23:41:47 +00006013 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
6014 bool exact) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00006015 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00006016 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00006017 AddNodeIDNode(ID, Opcode, VTList, Ops);
Nick Lewycky37a17502015-05-13 23:41:47 +00006018 if (isBinOpWithFlags(Opcode))
6019 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00006020 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00006021 if (SDNode *E = FindNodeOrInsertPos(ID, DebugLoc(), IP))
Evan Cheng31604a62008-03-22 01:55:50 +00006022 return E;
6023 }
Craig Topperc0196b12014-04-14 00:51:57 +00006024 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00006025}
6026
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006027/// getDbgValue - Creates a SDDbgValue node.
6028///
Adrian Prantl32da8892014-04-25 20:49:25 +00006029/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006030SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
6031 unsigned R, bool IsIndirect, uint64_t Off,
6032 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006033 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006034 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006035 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006036}
6037
Adrian Prantl32da8892014-04-25 20:49:25 +00006038/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006039SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
6040 const Value *C, uint64_t Off,
6041 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006042 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006043 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006044 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006045}
6046
Adrian Prantl32da8892014-04-25 20:49:25 +00006047/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006048SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
6049 unsigned FI, uint64_t Off,
6050 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006051 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006052 "Expected inlined-at fields to agree");
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006053 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006054}
6055
Dan Gohman7d099f72010-03-03 21:33:37 +00006056namespace {
6057
Dan Gohman9cc886b2010-03-04 19:11:28 +00006058/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00006059/// pointed to by a use iterator is deleted, increment the use iterator
6060/// so that it doesn't dangle.
6061///
Dan Gohman7d099f72010-03-03 21:33:37 +00006062class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00006063 SDNode::use_iterator &UI;
6064 SDNode::use_iterator &UE;
6065
Craig Topper7b883b32014-03-08 06:31:39 +00006066 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00006067 // Increment the iterator as needed.
6068 while (UI != UE && N == *UI)
6069 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00006070 }
6071
6072public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006073 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00006074 SDNode::use_iterator &ui,
6075 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006076 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00006077};
6078
6079}
6080
Evan Cheng445b91a2006-08-07 22:13:29 +00006081/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006082/// This can cause recursive merging of nodes in the DAG.
6083///
Chris Lattner76858912008-02-03 03:35:22 +00006084/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00006085///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006086void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006087 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006088 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00006089 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00006090 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00006091
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006092 // Iterate over all the existing uses of From. New uses will be added
6093 // to the beginning of the use list, which we avoid visiting.
6094 // This specifically avoids visiting uses of From that arise while the
6095 // replacement is happening, because any such uses would be the result
6096 // of CSE: If an existing node looks like From after one of its operands
6097 // is replaced by To, we don't want to replace of all its users with To
6098 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006099 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006100 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006101 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006102 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006103
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006104 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006105 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006106
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006107 // A user can appear in a use list multiple times, and when this
6108 // happens the uses are usually next to each other in the list.
6109 // To help reduce the number of CSE recomputations, process all
6110 // the uses of this user that we can find this way.
6111 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006112 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006113 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006114 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006115 } while (UI != UE && *UI == User);
6116
6117 // Now that we have modified User, add it back to the CSE maps. If it
6118 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006119 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006120 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006121
6122 // If we just RAUW'd the root, take note.
6123 if (FromN == getRoot())
6124 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00006125}
6126
6127/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6128/// This can cause recursive merging of nodes in the DAG.
6129///
Dan Gohman8aa28b92009-04-15 20:06:30 +00006130/// This version assumes that for each value of From, there is a
6131/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00006132///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006133void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00006134#ifndef NDEBUG
6135 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6136 assert((!From->hasAnyUseOfValue(i) ||
6137 From->getValueType(i) == To->getValueType(i)) &&
6138 "Cannot use this version of ReplaceAllUsesWith!");
6139#endif
Dan Gohman17059682008-07-17 19:10:17 +00006140
6141 // Handle the trivial case.
6142 if (From == To)
6143 return;
6144
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006145 // Iterate over just the existing users of From. See the comments in
6146 // the ReplaceAllUsesWith above.
6147 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006148 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006149 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006150 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006151
Chris Lattner373f0482005-08-26 18:36:28 +00006152 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006153 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006154
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006155 // A user can appear in a use list multiple times, and when this
6156 // happens the uses are usually next to each other in the list.
6157 // To help reduce the number of CSE recomputations, process all
6158 // the uses of this user that we can find this way.
6159 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006160 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006161 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006162 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006163 } while (UI != UE && *UI == User);
6164
6165 // Now that we have modified User, add it back to the CSE maps. If it
6166 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006167 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006168 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006169
6170 // If we just RAUW'd the root, take note.
6171 if (From == getRoot().getNode())
6172 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006173}
6174
Chris Lattner373f0482005-08-26 18:36:28 +00006175/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6176/// This can cause recursive merging of nodes in the DAG.
6177///
6178/// This version can replace From with any result values. To must match the
6179/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006180void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006181 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006182 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006183
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006184 // Iterate over just the existing users of From. See the comments in
6185 // the ReplaceAllUsesWith above.
6186 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006187 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006188 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006189 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006190
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006191 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006192 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006193
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006194 // A user can appear in a use list multiple times, and when this
6195 // happens the uses are usually next to each other in the list.
6196 // To help reduce the number of CSE recomputations, process all
6197 // the uses of this user that we can find this way.
6198 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006199 SDUse &Use = UI.getUse();
6200 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006201 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006202 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006203 } while (UI != UE && *UI == User);
6204
6205 // Now that we have modified User, add it back to the CSE maps. If it
6206 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006207 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006208 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006209
6210 // If we just RAUW'd the root, take note.
6211 if (From == getRoot().getNode())
6212 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006213}
6214
Chris Lattner375e1a72006-02-17 21:58:01 +00006215/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006216/// uses of other values produced by From.getNode() alone. The Deleted
6217/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006218void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006219 // Handle the really simple, really trivial case efficiently.
6220 if (From == To) return;
6221
Chris Lattner375e1a72006-02-17 21:58:01 +00006222 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006223 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006224 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006225 return;
6226 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006227
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006228 // Iterate over just the existing users of From. See the comments in
6229 // the ReplaceAllUsesWith above.
6230 SDNode::use_iterator UI = From.getNode()->use_begin(),
6231 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006232 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006233 while (UI != UE) {
6234 SDNode *User = *UI;
6235 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006236
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006237 // A user can appear in a use list multiple times, and when this
6238 // happens the uses are usually next to each other in the list.
6239 // To help reduce the number of CSE recomputations, process all
6240 // the uses of this user that we can find this way.
6241 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006242 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006243
6244 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006245 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006246 ++UI;
6247 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006248 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006249
6250 // If this node hasn't been modified yet, it's still in the CSE maps,
6251 // so remove its old self from the CSE maps.
6252 if (!UserRemovedFromCSEMaps) {
6253 RemoveNodeFromCSEMaps(User);
6254 UserRemovedFromCSEMaps = true;
6255 }
6256
6257 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006258 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006259 } while (UI != UE && *UI == User);
6260
6261 // We are iterating over all uses of the From node, so if a use
6262 // doesn't use the specific value, no changes are made.
6263 if (!UserRemovedFromCSEMaps)
6264 continue;
6265
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006266 // Now that we have modified User, add it back to the CSE maps. If it
6267 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006268 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006269 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006270
6271 // If we just RAUW'd the root, take note.
6272 if (From == getRoot())
6273 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006274}
6275
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006276namespace {
6277 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6278 /// to record information about a use.
6279 struct UseMemo {
6280 SDNode *User;
6281 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006282 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006283 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006284
6285 /// operator< - Sort Memos by User.
6286 bool operator<(const UseMemo &L, const UseMemo &R) {
6287 return (intptr_t)L.User < (intptr_t)R.User;
6288 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006289}
6290
Dan Gohman17059682008-07-17 19:10:17 +00006291/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006292/// uses of other values produced by From.getNode() alone. The same value
6293/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006294/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006295void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6296 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006297 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006298 // Handle the simple, trivial case efficiently.
6299 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006300 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006301
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006302 // Read up all the uses and make records of them. This helps
6303 // processing new uses that are introduced during the
6304 // replacement process.
6305 SmallVector<UseMemo, 4> Uses;
6306 for (unsigned i = 0; i != Num; ++i) {
6307 unsigned FromResNo = From[i].getResNo();
6308 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006309 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006310 E = FromNode->use_end(); UI != E; ++UI) {
6311 SDUse &Use = UI.getUse();
6312 if (Use.getResNo() == FromResNo) {
6313 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006314 Uses.push_back(Memo);
6315 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006316 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006317 }
Dan Gohman17059682008-07-17 19:10:17 +00006318
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006319 // Sort the uses, so that all the uses from a given User are together.
6320 std::sort(Uses.begin(), Uses.end());
6321
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006322 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6323 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006324 // We know that this user uses some value of From. If it is the right
6325 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006326 SDNode *User = Uses[UseIndex].User;
6327
6328 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006329 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006330
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006331 // The Uses array is sorted, so all the uses for a given User
6332 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006333 // To help reduce the number of CSE recomputations, process all
6334 // the uses of this user that we can find this way.
6335 do {
6336 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006337 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006338 ++UseIndex;
6339
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006340 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006341 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6342
Dan Gohman17059682008-07-17 19:10:17 +00006343 // Now that we have modified User, add it back to the CSE maps. If it
6344 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006345 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006346 }
6347}
6348
Evan Cheng9631a602006-08-01 08:20:41 +00006349/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006350/// based on their topological order. It returns the maximum id and a vector
6351/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006352unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006353
Dan Gohman86aa16a2008-09-30 18:30:35 +00006354 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006355
Dan Gohman86aa16a2008-09-30 18:30:35 +00006356 // SortedPos tracks the progress of the algorithm. Nodes before it are
6357 // sorted, nodes after it are unsorted. When the algorithm completes
6358 // it is at the end of the list.
6359 allnodes_iterator SortedPos = allnodes_begin();
6360
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006361 // Visit all the nodes. Move nodes with no operands to the front of
6362 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006363 // operand count. Before we do this, the Node Id fields of the nodes
6364 // may contain arbitrary values. After, the Node Id fields for nodes
6365 // before SortedPos will contain the topological sort index, and the
6366 // Node Id fields for nodes At SortedPos and after will contain the
6367 // count of outstanding operands.
6368 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6369 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006370 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006371 unsigned Degree = N->getNumOperands();
6372 if (Degree == 0) {
6373 // A node with no uses, add it to the result array immediately.
6374 N->setNodeId(DAGSize++);
6375 allnodes_iterator Q = N;
6376 if (Q != SortedPos)
6377 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006378 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006379 ++SortedPos;
6380 } else {
6381 // Temporarily use the Node Id as scratch space for the degree count.
6382 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006383 }
6384 }
6385
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006386 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006387 // such that by the time the end is reached all nodes will be sorted.
6388 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6389 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006390 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006391 // N is in sorted position, so all its uses have one less operand
6392 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006393 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6394 UI != UE; ++UI) {
6395 SDNode *P = *UI;
6396 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006397 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006398 --Degree;
6399 if (Degree == 0) {
6400 // All of P's operands are sorted, so P may sorted now.
6401 P->setNodeId(DAGSize++);
6402 if (P != SortedPos)
6403 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006404 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006405 ++SortedPos;
6406 } else {
6407 // Update P's outstanding operand count.
6408 P->setNodeId(Degree);
6409 }
6410 }
David Greene3b2a68c2010-01-20 00:59:23 +00006411 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006412#ifndef NDEBUG
6413 SDNode *S = ++I;
6414 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006415 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006416 dbgs() << "Checking if this is due to cycles\n";
6417 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006418#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006419 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006420 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006421 }
6422
6423 assert(SortedPos == AllNodes.end() &&
6424 "Topological sort incomplete!");
6425 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6426 "First node in topological sort is not the entry token!");
6427 assert(AllNodes.front().getNodeId() == 0 &&
6428 "First node in topological sort has non-zero id!");
6429 assert(AllNodes.front().getNumOperands() == 0 &&
6430 "First node in topological sort has operands!");
6431 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6432 "Last node in topologic sort has unexpected id!");
6433 assert(AllNodes.back().use_empty() &&
6434 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006435 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006436 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006437}
6438
Evan Cheng563fe3c2010-03-25 01:38:16 +00006439/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6440/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006441void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006442 if (SD) {
6443 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006444 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006445 }
6446 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006447}
Evan Cheng29eefc12006-07-27 06:39:06 +00006448
Devang Patelefc6b162011-01-25 23:27:42 +00006449/// TransferDbgValues - Transfer SDDbgValues.
6450void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6451 if (From == To || !From.getNode()->getHasDebugValue())
6452 return;
6453 SDNode *FromNode = From.getNode();
6454 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006455 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006456 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006457 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006458 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006459 SDDbgValue *Dbg = *I;
6460 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006461 SDDbgValue *Clone =
6462 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6463 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6464 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006465 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006466 }
6467 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006468 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006469 E = ClonedDVs.end(); I != E; ++I)
6470 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006471}
6472
Jim Laskeyd66e6162005-08-17 20:08:02 +00006473//===----------------------------------------------------------------------===//
6474// SDNode Class
6475//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006476
Chris Lattner3bf17b62007-02-04 02:41:42 +00006477HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006478 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006479}
6480
Andrew Trickef9de2a2013-05-25 02:42:55 +00006481GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6482 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006483 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006484 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006485 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006486}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006487
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006488AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6489 SDValue X, unsigned SrcAS,
6490 unsigned DestAS)
6491 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6492 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6493
Andrew Trickef9de2a2013-05-25 02:42:55 +00006494MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6495 EVT memvt, MachineMemOperand *mmo)
6496 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006497 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006498 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006499 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006500 assert(isNonTemporal() == MMO->isNonTemporal() &&
6501 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006502 // We check here that the size of the memory operand fits within the size of
6503 // the MMO. This is because the MMO might indicate only a possible address
6504 // range instead of specifying the affected memory addresses precisely.
6505 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006506}
6507
Andrew Trickef9de2a2013-05-25 02:42:55 +00006508MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006509 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6510 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006511 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006512 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006513 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006514 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006515 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006516}
6517
Jim Laskeyf576b422006-10-27 23:46:08 +00006518/// Profile - Gather unique data for the node.
6519///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006520void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006521 AddNodeIDNode(ID, this);
6522}
6523
Owen Anderson3b1665e2009-08-25 22:27:22 +00006524namespace {
6525 struct EVTArray {
6526 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006527
Owen Anderson3b1665e2009-08-25 22:27:22 +00006528 EVTArray() {
6529 VTs.reserve(MVT::LAST_VALUETYPE);
6530 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6531 VTs.push_back(MVT((MVT::SimpleValueType)i));
6532 }
6533 };
6534}
6535
Owen Anderson53aa7a92009-08-10 22:56:29 +00006536static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006537static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006538static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006539
Chris Lattner88fa11c2005-11-08 23:30:28 +00006540/// getValueTypeList - Return a pointer to the specified value type.
6541///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006542const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006543 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006544 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006545 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006546 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006547 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006548 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006549 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006550 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006551}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006552
Chris Lattner40e79822005-01-12 18:37:47 +00006553/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6554/// indicated value. This method ignores uses of other values defined by this
6555/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006556bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006557 assert(Value < getNumValues() && "Bad value!");
6558
Roman Levenstein51f532f2008-04-07 10:06:32 +00006559 // TODO: Only iterate over uses of a given value of the node
6560 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006561 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006562 if (NUses == 0)
6563 return false;
6564 --NUses;
6565 }
Chris Lattner40e79822005-01-12 18:37:47 +00006566 }
6567
6568 // Found exactly the right number of uses?
6569 return NUses == 0;
6570}
6571
6572
Evan Cheng358c3d12007-08-02 05:29:38 +00006573/// hasAnyUseOfValue - Return true if there are any use of the indicated
6574/// value. This method ignores uses of other values defined by this operation.
6575bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6576 assert(Value < getNumValues() && "Bad value!");
6577
Dan Gohman7a510c22008-07-09 22:39:01 +00006578 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006579 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006580 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006581
6582 return false;
6583}
6584
6585
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006586/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006587///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006588bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006589 bool Seen = false;
6590 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006591 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006592 if (User == this)
6593 Seen = true;
6594 else
6595 return false;
6596 }
6597
6598 return Seen;
6599}
6600
Evan Cheng9456dd82006-11-03 07:31:32 +00006601/// isOperand - Return true if this node is an operand of N.
6602///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006603bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006604 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6605 if (*this == N->getOperand(i))
6606 return true;
6607 return false;
6608}
6609
Evan Cheng567d2e52008-03-04 00:41:45 +00006610bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006611 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006612 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006613 return true;
6614 return false;
6615}
Evan Chengd37645c2006-02-05 06:29:23 +00006616
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006617/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006618/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006619/// side-effecting instructions on any chain path. In practice, this looks
6620/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006621/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006622bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006623 unsigned Depth) const {
6624 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006625
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006626 // Don't search too deeply, we just want to be able to see through
6627 // TokenFactor's etc.
6628 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006629
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006630 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006631 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006632 if (getOpcode() == ISD::TokenFactor) {
6633 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006634 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6635 return false;
6636 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006637 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006638
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006639 // Loads don't have side effects, look through them.
6640 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6641 if (!Ld->isVolatile())
6642 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6643 }
6644 return false;
6645}
6646
Lang Hames5a004992011-07-07 04:31:51 +00006647/// hasPredecessor - Return true if N is a predecessor of this node.
6648/// N is either an operand of this node, or can be reached by recursively
6649/// traversing up the operands.
6650/// NOTE: This is an expensive method. Use it carefully.
6651bool SDNode::hasPredecessor(const SDNode *N) const {
6652 SmallPtrSet<const SDNode *, 32> Visited;
6653 SmallVector<const SDNode *, 16> Worklist;
6654 return hasPredecessorHelper(N, Visited, Worklist);
6655}
Dan Gohmancd139c02009-10-28 03:44:30 +00006656
Craig Topperb94011f2013-07-14 04:42:23 +00006657bool
6658SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006659 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006660 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006661 if (Visited.empty()) {
6662 Worklist.push_back(this);
6663 } else {
6664 // Take a look in the visited set. If we've already encountered this node
6665 // we needn't search further.
6666 if (Visited.count(N))
6667 return true;
6668 }
6669
6670 // Haven't visited N yet. Continue the search.
6671 while (!Worklist.empty()) {
6672 const SDNode *M = Worklist.pop_back_val();
6673 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6674 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006675 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006676 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006677 if (Op == N)
6678 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006679 }
Lang Hames5a004992011-07-07 04:31:51 +00006680 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006681
6682 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006683}
6684
Evan Cheng5d9fd972006-10-04 00:56:09 +00006685uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6686 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006687 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006688}
6689
Mon P Wang32f8bb92009-11-30 02:42:02 +00006690SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6691 assert(N->getNumValues() == 1 &&
6692 "Can't unroll a vector with multiple results!");
6693
6694 EVT VT = N->getValueType(0);
6695 unsigned NE = VT.getVectorNumElements();
6696 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006697 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006698
6699 SmallVector<SDValue, 8> Scalars;
6700 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6701
6702 // If ResNE is 0, fully unroll the vector op.
6703 if (ResNE == 0)
6704 ResNE = NE;
6705 else if (NE > ResNE)
6706 NE = ResNE;
6707
6708 unsigned i;
6709 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006710 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006711 SDValue Operand = N->getOperand(j);
6712 EVT OperandVT = Operand.getValueType();
6713 if (OperandVT.isVector()) {
6714 // A vector operand; extract a single element.
6715 EVT OperandEltVT = OperandVT.getVectorElementType();
6716 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6717 OperandEltVT,
6718 Operand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006719 getConstant(i, dl, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006720 } else {
6721 // A scalar operand; just use it as is.
6722 Operands[j] = Operand;
6723 }
6724 }
6725
6726 switch (N->getOpcode()) {
6727 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006728 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006729 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006730 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006731 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006732 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006733 case ISD::SHL:
6734 case ISD::SRA:
6735 case ISD::SRL:
6736 case ISD::ROTL:
6737 case ISD::ROTR:
6738 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006739 getShiftAmountOperand(Operands[0].getValueType(),
6740 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006741 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006742 case ISD::SIGN_EXTEND_INREG:
6743 case ISD::FP_ROUND_INREG: {
6744 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6745 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6746 Operands[0],
6747 getValueType(ExtVT)));
6748 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006749 }
6750 }
6751
6752 for (; i < ResNE; ++i)
6753 Scalars.push_back(getUNDEF(EltVT));
6754
6755 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006756 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006757}
6758
Evan Chengf5938d52009-12-09 01:36:00 +00006759
Wesley Peck527da1b2010-11-23 03:31:01 +00006760/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6761/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006762/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006763bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006764 unsigned Bytes, int Dist) const {
6765 if (LD->getChain() != Base->getChain())
6766 return false;
6767 EVT VT = LD->getValueType(0);
6768 if (VT.getSizeInBits() / 8 != Bytes)
6769 return false;
6770
6771 SDValue Loc = LD->getOperand(1);
6772 SDValue BaseLoc = Base->getOperand(1);
6773 if (Loc.getOpcode() == ISD::FrameIndex) {
6774 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6775 return false;
6776 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6777 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6778 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6779 int FS = MFI->getObjectSize(FI);
6780 int BFS = MFI->getObjectSize(BFI);
6781 if (FS != BFS || FS != (int)Bytes) return false;
6782 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6783 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006784
Sanjay Patel7129c102014-12-16 21:57:18 +00006785 // Handle X + C.
6786 if (isBaseWithConstantOffset(Loc)) {
6787 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6788 if (Loc.getOperand(0) == BaseLoc) {
6789 // If the base location is a simple address with no offset itself, then
6790 // the second load's first add operand should be the base address.
6791 if (LocOffset == Dist * (int)Bytes)
6792 return true;
6793 } else if (isBaseWithConstantOffset(BaseLoc)) {
6794 // The base location itself has an offset, so subtract that value from the
6795 // second load's offset before comparing to distance * size.
6796 int64_t BOffset =
6797 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6798 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6799 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6800 return true;
6801 }
6802 }
6803 }
Craig Topperc0196b12014-04-14 00:51:57 +00006804 const GlobalValue *GV1 = nullptr;
6805 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006806 int64_t Offset1 = 0;
6807 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006808 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6809 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006810 if (isGA1 && isGA2 && GV1 == GV2)
6811 return Offset1 == (Offset2 + Dist*Bytes);
6812 return false;
6813}
6814
6815
Evan Cheng34a23ea2009-12-09 01:04:59 +00006816/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6817/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006818unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006819 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006820 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006821 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006822 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006823 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006824 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00006825 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
6826 *TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006827 unsigned AlignBits = KnownZero.countTrailingOnes();
6828 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6829 if (Align)
6830 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006831 }
Evan Chengd938faf2009-12-09 01:53:58 +00006832
Evan Cheng34a23ea2009-12-09 01:04:59 +00006833 // If this is a direct reference to a stack slot, use information about the
6834 // stack slot's alignment.
6835 int FrameIdx = 1 << 31;
6836 int64_t FrameOffset = 0;
6837 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6838 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006839 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006840 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006841 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006842 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6843 FrameOffset = Ptr.getConstantOperandVal(1);
6844 }
6845
6846 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006847 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006848 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6849 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006850 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006851 }
6852
6853 return 0;
6854}
6855
Juergen Ributzkab3487102013-11-19 21:20:17 +00006856/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6857/// which is split (or expanded) into two not necessarily identical pieces.
6858std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6859 // Currently all types are split in half.
6860 EVT LoVT, HiVT;
6861 if (!VT.isVector()) {
6862 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6863 } else {
6864 unsigned NumElements = VT.getVectorNumElements();
6865 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6866 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6867 NumElements/2);
6868 }
6869 return std::make_pair(LoVT, HiVT);
6870}
6871
6872/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6873/// low/high part.
6874std::pair<SDValue, SDValue>
6875SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6876 const EVT &HiVT) {
6877 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6878 N.getValueType().getVectorNumElements() &&
6879 "More vector elements requested than available!");
6880 SDValue Lo, Hi;
6881 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006882 getConstant(0, DL, TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006883 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006884 getConstant(LoVT.getVectorNumElements(), DL,
6885 TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006886 return std::make_pair(Lo, Hi);
6887}
6888
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006889void SelectionDAG::ExtractVectorElements(SDValue Op,
6890 SmallVectorImpl<SDValue> &Args,
6891 unsigned Start, unsigned Count) {
6892 EVT VT = Op.getValueType();
6893 if (Count == 0)
6894 Count = VT.getVectorNumElements();
6895
6896 EVT EltVT = VT.getVectorElementType();
6897 EVT IdxTy = TLI->getVectorIdxTy();
6898 SDLoc SL(Op);
6899 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6900 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006901 Op, getConstant(i, SL, IdxTy)));
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006902 }
6903}
6904
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006905// getAddressSpace - Return the address space this GlobalAddress belongs to.
6906unsigned GlobalAddressSDNode::getAddressSpace() const {
6907 return getGlobal()->getType()->getAddressSpace();
6908}
6909
6910
Chris Lattner229907c2011-07-18 04:54:35 +00006911Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006912 if (isMachineConstantPoolEntry())
6913 return Val.MachineCPVal->getType();
6914 return Val.ConstVal->getType();
6915}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006916
Bob Wilson85cefe82009-03-02 23:24:16 +00006917bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6918 APInt &SplatUndef,
6919 unsigned &SplatBitSize,
6920 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006921 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006922 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006923 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006924 assert(VT.isVector() && "Expected a vector type");
6925 unsigned sz = VT.getSizeInBits();
6926 if (MinSplatBits > sz)
6927 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006928
Bob Wilson85cefe82009-03-02 23:24:16 +00006929 SplatValue = APInt(sz, 0);
6930 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006931
Bob Wilson85cefe82009-03-02 23:24:16 +00006932 // Get the bits. Bits with undefined values (when the corresponding element
6933 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6934 // in SplatValue. If any of the values are not constant, give up and return
6935 // false.
6936 unsigned int nOps = getNumOperands();
6937 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6938 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006939
6940 for (unsigned j = 0; j < nOps; ++j) {
6941 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006942 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006943 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006944
Bob Wilson85cefe82009-03-02 23:24:16 +00006945 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006946 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006947 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006948 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006949 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006950 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006951 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006952 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006953 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006954 }
6955
Bob Wilson85cefe82009-03-02 23:24:16 +00006956 // The build_vector is all constants or undefs. Find the smallest element
6957 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006958
Bob Wilson85cefe82009-03-02 23:24:16 +00006959 HasAnyUndefs = (SplatUndef != 0);
6960 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006961
Bob Wilson85cefe82009-03-02 23:24:16 +00006962 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006963 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6964 APInt LowValue = SplatValue.trunc(HalfSize);
6965 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6966 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006967
Bob Wilson85cefe82009-03-02 23:24:16 +00006968 // If the two halves do not match (ignoring undef bits), stop here.
6969 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6970 MinSplatBits > HalfSize)
6971 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006972
Bob Wilson85cefe82009-03-02 23:24:16 +00006973 SplatValue = HighValue | LowValue;
6974 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006975
Bob Wilson85cefe82009-03-02 23:24:16 +00006976 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006977 }
6978
Bob Wilson85cefe82009-03-02 23:24:16 +00006979 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006980 return true;
6981}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006982
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006983SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6984 if (UndefElements) {
6985 UndefElements->clear();
6986 UndefElements->resize(getNumOperands());
6987 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006988 SDValue Splatted;
6989 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6990 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006991 if (Op.getOpcode() == ISD::UNDEF) {
6992 if (UndefElements)
6993 (*UndefElements)[i] = true;
6994 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006995 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006996 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006997 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006998 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006999 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00007000
Chandler Carruthb844e722014-07-08 07:19:55 +00007001 if (!Splatted) {
7002 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
7003 "Can only have a splat without a constant for all undefs.");
7004 return getOperand(0);
7005 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00007006
Chandler Carruthb844e722014-07-08 07:19:55 +00007007 return Splatted;
7008}
7009
7010ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007011BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00007012 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007013 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00007014}
7015
7016ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007017BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00007018 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007019 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00007020}
7021
Juergen Ributzka73844052014-01-13 20:51:35 +00007022bool BuildVectorSDNode::isConstant() const {
7023 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
7024 unsigned Opc = getOperand(i).getOpcode();
7025 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
7026 return false;
7027 }
7028 return true;
7029}
7030
Owen Anderson53aa7a92009-08-10 22:56:29 +00007031bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00007032 // Find the first non-undef value in the shuffle mask.
7033 unsigned i, e;
7034 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
7035 /* search */;
7036
Nate Begeman39b59db2009-04-29 18:13:31 +00007037 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00007038
Nate Begeman5f829d82009-04-29 05:20:52 +00007039 // Make sure all remaining elements are either undef or the same as the first
7040 // non-undef value.
7041 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007042 if (Mask[i] >= 0 && Mask[i] != Idx)
7043 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007044 return true;
7045}
David Greene09851602010-01-20 20:13:31 +00007046
Adam Nemetb4690e32014-05-31 16:23:20 +00007047#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00007048static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00007049 SmallPtrSetImpl<const SDNode*> &Visited,
7050 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00007051 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007052 // If this node has already been checked, don't check it again.
7053 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00007054 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00007055
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007056 // If a node has already been visited on this depth-first walk, reject it as
7057 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00007058 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007059 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00007060 dbgs() << "Offending node:\n";
7061 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007062 abort();
David Greene09851602010-01-20 20:13:31 +00007063 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007064
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007065 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00007066 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00007067
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007068 Checked.insert(N);
7069 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00007070}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007071#endif
David Greene09851602010-01-20 20:13:31 +00007072
Adam Nemet7d394302014-05-31 16:23:17 +00007073void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00007074 const llvm::SelectionDAG *DAG,
7075 bool force) {
7076#ifndef NDEBUG
7077 bool check = force;
David Greene09851602010-01-20 20:13:31 +00007078#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00007079 check = true;
7080#endif // XDEBUG
7081 if (check) {
7082 assert(N && "Checking nonexistent SDNode");
7083 SmallPtrSet<const SDNode*, 32> visited;
7084 SmallPtrSet<const SDNode*, 32> checked;
7085 checkForCyclesHelper(N, visited, checked, DAG);
7086 }
7087#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00007088}
7089
Adam Nemetb4690e32014-05-31 16:23:20 +00007090void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7091 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00007092}