blob: 2108e6b845ad85b280bae75f3a921f2e0aeada54 [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}
Sanjay Patelf1340482015-06-16 16:25:43 +0000403/// Add logical or fast math flag values to FoldingSetNodeID value.
404static void AddNodeIDFlags(FoldingSetNodeID &ID, unsigned Opcode,
405 const SDNodeFlags *Flags) {
406 if (!Flags || !isBinOpWithFlags(Opcode))
407 return;
Nick Lewycky37a17502015-05-13 23:41:47 +0000408
Sanjay Patelf1340482015-06-16 16:25:43 +0000409 unsigned RawFlags = Flags->getRawFlags();
410 // If no flags are set, do not alter the ID. We must match the ID of nodes
411 // that were created without explicitly specifying flags. This also saves time
412 // and allows a gradual increase in API usage of the optional optimization
413 // flags.
414 if (RawFlags != 0)
415 ID.AddInteger(RawFlags);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000416}
417
Sanjay Patelf1340482015-06-16 16:25:43 +0000418static void AddNodeIDFlags(FoldingSetNodeID &ID, const SDNode *N) {
419 if (auto *Node = dyn_cast<BinaryWithFlagsSDNode>(N))
420 AddNodeIDFlags(ID, Node->getOpcode(), &Node->Flags);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000421}
422
Craig Topper633d99b2014-04-27 23:22:43 +0000423static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
424 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000425 AddNodeIDOpcode(ID, OpC);
426 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000427 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000428}
429
Rafael Espindola36b718f2015-06-22 17:46:53 +0000430/// If this is an SDNode with special info, add this info to the NodeID data.
Duncan Sands835bdca2008-10-27 15:30:53 +0000431static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000432 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000433 case ISD::TargetExternalSymbol:
434 case ISD::ExternalSymbol:
Rafael Espindola36b718f2015-06-22 17:46:53 +0000435 case ISD::MCSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000436 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 default: break; // Normal nodes don't need extra info.
438 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000439 case ISD::Constant: {
440 const ConstantSDNode *C = cast<ConstantSDNode>(N);
441 ID.AddPointer(C->getConstantIntValue());
442 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000443 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000444 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000445 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000446 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000447 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000448 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000449 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000450 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000451 case ISD::GlobalAddress:
452 case ISD::TargetGlobalTLSAddress:
453 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000454 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000455 ID.AddPointer(GA->getGlobal());
456 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000457 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000458 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000459 break;
460 }
461 case ISD::BasicBlock:
462 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
463 break;
464 case ISD::Register:
465 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
466 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000467 case ISD::RegisterMask:
468 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
469 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000470 case ISD::SRCVALUE:
471 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
472 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000473 case ISD::FrameIndex:
474 case ISD::TargetFrameIndex:
475 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
476 break;
477 case ISD::JumpTable:
478 case ISD::TargetJumpTable:
479 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000480 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000481 break;
482 case ISD::ConstantPool:
483 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000484 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000485 ID.AddInteger(CP->getAlignment());
486 ID.AddInteger(CP->getOffset());
487 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000488 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000489 else
490 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000491 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000492 break;
493 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000494 case ISD::TargetIndex: {
495 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
496 ID.AddInteger(TI->getIndex());
497 ID.AddInteger(TI->getOffset());
498 ID.AddInteger(TI->getTargetFlags());
499 break;
500 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000501 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000502 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000503 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000504 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000505 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000506 break;
507 }
508 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000509 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000510 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000511 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000512 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000513 break;
514 }
Dan Gohman12f24902008-12-23 21:37:04 +0000515 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000516 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000517 case ISD::ATOMIC_SWAP:
518 case ISD::ATOMIC_LOAD_ADD:
519 case ISD::ATOMIC_LOAD_SUB:
520 case ISD::ATOMIC_LOAD_AND:
521 case ISD::ATOMIC_LOAD_OR:
522 case ISD::ATOMIC_LOAD_XOR:
523 case ISD::ATOMIC_LOAD_NAND:
524 case ISD::ATOMIC_LOAD_MIN:
525 case ISD::ATOMIC_LOAD_MAX:
526 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000527 case ISD::ATOMIC_LOAD_UMAX:
528 case ISD::ATOMIC_LOAD:
529 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000530 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000531 ID.AddInteger(AT->getMemoryVT().getRawBits());
532 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000533 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
534 break;
535 }
536 case ISD::PREFETCH: {
537 const MemSDNode *PF = cast<MemSDNode>(N);
538 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000539 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000540 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000541 case ISD::VECTOR_SHUFFLE: {
542 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000543 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000544 i != e; ++i)
545 ID.AddInteger(SVN->getMaskElt(i));
546 break;
547 }
Dan Gohman6c938802009-10-30 01:27:03 +0000548 case ISD::TargetBlockAddress:
549 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000550 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
551 ID.AddPointer(BA->getBlockAddress());
552 ID.AddInteger(BA->getOffset());
553 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000554 break;
555 }
Mon P Wang6a490372008-06-25 08:15:39 +0000556 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000557
Sanjay Patelf1340482015-06-16 16:25:43 +0000558 AddNodeIDFlags(ID, N);
559
Pete Cooper91244262012-07-30 20:23:19 +0000560 // Target specific memory nodes could also have address spaces to check.
561 if (N->isTargetMemoryOpcode())
562 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000563}
564
Duncan Sands835bdca2008-10-27 15:30:53 +0000565/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
566/// data.
567static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
568 AddNodeIDOpcode(ID, N->getOpcode());
569 // Add the return value info.
570 AddNodeIDValueTypes(ID, N->getVTList());
571 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000572 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000573
574 // Handle SDNode leafs with special info.
575 AddNodeIDCustom(ID, N);
576}
577
Dan Gohman2da2bed2008-08-20 15:58:01 +0000578/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000579/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000580/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000581///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000582static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000583encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000584 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000585 assert((ConvType & 3) == ConvType &&
586 "ConvType may not require more than 2 bits!");
587 assert((AM & 7) == AM &&
588 "AM may not require more than 3 bits!");
589 return ConvType |
590 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000591 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000592 (isNonTemporal << 6) |
593 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000594}
595
Jim Laskeyf576b422006-10-27 23:46:08 +0000596//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000597// SelectionDAG Class
598//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000599
Duncan Sands835bdca2008-10-27 15:30:53 +0000600/// doNotCSE - Return true if CSE should not be performed for this node.
601static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000602 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000603 return true; // Never CSE anything that produces a flag.
604
605 switch (N->getOpcode()) {
606 default: break;
607 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000608 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000609 return true; // Never CSE these nodes.
610 }
611
612 // Check that remaining values produced are not flags.
613 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000614 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000615 return true; // Never CSE anything that produces a flag.
616
617 return false;
618}
619
Chris Lattner9c667932005-01-07 21:09:16 +0000620/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000621/// SelectionDAG.
622void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000623 // Create a dummy node (which is not added to allnodes), that adds a reference
624 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000625 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000626
Chris Lattner8927c872006-08-04 17:45:20 +0000627 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000628
Chris Lattner8927c872006-08-04 17:45:20 +0000629 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000630 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000631 if (I->use_empty())
632 DeadNodes.push_back(I);
633
Dan Gohman91697632008-07-07 20:57:48 +0000634 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000635
Dan Gohman91697632008-07-07 20:57:48 +0000636 // If the root changed (e.g. it was a dead load, update the root).
637 setRoot(Dummy.getValue());
638}
639
640/// RemoveDeadNodes - This method deletes the unreachable nodes in the
641/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000642void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000643
Chris Lattner8927c872006-08-04 17:45:20 +0000644 // Process the worklist, deleting the nodes and adding their uses to the
645 // worklist.
646 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000647 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000648
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000649 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000650 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000651
Chris Lattner8927c872006-08-04 17:45:20 +0000652 // Take the node out of the appropriate CSE map.
653 RemoveNodeFromCSEMaps(N);
654
655 // Next, brutally remove the operand list. This is safe to do, as there are
656 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000657 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
658 SDUse &Use = *I++;
659 SDNode *Operand = Use.getNode();
660 Use.set(SDValue());
661
Chris Lattner8927c872006-08-04 17:45:20 +0000662 // Now that we removed this operand, see if there are no uses of it left.
663 if (Operand->use_empty())
664 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000665 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000666
Dan Gohman534c8a22009-01-19 22:39:36 +0000667 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000668 }
Chris Lattner9c667932005-01-07 21:09:16 +0000669}
670
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000671void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000672 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000673
674 // Create a dummy node that adds a reference to the root node, preventing
675 // it from being deleted. (This matters if the root is an operand of the
676 // dead node.)
677 HandleSDNode Dummy(getRoot());
678
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000679 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000680}
681
Chris Lattnerc738d002005-08-29 21:59:31 +0000682void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000683 // First take this out of the appropriate CSE map.
684 RemoveNodeFromCSEMaps(N);
685
Scott Michelcf0da6c2009-02-17 22:15:04 +0000686 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000687 // AllNodes list, and delete the node.
688 DeleteNodeNotInCSEMaps(N);
689}
690
691void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000692 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
693 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000694
Dan Gohman86aa16a2008-09-30 18:30:35 +0000695 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000696 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000697
Dan Gohman534c8a22009-01-19 22:39:36 +0000698 DeallocateNode(N);
699}
700
Frederic Riss8ad4f492014-11-11 21:21:08 +0000701void SDDbgInfo::erase(const SDNode *Node) {
702 DbgValMapType::iterator I = DbgValMap.find(Node);
703 if (I == DbgValMap.end())
704 return;
705 for (auto &Val: I->second)
706 Val->setIsInvalidated();
707 DbgValMap.erase(I);
708}
709
Dan Gohman534c8a22009-01-19 22:39:36 +0000710void SelectionDAG::DeallocateNode(SDNode *N) {
711 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000712 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000713
Dan Gohman534c8a22009-01-19 22:39:36 +0000714 // Set the opcode to DELETED_NODE to help catch bugs when node
715 // memory is reallocated.
716 N->NodeType = ISD::DELETED_NODE;
717
Dan Gohman2e834902008-08-26 01:44:34 +0000718 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000719
Frederic Riss8ad4f492014-11-11 21:21:08 +0000720 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
721 // them and forget about that node.
722 DbgInfo->erase(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000723}
724
Chandler Carruth41b20e72014-07-22 04:07:55 +0000725#ifndef NDEBUG
726/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
727static void VerifySDNode(SDNode *N) {
728 switch (N->getOpcode()) {
729 default:
730 break;
731 case ISD::BUILD_PAIR: {
732 EVT VT = N->getValueType(0);
733 assert(N->getNumValues() == 1 && "Too many results!");
734 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
735 "Wrong return type!");
736 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
737 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
738 "Mismatched operand types!");
739 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
740 "Wrong operand type!");
741 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
742 "Wrong return type size");
743 break;
744 }
745 case ISD::BUILD_VECTOR: {
746 assert(N->getNumValues() == 1 && "Too many results!");
747 assert(N->getValueType(0).isVector() && "Wrong return type!");
748 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
749 "Wrong number of operands!");
750 EVT EltVT = N->getValueType(0).getVectorElementType();
751 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
752 assert((I->getValueType() == EltVT ||
753 (EltVT.isInteger() && I->getValueType().isInteger() &&
754 EltVT.bitsLE(I->getValueType()))) &&
755 "Wrong operand type!");
756 assert(I->getValueType() == N->getOperand(0).getValueType() &&
757 "Operands must all have the same type");
758 }
759 break;
760 }
761 }
762}
763#endif // NDEBUG
764
765/// \brief Insert a newly allocated node into the DAG.
766///
767/// Handles insertion into the all nodes list and CSE map, as well as
768/// verification and other common operations when a new node is allocated.
769void SelectionDAG::InsertNode(SDNode *N) {
770 AllNodes.push_back(N);
771#ifndef NDEBUG
772 VerifySDNode(N);
773#endif
774}
775
Chris Lattner19732782005-08-16 18:17:10 +0000776/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
777/// correspond to it. This is useful when we're about to delete or repurpose
778/// the node. We don't want future request for structurally identical nodes
779/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000780bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000781 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000782 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000783 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000784 case ISD::CONDCODE:
785 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
786 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000787 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
788 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000789 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000790 case ISD::ExternalSymbol:
791 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000792 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000793 case ISD::TargetExternalSymbol: {
794 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
795 Erased = TargetExternalSymbols.erase(
796 std::pair<std::string,unsigned char>(ESN->getSymbol(),
797 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000798 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000799 }
Rafael Espindola36b718f2015-06-22 17:46:53 +0000800 case ISD::MCSymbol: {
801 auto *MCSN = cast<MCSymbolSDNode>(N);
802 Erased = MCSymbols.erase(MCSN->getMCSymbol());
803 break;
804 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000805 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000806 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000807 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000808 Erased = ExtendedValueTypeNodes.erase(VT);
809 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000810 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
811 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000812 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000813 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000814 }
Chris Lattner9c667932005-01-07 21:09:16 +0000815 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000816 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000817 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
818 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000819 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000820 break;
821 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000822#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000823 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000824 // flag result (which cannot be CSE'd) or is one of the special cases that are
825 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000826 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000827 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000828 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000829 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000830 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000831 }
832#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000833 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000834}
835
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000836/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
837/// maps and modified in place. Add it back to the CSE maps, unless an identical
838/// node already exists, in which case transfer all its users to the existing
839/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000840///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000841void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000842SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000843 // For node types that aren't CSE'd, just act as if no identical node
844 // already exists.
845 if (!doNotCSE(N)) {
846 SDNode *Existing = CSEMap.GetOrInsertNode(N);
847 if (Existing != N) {
848 // If there was already an existing matching node, use ReplaceAllUsesWith
849 // to replace the dead one with the existing one. This can cause
850 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000851 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000852
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000853 // N is now dead. Inform the listeners and delete it.
854 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
855 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000856 DeleteNodeNotInCSEMaps(N);
857 return;
858 }
859 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000860
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000861 // If the node doesn't already exist, we updated it. Inform listeners.
862 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
863 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000864}
865
Chris Lattnerf34156e2006-01-28 09:32:45 +0000866/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000867/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000868/// return null, otherwise return a pointer to the slot it would take. If a
869/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000870SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000871 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000872 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000873 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000874
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000875 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000876 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000877 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000878 AddNodeIDCustom(ID, N);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +0000879 SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000880 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000881}
882
883/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000884/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000885/// return null, otherwise return a pointer to the slot it would take. If a
886/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000887SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000888 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000889 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000890 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000891 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000892
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000893 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000894 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000895 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000896 AddNodeIDCustom(ID, N);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +0000897 SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000898 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000899}
900
901
902/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000903/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000904/// return null, otherwise return a pointer to the slot it would take. If a
905/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000906SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000907 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000908 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000909 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000910
Jim Laskeyf576b422006-10-27 23:46:08 +0000911 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000912 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000913 AddNodeIDCustom(ID, N);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +0000914 SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000915 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000916}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000917
Owen Anderson53aa7a92009-08-10 22:56:29 +0000918/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000919/// given type.
920///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000921unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000922 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000923 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000924 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000925
Eric Christopher1e845f22014-10-08 21:08:32 +0000926 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000927}
Chris Lattner9c667932005-01-07 21:09:16 +0000928
Dale Johannesen8ba713212009-02-07 02:15:05 +0000929// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000930SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000931 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000932 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
933 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
934 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000935 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000936 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000937}
938
Eric Christopher8d07f442014-10-08 01:57:58 +0000939void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000940 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000941 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000942 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000943 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000944}
945
Chris Lattner600d3082003-08-11 14:57:33 +0000946SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000947 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000948 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000949 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000950}
951
952void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000953 assert(&*AllNodes.begin() == &EntryNode);
954 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000955 while (!AllNodes.empty())
956 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000957}
958
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000959BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
960 SDVTList VTs, SDValue N1,
Sanjay Patelf1340482015-06-16 16:25:43 +0000961 SDValue N2,
962 const SDNodeFlags *Flags) {
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000963 if (isBinOpWithFlags(Opcode)) {
Sanjay Patelf1340482015-06-16 16:25:43 +0000964 // If no flags were passed in, use a default flags object.
965 SDNodeFlags F;
966 if (Flags == nullptr)
967 Flags = &F;
968
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +0000969 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
Sanjay Patelf1340482015-06-16 16:25:43 +0000970 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2, *Flags);
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();
Rafael Espindola36b718f2015-06-22 17:46:53 +00001022 MCSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001023 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +00001024 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001025 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +00001026 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001027
Craig Topperc0196b12014-04-14 00:51:57 +00001028 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001029 AllNodes.push_back(&EntryNode);
1030 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00001031 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +00001032}
1033
Andrew Trickef9de2a2013-05-25 02:42:55 +00001034SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +00001035 return VT.bitsGT(Op.getValueType()) ?
1036 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1037 getNode(ISD::TRUNCATE, DL, VT, Op);
1038}
1039
Andrew Trickef9de2a2013-05-25 02:42:55 +00001040SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001041 return VT.bitsGT(Op.getValueType()) ?
1042 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1043 getNode(ISD::TRUNCATE, DL, VT, Op);
1044}
1045
Andrew Trickef9de2a2013-05-25 02:42:55 +00001046SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +00001047 return VT.bitsGT(Op.getValueType()) ?
1048 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1049 getNode(ISD::TRUNCATE, DL, VT, Op);
1050}
1051
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001052SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1053 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001054 if (VT.bitsLE(Op.getValueType()))
1055 return getNode(ISD::TRUNCATE, SL, VT, Op);
1056
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001057 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001058 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1059}
1060
Andrew Trickef9de2a2013-05-25 02:42:55 +00001061SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001062 assert(!VT.isVector() &&
1063 "getZeroExtendInReg should use the vector element type instead of "
1064 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001065 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001066 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1067 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001068 VT.getSizeInBits());
1069 return getNode(ISD::AND, DL, Op.getValueType(), Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001070 getConstant(Imm, DL, Op.getValueType()));
Bill Wendlingc4093182009-01-30 22:23:15 +00001071}
1072
Chandler Carruth0b666e02014-07-10 12:32:32 +00001073SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1074 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1075 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1076 "The sizes of the input and result must match in order to perform the "
1077 "extend in-register.");
1078 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1079 "The destination vector type must have fewer lanes than the input.");
1080 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1081}
1082
1083SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1084 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1085 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1086 "The sizes of the input and result must match in order to perform the "
1087 "extend in-register.");
1088 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1089 "The destination vector type must have fewer lanes than the input.");
1090 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1091}
1092
Chandler Carruthafe4b252014-07-09 10:58:18 +00001093SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1094 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001095 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1096 "The sizes of the input and result must match in order to perform the "
1097 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001098 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1099 "The destination vector type must have fewer lanes than the input.");
1100 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1101}
1102
Bob Wilsonc5890052009-01-22 17:39:32 +00001103/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1104///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001105SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001106 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001107 SDValue NegOne =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001108 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001109 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1110}
1111
Pete Cooper7fd1d722014-05-12 23:26:58 +00001112SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1113 EVT EltVT = VT.getScalarType();
1114 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001115 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001116 case TargetLowering::ZeroOrOneBooleanContent:
1117 case TargetLowering::UndefinedBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001118 TrueValue = getConstant(1, DL, VT);
Pete Cooper7fd1d722014-05-12 23:26:58 +00001119 break;
1120 case TargetLowering::ZeroOrNegativeOneBooleanContent:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001121 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
Pete Cooper7fd1d722014-05-12 23:26:58 +00001122 VT);
1123 break;
1124 }
1125 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1126}
1127
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001128SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT,
1129 bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001130 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001131 assert((EltVT.getSizeInBits() >= 64 ||
1132 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1133 "getConstant with a uint64_t value that doesn't fit in the type!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001134 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001135}
1136
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001137SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT,
1138 bool isO)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001139{
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001140 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001141}
1142
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001143SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT,
1144 bool isT, bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001145 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001146
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001147 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001148 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001149
Duncan Sandsf2641e12011-09-06 19:07:46 +00001150 // In some cases the vector type is legal but the element type is illegal and
1151 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1152 // inserted value (the type does not need to match the vector element type).
1153 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001154 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001155 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001156 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001157 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1158 Elt = ConstantInt::get(*getContext(), NewVal);
1159 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001160 // In other cases the element type is illegal and needs to be expanded, for
1161 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1162 // the value into n parts and use a vector type with n-times the elements.
1163 // Then bitcast to the type requested.
1164 // Legalizing constants too early makes the DAGCombiner's job harder so we
1165 // only legalize if the DAG tells us we must produce legal types.
1166 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1167 TLI->getTypeAction(*getContext(), EltVT) ==
1168 TargetLowering::TypeExpandInteger) {
1169 APInt NewVal = Elt->getValue();
1170 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1171 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1172 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1173 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1174
1175 // Check the temporary vector is the correct size. If this fails then
1176 // getTypeToTransformTo() probably returned a type whose size (in bits)
1177 // isn't a power-of-2 factor of the requested type size.
1178 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1179
1180 SmallVector<SDValue, 2> EltParts;
1181 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1182 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001183 .trunc(ViaEltSizeInBits), DL,
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001184 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001185 }
1186
1187 // EltParts is currently in little endian order. If we actually want
1188 // big-endian order then reverse it now.
1189 if (TLI->isBigEndian())
1190 std::reverse(EltParts.begin(), EltParts.end());
1191
1192 // The elements must be reversed when the element order is different
1193 // to the endianness of the elements (because the BITCAST is itself a
1194 // vector shuffle in this situation). However, we do not need any code to
1195 // perform this reversal because getConstant() is producing a vector
1196 // splat.
1197 // This situation occurs in MIPS MSA.
1198
1199 SmallVector<SDValue, 8> Ops;
1200 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1201 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1202
1203 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1204 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001205 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001206 return Result;
1207 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001208
1209 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1210 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001211 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001212 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001213 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001214 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001215 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001216 void *IP = nullptr;
1217 SDNode *N = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001218 if ((N = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001219 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001220 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001221
Dan Gohman7a7742c2007-12-12 22:21:26 +00001222 if (!N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001223 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, DL.getDebugLoc(),
1224 EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001225 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001226 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001227 }
1228
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001229 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001230 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001231 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001232 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001233 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001234 }
1235 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001236}
1237
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001238SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) {
1239 return getConstant(Val, DL, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001240}
1241
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001242SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT,
1243 bool isTarget) {
1244 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001245}
1246
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001247SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT,
1248 bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001249 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001250
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001251 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001252
Chris Lattner381dddc2005-02-17 20:17:32 +00001253 // Do the map lookup using the actual bit pattern for the floating point
1254 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1255 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001256 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001257 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001258 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001259 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001260 void *IP = nullptr;
1261 SDNode *N = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001262 if ((N = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001263 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001264 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001265
Evan Cheng9458e6a2007-06-29 21:36:04 +00001266 if (!N) {
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001267 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, DL.getDebugLoc(),
1268 EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001269 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001270 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001271 }
1272
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001273 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001274 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001275 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001276 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001277 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001278 }
1279 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001280}
1281
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001282SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT,
1283 bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001284 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001285 if (EltVT==MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001286 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001287 else if (EltVT==MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001288 return getConstantFP(APFloat(Val), DL, VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001289 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1290 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001291 bool ignored;
1292 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001293 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001294 &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001295 return getConstantFP(apf, DL, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001296 } else
1297 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001298}
1299
Andrew Trickef9de2a2013-05-25 02:42:55 +00001300SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001301 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001302 bool isTargetGA,
1303 unsigned char TargetFlags) {
1304 assert((TargetFlags == 0 || isTargetGA) &&
1305 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001306
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001307 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001308 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001309 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001310 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001311
Chris Lattner8e34f982009-06-25 21:21:14 +00001312 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001313 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001314 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1315 else
1316 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001317
Jim Laskeyf576b422006-10-27 23:46:08 +00001318 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001319 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001320 ID.AddPointer(GV);
1321 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001322 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001323 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001324 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001325 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001326 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001327
Andrew Trickef9de2a2013-05-25 02:42:55 +00001328 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1329 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001330 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001331 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001332 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001333 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001334}
1335
Owen Anderson53aa7a92009-08-10 22:56:29 +00001336SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001337 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001338 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001339 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001340 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001341 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001342 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001343 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001344
Dan Gohman01c65a22010-03-18 18:49:47 +00001345 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001346 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001347 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001348 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001349}
1350
Owen Anderson53aa7a92009-08-10 22:56:29 +00001351SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001352 unsigned char TargetFlags) {
1353 assert((TargetFlags == 0 || isTarget) &&
1354 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001355 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001356 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001357 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001358 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001359 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001360 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001361 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001362 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001363
Dan Gohman01c65a22010-03-18 18:49:47 +00001364 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1365 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001366 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001367 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001368 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001369}
1370
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001371SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001372 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001373 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001374 unsigned char TargetFlags) {
1375 assert((TargetFlags == 0 || isTarget) &&
1376 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001377 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001378 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001379 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001380 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001381 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001382 ID.AddInteger(Alignment);
1383 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001384 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001385 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001386 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001387 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001388 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001389
Dan Gohman01c65a22010-03-18 18:49:47 +00001390 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1391 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001392 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001393 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001394 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001395}
1396
Chris Lattner061a1ea2005-01-07 07:46:32 +00001397
Owen Anderson53aa7a92009-08-10 22:56:29 +00001398SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001399 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001400 bool isTarget,
1401 unsigned char TargetFlags) {
1402 assert((TargetFlags == 0 || isTarget) &&
1403 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001404 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001405 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001406 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001407 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001408 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001409 ID.AddInteger(Alignment);
1410 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001411 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001412 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001413 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001414 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001415 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001416
Dan Gohman01c65a22010-03-18 18:49:47 +00001417 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1418 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001419 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001420 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001421 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001422}
1423
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001424SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1425 unsigned char TargetFlags) {
1426 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001427 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001428 ID.AddInteger(Index);
1429 ID.AddInteger(Offset);
1430 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001431 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001432 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001433 return SDValue(E, 0);
1434
1435 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1436 TargetFlags);
1437 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001438 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001439 return SDValue(N, 0);
1440}
1441
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001442SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001443 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001444 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001445 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001446 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001447 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001448 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001449
Dan Gohman01c65a22010-03-18 18:49:47 +00001450 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001451 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001452 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001453 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001454}
1455
Owen Anderson53aa7a92009-08-10 22:56:29 +00001456SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001457 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1458 ValueTypeNodes.size())
1459 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001460
Duncan Sands13237ac2008-06-06 12:08:01 +00001461 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001462 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001463
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001464 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001465 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001466 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001467 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001468}
1469
Owen Anderson53aa7a92009-08-10 22:56:29 +00001470SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001471 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001472 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001473 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001474 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001475 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001476}
1477
Rafael Espindola36b718f2015-06-22 17:46:53 +00001478SDValue SelectionDAG::getMCSymbol(MCSymbol *Sym, EVT VT) {
1479 SDNode *&N = MCSymbols[Sym];
1480 if (N)
1481 return SDValue(N, 0);
1482 N = new (NodeAllocator) MCSymbolSDNode(Sym, VT);
1483 InsertNode(N);
1484 return SDValue(N, 0);
1485}
1486
Owen Anderson53aa7a92009-08-10 22:56:29 +00001487SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001488 unsigned char TargetFlags) {
1489 SDNode *&N =
1490 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1491 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001492 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001493 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001494 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001495 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001496}
1497
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001498SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001499 if ((unsigned)Cond >= CondCodeNodes.size())
1500 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001501
Craig Topperc0196b12014-04-14 00:51:57 +00001502 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001503 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001504 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001505 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001506 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001507
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001508 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001509}
1510
Nate Begeman5f829d82009-04-29 05:20:52 +00001511// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1512// the shuffle mask M that point at N1 to point at N2, and indices that point
1513// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001514static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1515 std::swap(N1, N2);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001516 ShuffleVectorSDNode::commuteMask(M);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001517}
1518
Andrew Trickef9de2a2013-05-25 02:42:55 +00001519SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001520 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001521 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1522 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001523
1524 // Canonicalize shuffle undef, undef -> undef
1525 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001526 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001527
Eric Christopherdfda92b2009-08-22 00:40:45 +00001528 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001529 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001530 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001531 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001532 for (unsigned i = 0; i != NElts; ++i) {
1533 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001534 MaskVec.push_back(Mask[i]);
1535 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001536
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001537 // Canonicalize shuffle v, v -> v, undef
1538 if (N1 == N2) {
1539 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001540 for (unsigned i = 0; i != NElts; ++i)
1541 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001542 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001543
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001544 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1545 if (N1.getOpcode() == ISD::UNDEF)
1546 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001547
Chandler Carruth1b5285d2015-02-15 12:18:12 +00001548 // If shuffling a splat, try to blend the splat instead. We do this here so
1549 // that even when this arises during lowering we don't have to re-handle it.
1550 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1551 BitVector UndefElements;
1552 SDValue Splat = BV->getSplatValue(&UndefElements);
1553 if (!Splat)
1554 return;
1555
1556 for (int i = 0; i < (int)NElts; ++i) {
1557 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1558 continue;
1559
1560 // If this input comes from undef, mark it as such.
1561 if (UndefElements[MaskVec[i] - Offset]) {
1562 MaskVec[i] = -1;
1563 continue;
1564 }
1565
1566 // If we can blend a non-undef lane, use that instead.
1567 if (!UndefElements[i])
1568 MaskVec[i] = i + Offset;
1569 }
1570 };
1571 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1572 BlendSplat(N1BV, 0);
1573 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1574 BlendSplat(N2BV, NElts);
1575
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001576 // Canonicalize all index into lhs, -> shuffle lhs, undef
1577 // Canonicalize all index into rhs, -> shuffle rhs, undef
1578 bool AllLHS = true, AllRHS = true;
1579 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001580 for (unsigned i = 0; i != NElts; ++i) {
1581 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001582 if (N2Undef)
1583 MaskVec[i] = -1;
1584 else
1585 AllLHS = false;
1586 } else if (MaskVec[i] >= 0) {
1587 AllRHS = false;
1588 }
1589 }
1590 if (AllLHS && AllRHS)
1591 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001592 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001593 N2 = getUNDEF(VT);
1594 if (AllRHS) {
1595 N1 = getUNDEF(VT);
1596 commuteShuffle(N1, N2, MaskVec);
1597 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001598 // Reset our undef status after accounting for the mask.
1599 N2Undef = N2.getOpcode() == ISD::UNDEF;
1600 // Re-check whether both sides ended up undef.
1601 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1602 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001603
Craig Topper9a39b072013-08-08 08:03:12 +00001604 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001605 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001606 for (unsigned i = 0; i != NElts; ++i) {
1607 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001608 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001609 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001610 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001611 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001612
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001613 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001614 if (N2Undef) {
1615 SDValue V = N1;
1616
1617 // Look through any bitcasts. We check that these don't change the number
1618 // (and size) of elements and just changes their types.
1619 while (V.getOpcode() == ISD::BITCAST)
1620 V = V->getOperand(0);
1621
1622 // A splat should always show up as a build vector node.
1623 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001624 BitVector UndefElements;
1625 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001626 // If this is a splat of an undef, shuffling it is also undef.
1627 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1628 return getUNDEF(VT);
1629
Andrea Di Biagio83814752015-01-24 11:54:29 +00001630 bool SameNumElts =
1631 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1632
Chandler Carruth142e9662014-07-08 08:45:38 +00001633 // We only have a splat which can skip shuffles if there is a splatted
1634 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001635 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001636 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1637 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001638 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001639 return N1;
1640 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1641 if (C->isNullValue())
1642 return N1;
1643 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001644
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001645 // If the shuffle itself creates a splat, build the vector directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001646 if (AllSame && SameNumElts) {
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001647 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1648 SmallVector<SDValue, 8> Ops(NElts, Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001649
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001650 EVT BuildVT = BV->getValueType(0);
1651 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001652
Sanjay Patelab7e86e2015-02-17 16:54:32 +00001653 // We may have jumped through bitcasts, so the type of the
1654 // BUILD_VECTOR may not match the type of the shuffle.
1655 if (BuildVT != VT)
1656 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1657 return NewBV;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001658 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001659 }
1660 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001661
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001662 FoldingSetNodeID ID;
1663 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001664 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001665 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001666 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001667
Craig Topperc0196b12014-04-14 00:51:57 +00001668 void* IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001669 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001670 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001671
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001672 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1673 // SDNode doesn't have access to it. This memory will be "leaked" when
1674 // the node is deallocated, but recovered when the NodeAllocator is released.
1675 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1676 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001677
Dan Gohman01c65a22010-03-18 18:49:47 +00001678 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001679 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1680 dl.getDebugLoc(), N1, N2,
1681 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001682 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001683 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001684 return SDValue(N, 0);
1685}
1686
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001687SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1688 MVT VT = SV.getSimpleValueType(0);
Simon Pilgrim8c58c062015-03-07 22:33:11 +00001689 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1690 ShuffleVectorSDNode::commuteMask(MaskVec);
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001691
1692 SDValue Op0 = SV.getOperand(0);
1693 SDValue Op1 = SV.getOperand(1);
1694 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1695}
1696
Andrew Trickef9de2a2013-05-25 02:42:55 +00001697SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001698 SDValue Val, SDValue DTy,
1699 SDValue STy, SDValue Rnd, SDValue Sat,
1700 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001701 // If the src and dest types are the same and the conversion is between
1702 // integer types of the same sign or two floats, no conversion is necessary.
1703 if (DTy == STy &&
1704 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001705 return Val;
1706
1707 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001708 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001709 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001710 void* IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001711 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001712 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001713
Jack Carter170a5f22013-09-09 22:02:08 +00001714 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1715 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001716 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001717 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001718 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001719 return SDValue(N, 0);
1720}
1721
Owen Anderson53aa7a92009-08-10 22:56:29 +00001722SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001723 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001724 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001725 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001726 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001727 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001728 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001729
Dan Gohman01c65a22010-03-18 18:49:47 +00001730 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001731 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001732 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001733 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001734}
1735
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001736SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1737 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001738 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001739 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001740 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001741 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001742 return SDValue(E, 0);
1743
1744 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1745 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001746 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001747 return SDValue(N, 0);
1748}
1749
Andrew Trickef9de2a2013-05-25 02:42:55 +00001750SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001751 FoldingSetNodeID ID;
1752 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001753 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001754 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001755 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001756 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001757 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001758
Jack Carter170a5f22013-09-09 22:02:08 +00001759 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1760 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001761 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001762 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001763 return SDValue(N, 0);
1764}
1765
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001766
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001767SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001768 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001769 bool isTarget,
1770 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001771 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1772
1773 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001774 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001775 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001776 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001777 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001778 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001779 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001780 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001781
Michael Liaoabb87d42012-09-12 21:43:09 +00001782 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1783 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001784 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001785 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001786 return SDValue(N, 0);
1787}
1788
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001789SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001790 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001791 "SrcValue is not a pointer?");
1792
Jim Laskeyf576b422006-10-27 23:46:08 +00001793 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001794 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001795 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001796
Craig Topperc0196b12014-04-14 00:51:57 +00001797 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001798 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001799 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001800
Dan Gohman01c65a22010-03-18 18:49:47 +00001801 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001802 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001803 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001804 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001805}
1806
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001807/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1808SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1809 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001810 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001811 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001812
Craig Topperc0196b12014-04-14 00:51:57 +00001813 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001814 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001815 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001816
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001817 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1818 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001819 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001820 return SDValue(N, 0);
1821}
1822
Chandler Carruth502b23a2015-05-30 04:14:10 +00001823SDValue SelectionDAG::getBitcast(EVT VT, SDValue V) {
1824 if (VT == V.getValueType())
1825 return V;
1826
1827 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
1828}
1829
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001830/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1831SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1832 unsigned SrcAS, unsigned DestAS) {
1833 SDValue Ops[] = {Ptr};
1834 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001835 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001836 ID.AddInteger(SrcAS);
1837 ID.AddInteger(DestAS);
1838
Craig Topperc0196b12014-04-14 00:51:57 +00001839 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00001840 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001841 return SDValue(E, 0);
1842
1843 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1844 dl.getDebugLoc(),
1845 VT, Ptr, SrcAS, DestAS);
1846 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001847 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001848 return SDValue(N, 0);
1849}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001850
Duncan Sands41826032009-01-31 15:50:11 +00001851/// getShiftAmountOperand - Return the specified value casted to
1852/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001853SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001854 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001855 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001856 if (OpTy == ShTy || OpTy.isVector()) return Op;
1857
1858 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001859 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001860}
1861
Chris Lattner9eb7a822007-10-15 17:47:20 +00001862/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1863/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001864SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001865 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001866 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001867 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001868 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001869 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001870
David Greene1fbe0542009-11-12 20:49:22 +00001871 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001872 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001873}
1874
Duncan Sands445071c2008-12-09 21:33:20 +00001875/// CreateStackTemporary - Create a stack temporary suitable for holding
1876/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001877SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001878 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1879 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001880 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1881 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001882 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001883 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1884 TD->getPrefTypeAlignment(Ty2));
1885
1886 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001887 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001888 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001889}
1890
Owen Anderson53aa7a92009-08-10 22:56:29 +00001891SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001892 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001893 // These setcc operations always fold.
1894 switch (Cond) {
1895 default: break;
1896 case ISD::SETFALSE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001897 case ISD::SETFALSE2: return getConstant(0, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001898 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001899 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001900 TargetLowering::BooleanContent Cnt =
1901 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001902 return getConstant(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001903 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1904 VT);
Tim Northover950fcc02013-09-06 12:38:12 +00001905 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001906
Chris Lattner393d96a2006-04-27 05:01:07 +00001907 case ISD::SETOEQ:
1908 case ISD::SETOGT:
1909 case ISD::SETOGE:
1910 case ISD::SETOLT:
1911 case ISD::SETOLE:
1912 case ISD::SETONE:
1913 case ISD::SETO:
1914 case ISD::SETUO:
1915 case ISD::SETUEQ:
1916 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001917 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001918 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001919 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001920
Gabor Greiff304a7a2008-08-28 21:40:38 +00001921 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001922 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001923 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001924 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001925
Chris Lattner061a1ea2005-01-07 07:46:32 +00001926 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001927 default: llvm_unreachable("Unknown integer setcc!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001928 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT);
1929 case ISD::SETNE: return getConstant(C1 != C2, dl, VT);
1930 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1931 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1932 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1933 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1934 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT);
1935 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT);
1936 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT);
1937 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001938 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001939 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001940 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001941 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1942 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001943 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001944 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001945 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001946 case ISD::SETEQ: 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
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001949 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001950 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001951 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001952 // fall through
1953 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001954 R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001955 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001956 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001957 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001958 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001959 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001960 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001961 // fall through
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001962 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001963 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001964 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001965 // fall through
1966 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001967 R==APFloat::cmpEqual, dl, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001968 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001969 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001970 // fall through
1971 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001972 R==APFloat::cmpEqual, dl, VT);
1973 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1974 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001975 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001976 R==APFloat::cmpEqual, dl, VT);
1977 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001978 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001979 R==APFloat::cmpLessThan, dl, VT);
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001980 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001981 R==APFloat::cmpUnordered, dl, VT);
1982 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1983 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001984 }
1985 } else {
1986 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001987 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1988 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001989 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001990 return SDValue();
1991
1992 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001993 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001994 }
1995
Chris Lattnerd47675e2005-08-09 20:20:18 +00001996 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001997 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001998}
1999
Dan Gohman1f372ed2008-02-25 21:11:39 +00002000/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2001/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002002bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00002003 // This predicate is not safe for vector operations.
2004 if (Op.getValueType().isVector())
2005 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00002006
Dan Gohman1d459e42009-12-11 21:31:27 +00002007 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00002008 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
2009}
2010
Dan Gohman309d3d52007-06-22 14:59:07 +00002011/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2012/// this predicate to simplify operations downstream. Mask is known to be zero
2013/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002014bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00002015 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00002016 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002017 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002018 return (KnownZero & Mask) == Mask;
2019}
2020
Jay Foada0653a32014-05-14 21:14:37 +00002021/// Determine which bits of Op are known to be either zero or one and return
2022/// them in the KnownZero/KnownOne bitsets.
2023void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
2024 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002025 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00002026
Dan Gohmanf990faf2008-02-13 00:35:47 +00002027 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002028 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00002029 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002030
Dan Gohmanf990faf2008-02-13 00:35:47 +00002031 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00002032
2033 switch (Op.getOpcode()) {
2034 case ISD::Constant:
2035 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002036 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
2037 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00002038 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002039 case ISD::AND:
2040 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00002041 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2042 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002043
2044 // Output known-1 bits are only known if set in both the LHS & RHS.
2045 KnownOne &= KnownOne2;
2046 // Output known-0 are known to be clear if zero in either the LHS | RHS.
2047 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002048 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002049 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00002050 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2051 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002052
Dan Gohman309d3d52007-06-22 14:59:07 +00002053 // Output known-0 bits are only known if clear in both the LHS & RHS.
2054 KnownZero &= KnownZero2;
2055 // Output known-1 are known to be set if set in either the LHS | RHS.
2056 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00002057 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002058 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00002059 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2060 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002061
Dan Gohman309d3d52007-06-22 14:59:07 +00002062 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002063 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00002064 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2065 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2066 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00002067 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002068 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002069 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00002070 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2071 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002072
2073 // If low bits are zero in either operand, output low known-0 bits.
2074 // Also compute a conserative estimate for high known-0 bits.
2075 // More trickiness is possible, but this is sufficient for the
2076 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00002077 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00002078 unsigned TrailZ = KnownZero.countTrailingOnes() +
2079 KnownZero2.countTrailingOnes();
2080 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002081 KnownZero2.countLeadingOnes(),
2082 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002083
2084 TrailZ = std::min(TrailZ, BitWidth);
2085 LeadZ = std::min(LeadZ, BitWidth);
2086 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2087 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002088 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002089 }
2090 case ISD::UDIV: {
2091 // For the purposes of computing leading zeros we can conservatively
2092 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002093 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002094 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002095 unsigned LeadZ = KnownZero2.countLeadingOnes();
2096
Jay Foad25a5e4c2010-12-01 08:53:58 +00002097 KnownOne2.clearAllBits();
2098 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002099 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002100 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2101 if (RHSUnknownLeadingOnes != BitWidth)
2102 LeadZ = std::min(BitWidth,
2103 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002104
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002105 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002106 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002107 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002108 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002109 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2110 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002111
Dan Gohman309d3d52007-06-22 14:59:07 +00002112 // Only known if known in both the LHS and RHS.
2113 KnownOne &= KnownOne2;
2114 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002115 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002116 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002117 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2118 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002119
Dan Gohman309d3d52007-06-22 14:59:07 +00002120 // Only known if known in both the LHS and RHS.
2121 KnownOne &= KnownOne2;
2122 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002123 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002124 case ISD::SADDO:
2125 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002126 case ISD::SSUBO:
2127 case ISD::USUBO:
2128 case ISD::SMULO:
2129 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002130 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002131 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002132 // The boolean result conforms to getBooleanContents.
2133 // If we know the result of a setcc has the top bits zero, use this info.
2134 // We know that we have an integer-based boolean since these operations
2135 // are only available for integer.
2136 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2137 TargetLowering::ZeroOrOneBooleanContent &&
2138 BitWidth > 1)
2139 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2140 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002141 case ISD::SETCC:
2142 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002143 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2144 TargetLowering::ZeroOrOneBooleanContent &&
2145 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002146 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002147 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002148 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002149 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002150 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002151 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002152
2153 // If the shift count is an invalid immediate, don't do anything.
2154 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002155 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002156
Jay Foada0653a32014-05-14 21:14:37 +00002157 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002158 KnownZero <<= ShAmt;
2159 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002160 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002161 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002162 }
Jay Foad5a29c362014-05-15 12:12:55 +00002163 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002164 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002165 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002166 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002167 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002168
Dan Gohman9db0aa82008-02-26 18:50:50 +00002169 // If the shift count is an invalid immediate, don't do anything.
2170 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002171 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002172
Jay Foada0653a32014-05-14 21:14:37 +00002173 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002174 KnownZero = KnownZero.lshr(ShAmt);
2175 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002176
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002177 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002178 KnownZero |= HighBits; // High bits known zero.
2179 }
Jay Foad5a29c362014-05-15 12:12:55 +00002180 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002181 case ISD::SRA:
2182 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002183 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002184
Dan Gohman9db0aa82008-02-26 18:50:50 +00002185 // If the shift count is an invalid immediate, don't do anything.
2186 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002187 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002188
Dan Gohman309d3d52007-06-22 14:59:07 +00002189 // If any of the demanded bits are produced by the sign extension, we also
2190 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002191 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002192
Jay Foada0653a32014-05-14 21:14:37 +00002193 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002194 KnownZero = KnownZero.lshr(ShAmt);
2195 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002196
Dan Gohman309d3d52007-06-22 14:59:07 +00002197 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002198 APInt SignBit = APInt::getSignBit(BitWidth);
2199 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002200
Dan Gohmanb717fda2008-02-20 16:30:17 +00002201 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002202 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002203 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002204 KnownOne |= HighBits; // New bits are known one.
2205 }
2206 }
Jay Foad5a29c362014-05-15 12:12:55 +00002207 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002208 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002209 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002210 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002211
2212 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002213 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002214 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002215
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002216 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002217 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002218
Dan Gohman309d3d52007-06-22 14:59:07 +00002219 // If the sign extended bits are demanded, we know that the sign
2220 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002221 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002222 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002223 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002224
Jay Foada0653a32014-05-14 21:14:37 +00002225 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002226 KnownOne &= InputDemandedBits;
2227 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002228
Dan Gohman309d3d52007-06-22 14:59:07 +00002229 // If the sign bit of the input is known set or clear, then we know the
2230 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002231 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002232 KnownZero |= NewBits;
2233 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002234 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002235 KnownOne |= NewBits;
2236 KnownZero &= ~NewBits;
2237 } else { // Input sign bit unknown
2238 KnownZero &= ~NewBits;
2239 KnownOne &= ~NewBits;
2240 }
Jay Foad5a29c362014-05-15 12:12:55 +00002241 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002242 }
2243 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002244 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002245 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002246 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002247 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002248 unsigned LowBits = Log2_32(BitWidth)+1;
2249 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002250 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002251 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002252 }
2253 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002254 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002255 // If this is a ZEXTLoad and we are looking at the loaded value.
2256 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002257 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002258 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002259 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002260 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002261 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002262 }
Jay Foad5a29c362014-05-15 12:12:55 +00002263 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002264 }
2265 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002266 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002267 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002268 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002269 KnownZero = KnownZero.trunc(InBits);
2270 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002271 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002272 KnownZero = KnownZero.zext(BitWidth);
2273 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002274 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002275 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002276 }
2277 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002278 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002279 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002280 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002281
Jay Foad583abbc2010-12-07 08:25:19 +00002282 KnownZero = KnownZero.trunc(InBits);
2283 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002284 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002285
2286 // Note if the sign bit is known to be zero or one.
2287 bool SignBitKnownZero = KnownZero.isNegative();
2288 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002289
Jay Foad583abbc2010-12-07 08:25:19 +00002290 KnownZero = KnownZero.zext(BitWidth);
2291 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002292
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002293 // If the sign bit is known zero or one, the top bits match.
2294 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002295 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002296 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002297 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002298 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002299 }
2300 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002301 EVT InVT = Op.getOperand(0).getValueType();
2302 unsigned InBits = InVT.getScalarType().getSizeInBits();
2303 KnownZero = KnownZero.trunc(InBits);
2304 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002305 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002306 KnownZero = KnownZero.zext(BitWidth);
2307 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002308 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002309 }
2310 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002311 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002312 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002313 KnownZero = KnownZero.zext(InBits);
2314 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002315 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002316 KnownZero = KnownZero.trunc(BitWidth);
2317 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002318 break;
2319 }
2320 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002321 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002322 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002323 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002324 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002325 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002326 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002327 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002328 case ISD::FGETSIGN:
2329 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002330 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002331 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002332
Dan Gohman72ec3f42008-04-28 17:02:21 +00002333 case ISD::SUB: {
2334 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2335 // We know that the top bits of C-X are clear if X contains less bits
2336 // than C (i.e. no wrap-around can happen). For example, 20-X is
2337 // positive if we can prove that X is >= 0 and < 16.
2338 if (CLHS->getAPIntValue().isNonNegative()) {
2339 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2340 // NLZ can't be BitWidth with no sign bit
2341 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002342 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002343
2344 // If all of the MaskV bits are known to be zero, then we know the
2345 // output top bits are zero, because we now know that the output is
2346 // from [0-C].
2347 if ((KnownZero2 & MaskV) == MaskV) {
2348 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2349 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002350 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002351 }
2352 }
2353 }
2354 }
2355 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002356 case ISD::ADD:
2357 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002358 // Output known-0 bits are known if clear or set in both the low clear bits
2359 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2360 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002361 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002362 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2363
Jay Foada0653a32014-05-14 21:14:37 +00002364 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002365 KnownZeroOut = std::min(KnownZeroOut,
2366 KnownZero2.countTrailingOnes());
2367
Chris Lattner440b2802010-12-19 20:38:28 +00002368 if (Op.getOpcode() == ISD::ADD) {
2369 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002370 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002371 }
2372
2373 // With ADDE, a carry bit may be added in, so we can only use this
2374 // information if we know (at least) that the low two bits are clear. We
2375 // then return to the caller that the low bit is unknown but that other bits
2376 // are known zero.
2377 if (KnownZeroOut >= 2) // ADDE
2378 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002379 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002380 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002381 case ISD::SREM:
2382 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002383 const APInt &RA = Rem->getAPIntValue().abs();
2384 if (RA.isPowerOf2()) {
2385 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002386 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002387
Duncan Sands33274982010-01-29 09:45:26 +00002388 // The low bits of the first operand are unchanged by the srem.
2389 KnownZero = KnownZero2 & LowBits;
2390 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002391
Duncan Sands33274982010-01-29 09:45:26 +00002392 // If the first operand is non-negative or has all low bits zero, then
2393 // the upper bits are all zero.
2394 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2395 KnownZero |= ~LowBits;
2396
2397 // If the first operand is negative and not all low bits are zero, then
2398 // the upper bits are all one.
2399 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2400 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002401 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002402 }
2403 }
Jay Foad5a29c362014-05-15 12:12:55 +00002404 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002405 case ISD::UREM: {
2406 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002407 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002408 if (RA.isPowerOf2()) {
2409 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002410 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2411
2412 // The upper bits are all zero, the lower ones are unchanged.
2413 KnownZero = KnownZero2 | ~LowBits;
2414 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002415 break;
2416 }
2417 }
2418
2419 // Since the result is less than or equal to either operand, any leading
2420 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002421 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2422 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002423
2424 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2425 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002426 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002427 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002428 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002429 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002430 case ISD::EXTRACT_ELEMENT: {
2431 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2432 const unsigned Index =
2433 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2434 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2435
2436 // Remove low part of known bits mask
2437 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2438 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2439
2440 // Remove high part of known bit mask
2441 KnownZero = KnownZero.trunc(BitWidth);
2442 KnownOne = KnownOne.trunc(BitWidth);
2443 break;
2444 }
Matt Arsenault705eb8f2015-06-09 00:52:41 +00002445 case ISD::SMIN:
2446 case ISD::SMAX:
2447 case ISD::UMIN:
2448 case ISD::UMAX: {
2449 APInt Op0Zero, Op0One;
2450 APInt Op1Zero, Op1One;
2451 computeKnownBits(Op.getOperand(0), Op0Zero, Op0One, Depth);
2452 computeKnownBits(Op.getOperand(1), Op1Zero, Op1One, Depth);
2453
2454 KnownZero = Op0Zero & Op1Zero;
2455 KnownOne = Op0One & Op1One;
2456 break;
2457 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002458 case ISD::FrameIndex:
2459 case ISD::TargetFrameIndex:
2460 if (unsigned Align = InferPtrAlignment(Op)) {
2461 // The low bits are known zero if the pointer is aligned.
2462 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002463 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002464 }
2465 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002466
Dan Gohman309d3d52007-06-22 14:59:07 +00002467 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002468 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2469 break;
2470 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002471 case ISD::INTRINSIC_WO_CHAIN:
2472 case ISD::INTRINSIC_W_CHAIN:
2473 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002474 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002475 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002476 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002477 }
Jay Foad5a29c362014-05-15 12:12:55 +00002478
2479 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002480}
2481
2482/// ComputeNumSignBits - Return the number of times the sign bit of the
2483/// register is replicated into the other bits. We know that at least 1 bit
2484/// is always equal to the sign bit (itself), but other cases can give us
2485/// information. For example, immediately after an "SRA X, 2", we know that
2486/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002487unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002488 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002489 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002490 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002491 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002492 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002493
Dan Gohman309d3d52007-06-22 14:59:07 +00002494 if (Depth == 6)
2495 return 1; // Limit search depth.
2496
2497 switch (Op.getOpcode()) {
2498 default: break;
2499 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002500 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002501 return VTBits-Tmp+1;
2502 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002503 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002504 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002505
Dan Gohman309d3d52007-06-22 14:59:07 +00002506 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002507 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002508 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002509 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002510
Dan Gohman309d3d52007-06-22 14:59:07 +00002511 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002512 Tmp =
2513 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002514 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002515
Dan Gohman309d3d52007-06-22 14:59:07 +00002516 case ISD::SIGN_EXTEND_INREG:
2517 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002518 Tmp =
2519 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002520 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002521
Dan Gohman309d3d52007-06-22 14:59:07 +00002522 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2523 return std::max(Tmp, Tmp2);
2524
2525 case ISD::SRA:
2526 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2527 // SRA X, C -> adds C sign bits.
2528 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002529 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002530 if (Tmp > VTBits) Tmp = VTBits;
2531 }
2532 return Tmp;
2533 case ISD::SHL:
2534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2535 // shl destroys sign bits.
2536 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002537 if (C->getZExtValue() >= VTBits || // Bad shift.
2538 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2539 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002540 }
2541 break;
2542 case ISD::AND:
2543 case ISD::OR:
2544 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002545 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002546 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002547 if (Tmp != 1) {
2548 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2549 FirstAnswer = std::min(Tmp, Tmp2);
2550 // We computed what we know about the sign bits as our first
2551 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002552 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002553 }
2554 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002555
2556 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002557 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002558 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002559 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002560 return std::min(Tmp, Tmp2);
Matt Arsenault705eb8f2015-06-09 00:52:41 +00002561 case ISD::SMIN:
2562 case ISD::SMAX:
2563 case ISD::UMIN:
2564 case ISD::UMAX:
2565 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
2566 if (Tmp == 1)
2567 return 1; // Early out.
2568 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
2569 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002570 case ISD::SADDO:
2571 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002572 case ISD::SSUBO:
2573 case ISD::USUBO:
2574 case ISD::SMULO:
2575 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002576 if (Op.getResNo() != 1)
2577 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002578 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002579 // If setcc returns 0/-1, all bits are sign bits.
2580 // We know that we have an integer-based boolean since these operations
2581 // are only available for integer.
2582 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2583 TargetLowering::ZeroOrNegativeOneBooleanContent)
2584 return VTBits;
2585 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002586 case ISD::SETCC:
2587 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002588 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002589 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002590 return VTBits;
2591 break;
2592 case ISD::ROTL:
2593 case ISD::ROTR:
2594 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002595 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002596
Dan Gohman309d3d52007-06-22 14:59:07 +00002597 // Handle rotate right by N like a rotate left by 32-N.
2598 if (Op.getOpcode() == ISD::ROTR)
2599 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2600
2601 // If we aren't rotating out all of the known-in sign bits, return the
2602 // number that are left. This handles rotl(sext(x), 1) for example.
2603 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2604 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2605 }
2606 break;
2607 case ISD::ADD:
2608 // Add can have at most one carry bit. Thus we know that the output
2609 // is, at worst, one more bit than the inputs.
2610 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2611 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002612
Dan Gohman309d3d52007-06-22 14:59:07 +00002613 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002614 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002615 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002616 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002617 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002618
Dan Gohman309d3d52007-06-22 14:59:07 +00002619 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2620 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002621 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002622 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002623
Dan Gohman309d3d52007-06-22 14:59:07 +00002624 // If we are subtracting one from a positive number, there is no carry
2625 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002626 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002627 return Tmp;
2628 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002629
Dan Gohman309d3d52007-06-22 14:59:07 +00002630 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2631 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002632 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002633
Dan Gohman309d3d52007-06-22 14:59:07 +00002634 case ISD::SUB:
2635 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2636 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002637
Dan Gohman309d3d52007-06-22 14:59:07 +00002638 // Handle NEG.
2639 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002640 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002641 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002642 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002643 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2644 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002645 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002646 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002647
Dan Gohman309d3d52007-06-22 14:59:07 +00002648 // If the input is known to be positive (the sign bit is known clear),
2649 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002650 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002651 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002652
Dan Gohman309d3d52007-06-22 14:59:07 +00002653 // Otherwise, we treat this like a SUB.
2654 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002655
Dan Gohman309d3d52007-06-22 14:59:07 +00002656 // Sub can have at most one carry bit. Thus we know that the output
2657 // is, at worst, one more bit than the inputs.
2658 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2659 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002660 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002661 case ISD::TRUNCATE:
2662 // FIXME: it's tricky to do anything useful for this, but it is an important
2663 // case for targets like X86.
2664 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002665 case ISD::EXTRACT_ELEMENT: {
2666 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2667 const int BitWidth = Op.getValueType().getSizeInBits();
2668 const int Items =
2669 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2670
2671 // Get reverse index (starting from 1), Op1 value indexes elements from
2672 // little end. Sign starts at big end.
2673 const int rIndex = Items - 1 -
2674 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2675
2676 // If the sign portion ends in our element the substraction gives correct
2677 // result. Otherwise it gives either negative or > bitwidth result
2678 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2679 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002680 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002681
Nadav Rotem4536d582013-03-20 22:53:44 +00002682 // If we are looking at the loaded value of the SDNode.
2683 if (Op.getResNo() == 0) {
2684 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2685 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2686 unsigned ExtType = LD->getExtensionType();
2687 switch (ExtType) {
2688 default: break;
2689 case ISD::SEXTLOAD: // '17' bits known
2690 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2691 return VTBits-Tmp+1;
2692 case ISD::ZEXTLOAD: // '16' bits known
2693 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2694 return VTBits-Tmp;
2695 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002696 }
2697 }
2698
2699 // Allow the target to implement this method for its nodes.
2700 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002701 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002702 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2703 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002704 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002705 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002706 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002707
Dan Gohman309d3d52007-06-22 14:59:07 +00002708 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2709 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002710 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002711 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002712
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002713 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002714 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002715 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002716 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002717 Mask = KnownOne;
2718 } else {
2719 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002720 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002721 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002722
Dan Gohman309d3d52007-06-22 14:59:07 +00002723 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2724 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002725 Mask = ~Mask;
2726 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002727 // Return # leading zeros. We use 'min' here in case Val was zero before
2728 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002729 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002730}
2731
Chris Lattner46c01a32011-02-13 22:25:43 +00002732/// isBaseWithConstantOffset - Return true if the specified operand is an
2733/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2734/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2735/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002736/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002737bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2738 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2739 !isa<ConstantSDNode>(Op.getOperand(1)))
2740 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002741
2742 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002743 !MaskedValueIsZero(Op.getOperand(0),
2744 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2745 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002746
Chris Lattner46c01a32011-02-13 22:25:43 +00002747 return true;
2748}
2749
2750
Dan Gohmand0d5e682009-09-03 20:34:31 +00002751bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2752 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002753 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002754 return true;
2755
2756 // If the value is a constant, we can obviously see if it is a NaN or not.
2757 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2758 return !C->getValueAPF().isNaN();
2759
2760 // TODO: Recognize more cases here.
2761
2762 return false;
2763}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002764
Dan Gohman38605212010-02-24 06:52:40 +00002765bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2766 // If the value is a constant, we can obviously see if it is a zero or not.
2767 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2768 return !C->isZero();
2769
2770 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002771 switch (Op.getOpcode()) {
2772 default: break;
2773 case ISD::OR:
2774 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2775 return !C->isNullValue();
2776 break;
2777 }
Dan Gohman38605212010-02-24 06:52:40 +00002778
2779 return false;
2780}
2781
2782bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2783 // Check the obvious case.
2784 if (A == B) return true;
2785
2786 // For for negative and positive zero.
2787 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2788 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2789 if (CA->isZero() && CB->isZero()) return true;
2790
2791 // Otherwise they may not be equal.
2792 return false;
2793}
2794
Chris Lattner061a1ea2005-01-07 07:46:32 +00002795/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002796///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002797SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002798 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002799 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002800 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00002801 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002802 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002803
Jack Carter170a5f22013-09-09 22:02:08 +00002804 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2805 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002806 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002807
Chandler Carruth41b20e72014-07-22 04:07:55 +00002808 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002809 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002810}
2811
Andrew Trickef9de2a2013-05-25 02:42:55 +00002812SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002813 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002814 // Constant fold unary operations with an integer constant operand. Even
2815 // opaque constant will be folded, because the folding of unary operations
2816 // doesn't create new constants with different values. Nevertheless, the
2817 // opaque flag is preserved during folding to prevent future folding with
2818 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002819 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002820 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002821 switch (Opcode) {
2822 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002823 case ISD::SIGN_EXTEND:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002824 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002825 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002826 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002827 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002828 case ISD::TRUNCATE:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002829 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002830 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002831 case ISD::UINT_TO_FP:
2832 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002833 APFloat apf(EVTToAPFloatSemantics(VT),
2834 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002835 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002836 Opcode==ISD::SINT_TO_FP,
2837 APFloat::rmNearestTiesToEven);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002838 return getConstantFP(apf, DL, VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002839 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002840 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002841 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002842 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002843 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002844 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002845 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002846 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002847 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002848 case ISD::BSWAP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002849 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002850 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002851 case ISD::CTPOP:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002852 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002853 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002854 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002855 case ISD::CTLZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002856 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002857 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002858 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002859 case ISD::CTTZ_ZERO_UNDEF:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002860 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002861 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002862 }
2863 }
2864
Dale Johannesen446b9002007-08-31 23:34:27 +00002865 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002866 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002867 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002868 switch (Opcode) {
2869 case ISD::FNEG:
2870 V.changeSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002871 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002872 case ISD::FABS:
2873 V.clearSign();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002874 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002875 case ISD::FCEIL: {
2876 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2877 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002878 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002879 break;
2880 }
2881 case ISD::FTRUNC: {
2882 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2883 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002884 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002885 break;
2886 }
2887 case ISD::FFLOOR: {
2888 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2889 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002890 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002891 break;
2892 }
2893 case ISD::FP_EXTEND: {
2894 bool ignored;
2895 // This can return overflow, underflow, or inexact; we don't care.
2896 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002897 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002898 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002899 return getConstantFP(V, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002900 }
2901 case ISD::FP_TO_SINT:
2902 case ISD::FP_TO_UINT: {
2903 integerPart x[2];
2904 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002905 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002906 // FIXME need to be more flexible about rounding mode.
2907 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2908 Opcode==ISD::FP_TO_SINT,
2909 APFloat::rmTowardZero, &ignored);
2910 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002911 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002912 APInt api(VT.getSizeInBits(), x);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002913 return getConstant(api, DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002914 }
2915 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002916 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002917 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Owen Anderson558012a2014-12-09 06:50:39 +00002918 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002919 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002920 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002921 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002922 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002923 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002924 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002925
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002926 // Constant fold unary operations with a vector integer or float operand.
Jim Grosbachf7502c42014-07-18 00:40:52 +00002927 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002928 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002929 switch (Opcode) {
2930 default:
2931 // FIXME: Entirely reasonable to perform folding of other unary
2932 // operations here as the need arises.
2933 break;
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002934 case ISD::FNEG:
2935 case ISD::FABS:
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002936 case ISD::FCEIL:
2937 case ISD::FTRUNC:
2938 case ISD::FFLOOR:
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002939 case ISD::FP_EXTEND:
Simon Pilgrim017ca192015-05-02 13:04:07 +00002940 case ISD::FP_TO_SINT:
2941 case ISD::FP_TO_UINT:
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002942 case ISD::TRUNCATE:
Jim Grosbachf7502c42014-07-18 00:40:52 +00002943 case ISD::UINT_TO_FP:
Simon Pilgrim68cd2372015-06-07 15:37:14 +00002944 case ISD::SINT_TO_FP:
Simon Pilgrim011381d2015-06-13 14:08:15 +00002945 case ISD::BSWAP:
Simon Pilgrim4791f6d2015-06-08 16:19:00 +00002946 case ISD::CTLZ:
2947 case ISD::CTLZ_ZERO_UNDEF:
Simon Pilgrimc789e1d2015-06-08 09:57:09 +00002948 case ISD::CTTZ:
2949 case ISD::CTTZ_ZERO_UNDEF:
Simon Pilgrim68cd2372015-06-07 15:37:14 +00002950 case ISD::CTPOP: {
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002951 EVT SVT = VT.getScalarType();
2952 EVT InVT = BV->getValueType(0);
2953 EVT InSVT = InVT.getScalarType();
2954
Simon Pilgrime09584c2015-05-10 14:14:51 +00002955 // Find legal integer scalar type for constant promotion and
2956 // ensure that its scalar size is at least as large as source.
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002957 EVT LegalSVT = SVT;
2958 if (SVT.isInteger()) {
2959 LegalSVT = TLI->getTypeToTransformTo(*getContext(), SVT);
Simon Pilgrime09584c2015-05-10 14:14:51 +00002960 if (LegalSVT.bitsLT(SVT)) break;
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002961 }
2962
Simon Pilgrim481f4142015-03-23 22:44:55 +00002963 // Let the above scalar folding handle the folding of each element.
Jim Grosbach19dd3082014-07-23 20:41:31 +00002964 SmallVector<SDValue, 8> Ops;
2965 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2966 SDValue OpN = BV->getOperand(i);
Simon Pilgrim9fb06bc2015-05-01 08:20:04 +00002967 EVT OpVT = OpN.getValueType();
2968
2969 // Build vector (integer) scalar operands may need implicit
2970 // truncation - do this before constant folding.
2971 if (OpVT.isInteger() && OpVT.bitsGT(InSVT))
2972 OpN = getNode(ISD::TRUNCATE, DL, InSVT, OpN);
2973
2974 OpN = getNode(Opcode, DL, SVT, OpN);
2975
2976 // Legalize the (integer) scalar constant if necessary.
2977 if (LegalSVT != SVT)
2978 OpN = getNode(ISD::ANY_EXTEND, DL, LegalSVT, OpN);
2979
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002980 if (OpN.getOpcode() != ISD::UNDEF &&
2981 OpN.getOpcode() != ISD::Constant &&
2982 OpN.getOpcode() != ISD::ConstantFP)
2983 break;
Jim Grosbach19dd3082014-07-23 20:41:31 +00002984 Ops.push_back(OpN);
2985 }
Simon Pilgrim09f3ff92015-03-25 22:30:31 +00002986 if (Ops.size() == VT.getVectorNumElements())
2987 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Simon Pilgrim07e063e2015-04-06 17:15:41 +00002988 break;
Jim Grosbachf7502c42014-07-18 00:40:52 +00002989 }
2990 }
2991 }
2992 }
2993
Gabor Greiff304a7a2008-08-28 21:40:38 +00002994 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002995 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002996 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002997 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002998 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002999 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00003000 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00003001 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003002 assert(VT.isFloatingPoint() &&
3003 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00003004 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00003005 assert((!VT.isVector() ||
3006 VT.getVectorNumElements() ==
3007 Operand.getValueType().getVectorNumElements()) &&
3008 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00003009 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003010 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00003011 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00003012 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003013 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00003014 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003015 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00003016 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
3017 "Invalid sext node, dst < src!");
3018 assert((!VT.isVector() ||
3019 VT.getVectorNumElements() ==
3020 Operand.getValueType().getVectorNumElements()) &&
3021 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00003022 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
3023 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3024 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00003025 // sext(undef) = 0, because the top bits will all be the same.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003026 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003027 break;
3028 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003029 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00003030 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003031 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00003032 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
3033 "Invalid zext node, dst < src!");
3034 assert((!VT.isVector() ||
3035 VT.getVectorNumElements() ==
3036 Operand.getValueType().getVectorNumElements()) &&
3037 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00003038 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00003039 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00003040 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00003041 else if (OpOpcode == ISD::UNDEF)
3042 // zext(undef) = 0, because the top bits will be zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003043 return getConstant(0, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003044 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00003045 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003046 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00003047 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00003048 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00003049 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
3050 "Invalid anyext node, dst < src!");
3051 assert((!VT.isVector() ||
3052 VT.getVectorNumElements() ==
3053 Operand.getValueType().getVectorNumElements()) &&
3054 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00003055
Dan Gohman08837892010-06-18 00:08:30 +00003056 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3057 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00003058 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00003059 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00003060 else if (OpOpcode == ISD::UNDEF)
3061 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00003062
3063 // (ext (trunx x)) -> x
3064 if (OpOpcode == ISD::TRUNCATE) {
3065 SDValue OpOp = Operand.getNode()->getOperand(0);
3066 if (OpOp.getValueType() == VT)
3067 return OpOp;
3068 }
Chris Lattner8c393c22005-09-02 00:17:32 +00003069 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003070 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00003071 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00003072 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003073 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00003074 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
3075 "Invalid truncate node, src < dst!");
3076 assert((!VT.isVector() ||
3077 VT.getVectorNumElements() ==
3078 Operand.getValueType().getVectorNumElements()) &&
3079 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00003080 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00003081 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003082 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3083 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00003084 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00003085 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
3086 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00003087 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003088 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00003089 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00003090 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00003091 }
Craig Topper201c1a32012-01-15 01:05:11 +00003092 if (OpOpcode == ISD::UNDEF)
3093 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003094 break;
Simon Pilgrim2c35e7a2015-06-13 15:23:58 +00003095 case ISD::BSWAP:
3096 assert(VT.isInteger() && VT == Operand.getValueType() &&
3097 "Invalid BSWAP!");
3098 assert((VT.getScalarSizeInBits() % 16 == 0) &&
3099 "BSWAP types must be a multiple of 16 bits!");
3100 if (OpOpcode == ISD::UNDEF)
3101 return getUNDEF(VT);
3102 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003103 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00003104 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00003105 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00003106 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00003107 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00003108 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
3109 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00003110 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003111 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003112 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003113 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003114 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00003115 (VT.getVectorElementType() == Operand.getValueType() ||
3116 (VT.getVectorElementType().isInteger() &&
3117 Operand.getValueType().isInteger() &&
3118 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003119 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00003120 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003121 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00003122 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3123 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3124 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3125 Operand.getConstantOperandVal(1) == 0 &&
3126 Operand.getOperand(0).getValueType() == VT)
3127 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003128 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00003129 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00003130 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003131 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00003132 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00003133 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003134 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00003135 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00003136 break;
3137 case ISD::FABS:
3138 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00003139 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00003140 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003141 }
3142
Chris Lattnerf9c19152005-08-25 19:12:10 +00003143 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003144 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003145 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00003146 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003147 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00003148 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003149 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00003150 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003151 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003152
Jack Carter170a5f22013-09-09 22:02:08 +00003153 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3154 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003155 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003156 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003157 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3158 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003159 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003160
Chandler Carruth41b20e72014-07-22 04:07:55 +00003161 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003162 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003163}
3164
Matthias Braun20683ef2015-05-19 01:40:21 +00003165static std::pair<APInt, bool> FoldValue(unsigned Opcode, const APInt &C1,
3166 const APInt &C2) {
3167 switch (Opcode) {
3168 case ISD::ADD: return std::make_pair(C1 + C2, true);
3169 case ISD::SUB: return std::make_pair(C1 - C2, true);
3170 case ISD::MUL: return std::make_pair(C1 * C2, true);
3171 case ISD::AND: return std::make_pair(C1 & C2, true);
3172 case ISD::OR: return std::make_pair(C1 | C2, true);
3173 case ISD::XOR: return std::make_pair(C1 ^ C2, true);
3174 case ISD::SHL: return std::make_pair(C1 << C2, true);
3175 case ISD::SRL: return std::make_pair(C1.lshr(C2), true);
3176 case ISD::SRA: return std::make_pair(C1.ashr(C2), true);
3177 case ISD::ROTL: return std::make_pair(C1.rotl(C2), true);
3178 case ISD::ROTR: return std::make_pair(C1.rotr(C2), true);
3179 case ISD::UDIV:
3180 if (!C2.getBoolValue())
3181 break;
3182 return std::make_pair(C1.udiv(C2), true);
3183 case ISD::UREM:
3184 if (!C2.getBoolValue())
3185 break;
3186 return std::make_pair(C1.urem(C2), true);
3187 case ISD::SDIV:
3188 if (!C2.getBoolValue())
3189 break;
3190 return std::make_pair(C1.sdiv(C2), true);
3191 case ISD::SREM:
3192 if (!C2.getBoolValue())
3193 break;
3194 return std::make_pair(C1.srem(C2), true);
3195 }
3196 return std::make_pair(APInt(1, 0), false);
3197}
3198
3199SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
3200 const ConstantSDNode *Cst1,
3201 const ConstantSDNode *Cst2) {
3202 if (Cst1->isOpaque() || Cst2->isOpaque())
3203 return SDValue();
3204
3205 std::pair<APInt, bool> Folded = FoldValue(Opcode, Cst1->getAPIntValue(),
3206 Cst2->getAPIntValue());
3207 if (!Folded.second)
3208 return SDValue();
3209 return getConstant(Folded.first, DL, VT);
3210}
3211
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003212SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003213 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003214 // If the opcode is a target-specific ISD node, there's nothing we can
3215 // do here and the operand rules may not line up with the below, so
3216 // bail early.
3217 if (Opcode >= ISD::BUILTIN_OP_END)
3218 return SDValue();
3219
Matthias Braun20683ef2015-05-19 01:40:21 +00003220 // Handle the case of two scalars.
3221 if (const ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1)) {
3222 if (const ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2)) {
3223 if (SDValue Folded =
3224 FoldConstantArithmetic(Opcode, DL, VT, Scalar1, Scalar2)) {
3225 if (!VT.isVector())
3226 return Folded;
3227 SmallVector<SDValue, 4> Outputs;
3228 // We may have a vector type but a scalar result. Create a splat.
3229 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3230 // Build a big vector out of the scalar elements we generated.
3231 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
3232 } else {
3233 return SDValue();
3234 }
3235 }
3236 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003237
Matthias Braun20683ef2015-05-19 01:40:21 +00003238 // For vectors extract each constant element into Inputs so we can constant
3239 // fold them individually.
3240 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3241 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3242 if (!BV1 || !BV2)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003243 return SDValue();
3244
Matthias Braun20683ef2015-05-19 01:40:21 +00003245 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3246
3247 EVT SVT = VT.getScalarType();
3248 SmallVector<SDValue, 4> Outputs;
3249 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3250 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3251 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3252 if (!V1 || !V2) // Not a constant, bail.
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003253 return SDValue();
3254
Matthias Braun20683ef2015-05-19 01:40:21 +00003255 if (V1->isOpaque() || V2->isOpaque())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003256 return SDValue();
Matthias Braun20683ef2015-05-19 01:40:21 +00003257
3258 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3259 // FIXME: This is valid and could be handled by truncating the APInts.
3260 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3261 return SDValue();
3262
3263 // Fold one vector element.
3264 std::pair<APInt, bool> Folded = FoldValue(Opcode, V1->getAPIntValue(),
3265 V2->getAPIntValue());
3266 if (!Folded.second)
3267 return SDValue();
3268 Outputs.push_back(getConstant(Folded.first, DL, SVT));
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003269 }
3270
Matthias Braun20683ef2015-05-19 01:40:21 +00003271 assert(VT.getVectorNumElements() == Outputs.size() &&
3272 "Vector size mismatch!");
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003273
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003274 // We may have a vector type but a scalar result. Create a splat.
3275 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3276
3277 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003278 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003279}
3280
Andrew Trickef9de2a2013-05-25 02:42:55 +00003281SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Sanjay Patelf1340482015-06-16 16:25:43 +00003282 SDValue N2, const SDNodeFlags *Flags) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003283 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3284 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003285 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003286 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003287 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003288 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3289 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003290 // Fold trivial token factors.
3291 if (N1.getOpcode() == ISD::EntryToken) return N2;
3292 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003293 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003294 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003295 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003296 // Concat of UNDEFs is UNDEF.
3297 if (N1.getOpcode() == ISD::UNDEF &&
3298 N2.getOpcode() == ISD::UNDEF)
3299 return getUNDEF(VT);
3300
Dan Gohman550c9af2008-08-14 20:04:46 +00003301 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3302 // one big BUILD_VECTOR.
3303 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3304 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003305 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3306 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003307 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Simon Pilgrim860f0872015-04-21 08:05:43 +00003308
3309 // BUILD_VECTOR requires all inputs to be of the same type, find the
3310 // maximum type and extend them all.
3311 EVT SVT = VT.getScalarType();
3312 for (SDValue Op : Elts)
3313 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
3314 if (SVT.bitsGT(VT.getScalarType()))
3315 for (SDValue &Op : Elts)
3316 Op = TLI->isZExtFree(Op.getValueType(), SVT)
3317 ? getZExtOrTrunc(Op, DL, SVT)
3318 : getSExtOrTrunc(Op, DL, SVT);
3319
Craig Topper48d114b2014-04-26 18:35:24 +00003320 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003321 }
3322 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003323 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003324 assert(VT.isInteger() && "This operator does not apply to FP types!");
3325 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003326 N1.getValueType() == VT && "Binary operator types must match!");
3327 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3328 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003329 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003330 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003331 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3332 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003333 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003334 case ISD::OR:
3335 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003336 case ISD::ADD:
3337 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003338 assert(VT.isInteger() && "This operator does not apply to FP types!");
3339 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003340 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003341 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3342 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003343 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003344 return N1;
3345 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003346 case ISD::UDIV:
3347 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003348 case ISD::MULHU:
3349 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003350 case ISD::MUL:
3351 case ISD::SDIV:
3352 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003353 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003354 assert(N1.getValueType() == N2.getValueType() &&
3355 N1.getValueType() == VT && "Binary operator types must match!");
3356 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003357 case ISD::FADD:
3358 case ISD::FSUB:
3359 case ISD::FMUL:
3360 case ISD::FDIV:
3361 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003362 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003363 if (Opcode == ISD::FADD) {
3364 // 0+x --> x
3365 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3366 if (CFP->getValueAPF().isZero())
3367 return N2;
3368 // x+0 --> x
3369 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3370 if (CFP->getValueAPF().isZero())
3371 return N1;
3372 } else if (Opcode == ISD::FSUB) {
3373 // x-0 --> x
3374 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3375 if (CFP->getValueAPF().isZero())
3376 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003377 } else if (Opcode == ISD::FMUL) {
3378 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3379 SDValue V = N2;
3380
3381 // If the first operand isn't the constant, try the second
3382 if (!CFP) {
3383 CFP = dyn_cast<ConstantFPSDNode>(N2);
3384 V = N1;
3385 }
3386
3387 if (CFP) {
3388 // 0*x --> 0
3389 if (CFP->isZero())
3390 return SDValue(CFP,0);
3391 // 1*x --> x
3392 if (CFP->isExactlyValue(1.0))
3393 return V;
3394 }
Dan Gohman1275e282009-01-23 19:10:37 +00003395 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003396 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003397 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003398 assert(N1.getValueType() == N2.getValueType() &&
3399 N1.getValueType() == VT && "Binary operator types must match!");
3400 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003401 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3402 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003403 N1.getValueType().isFloatingPoint() &&
3404 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003405 "Invalid FCOPYSIGN!");
3406 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003407 case ISD::SHL:
3408 case ISD::SRA:
3409 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003410 case ISD::ROTL:
3411 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003412 assert(VT == N1.getValueType() &&
3413 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003414 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003415 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003416 assert((!VT.isVector() || VT == N2.getValueType()) &&
3417 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003418 // Verify that the shift amount VT is bit enough to hold valid shift
3419 // amounts. This catches things like trying to shift an i1024 value by an
3420 // i8, which is easy to fall into in generic code that uses
3421 // TLI.getShiftAmount().
3422 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003423 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003424 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003425
3426 // Always fold shifts of i1 values so the code generator doesn't need to
3427 // handle them. Since we know the size of the shift has to be less than the
3428 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003429 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003430 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003431 if (N2C && N2C->isNullValue())
3432 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003433 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003434 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003435 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003436 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003437 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003438 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003439 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003440 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003441 "type is vector!");
3442 assert((!EVT.isVector() ||
3443 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3444 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003445 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003446 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003447 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003448 break;
3449 }
Chris Lattner72733e52008-01-17 07:00:52 +00003450 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003451 assert(VT.isFloatingPoint() &&
3452 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003453 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003454 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003455 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003456 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003457 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003458 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003459 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003460 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003461 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003462 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003463 assert(!EVT.isVector() &&
3464 "AssertSExt/AssertZExt type should be the vector element type "
3465 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003466 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003467 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003468 break;
3469 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003470 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003471 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003472 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003473 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003474 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003475 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003476 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003477 "type is vector!");
3478 assert((!EVT.isVector() ||
3479 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3480 "Vector element counts must match in SIGN_EXTEND_INREG");
3481 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003482 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003483
Simon Pilgrime0541992015-05-21 10:05:03 +00003484 auto SignExtendInReg = [&](APInt Val) {
3485 unsigned FromBits = EVT.getScalarType().getSizeInBits();
3486 Val <<= Val.getBitWidth() - FromBits;
3487 Val = Val.ashr(Val.getBitWidth() - FromBits);
3488 return getConstant(Val, DL, VT.getScalarType());
3489 };
3490
Chris Lattner16713612008-01-22 19:09:33 +00003491 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003492 APInt Val = N1C->getAPIntValue();
Simon Pilgrime0541992015-05-21 10:05:03 +00003493 return SignExtendInReg(Val);
3494 }
3495 if (ISD::isBuildVectorOfConstantSDNodes(N1.getNode())) {
3496 SmallVector<SDValue, 8> Ops;
3497 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
3498 SDValue Op = N1.getOperand(i);
3499 if (Op.getValueType() != VT.getScalarType()) break;
3500 if (Op.getOpcode() == ISD::UNDEF) {
3501 Ops.push_back(Op);
3502 continue;
3503 }
3504 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getNode())) {
3505 APInt Val = C->getAPIntValue();
3506 Ops.push_back(SignExtendInReg(Val));
3507 continue;
3508 }
3509 break;
3510 }
3511 if (Ops.size() == VT.getVectorNumElements())
3512 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Chris Lattner751817c2006-05-06 23:05:41 +00003513 }
Chris Lattner16713612008-01-22 19:09:33 +00003514 break;
3515 }
3516 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003517 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3518 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003519 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003520
Pawel Bylica9f1fb9d2015-05-06 10:19:14 +00003521 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF
3522 if (N2C && N2C->getZExtValue() >= N1.getValueType().getVectorNumElements())
3523 return getUNDEF(VT);
3524
Chris Lattner16713612008-01-22 19:09:33 +00003525 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3526 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003527 if (N2C &&
3528 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003529 N1.getNumOperands() > 0) {
3530 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003531 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003532 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003533 N1.getOperand(N2C->getZExtValue() / Factor),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003534 getConstant(N2C->getZExtValue() % Factor, DL,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003535 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003536 }
3537
3538 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3539 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003540 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3541 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003542
3543 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003544 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003545 // are promoted and implicitly truncated, and the result implicitly
3546 // extended. Make that explicit here.
3547 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003548
Bob Wilson59dbbb22009-04-13 22:05:19 +00003549 return Elt;
3550 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003551
Chris Lattner16713612008-01-22 19:09:33 +00003552 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3553 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003554 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003555 // If the indices are the same, return the inserted element else
3556 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003557 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003558 SDValue N1Op2 = N1.getOperand(2);
3559 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3560
3561 if (N1Op2C && N2C) {
3562 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3563 if (VT == N1.getOperand(1).getValueType())
3564 return N1.getOperand(1);
3565 else
3566 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3567 }
3568
Dale Johannesendb393622009-02-03 01:55:44 +00003569 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003570 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003571 }
Chris Lattner16713612008-01-22 19:09:33 +00003572 break;
3573 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003574 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003575 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3576 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003577 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003578 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003579
Chris Lattner16713612008-01-22 19:09:33 +00003580 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3581 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003582 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003583 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003584 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003585
Chris Lattner16713612008-01-22 19:09:33 +00003586 // EXTRACT_ELEMENT of a constant int is also very common.
3587 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003588 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003589 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003590 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003591 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
Chris Lattner16713612008-01-22 19:09:33 +00003592 }
3593 break;
David Greeneb6f16112011-01-26 15:38:49 +00003594 case ISD::EXTRACT_SUBVECTOR: {
3595 SDValue Index = N2;
3596 if (VT.isSimple() && N1.getValueType().isSimple()) {
3597 assert(VT.isVector() && N1.getValueType().isVector() &&
3598 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003599 assert(VT.getVectorElementType() ==
3600 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003601 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003602 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003603 "Extract subvector must be from larger vector to smaller vector!");
3604
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003605 if (isa<ConstantSDNode>(Index.getNode())) {
3606 assert((VT.getVectorNumElements() +
3607 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003608 <= N1.getValueType().getVectorNumElements())
3609 && "Extract subvector overflow!");
3610 }
3611
3612 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003613 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003614 return N1;
3615 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003616 break;
Chris Lattner16713612008-01-22 19:09:33 +00003617 }
David Greeneb6f16112011-01-26 15:38:49 +00003618 }
Chris Lattner16713612008-01-22 19:09:33 +00003619
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003620 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003621 if (SDValue SV =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003622 FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
Matthias Braunf50ab432015-01-13 22:17:46 +00003623 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003624
3625 // Canonicalize constant to RHS if commutative.
3626 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3627 std::swap(N1C, N2C);
3628 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003629 }
3630
Chris Lattner16713612008-01-22 19:09:33 +00003631 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003632 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003633 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3634 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003635 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003636 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003637 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003638 std::swap(N1CFP, N2CFP);
3639 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003640 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003641 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3642 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003643 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003644 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003645 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003646 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003647 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003648 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003649 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003650 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003651 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003652 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003653 break;
3654 case ISD::FMUL:
3655 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003656 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003657 return getConstantFP(V1, DL, VT);
Dale Johannesen446b9002007-08-31 23:34:27 +00003658 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003659 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003660 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003661 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3662 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003663 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003664 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003665 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003666 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003667 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003668 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3669 s!=APFloat::opDivByZero)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003670 return getConstantFP(V1, DL, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003671 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003672 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003673 case ISD::FCOPYSIGN:
3674 V1.copySign(V2);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003675 return getConstantFP(V1, DL, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003676 default: break;
3677 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003678 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003679
3680 if (Opcode == ISD::FP_ROUND) {
3681 APFloat V = N1CFP->getValueAPF(); // make copy
3682 bool ignored;
3683 // This can return overflow, underflow, or inexact; we don't care.
3684 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003685 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003686 APFloat::rmNearestTiesToEven, &ignored);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003687 return getConstantFP(V, DL, VT);
Owen Anderson6f1ee162012-04-10 22:46:53 +00003688 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003689 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003690
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003691 // Canonicalize an UNDEF to the RHS, even over a constant.
3692 if (N1.getOpcode() == ISD::UNDEF) {
3693 if (isCommutativeBinOp(Opcode)) {
3694 std::swap(N1, N2);
3695 } else {
3696 switch (Opcode) {
3697 case ISD::FP_ROUND_INREG:
3698 case ISD::SIGN_EXTEND_INREG:
3699 case ISD::SUB:
3700 case ISD::FSUB:
3701 case ISD::FDIV:
3702 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003703 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003704 return N1; // fold op(undef, arg2) -> undef
3705 case ISD::UDIV:
3706 case ISD::SDIV:
3707 case ISD::UREM:
3708 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003709 case ISD::SRL:
3710 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003711 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003712 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003713 // For vectors, we can't easily build an all zero vector, just return
3714 // the LHS.
3715 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003716 }
3717 }
3718 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003719
3720 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003721 if (N2.getOpcode() == ISD::UNDEF) {
3722 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003723 case ISD::XOR:
3724 if (N1.getOpcode() == ISD::UNDEF)
3725 // Handle undef ^ undef -> 0 special case. This is a common
3726 // idiom (misuse).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003727 return getConstant(0, DL, VT);
Evan Chengdf1690d2008-03-25 20:08:07 +00003728 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003729 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003730 case ISD::ADDC:
3731 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003732 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003733 case ISD::UDIV:
3734 case ISD::SDIV:
3735 case ISD::UREM:
3736 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003737 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003738 case ISD::FADD:
3739 case ISD::FSUB:
3740 case ISD::FMUL:
3741 case ISD::FDIV:
3742 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003743 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003744 return N2;
3745 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003746 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003747 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003748 case ISD::SRL:
3749 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003750 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003751 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
Chris Lattner01a26c72007-04-25 00:00:45 +00003752 // For vectors, we can't easily build an all zero vector, just return
3753 // the LHS.
3754 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003755 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003756 if (!VT.isVector())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003757 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003758 // For vectors, we can't easily build an all one vector, just return
3759 // the LHS.
3760 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003761 case ISD::SRA:
3762 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003763 }
3764 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003765
Chris Lattner724f7ee2005-05-11 18:57:39 +00003766 // Memoize this node if possible.
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003767 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003768 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003769 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003770 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003771 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003772 AddNodeIDNode(ID, Opcode, VTs, Ops);
Sanjay Patelf1340482015-06-16 16:25:43 +00003773 AddNodeIDFlags(ID, Opcode, Flags);
Craig Topperc0196b12014-04-14 00:51:57 +00003774 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00003775 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003776 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003777
Sanjay Patelf1340482015-06-16 16:25:43 +00003778 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, Flags);
NAKAMURA Takumid7c0be92015-05-06 14:03:12 +00003779
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003780 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003781 } else {
Sanjay Patelf1340482015-06-16 16:25:43 +00003782 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, Flags);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003783 }
3784
Chandler Carruth41b20e72014-07-22 04:07:55 +00003785 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003786 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003787}
3788
Andrew Trickef9de2a2013-05-25 02:42:55 +00003789SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003790 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003791 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003792 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003793 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003794 case ISD::FMA: {
3795 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3796 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3797 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3798 if (N1CFP && N2CFP && N3CFP) {
3799 APFloat V1 = N1CFP->getValueAPF();
3800 const APFloat &V2 = N2CFP->getValueAPF();
3801 const APFloat &V3 = N3CFP->getValueAPF();
3802 APFloat::opStatus s =
3803 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003804 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003805 return getConstantFP(V1, DL, VT);
Owen Anderson32baf992013-05-09 22:27:13 +00003806 }
3807 break;
3808 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003809 case ISD::CONCAT_VECTORS:
3810 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3811 // one big BUILD_VECTOR.
3812 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3813 N2.getOpcode() == ISD::BUILD_VECTOR &&
3814 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003815 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3816 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003817 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3818 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003819 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003820 }
3821 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003822 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003823 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003824 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003825 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003826 break;
3827 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003828 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003829 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003830 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003831 return N2; // select true, X, Y -> X
3832 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003833 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003834
3835 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003836 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003837 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003838 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003839 case ISD::INSERT_SUBVECTOR: {
3840 SDValue Index = N3;
3841 if (VT.isSimple() && N1.getValueType().isSimple()
3842 && N2.getValueType().isSimple()) {
3843 assert(VT.isVector() && N1.getValueType().isVector() &&
3844 N2.getValueType().isVector() &&
3845 "Insert subvector VTs must be a vectors");
3846 assert(VT == N1.getValueType() &&
3847 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003848 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003849 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003850 if (isa<ConstantSDNode>(Index.getNode())) {
3851 assert((N2.getValueType().getVectorNumElements() +
3852 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003853 <= VT.getVectorNumElements())
3854 && "Insert subvector overflow!");
3855 }
3856
3857 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003858 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003859 return N2;
3860 }
3861 break;
3862 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003863 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003864 // Fold bit_convert nodes from a type to themselves.
3865 if (N1.getValueType() == VT)
3866 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003867 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003868 }
3869
Chris Lattnerf9c19152005-08-25 19:12:10 +00003870 // Memoize node if it doesn't produce a flag.
3871 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003872 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003873 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003874 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003875 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003876 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003877 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00003878 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003879 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003880
Jack Carter170a5f22013-09-09 22:02:08 +00003881 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3882 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003883 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003884 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003885 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3886 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003887 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003888
Chandler Carruth41b20e72014-07-22 04:07:55 +00003889 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003890 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003891}
3892
Andrew Trickef9de2a2013-05-25 02:42:55 +00003893SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003894 SDValue N1, SDValue N2, SDValue N3,
3895 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003896 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003897 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003898}
3899
Andrew Trickef9de2a2013-05-25 02:42:55 +00003900SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003901 SDValue N1, SDValue N2, SDValue N3,
3902 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003903 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003904 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003905}
3906
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003907/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3908/// the incoming stack arguments to be loaded from the stack.
3909SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3910 SmallVector<SDValue, 8> ArgChains;
3911
3912 // Include the original chain at the beginning of the list. When this is
3913 // used by target LowerCall hooks, this helps legalize find the
3914 // CALLSEQ_BEGIN node.
3915 ArgChains.push_back(Chain);
3916
3917 // Add a chain value for each stack argument.
3918 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3919 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3920 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3921 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3922 if (FI->getIndex() < 0)
3923 ArgChains.push_back(SDValue(L, 1));
3924
3925 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003926 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003927}
3928
Dan Gohman544ab2c2008-04-12 04:36:06 +00003929/// getMemsetValue - Vectorized representation of the memset value
3930/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003931static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003932 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003933 assert(Value.getOpcode() != ISD::UNDEF);
3934
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003935 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003936 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003937 assert(C->getAPIntValue().getBitWidth() == 8);
3938 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003939 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003940 return DAG.getConstant(Val, dl, VT);
3941 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
3942 VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003943 }
Evan Chengef377ad2008-05-15 08:39:06 +00003944
Hal Finkel17b6d772015-03-31 20:35:26 +00003945 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
3946 EVT IntVT = VT.getScalarType();
3947 if (!IntVT.isInteger())
3948 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
3949
3950 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003951 if (NumBits > 8) {
3952 // Use a multiplication with 0x010101... to extend the input to the
3953 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003954 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Hal Finkel17b6d772015-03-31 20:35:26 +00003955 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003956 DAG.getConstant(Magic, dl, IntVT));
Hal Finkel17b6d772015-03-31 20:35:26 +00003957 }
3958
3959 if (VT != Value.getValueType() && !VT.isInteger())
3960 Value = DAG.getNode(ISD::BITCAST, dl, VT.getScalarType(), Value);
3961 if (VT != Value.getValueType()) {
3962 assert(VT.getVectorElementType() == Value.getValueType() &&
3963 "value type should be one vector element here");
3964 SmallVector<SDValue, 8> BVOps(VT.getVectorNumElements(), Value);
3965 Value = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, BVOps);
Evan Chengef377ad2008-05-15 08:39:06 +00003966 }
3967
3968 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003969}
3970
Dan Gohman544ab2c2008-04-12 04:36:06 +00003971/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3972/// used when a memcpy is turned into a memset when the source is a constant
3973/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003974static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003975 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003976 // Handle vector with all elements zero.
3977 if (Str.empty()) {
3978 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003979 return DAG.getConstant(0, dl, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003980 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003981 return DAG.getConstantFP(0.0, dl, VT);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003982 else if (VT.isVector()) {
3983 unsigned NumElts = VT.getVectorNumElements();
3984 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003985 return DAG.getNode(ISD::BITCAST, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003986 DAG.getConstant(0, dl,
3987 EVT::getVectorVT(*DAG.getContext(),
3988 EltVT, NumElts)));
Evan Cheng43cd9e32010-04-01 06:04:33 +00003989 } else
3990 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003991 }
3992
Duncan Sands13237ac2008-06-06 12:08:01 +00003993 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003994 unsigned NumVTBits = VT.getSizeInBits();
3995 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003996 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3997
Evan Chengc8444b12013-01-10 22:13:27 +00003998 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003999 if (TLI.isLittleEndian()) {
4000 for (unsigned i = 0; i != NumBytes; ++i)
4001 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
4002 } else {
4003 for (unsigned i = 0; i != NumBytes; ++i)
4004 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004005 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004006
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00004007 // If the "cost" of materializing the integer immediate is less than the cost
4008 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00004009 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
4010 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004011 return DAG.getConstant(Val, dl, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00004012 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004013}
4014
Scott Michelcf0da6c2009-02-17 22:15:04 +00004015/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00004016///
Andrew Tricke2431c62013-05-25 03:08:10 +00004017static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00004018 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004019 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00004020 return DAG.getNode(ISD::ADD, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004021 VT, Base, DAG.getConstant(Offset, dl, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00004022}
4023
Evan Chengef377ad2008-05-15 08:39:06 +00004024/// isMemSrcFromString - Returns true if memcpy source is a string constant.
4025///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004026static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00004027 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00004028 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00004029 if (Src.getOpcode() == ISD::GlobalAddress)
4030 G = cast<GlobalAddressSDNode>(Src);
4031 else if (Src.getOpcode() == ISD::ADD &&
4032 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
4033 Src.getOperand(1).getOpcode() == ISD::Constant) {
4034 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00004035 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00004036 }
4037 if (!G)
4038 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004039
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004040 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00004041}
Dan Gohman544ab2c2008-04-12 04:36:06 +00004042
Sanjay Patel8730ef72015-06-18 15:53:33 +00004043/// Determines the optimal series of memory ops to replace the memset / memcpy.
4044/// Return true if the number of memory ops is below the threshold (Limit).
4045/// It returns the types of the sequence of memory ops to perform
4046/// memset / memcpy by reference.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004047static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004048 unsigned Limit, uint64_t Size,
4049 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00004050 bool IsMemset,
4051 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00004052 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00004053 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004054 SelectionDAG &DAG,
4055 const TargetLowering &TLI) {
4056 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
4057 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00004058 // If 'SrcAlign' is zero, that means the memory operation does not need to
4059 // load the value, i.e. memset or memcpy from constant string. Otherwise,
4060 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
4061 // is the specified alignment of the memory operation. If it is zero, that
4062 // means it's possible to change the alignment of the destination.
4063 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
4064 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00004065 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00004066 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00004067 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00004068
Chris Lattner28dc6c12010-03-07 07:45:08 +00004069 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00004070 unsigned AS = 0;
4071 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00004072 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00004073 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00004074 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00004075 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00004076 case 0: VT = MVT::i64; break;
4077 case 4: VT = MVT::i32; break;
4078 case 2: VT = MVT::i16; break;
4079 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00004080 }
4081 }
4082
Owen Andersonc6daf8f2009-08-11 21:59:30 +00004083 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00004084 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00004085 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00004086 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00004087
Duncan Sands11dd4242008-06-08 20:54:56 +00004088 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00004089 VT = LVT;
4090 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004091
Dan Gohman544ab2c2008-04-12 04:36:06 +00004092 unsigned NumMemOps = 0;
4093 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00004094 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004095 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00004096 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00004097 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004098 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00004099
4100 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004101 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00004102 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00004103 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004104 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00004105 Found = true;
4106 else if (NewVT == MVT::i64 &&
4107 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00004108 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00004109 // i64 is usually not legal on 32-bit targets, but f64 may be.
4110 NewVT = MVT::f64;
4111 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004112 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00004113 }
4114
Evan Cheng04e55182012-12-12 00:42:09 +00004115 if (!Found) {
4116 do {
4117 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
4118 if (NewVT == MVT::i8)
4119 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00004120 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00004121 }
4122 NewVTSize = NewVT.getSizeInBits() / 8;
4123
Evan Cheng79e2ca92012-12-10 23:21:26 +00004124 // If the new VT cannot cover all of the remaining bits, then consider
4125 // issuing a (or a pair of) unaligned and overlapping load / store.
4126 // FIXME: Only does this for 64-bit or more since we don't have proper
4127 // cost model for unaligned load / store.
4128 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00004129 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00004130 if (NumMemOps && AllowOverlap &&
4131 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00004132 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00004133 VTSize = Size;
4134 else {
4135 VT = NewVT;
4136 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00004137 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004138 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004139
Evan Chengb7d3d032012-12-12 20:43:23 +00004140 if (++NumMemOps > Limit)
4141 return false;
4142
Dan Gohman544ab2c2008-04-12 04:36:06 +00004143 MemOps.push_back(VT);
4144 Size -= VTSize;
4145 }
4146
4147 return true;
4148}
4149
Andrew Trickef9de2a2013-05-25 02:42:55 +00004150static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00004151 SDValue Chain, SDValue Dst,
4152 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004153 unsigned Align, bool isVol,
4154 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004155 MachinePointerInfo DstPtrInfo,
4156 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004157 // Turn a memcpy of undef to nop.
4158 if (Src.getOpcode() == ISD::UNDEF)
4159 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004160
Dan Gohman714663a2008-05-29 19:42:22 +00004161 // Expand memcpy to a series of load and store ops if the size operand falls
4162 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00004163 // TODO: In the AlwaysInline case, if the size is big then generate a loop
4164 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00004165 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004166 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004167 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004168 MachineFunction &MF = DAG.getMachineFunction();
4169 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004170 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004171 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4172 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4173 DstAlignCanChange = true;
4174 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4175 if (Align > SrcAlign)
4176 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004177 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004178 bool CopyFromStr = isMemSrcFromString(Src, Str);
4179 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004180 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00004181
Evan Cheng4c014c82010-04-01 18:19:11 +00004182 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004183 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00004184 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00004185 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004186 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004187
Evan Cheng43cd9e32010-04-01 06:04:33 +00004188 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004189 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004190 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00004191
4192 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00004193 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004194 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004195 if (!TRI->needsStackRealignment(MF))
4196 while (NewAlign > Align &&
4197 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4198 NewAlign /= 2;
4199
Evan Cheng43cd9e32010-04-01 06:04:33 +00004200 if (NewAlign > Align) {
4201 // Give the stack frame object a larger alignment if needed.
4202 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4203 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4204 Align = NewAlign;
4205 }
4206 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004207
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004208 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004209 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004210 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004211 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004212 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004213 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004214 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004215
Evan Cheng79e2ca92012-12-10 23:21:26 +00004216 if (VTSize > Size) {
4217 // Issuing an unaligned load / store pair that overlaps with the previous
4218 // pair. Adjust the offset accordingly.
4219 assert(i == NumMemOps-1 && i != 0);
4220 SrcOff -= VTSize - Size;
4221 DstOff -= VTSize - Size;
4222 }
4223
Evan Cheng43cd9e32010-04-01 06:04:33 +00004224 if (CopyFromStr &&
4225 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004226 // It's unlikely a store of a vector immediate can be done in a single
4227 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004228 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004229 // FIXME: Handle other cases where store of vector immediate is done in
4230 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004231 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004232 if (Value.getNode())
4233 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004234 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004235 DstPtrInfo.getWithOffset(DstOff), isVol,
4236 false, Align);
4237 }
4238
4239 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004240 // The type might not be legal for the target. This should only happen
4241 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004242 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4243 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004244 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004245 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004246 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004247 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004248 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004249 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004250 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004251 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004252 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004253 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4254 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004255 }
4256 OutChains.push_back(Store);
4257 SrcOff += VTSize;
4258 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004259 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004260 }
4261
Craig Topper48d114b2014-04-26 18:35:24 +00004262 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004263}
4264
Andrew Trickef9de2a2013-05-25 02:42:55 +00004265static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004266 SDValue Chain, SDValue Dst,
4267 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004268 unsigned Align, bool isVol,
4269 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004270 MachinePointerInfo DstPtrInfo,
4271 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004272 // Turn a memmove of undef to nop.
4273 if (Src.getOpcode() == ISD::UNDEF)
4274 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004275
4276 // Expand memmove to a series of load and store ops if the size operand falls
4277 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004278 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004279 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004280 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004281 MachineFunction &MF = DAG.getMachineFunction();
4282 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004283 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004284 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4285 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4286 DstAlignCanChange = true;
4287 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4288 if (Align > SrcAlign)
4289 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004290 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004291
Evan Cheng4c014c82010-04-01 18:19:11 +00004292 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004293 (DstAlignCanChange ? 0 : Align), SrcAlign,
4294 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004295 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004296
Evan Cheng43cd9e32010-04-01 06:04:33 +00004297 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004298 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004299 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004300 if (NewAlign > Align) {
4301 // Give the stack frame object a larger alignment if needed.
4302 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4303 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4304 Align = NewAlign;
4305 }
4306 }
Dan Gohman714663a2008-05-29 19:42:22 +00004307
Evan Cheng43cd9e32010-04-01 06:04:33 +00004308 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004309 SmallVector<SDValue, 8> LoadValues;
4310 SmallVector<SDValue, 8> LoadChains;
4311 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004312 unsigned NumMemOps = MemOps.size();
4313 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004314 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004315 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004316 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004317
Dale Johannesenabf66b82009-02-03 22:26:09 +00004318 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004319 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004320 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004321 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004322 LoadValues.push_back(Value);
4323 LoadChains.push_back(Value.getValue(1));
4324 SrcOff += VTSize;
4325 }
Craig Topper48d114b2014-04-26 18:35:24 +00004326 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004327 OutChains.clear();
4328 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004329 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004330 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004331 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004332
Dale Johannesenabf66b82009-02-03 22:26:09 +00004333 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004334 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004335 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004336 OutChains.push_back(Store);
4337 DstOff += VTSize;
4338 }
4339
Craig Topper48d114b2014-04-26 18:35:24 +00004340 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004341}
4342
Serge Pavlov8ec39992013-09-17 16:24:42 +00004343/// \brief Lower the call to 'memset' intrinsic function into a series of store
4344/// operations.
4345///
4346/// \param DAG Selection DAG where lowered code is placed.
4347/// \param dl Link to corresponding IR location.
4348/// \param Chain Control flow dependency.
4349/// \param Dst Pointer to destination memory location.
4350/// \param Src Value of byte to write into the memory.
4351/// \param Size Number of bytes to write.
4352/// \param Align Alignment of the destination in bytes.
4353/// \param isVol True if destination is volatile.
4354/// \param DstPtrInfo IR information on the memory pointer.
4355/// \returns New head in the control flow, if lowering was successful, empty
4356/// SDValue otherwise.
4357///
4358/// The function tries to replace 'llvm.memset' intrinsic with several store
4359/// operations and value calculation code. This is usually profitable for small
4360/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004361static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004362 SDValue Chain, SDValue Dst,
4363 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004364 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004365 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004366 // Turn a memset of undef to nop.
4367 if (Src.getOpcode() == ISD::UNDEF)
4368 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004369
4370 // Expand memset to a series of load/store ops if the size operand
4371 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004372 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004373 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004374 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004375 MachineFunction &MF = DAG.getMachineFunction();
4376 MachineFrameInfo *MFI = MF.getFrameInfo();
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +00004377 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004378 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4379 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4380 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004381 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004382 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004383 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004384 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004385 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004386 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004387
Evan Cheng43cd9e32010-04-01 06:04:33 +00004388 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004389 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004390 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004391 if (NewAlign > Align) {
4392 // Give the stack frame object a larger alignment if needed.
4393 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4394 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4395 Align = NewAlign;
4396 }
4397 }
4398
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004399 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004400 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004401 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004402
4403 // Find the largest store and generate the bit pattern for it.
4404 EVT LargestVT = MemOps[0];
4405 for (unsigned i = 1; i < NumMemOps; i++)
4406 if (MemOps[i].bitsGT(LargestVT))
4407 LargestVT = MemOps[i];
4408 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4409
Dan Gohman544ab2c2008-04-12 04:36:06 +00004410 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004411 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004412 unsigned VTSize = VT.getSizeInBits() / 8;
4413 if (VTSize > Size) {
4414 // Issuing an unaligned load / store pair that overlaps with the previous
4415 // pair. Adjust the offset accordingly.
4416 assert(i == NumMemOps-1 && i != 0);
4417 DstOff -= VTSize - Size;
4418 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004419
4420 // If this store is smaller than the largest store see whether we can get
4421 // the smaller value for free with a truncate.
4422 SDValue Value = MemSetValue;
4423 if (VT.bitsLT(LargestVT)) {
4424 if (!LargestVT.isVector() && !VT.isVector() &&
4425 TLI.isTruncateFree(LargestVT, VT))
4426 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4427 else
4428 Value = getMemsetValue(Src, VT, DAG, dl);
4429 }
4430 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004431 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004432 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004433 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004434 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004435 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004436 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004437 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004438 }
4439
Craig Topper48d114b2014-04-26 18:35:24 +00004440 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004441}
4442
Andrew Trickef9de2a2013-05-25 02:42:55 +00004443SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004444 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004445 unsigned Align, bool isVol, bool AlwaysInline,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004446 bool isTailCall, MachinePointerInfo DstPtrInfo,
Chris Lattner2510de22010-09-21 05:40:29 +00004447 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004448 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004449
4450 // Check to see if we should lower the memcpy to loads and stores first.
4451 // For cases within the target-specified limits, this is the best choice.
4452 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4453 if (ConstantSize) {
4454 // Memcpy with size zero? Just return the original chain.
4455 if (ConstantSize->isNullValue())
4456 return Chain;
4457
Evan Cheng43cd9e32010-04-01 06:04:33 +00004458 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4459 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004460 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004461 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004462 return Result;
4463 }
4464
4465 // Then check to see if we should lower the memcpy with target-specific
4466 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004467 if (TSI) {
4468 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4469 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4470 DstPtrInfo, SrcPtrInfo);
4471 if (Result.getNode())
4472 return Result;
4473 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004474
4475 // If we really need inline code and the target declined to provide it,
4476 // use a (potentially long) sequence of loads and stores.
4477 if (AlwaysInline) {
4478 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004479 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004480 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004481 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004482 }
4483
Dan Gohmanf38547c2010-04-05 20:24:08 +00004484 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4485 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4486 // respect volatile, so they may do things like read or write memory
4487 // beyond the given memory regions. But fixing this isn't easy, and most
4488 // people don't care.
4489
Dan Gohman544ab2c2008-04-12 04:36:06 +00004490 // Emit a library call.
4491 TargetLowering::ArgListTy Args;
4492 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004493 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004494 Entry.Node = Dst; Args.push_back(Entry);
4495 Entry.Node = Src; Args.push_back(Entry);
4496 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004497 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004498 TargetLowering::CallLoweringInfo CLI(*this);
4499 CLI.setDebugLoc(dl).setChain(Chain)
4500 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4501 Type::getVoidTy(*getContext()),
4502 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004503 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004504 .setDiscardResult()
4505 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004506
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004507 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004508 return CallResult.second;
4509}
4510
Andrew Trickef9de2a2013-05-25 02:42:55 +00004511SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004512 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004513 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004514 MachinePointerInfo DstPtrInfo,
4515 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004516 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004517
Dan Gohman714663a2008-05-29 19:42:22 +00004518 // Check to see if we should lower the memmove to loads and stores first.
4519 // For cases within the target-specified limits, this is the best choice.
4520 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4521 if (ConstantSize) {
4522 // Memmove with size zero? Just return the original chain.
4523 if (ConstantSize->isNullValue())
4524 return Chain;
4525
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004526 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004527 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004528 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004529 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004530 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004531 return Result;
4532 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004533
4534 // Then check to see if we should lower the memmove with target-specific
4535 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004536 if (TSI) {
4537 SDValue Result = TSI->EmitTargetCodeForMemmove(
4538 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4539 if (Result.getNode())
4540 return Result;
4541 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004542
Mon P Wangbf862242010-04-06 08:27:51 +00004543 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4544 // not be safe. See memcpy above for more details.
4545
Dan Gohman544ab2c2008-04-12 04:36:06 +00004546 // Emit a library call.
4547 TargetLowering::ArgListTy Args;
4548 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004549 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004550 Entry.Node = Dst; Args.push_back(Entry);
4551 Entry.Node = Src; Args.push_back(Entry);
4552 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004553 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004554 TargetLowering::CallLoweringInfo CLI(*this);
4555 CLI.setDebugLoc(dl).setChain(Chain)
4556 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4557 Type::getVoidTy(*getContext()),
4558 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004559 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004560 .setDiscardResult()
4561 .setTailCall(isTailCall);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004562
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004563 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004564 return CallResult.second;
4565}
4566
Andrew Trickef9de2a2013-05-25 02:42:55 +00004567SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004568 SDValue Src, SDValue Size,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004569 unsigned Align, bool isVol, bool isTailCall,
Chris Lattner2510de22010-09-21 05:40:29 +00004570 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004571 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004572
4573 // Check to see if we should lower the memset to stores first.
4574 // For cases within the target-specified limits, this is the best choice.
4575 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4576 if (ConstantSize) {
4577 // Memset with size zero? Just return the original chain.
4578 if (ConstantSize->isNullValue())
4579 return Chain;
4580
Mon P Wangc576ee92010-04-04 03:10:48 +00004581 SDValue Result =
4582 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004583 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004584
Gabor Greiff304a7a2008-08-28 21:40:38 +00004585 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004586 return Result;
4587 }
4588
4589 // Then check to see if we should lower the memset with target-specific
4590 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004591 if (TSI) {
4592 SDValue Result = TSI->EmitTargetCodeForMemset(
4593 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4594 if (Result.getNode())
4595 return Result;
4596 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004597
Wesley Peck527da1b2010-11-23 03:31:01 +00004598 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004599 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004600 TargetLowering::ArgListTy Args;
4601 TargetLowering::ArgListEntry Entry;
4602 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4603 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004604 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004605 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004606 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004607 Entry.Node = Size;
4608 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004609 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004610
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004611 // FIXME: pass in SDLoc
4612 TargetLowering::CallLoweringInfo CLI(*this);
4613 CLI.setDebugLoc(dl).setChain(Chain)
4614 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4615 Type::getVoidTy(*getContext()),
4616 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004617 TLI->getPointerTy()), std::move(Args), 0)
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004618 .setDiscardResult()
4619 .setTailCall(isTailCall);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004620
4621 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004622 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004623}
4624
Andrew Trickef9de2a2013-05-25 02:42:55 +00004625SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004626 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004627 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004628 AtomicOrdering SuccessOrdering,
4629 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004630 SynchronizationScope SynchScope) {
4631 FoldingSetNodeID ID;
4632 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004633 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004634 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004635 void* IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00004636 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004637 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4638 return SDValue(E, 0);
4639 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004640
4641 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4642 // SDNode doesn't have access to it. This memory will be "leaked" when
4643 // the node is deallocated, but recovered when the allocator is released.
4644 // If the number of operands is less than 5 we use AtomicSDNode's internal
4645 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004646 unsigned NumOps = Ops.size();
4647 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4648 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004649
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004650 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4651 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004652 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004653 SuccessOrdering, FailureOrdering,
4654 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004655 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004656 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004657 return SDValue(N, 0);
4658}
4659
4660SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004661 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004662 MachineMemOperand *MMO,
4663 AtomicOrdering Ordering,
4664 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004665 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004666 Ordering, SynchScope);
4667}
4668
Tim Northover420a2162014-06-13 14:24:07 +00004669SDValue SelectionDAG::getAtomicCmpSwap(
4670 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4671 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4672 unsigned Alignment, AtomicOrdering SuccessOrdering,
4673 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4674 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4675 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4676 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4677
Dan Gohman48b185d2009-09-25 20:36:54 +00004678 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4679 Alignment = getEVTAlignment(MemVT);
4680
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004681 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004682
Eli Friedmane978d2f2011-09-07 02:23:42 +00004683 // FIXME: Volatile isn't really correct; we should keep track of atomic
4684 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004685 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004686 Flags |= MachineMemOperand::MOLoad;
4687 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004688
4689 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004690 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004691
Tim Northover420a2162014-06-13 14:24:07 +00004692 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4693 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004694}
4695
Tim Northover420a2162014-06-13 14:24:07 +00004696SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4697 SDVTList VTs, SDValue Chain, SDValue Ptr,
4698 SDValue Cmp, SDValue Swp,
4699 MachineMemOperand *MMO,
4700 AtomicOrdering SuccessOrdering,
4701 AtomicOrdering FailureOrdering,
4702 SynchronizationScope SynchScope) {
4703 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4704 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004705 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4706
Dale Johannesen839acbb2009-01-29 00:47:48 +00004707 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004708 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4709 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004710}
4711
Andrew Trickef9de2a2013-05-25 02:42:55 +00004712SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004713 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004714 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004715 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004716 unsigned Alignment,
4717 AtomicOrdering Ordering,
4718 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004719 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4720 Alignment = getEVTAlignment(MemVT);
4721
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004722 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004723 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004724 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004725 // For now, atomics are considered to be volatile always, and they are
4726 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004727 // FIXME: Volatile isn't really correct; we should keep track of atomic
4728 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004729 unsigned Flags = MachineMemOperand::MOVolatile;
4730 if (Opcode != ISD::ATOMIC_STORE)
4731 Flags |= MachineMemOperand::MOLoad;
4732 if (Opcode != ISD::ATOMIC_LOAD)
4733 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004734
4735 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004736 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004737 MemVT.getStoreSize(), Alignment);
4738
Eli Friedmanadec5872011-07-29 03:05:32 +00004739 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4740 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004741}
4742
Andrew Trickef9de2a2013-05-25 02:42:55 +00004743SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004744 SDValue Chain,
4745 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004746 MachineMemOperand *MMO,
4747 AtomicOrdering Ordering,
4748 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004749 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4750 Opcode == ISD::ATOMIC_LOAD_SUB ||
4751 Opcode == ISD::ATOMIC_LOAD_AND ||
4752 Opcode == ISD::ATOMIC_LOAD_OR ||
4753 Opcode == ISD::ATOMIC_LOAD_XOR ||
4754 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004755 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004756 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004757 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004758 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004759 Opcode == ISD::ATOMIC_SWAP ||
4760 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004761 "Invalid Atomic Op");
4762
Owen Anderson53aa7a92009-08-10 22:56:29 +00004763 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004764
Eli Friedman342e8df2011-08-24 20:50:09 +00004765 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4766 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004767 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004768 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004769}
4770
Andrew Trickef9de2a2013-05-25 02:42:55 +00004771SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004772 EVT VT, SDValue Chain,
4773 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004774 MachineMemOperand *MMO,
4775 AtomicOrdering Ordering,
4776 SynchronizationScope SynchScope) {
4777 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4778
4779 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004780 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004781 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004782}
4783
Duncan Sands739a0542008-07-02 17:40:58 +00004784/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004785SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4786 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004787 return Ops[0];
4788
Owen Anderson53aa7a92009-08-10 22:56:29 +00004789 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004790 VTs.reserve(Ops.size());
4791 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004792 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004793 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004794}
4795
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004796SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004797SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004798 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004799 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004800 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004801 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004802 if (Align == 0) // Ensure that codegen never sees alignment 0
4803 Align = getEVTAlignment(MemVT);
4804
4805 MachineFunction &MF = getMachineFunction();
4806 unsigned Flags = 0;
4807 if (WriteMem)
4808 Flags |= MachineMemOperand::MOStore;
4809 if (ReadMem)
4810 Flags |= MachineMemOperand::MOLoad;
4811 if (Vol)
4812 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004813 if (!Size)
4814 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004815 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004816 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004817
Craig Topper206fcd42014-04-26 19:29:41 +00004818 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004819}
4820
4821SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004822SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004823 ArrayRef<SDValue> Ops, EVT MemVT,
4824 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004825 assert((Opcode == ISD::INTRINSIC_VOID ||
4826 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004827 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004828 Opcode == ISD::LIFETIME_START ||
4829 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004830 (Opcode <= INT_MAX &&
4831 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4832 "Opcode is not a memory-accessing opcode!");
4833
Dale Johannesen839acbb2009-01-29 00:47:48 +00004834 // Memoize the node unless it returns a flag.
4835 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004836 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004837 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004838 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004839 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004840 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00004841 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004842 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004843 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004844 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004845
Jack Carter170a5f22013-09-09 22:02:08 +00004846 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4847 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004848 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004849 CSEMap.InsertNode(N, IP);
4850 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004851 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4852 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004853 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004854 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004855 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004856 return SDValue(N, 0);
4857}
4858
Chris Lattnerea952f02010-09-21 17:24:05 +00004859/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4860/// MachinePointerInfo record from it. This is particularly useful because the
4861/// code generator has many cases where it doesn't bother passing in a
4862/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4863static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4864 // If this is FI+Offset, we can model it.
4865 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4866 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4867
4868 // If this is (FI+Offset1)+Offset2, we can model it.
4869 if (Ptr.getOpcode() != ISD::ADD ||
4870 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4871 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4872 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004873
Chris Lattnerea952f02010-09-21 17:24:05 +00004874 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4875 return MachinePointerInfo::getFixedStack(FI, Offset+
4876 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4877}
4878
4879/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4880/// MachinePointerInfo record from it. This is particularly useful because the
4881/// code generator has many cases where it doesn't bother passing in a
4882/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4883static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4884 // If the 'Offset' value isn't a constant, we can't handle this.
4885 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4886 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4887 if (OffsetOp.getOpcode() == ISD::UNDEF)
4888 return InferPointerInfo(Ptr);
4889 return MachinePointerInfo();
4890}
Wesley Peck527da1b2010-11-23 03:31:01 +00004891
Chris Lattnerea952f02010-09-21 17:24:05 +00004892
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004893SDValue
4894SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004895 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004896 SDValue Ptr, SDValue Offset,
4897 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004898 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004899 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004900 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004901 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004902 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004903 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4904 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004905
Dan Gohman48b185d2009-09-25 20:36:54 +00004906 unsigned Flags = MachineMemOperand::MOLoad;
4907 if (isVolatile)
4908 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004909 if (isNonTemporal)
4910 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004911 if (isInvariant)
4912 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004913
Chris Lattnerea952f02010-09-21 17:24:05 +00004914 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4915 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004916 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004917 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004918
Chris Lattnerea952f02010-09-21 17:24:05 +00004919 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004920 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004921 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004922 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004923 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004924}
4925
4926SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004927SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004928 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004929 SDValue Ptr, SDValue Offset, EVT MemVT,
4930 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004931 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004932 ExtType = ISD::NON_EXTLOAD;
4933 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004934 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004935 } else {
4936 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004937 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4938 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004939 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004940 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004941 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004942 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004943 assert((!VT.isVector() ||
4944 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004945 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004946 }
4947
4948 bool Indexed = AM != ISD::UNINDEXED;
4949 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4950 "Unindexed load with an offset!");
4951
4952 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004953 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004954 SDValue Ops[] = { Chain, Ptr, Offset };
4955 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004956 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004957 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004958 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004959 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004960 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004961 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004962 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00004963 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004964 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004965 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004966 }
Jack Carter170a5f22013-09-09 22:02:08 +00004967 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4968 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004969 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004970 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004971 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004972 return SDValue(N, 0);
4973}
4974
Andrew Trickef9de2a2013-05-25 02:42:55 +00004975SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004976 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004977 MachinePointerInfo PtrInfo,
4978 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004979 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004980 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004981 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004982 SDValue Undef = getUNDEF(Ptr.getValueType());
4983 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004984 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004985 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004986}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004987
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004988SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4989 SDValue Chain, SDValue Ptr,
4990 MachineMemOperand *MMO) {
4991 SDValue Undef = getUNDEF(Ptr.getValueType());
4992 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4993 VT, MMO);
4994}
4995
Andrew Trickef9de2a2013-05-25 02:42:55 +00004996SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004997 SDValue Chain, SDValue Ptr,
4998 MachinePointerInfo PtrInfo, EVT MemVT,
4999 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005000 bool isInvariant, unsigned Alignment,
5001 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00005002 SDValue Undef = getUNDEF(Ptr.getValueType());
5003 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005004 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
5005 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00005006}
5007
5008
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005009SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
5010 SDValue Chain, SDValue Ptr, EVT MemVT,
5011 MachineMemOperand *MMO) {
5012 SDValue Undef = getUNDEF(Ptr.getValueType());
5013 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
5014 MemVT, MMO);
5015}
5016
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005017SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005018SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00005019 SDValue Offset, ISD::MemIndexedMode AM) {
5020 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
5021 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
5022 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00005023 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00005024 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00005025 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005026 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005027}
5028
Andrew Trickef9de2a2013-05-25 02:42:55 +00005029SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00005030 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00005031 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00005032 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00005033 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005034 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005035 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00005036 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005037
Dan Gohman48b185d2009-09-25 20:36:54 +00005038 unsigned Flags = MachineMemOperand::MOStore;
5039 if (isVolatile)
5040 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00005041 if (isNonTemporal)
5042 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00005043
Nick Lewyckyaad475b2014-04-15 07:22:52 +00005044 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00005045 PtrInfo = InferPointerInfo(Ptr);
5046
5047 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00005048 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00005049 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00005050 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00005051 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00005052
5053 return getStore(Chain, dl, Val, Ptr, MMO);
5054}
5055
Andrew Trickef9de2a2013-05-25 02:42:55 +00005056SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00005057 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00005058 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005059 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00005060 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00005061 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00005062 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005063 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5064 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005065 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005066 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00005067 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005068 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00005069 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005070 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005071 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005072 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005073 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005074 }
Jack Carter170a5f22013-09-09 22:02:08 +00005075 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5076 dl.getDebugLoc(), VTs,
5077 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005078 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005079 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005080 return SDValue(N, 0);
5081}
5082
Andrew Trickef9de2a2013-05-25 02:42:55 +00005083SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00005084 SDValue Ptr, MachinePointerInfo PtrInfo,
5085 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00005086 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00005087 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00005088 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005089 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00005090 if (Alignment == 0) // Ensure that codegen never sees alignment 0
5091 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00005092
Dan Gohman48b185d2009-09-25 20:36:54 +00005093 unsigned Flags = MachineMemOperand::MOStore;
5094 if (isVolatile)
5095 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00005096 if (isNonTemporal)
5097 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00005098
Nick Lewyckyaad475b2014-04-15 07:22:52 +00005099 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00005100 PtrInfo = InferPointerInfo(Ptr);
5101
5102 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00005103 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00005104 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00005105 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00005106
5107 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
5108}
5109
Andrew Trickef9de2a2013-05-25 02:42:55 +00005110SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00005111 SDValue Ptr, EVT SVT,
5112 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005113 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00005114
Michael Ilsemand5f91512012-09-10 16:56:31 +00005115 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00005116 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005117 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00005118 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005119
Dan Gohmancecad352009-12-14 23:40:38 +00005120 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
5121 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005122 assert(VT.isInteger() == SVT.isInteger() &&
5123 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00005124 assert(VT.isVector() == SVT.isVector() &&
5125 "Cannot use trunc store to convert to or from a vector!");
5126 assert((!VT.isVector() ||
5127 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
5128 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00005129
Owen Anderson9f944592009-08-11 20:47:22 +00005130 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00005131 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005132 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5133 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005134 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005135 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00005136 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005137 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00005138 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005139 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005140 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005141 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005142 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00005143 }
Jack Carter170a5f22013-09-09 22:02:08 +00005144 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5145 dl.getDebugLoc(), VTs,
5146 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005147 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005148 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005149 return SDValue(N, 0);
5150}
5151
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005152SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005153SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00005154 SDValue Offset, ISD::MemIndexedMode AM) {
5155 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5156 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
5157 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00005158 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005159 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5160 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005161 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005162 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00005163 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00005164 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00005165 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005166 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00005167 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005168
Jack Carter170a5f22013-09-09 22:02:08 +00005169 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
5170 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00005171 ST->isTruncatingStore(),
5172 ST->getMemoryVT(),
5173 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00005174 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00005175 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005176 return SDValue(N, 0);
5177}
5178
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005179SDValue
5180SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005181 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5182 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005183
5184 SDVTList VTs = getVTList(VT, MVT::Other);
5185 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5186 FoldingSetNodeID ID;
5187 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5188 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005189 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005190 MMO->isVolatile(),
5191 MMO->isNonTemporal(),
5192 MMO->isInvariant()));
5193 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5194 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005195 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005196 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5197 return SDValue(E, 0);
5198 }
5199 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5200 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005201 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005202 CSEMap.InsertNode(N, IP);
5203 InsertNode(N);
5204 return SDValue(N, 0);
5205}
5206
5207SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005208 SDValue Ptr, SDValue Mask, EVT MemVT,
5209 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005210 assert(Chain.getValueType() == MVT::Other &&
5211 "Invalid chain type");
5212 EVT VT = Val.getValueType();
5213 SDVTList VTs = getVTList(MVT::Other);
5214 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5215 FoldingSetNodeID ID;
5216 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5217 ID.AddInteger(VT.getRawBits());
5218 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5219 MMO->isNonTemporal(), MMO->isInvariant()));
5220 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5221 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005222 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005223 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5224 return SDValue(E, 0);
5225 }
5226 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5227 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005228 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005229 CSEMap.InsertNode(N, IP);
5230 InsertNode(N);
5231 return SDValue(N, 0);
5232}
5233
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005234SDValue
5235SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl,
5236 ArrayRef<SDValue> Ops,
5237 MachineMemOperand *MMO) {
5238
5239 FoldingSetNodeID ID;
5240 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5241 ID.AddInteger(VT.getRawBits());
5242 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5243 MMO->isVolatile(),
5244 MMO->isNonTemporal(),
5245 MMO->isInvariant()));
5246 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5247 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005248 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005249 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5250 return SDValue(E, 0);
5251 }
5252 MaskedGatherSDNode *N =
5253 new (NodeAllocator) MaskedGatherSDNode(dl.getIROrder(), dl.getDebugLoc(),
5254 Ops, VTs, VT, MMO);
5255 CSEMap.InsertNode(N, IP);
5256 InsertNode(N);
5257 return SDValue(N, 0);
5258}
5259
5260SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl,
5261 ArrayRef<SDValue> Ops,
5262 MachineMemOperand *MMO) {
5263 FoldingSetNodeID ID;
5264 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5265 ID.AddInteger(VT.getRawBits());
5266 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5267 MMO->isNonTemporal(),
5268 MMO->isInvariant()));
5269 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5270 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005271 if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
Elena Demikhovsky584ce372015-04-28 07:57:37 +00005272 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5273 return SDValue(E, 0);
5274 }
5275 SDNode *N =
5276 new (NodeAllocator) MaskedScatterSDNode(dl.getIROrder(), dl.getDebugLoc(),
5277 Ops, VTs, VT, MMO);
5278 CSEMap.InsertNode(N, IP);
5279 InsertNode(N);
5280 return SDValue(N, 0);
5281}
5282
Andrew Trickef9de2a2013-05-25 02:42:55 +00005283SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005284 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005285 SDValue SV,
5286 unsigned Align) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005287 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005288 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005289}
5290
Andrew Trickef9de2a2013-05-25 02:42:55 +00005291SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005292 ArrayRef<SDUse> Ops) {
5293 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005294 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005295 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005296 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5297 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005298 default: break;
5299 }
5300
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005301 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005302 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005303 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005304 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005305}
5306
Andrew Trickef9de2a2013-05-25 02:42:55 +00005307SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005308 ArrayRef<SDValue> Ops) {
5309 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005310 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005311 case 0: return getNode(Opcode, DL, VT);
5312 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5313 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5314 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005315 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005316 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005317
Chris Lattnerb0713c72005-04-09 03:27:28 +00005318 switch (Opcode) {
5319 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005320 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005321 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005322 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5323 "LHS and RHS of condition must have same type!");
5324 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5325 "True and False arms of SelectCC must have same type!");
5326 assert(Ops[2].getValueType() == VT &&
5327 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005328 break;
5329 }
5330 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005331 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005332 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5333 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005334 break;
5335 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005336 }
5337
Chris Lattner566307f2005-05-14 07:42:29 +00005338 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005339 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005340 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005341
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005342 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005343 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005344 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005345 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005346
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005347 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005348 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005349
Jack Carter170a5f22013-09-09 22:02:08 +00005350 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005351 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005352 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005353 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005354 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005355 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005356 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005357
Chandler Carruth41b20e72014-07-22 04:07:55 +00005358 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005359 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005360}
5361
Andrew Trickef9de2a2013-05-25 02:42:55 +00005362SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005363 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5364 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005365}
5366
Andrew Trickef9de2a2013-05-25 02:42:55 +00005367SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005368 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005369 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005370 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005371
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005372#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005373 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005374 // FIXME: figure out how to safely handle things like
5375 // int foo(int x) { return 1 << (x & 255); }
5376 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005377 case ISD::SRA_PARTS:
5378 case ISD::SRL_PARTS:
5379 case ISD::SHL_PARTS:
5380 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005381 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005382 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005383 else if (N3.getOpcode() == ISD::AND)
5384 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5385 // If the and is only masking out bits that cannot effect the shift,
5386 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005387 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005388 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005389 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005390 }
5391 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005392 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005393#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005394
Chris Lattnerf9c19152005-08-25 19:12:10 +00005395 // Memoize the node unless it returns a flag.
5396 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005397 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005398 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005399 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005400 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005401 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005402 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005403 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005404
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005405 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005406 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5407 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005408 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005409 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5410 DL.getDebugLoc(), VTList, Ops[0],
5411 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005412 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005413 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5414 DL.getDebugLoc(), VTList, Ops[0],
5415 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005416 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005417 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005418 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005419 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005420 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005421 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005422 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005423 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5424 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005425 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005426 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5427 DL.getDebugLoc(), VTList, Ops[0],
5428 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005429 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005430 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5431 DL.getDebugLoc(), VTList, Ops[0],
5432 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005433 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005434 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005435 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005436 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005437 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005438 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005439 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005440}
5441
Andrew Trickef9de2a2013-05-25 02:42:55 +00005442SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005443 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005444}
5445
Andrew Trickef9de2a2013-05-25 02:42:55 +00005446SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005447 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005448 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005449 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005450}
5451
Andrew Trickef9de2a2013-05-25 02:42:55 +00005452SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005453 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005454 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005455 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005456}
5457
Andrew Trickef9de2a2013-05-25 02:42:55 +00005458SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005459 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005460 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005461 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005462}
5463
Andrew Trickef9de2a2013-05-25 02:42:55 +00005464SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005465 SDValue N1, SDValue N2, SDValue N3,
5466 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005467 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005468 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005469}
5470
Andrew Trickef9de2a2013-05-25 02:42:55 +00005471SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005472 SDValue N1, SDValue N2, SDValue N3,
5473 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005474 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005475 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005476}
5477
Owen Anderson53aa7a92009-08-10 22:56:29 +00005478SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005479 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005480}
5481
Owen Anderson53aa7a92009-08-10 22:56:29 +00005482SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005483 FoldingSetNodeID ID;
5484 ID.AddInteger(2U);
5485 ID.AddInteger(VT1.getRawBits());
5486 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005487
Craig Topperc0196b12014-04-14 00:51:57 +00005488 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005489 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005490 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005491 EVT *Array = Allocator.Allocate<EVT>(2);
5492 Array[0] = VT1;
5493 Array[1] = VT2;
5494 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5495 VTListMap.InsertNode(Result, IP);
5496 }
5497 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005498}
Dan Gohman17059682008-07-17 19:10:17 +00005499
Owen Anderson53aa7a92009-08-10 22:56:29 +00005500SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005501 FoldingSetNodeID ID;
5502 ID.AddInteger(3U);
5503 ID.AddInteger(VT1.getRawBits());
5504 ID.AddInteger(VT2.getRawBits());
5505 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005506
Craig Topperc0196b12014-04-14 00:51:57 +00005507 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005508 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005509 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005510 EVT *Array = Allocator.Allocate<EVT>(3);
5511 Array[0] = VT1;
5512 Array[1] = VT2;
5513 Array[2] = VT3;
5514 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5515 VTListMap.InsertNode(Result, IP);
5516 }
5517 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005518}
5519
Owen Anderson53aa7a92009-08-10 22:56:29 +00005520SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005521 FoldingSetNodeID ID;
5522 ID.AddInteger(4U);
5523 ID.AddInteger(VT1.getRawBits());
5524 ID.AddInteger(VT2.getRawBits());
5525 ID.AddInteger(VT3.getRawBits());
5526 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005527
Craig Topperc0196b12014-04-14 00:51:57 +00005528 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005529 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005530 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005531 EVT *Array = Allocator.Allocate<EVT>(4);
5532 Array[0] = VT1;
5533 Array[1] = VT2;
5534 Array[2] = VT3;
5535 Array[3] = VT4;
5536 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5537 VTListMap.InsertNode(Result, IP);
5538 }
5539 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005540}
5541
Craig Topperabb4ac72014-04-16 06:10:51 +00005542SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5543 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005544 FoldingSetNodeID ID;
5545 ID.AddInteger(NumVTs);
5546 for (unsigned index = 0; index < NumVTs; index++) {
5547 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005548 }
5549
Craig Topperc0196b12014-04-14 00:51:57 +00005550 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005551 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005552 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005553 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005554 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005555 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5556 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005557 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005558 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005559}
5560
5561
Chris Lattnerf34156e2006-01-28 09:32:45 +00005562/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5563/// specified operands. If the resultant node already exists in the DAG,
5564/// this does not modify the specified node, instead it returns the node that
5565/// already exists. If the resultant node does not exist in the DAG, the
5566/// input node is returned. As a degenerate case, if you specify the same
5567/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005568SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005569 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005570
Chris Lattnerf34156e2006-01-28 09:32:45 +00005571 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005572 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005573
Chris Lattnerf34156e2006-01-28 09:32:45 +00005574 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005575 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005576 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005577 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005578
Dan Gohmanebeccb42008-07-21 22:38:59 +00005579 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005580 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005581 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005582 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005583
Chris Lattnerf34156e2006-01-28 09:32:45 +00005584 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005585 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005586
Chris Lattnerf34156e2006-01-28 09:32:45 +00005587 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005588 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005589 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005590}
5591
Dan Gohman92c11ac2010-06-18 15:30:29 +00005592SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005593 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005594
Chris Lattnerf34156e2006-01-28 09:32:45 +00005595 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005596 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005597 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005598
Chris Lattnerf34156e2006-01-28 09:32:45 +00005599 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005600 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005601 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005602 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005603
Dan Gohmanebeccb42008-07-21 22:38:59 +00005604 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005605 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005606 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005607 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005608
Chris Lattnerf34156e2006-01-28 09:32:45 +00005609 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005610 if (N->OperandList[0] != Op1)
5611 N->OperandList[0].set(Op1);
5612 if (N->OperandList[1] != Op2)
5613 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005614
Chris Lattnerf34156e2006-01-28 09:32:45 +00005615 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005616 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005617 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005618}
5619
Dan Gohman92c11ac2010-06-18 15:30:29 +00005620SDNode *SelectionDAG::
5621UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005622 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005623 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005624}
5625
Dan Gohman92c11ac2010-06-18 15:30:29 +00005626SDNode *SelectionDAG::
5627UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005628 SDValue Op3, SDValue Op4) {
5629 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005630 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005631}
5632
Dan Gohman92c11ac2010-06-18 15:30:29 +00005633SDNode *SelectionDAG::
5634UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005635 SDValue Op3, SDValue Op4, SDValue Op5) {
5636 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005637 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005638}
5639
Dan Gohman92c11ac2010-06-18 15:30:29 +00005640SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005641UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5642 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005643 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005644 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005645
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005646 // If no operands changed just return the input node.
Benjamin Kramer0b6742a2015-03-02 16:45:08 +00005647 if (Ops.empty() || std::equal(Ops.begin(), Ops.end(), N->op_begin()))
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00005648 return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005649
Chris Lattnerf34156e2006-01-28 09:32:45 +00005650 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005651 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005652 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005653 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005654
Dan Gohman2f83b472008-05-02 00:05:03 +00005655 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005656 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005657 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005658 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005659
Chris Lattnerf34156e2006-01-28 09:32:45 +00005660 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005661 for (unsigned i = 0; i != NumOps; ++i)
5662 if (N->OperandList[i] != Ops[i])
5663 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005664
5665 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005666 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005667 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005668}
5669
Dan Gohman91697632008-07-07 20:57:48 +00005670/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005671/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005672void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005673 // Unlike the code in MorphNodeTo that does this, we don't need to
5674 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005675 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5676 SDUse &Use = *I++;
5677 Use.set(SDValue());
5678 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005679}
Chris Lattner19732782005-08-16 18:17:10 +00005680
Dan Gohman17059682008-07-17 19:10:17 +00005681/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5682/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005683///
Dan Gohman17059682008-07-17 19:10:17 +00005684SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005685 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005686 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005687 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005688}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005689
Dan Gohman17059682008-07-17 19:10:17 +00005690SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005691 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005692 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005693 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005694 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005695}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005696
Dan Gohman17059682008-07-17 19:10:17 +00005697SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005698 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005699 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005700 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005701 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005702 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005703}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005704
Dan Gohman17059682008-07-17 19:10:17 +00005705SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005706 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005707 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005708 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005709 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005710 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005711}
Chris Lattner466fece2005-08-21 22:30:30 +00005712
Dan Gohman17059682008-07-17 19:10:17 +00005713SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005714 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005715 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005716 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005717}
5718
Dan Gohman17059682008-07-17 19:10:17 +00005719SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005720 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005721 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005722 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005723}
5724
Dan Gohman17059682008-07-17 19:10:17 +00005725SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005726 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005727 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005728 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005729}
5730
Dan Gohman17059682008-07-17 19:10:17 +00005731SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005732 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005733 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005734 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005735 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005736}
5737
Bill Wendling2d598632008-12-01 23:28:22 +00005738SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005739 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005740 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005741 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005742 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005743}
5744
Scott Michelcf0da6c2009-02-17 22:15:04 +00005745SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005746 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005747 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005748 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005749 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005750 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005751}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005752
Scott Michelcf0da6c2009-02-17 22:15:04 +00005753SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005754 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005755 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005756 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005757 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005758 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005759}
5760
Dan Gohman17059682008-07-17 19:10:17 +00005761SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005762 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005763 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005764 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005765 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005766 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005767 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005768}
5769
Dan Gohman17059682008-07-17 19:10:17 +00005770SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005771 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005772 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005773 SDValue Op3) {
5774 SDVTList VTs = getVTList(VT1, VT2, VT3);
5775 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005776 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005777}
5778
5779SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005780 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005781 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005782 // Reset the NodeID to -1.
5783 N->setNodeId(-1);
5784 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005785}
5786
Andrew Trickef9de2a2013-05-25 02:42:55 +00005787/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005788/// the line number information on the merged node since it is not possible to
5789/// preserve the information that operation is associated with multiple lines.
5790/// This will make the debugger working better at -O0, were there is a higher
5791/// probability having other instructions associated with that line.
5792///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005793/// For IROrder, we keep the smaller of the two
5794SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005795 DebugLoc NLoc = N->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00005796 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005797 N->setDebugLoc(DebugLoc());
5798 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005799 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5800 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005801 return N;
5802}
5803
Chris Lattneraf197502010-02-28 21:36:14 +00005804/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005805/// return type, opcode, and operands.
5806///
5807/// Note that MorphNodeTo returns the resultant node. If there is already a
5808/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005809/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005810///
5811/// Using MorphNodeTo is faster than creating a new node and swapping it in
5812/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005813/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005814/// the node's users.
5815///
Chandler Carruth356665a2014-08-01 22:09:43 +00005816/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5817/// As a consequence it isn't appropriate to use from within the DAG combiner or
5818/// the legalizer which maintain worklists that would need to be updated when
5819/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005820SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005821 SDVTList VTs, ArrayRef<SDValue> Ops) {
5822 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005823 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005824 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005825 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005826 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005827 AddNodeIDNode(ID, Opc, VTs, Ops);
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00005828 if (SDNode *ON = FindNodeOrInsertPos(ID, N->getDebugLoc(), IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005829 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005830 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005831
Dan Gohmand3fe1742008-09-13 01:54:27 +00005832 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005833 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005834
Dan Gohman17059682008-07-17 19:10:17 +00005835 // Start the morphing.
5836 N->NodeType = Opc;
5837 N->ValueList = VTs.VTs;
5838 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005839
Dan Gohman17059682008-07-17 19:10:17 +00005840 // Clear the operands list, updating used nodes to remove this from their
5841 // use list. Keep track of any operands that become dead as a result.
5842 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005843 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5844 SDUse &Use = *I++;
5845 SDNode *Used = Use.getNode();
5846 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005847 if (Used->use_empty())
5848 DeadNodeSet.insert(Used);
5849 }
5850
Dan Gohman48b185d2009-09-25 20:36:54 +00005851 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5852 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005853 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005854 // If NumOps is larger than the # of operands we can have in a
5855 // MachineSDNode, reallocate the operand list.
5856 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5857 if (MN->OperandsNeedDelete)
5858 delete[] MN->OperandList;
5859 if (NumOps > array_lengthof(MN->LocalOperands))
5860 // We're creating a final node that will live unmorphed for the
5861 // remainder of the current SelectionDAG iteration, so we can allocate
5862 // the operands directly out of a pool with no recycling metadata.
5863 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005864 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005865 else
Craig Topper131de822014-04-27 19:21:16 +00005866 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005867 MN->OperandsNeedDelete = false;
5868 } else
Craig Topper131de822014-04-27 19:21:16 +00005869 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005870 } else {
5871 // If NumOps is larger than the # of operands we currently have, reallocate
5872 // the operand list.
5873 if (NumOps > N->NumOperands) {
5874 if (N->OperandsNeedDelete)
5875 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005876 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005877 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005878 } else
Craig Topper131de822014-04-27 19:21:16 +00005879 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005880 }
5881
5882 // Delete any nodes that are still dead after adding the uses for the
5883 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005884 if (!DeadNodeSet.empty()) {
5885 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005886 for (SDNode *N : DeadNodeSet)
5887 if (N->use_empty())
5888 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005889 RemoveDeadNodes(DeadNodes);
5890 }
Dan Gohman91697632008-07-07 20:57:48 +00005891
Dan Gohman17059682008-07-17 19:10:17 +00005892 if (IP)
5893 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005894 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005895}
5896
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005897
Dan Gohman32f71d72009-09-25 18:54:59 +00005898/// getMachineNode - These are used for target selectors to create a new node
5899/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005900///
Dan Gohman32f71d72009-09-25 18:54:59 +00005901/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005902/// node of the specified opcode and operands, it returns that node instead of
5903/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005904MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005905SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005906 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005907 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005908}
Bill Wendlinga434d932009-01-29 09:01:55 +00005909
Dan Gohmana22f2d82009-10-10 01:29:16 +00005910MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005911SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005912 SDVTList VTs = getVTList(VT);
5913 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005914 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005915}
Bill Wendlinga434d932009-01-29 09:01:55 +00005916
Dan Gohmana22f2d82009-10-10 01:29:16 +00005917MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005918SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005919 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005920 SDVTList VTs = getVTList(VT);
5921 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005922 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005923}
Bill Wendlinga434d932009-01-29 09:01:55 +00005924
Dan Gohmana22f2d82009-10-10 01:29:16 +00005925MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005926SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005927 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005928 SDVTList VTs = getVTList(VT);
5929 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005930 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005931}
Bill Wendlinga434d932009-01-29 09:01:55 +00005932
Dan Gohmana22f2d82009-10-10 01:29:16 +00005933MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005934SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005935 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005936 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005937 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005938}
Bill Wendlinga434d932009-01-29 09:01:55 +00005939
Dan Gohmana22f2d82009-10-10 01:29:16 +00005940MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005941SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005942 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005943 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005944}
Bill Wendlinga434d932009-01-29 09:01:55 +00005945
Dan Gohmana22f2d82009-10-10 01:29:16 +00005946MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005947SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005948 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005949 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005950 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005951 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005952}
Bill Wendlinga434d932009-01-29 09:01:55 +00005953
Dan Gohmana22f2d82009-10-10 01:29:16 +00005954MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005955SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005956 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005957 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005958 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005959 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005960}
5961
Dan Gohmana22f2d82009-10-10 01:29:16 +00005962MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005963SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005964 EVT VT1, EVT VT2, SDValue Op1,
5965 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005966 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005967 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005968 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005969}
5970
Dan Gohmana22f2d82009-10-10 01:29:16 +00005971MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005972SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005973 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005974 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005975 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005976 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005977}
Bill Wendlinga434d932009-01-29 09:01:55 +00005978
Dan Gohmana22f2d82009-10-10 01:29:16 +00005979MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005980SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005981 EVT VT1, EVT VT2, EVT VT3,
5982 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005983 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005984 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005985 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005986}
Bill Wendlinga434d932009-01-29 09:01:55 +00005987
Dan Gohmana22f2d82009-10-10 01:29:16 +00005988MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005989SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005990 EVT VT1, EVT VT2, EVT VT3,
5991 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005992 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005993 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005994 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005995}
Bill Wendlinga434d932009-01-29 09:01:55 +00005996
Dan Gohmana22f2d82009-10-10 01:29:16 +00005997MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005998SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005999 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00006000 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00006001 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00006002 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00006003}
Bill Wendlinga434d932009-01-29 09:01:55 +00006004
Dan Gohmana22f2d82009-10-10 01:29:16 +00006005MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00006006SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00006007 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00006008 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00006009 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00006010 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00006011}
6012
Dan Gohmana22f2d82009-10-10 01:29:16 +00006013MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00006014SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006015 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00006016 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00006017 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00006018 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00006019}
6020
Dan Gohmana22f2d82009-10-10 01:29:16 +00006021MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00006022SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00006023 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00006024 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00006025 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00006026 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00006027 const SDValue *Ops = OpsArray.data();
6028 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00006029
6030 if (DoCSE) {
6031 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00006032 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00006033 IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00006034 if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006035 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00006036 }
Dan Gohman48b185d2009-09-25 20:36:54 +00006037 }
6038
6039 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00006040 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
6041 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00006042
6043 // Initialize the operands list.
6044 if (NumOps > array_lengthof(N->LocalOperands))
6045 // We're creating a final node that will live unmorphed for the
6046 // remainder of the current SelectionDAG iteration, so we can allocate
6047 // the operands directly out of a pool with no recycling metadata.
6048 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
6049 Ops, NumOps);
6050 else
6051 N->InitOperands(N->LocalOperands, Ops, NumOps);
6052 N->OperandsNeedDelete = false;
6053
6054 if (DoCSE)
6055 CSEMap.InsertNode(N, IP);
6056
Chandler Carruth41b20e72014-07-22 04:07:55 +00006057 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00006058 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00006059}
Evan Chengd3f1db92006-02-09 07:15:23 +00006060
Dan Gohmanac33a902009-08-19 18:16:17 +00006061/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00006062/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00006063SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006064SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00006065 SDValue Operand) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006066 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00006067 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00006068 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00006069 return SDValue(Subreg, 0);
6070}
6071
Bob Wilson2a45a652009-10-08 18:49:46 +00006072/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00006073/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00006074SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006075SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00006076 SDValue Operand, SDValue Subreg) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006077 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00006078 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00006079 VT, Operand, Subreg, SRIdxVal);
6080 return SDValue(Result, 0);
6081}
6082
Evan Cheng31604a62008-03-22 01:55:50 +00006083/// getNodeIfExists - Get the specified node if it's already available, or
6084/// else return NULL.
6085SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Sanjay Patelf1340482015-06-16 16:25:43 +00006086 ArrayRef<SDValue> Ops,
6087 const SDNodeFlags *Flags) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00006088 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00006089 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00006090 AddNodeIDNode(ID, Opcode, VTList, Ops);
Sanjay Patelf1340482015-06-16 16:25:43 +00006091 AddNodeIDFlags(ID, Opcode, Flags);
Craig Topperc0196b12014-04-14 00:51:57 +00006092 void *IP = nullptr;
Sergey Dmitrouk46c4f022015-05-13 08:58:03 +00006093 if (SDNode *E = FindNodeOrInsertPos(ID, DebugLoc(), IP))
Evan Cheng31604a62008-03-22 01:55:50 +00006094 return E;
6095 }
Craig Topperc0196b12014-04-14 00:51:57 +00006096 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00006097}
6098
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006099/// getDbgValue - Creates a SDDbgValue node.
6100///
Adrian Prantl32da8892014-04-25 20:49:25 +00006101/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006102SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
6103 unsigned R, bool IsIndirect, uint64_t Off,
6104 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006105 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006106 "Expected inlined-at fields to agree");
Duncan P. N. Exon Smith0c541972015-05-22 05:45:19 +00006107 return new (DbgInfo->getAlloc())
6108 SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006109}
6110
Adrian Prantl32da8892014-04-25 20:49:25 +00006111/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006112SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
6113 const Value *C, uint64_t Off,
6114 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006115 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006116 "Expected inlined-at fields to agree");
Duncan P. N. Exon Smith0c541972015-05-22 05:45:19 +00006117 return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006118}
6119
Adrian Prantl32da8892014-04-25 20:49:25 +00006120/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006121SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
6122 unsigned FI, uint64_t Off,
6123 DebugLoc DL, unsigned O) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00006124 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00006125 "Expected inlined-at fields to agree");
Duncan P. N. Exon Smith0c541972015-05-22 05:45:19 +00006126 return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00006127}
6128
Dan Gohman7d099f72010-03-03 21:33:37 +00006129namespace {
6130
Dan Gohman9cc886b2010-03-04 19:11:28 +00006131/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00006132/// pointed to by a use iterator is deleted, increment the use iterator
6133/// so that it doesn't dangle.
6134///
Dan Gohman7d099f72010-03-03 21:33:37 +00006135class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00006136 SDNode::use_iterator &UI;
6137 SDNode::use_iterator &UE;
6138
Craig Topper7b883b32014-03-08 06:31:39 +00006139 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00006140 // Increment the iterator as needed.
6141 while (UI != UE && N == *UI)
6142 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00006143 }
6144
6145public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006146 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00006147 SDNode::use_iterator &ui,
6148 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006149 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00006150};
6151
Alexander Kornienkof00654e2015-06-23 09:49:53 +00006152}
Dan Gohman7d099f72010-03-03 21:33:37 +00006153
Evan Cheng445b91a2006-08-07 22:13:29 +00006154/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006155/// This can cause recursive merging of nodes in the DAG.
6156///
Chris Lattner76858912008-02-03 03:35:22 +00006157/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00006158///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006159void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006160 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006161 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00006162 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00006163 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00006164
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006165 // Iterate over all the existing uses of From. New uses will be added
6166 // to the beginning of the use list, which we avoid visiting.
6167 // This specifically avoids visiting uses of From that arise while the
6168 // replacement is happening, because any such uses would be the result
6169 // of CSE: If an existing node looks like From after one of its operands
6170 // is replaced by To, we don't want to replace of all its users with To
6171 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006172 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006173 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006174 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006175 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006176
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006177 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006178 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006179
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006180 // A user can appear in a use list multiple times, and when this
6181 // happens the uses are usually next to each other in the list.
6182 // To help reduce the number of CSE recomputations, process all
6183 // the uses of this user that we can find this way.
6184 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006185 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006186 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006187 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006188 } while (UI != UE && *UI == User);
6189
6190 // Now that we have modified User, add it back to the CSE maps. If it
6191 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006192 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00006193 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006194
6195 // If we just RAUW'd the root, take note.
6196 if (FromN == getRoot())
6197 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00006198}
6199
6200/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6201/// This can cause recursive merging of nodes in the DAG.
6202///
Dan Gohman8aa28b92009-04-15 20:06:30 +00006203/// This version assumes that for each value of From, there is a
6204/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00006205///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006206void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00006207#ifndef NDEBUG
6208 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6209 assert((!From->hasAnyUseOfValue(i) ||
6210 From->getValueType(i) == To->getValueType(i)) &&
6211 "Cannot use this version of ReplaceAllUsesWith!");
6212#endif
Dan Gohman17059682008-07-17 19:10:17 +00006213
6214 // Handle the trivial case.
6215 if (From == To)
6216 return;
6217
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006218 // Iterate over just the existing users of From. See the comments in
6219 // the ReplaceAllUsesWith above.
6220 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006221 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006222 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006223 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006224
Chris Lattner373f0482005-08-26 18:36:28 +00006225 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006226 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006227
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006228 // A user can appear in a use list multiple times, and when this
6229 // happens the uses are usually next to each other in the list.
6230 // To help reduce the number of CSE recomputations, process all
6231 // the uses of this user that we can find this way.
6232 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006233 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006234 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006235 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006236 } while (UI != UE && *UI == User);
6237
6238 // Now that we have modified User, add it back to the CSE maps. If it
6239 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006240 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006241 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006242
6243 // If we just RAUW'd the root, take note.
6244 if (From == getRoot().getNode())
6245 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006246}
6247
Chris Lattner373f0482005-08-26 18:36:28 +00006248/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6249/// This can cause recursive merging of nodes in the DAG.
6250///
6251/// This version can replace From with any result values. To must match the
6252/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006253void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006254 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006255 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006256
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006257 // Iterate over just the existing users of From. See the comments in
6258 // the ReplaceAllUsesWith above.
6259 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006260 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006261 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006262 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006263
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006264 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006265 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006266
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006267 // A user can appear in a use list multiple times, and when this
6268 // happens the uses are usually next to each other in the list.
6269 // To help reduce the number of CSE recomputations, process all
6270 // the uses of this user that we can find this way.
6271 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006272 SDUse &Use = UI.getUse();
6273 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006274 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006275 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006276 } while (UI != UE && *UI == User);
6277
6278 // Now that we have modified User, add it back to the CSE maps. If it
6279 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006280 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006281 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006282
6283 // If we just RAUW'd the root, take note.
6284 if (From == getRoot().getNode())
6285 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006286}
6287
Chris Lattner375e1a72006-02-17 21:58:01 +00006288/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006289/// uses of other values produced by From.getNode() alone. The Deleted
6290/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006291void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006292 // Handle the really simple, really trivial case efficiently.
6293 if (From == To) return;
6294
Chris Lattner375e1a72006-02-17 21:58:01 +00006295 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006296 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006297 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006298 return;
6299 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006300
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006301 // Iterate over just the existing users of From. See the comments in
6302 // the ReplaceAllUsesWith above.
6303 SDNode::use_iterator UI = From.getNode()->use_begin(),
6304 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006305 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006306 while (UI != UE) {
6307 SDNode *User = *UI;
6308 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006309
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006310 // A user can appear in a use list multiple times, and when this
6311 // happens the uses are usually next to each other in the list.
6312 // To help reduce the number of CSE recomputations, process all
6313 // the uses of this user that we can find this way.
6314 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006315 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006316
6317 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006318 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006319 ++UI;
6320 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006321 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006322
6323 // If this node hasn't been modified yet, it's still in the CSE maps,
6324 // so remove its old self from the CSE maps.
6325 if (!UserRemovedFromCSEMaps) {
6326 RemoveNodeFromCSEMaps(User);
6327 UserRemovedFromCSEMaps = true;
6328 }
6329
6330 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006331 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006332 } while (UI != UE && *UI == User);
6333
6334 // We are iterating over all uses of the From node, so if a use
6335 // doesn't use the specific value, no changes are made.
6336 if (!UserRemovedFromCSEMaps)
6337 continue;
6338
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006339 // Now that we have modified User, add it back to the CSE maps. If it
6340 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006341 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006342 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006343
6344 // If we just RAUW'd the root, take note.
6345 if (From == getRoot())
6346 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006347}
6348
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006349namespace {
6350 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6351 /// to record information about a use.
6352 struct UseMemo {
6353 SDNode *User;
6354 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006355 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006356 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006357
6358 /// operator< - Sort Memos by User.
6359 bool operator<(const UseMemo &L, const UseMemo &R) {
6360 return (intptr_t)L.User < (intptr_t)R.User;
6361 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00006362}
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006363
Dan Gohman17059682008-07-17 19:10:17 +00006364/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006365/// uses of other values produced by From.getNode() alone. The same value
6366/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006367/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006368void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6369 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006370 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006371 // Handle the simple, trivial case efficiently.
6372 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006373 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006374
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006375 // Read up all the uses and make records of them. This helps
6376 // processing new uses that are introduced during the
6377 // replacement process.
6378 SmallVector<UseMemo, 4> Uses;
6379 for (unsigned i = 0; i != Num; ++i) {
6380 unsigned FromResNo = From[i].getResNo();
6381 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006382 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006383 E = FromNode->use_end(); UI != E; ++UI) {
6384 SDUse &Use = UI.getUse();
6385 if (Use.getResNo() == FromResNo) {
6386 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006387 Uses.push_back(Memo);
6388 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006389 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006390 }
Dan Gohman17059682008-07-17 19:10:17 +00006391
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006392 // Sort the uses, so that all the uses from a given User are together.
6393 std::sort(Uses.begin(), Uses.end());
6394
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006395 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6396 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006397 // We know that this user uses some value of From. If it is the right
6398 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006399 SDNode *User = Uses[UseIndex].User;
6400
6401 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006402 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006403
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006404 // The Uses array is sorted, so all the uses for a given User
6405 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006406 // To help reduce the number of CSE recomputations, process all
6407 // the uses of this user that we can find this way.
6408 do {
6409 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006410 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006411 ++UseIndex;
6412
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006413 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006414 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6415
Dan Gohman17059682008-07-17 19:10:17 +00006416 // Now that we have modified User, add it back to the CSE maps. If it
6417 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006418 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006419 }
6420}
6421
Evan Cheng9631a602006-08-01 08:20:41 +00006422/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006423/// based on their topological order. It returns the maximum id and a vector
6424/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006425unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006426
Dan Gohman86aa16a2008-09-30 18:30:35 +00006427 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006428
Dan Gohman86aa16a2008-09-30 18:30:35 +00006429 // SortedPos tracks the progress of the algorithm. Nodes before it are
6430 // sorted, nodes after it are unsorted. When the algorithm completes
6431 // it is at the end of the list.
6432 allnodes_iterator SortedPos = allnodes_begin();
6433
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006434 // Visit all the nodes. Move nodes with no operands to the front of
6435 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006436 // operand count. Before we do this, the Node Id fields of the nodes
6437 // may contain arbitrary values. After, the Node Id fields for nodes
6438 // before SortedPos will contain the topological sort index, and the
6439 // Node Id fields for nodes At SortedPos and after will contain the
6440 // count of outstanding operands.
6441 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6442 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006443 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006444 unsigned Degree = N->getNumOperands();
6445 if (Degree == 0) {
6446 // A node with no uses, add it to the result array immediately.
6447 N->setNodeId(DAGSize++);
6448 allnodes_iterator Q = N;
6449 if (Q != SortedPos)
6450 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006451 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006452 ++SortedPos;
6453 } else {
6454 // Temporarily use the Node Id as scratch space for the degree count.
6455 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006456 }
6457 }
6458
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006459 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006460 // such that by the time the end is reached all nodes will be sorted.
6461 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6462 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006463 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006464 // N is in sorted position, so all its uses have one less operand
6465 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006466 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6467 UI != UE; ++UI) {
6468 SDNode *P = *UI;
6469 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006470 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006471 --Degree;
6472 if (Degree == 0) {
6473 // All of P's operands are sorted, so P may sorted now.
6474 P->setNodeId(DAGSize++);
6475 if (P != SortedPos)
6476 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006477 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006478 ++SortedPos;
6479 } else {
6480 // Update P's outstanding operand count.
6481 P->setNodeId(Degree);
6482 }
6483 }
David Greene3b2a68c2010-01-20 00:59:23 +00006484 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006485#ifndef NDEBUG
6486 SDNode *S = ++I;
6487 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006488 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006489 dbgs() << "Checking if this is due to cycles\n";
6490 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006491#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006492 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006493 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006494 }
6495
6496 assert(SortedPos == AllNodes.end() &&
6497 "Topological sort incomplete!");
6498 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6499 "First node in topological sort is not the entry token!");
6500 assert(AllNodes.front().getNodeId() == 0 &&
6501 "First node in topological sort has non-zero id!");
6502 assert(AllNodes.front().getNumOperands() == 0 &&
6503 "First node in topological sort has operands!");
6504 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6505 "Last node in topologic sort has unexpected id!");
6506 assert(AllNodes.back().use_empty() &&
6507 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006508 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006509 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006510}
6511
Evan Cheng563fe3c2010-03-25 01:38:16 +00006512/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6513/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006514void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006515 if (SD) {
6516 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006517 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006518 }
6519 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006520}
Evan Cheng29eefc12006-07-27 06:39:06 +00006521
Devang Patelefc6b162011-01-25 23:27:42 +00006522/// TransferDbgValues - Transfer SDDbgValues.
6523void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6524 if (From == To || !From.getNode()->getHasDebugValue())
6525 return;
6526 SDNode *FromNode = From.getNode();
6527 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006528 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006529 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006530 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006531 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006532 SDDbgValue *Dbg = *I;
6533 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006534 SDDbgValue *Clone =
6535 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6536 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6537 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006538 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006539 }
6540 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006541 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006542 E = ClonedDVs.end(); I != E; ++I)
6543 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006544}
6545
Jim Laskeyd66e6162005-08-17 20:08:02 +00006546//===----------------------------------------------------------------------===//
6547// SDNode Class
6548//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006549
Chris Lattner3bf17b62007-02-04 02:41:42 +00006550HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006551 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006552}
6553
Andrew Trickef9de2a2013-05-25 02:42:55 +00006554GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6555 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006556 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006557 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006558 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006559}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006560
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006561AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6562 SDValue X, unsigned SrcAS,
6563 unsigned DestAS)
6564 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6565 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6566
Andrew Trickef9de2a2013-05-25 02:42:55 +00006567MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6568 EVT memvt, MachineMemOperand *mmo)
6569 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006570 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006571 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006572 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006573 assert(isNonTemporal() == MMO->isNonTemporal() &&
6574 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006575 // We check here that the size of the memory operand fits within the size of
6576 // the MMO. This is because the MMO might indicate only a possible address
6577 // range instead of specifying the affected memory addresses precisely.
6578 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006579}
6580
Andrew Trickef9de2a2013-05-25 02:42:55 +00006581MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006582 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6583 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006584 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006585 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006586 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006587 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006588 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006589}
6590
Jim Laskeyf576b422006-10-27 23:46:08 +00006591/// Profile - Gather unique data for the node.
6592///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006593void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006594 AddNodeIDNode(ID, this);
6595}
6596
Owen Anderson3b1665e2009-08-25 22:27:22 +00006597namespace {
6598 struct EVTArray {
6599 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006600
Owen Anderson3b1665e2009-08-25 22:27:22 +00006601 EVTArray() {
6602 VTs.reserve(MVT::LAST_VALUETYPE);
6603 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6604 VTs.push_back(MVT((MVT::SimpleValueType)i));
6605 }
6606 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +00006607}
Owen Anderson3b1665e2009-08-25 22:27:22 +00006608
Owen Anderson53aa7a92009-08-10 22:56:29 +00006609static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006610static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006611static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006612
Chris Lattner88fa11c2005-11-08 23:30:28 +00006613/// getValueTypeList - Return a pointer to the specified value type.
6614///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006615const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006616 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006617 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006618 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006619 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006620 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006621 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006622 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006623 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006624}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006625
Chris Lattner40e79822005-01-12 18:37:47 +00006626/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6627/// indicated value. This method ignores uses of other values defined by this
6628/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006629bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006630 assert(Value < getNumValues() && "Bad value!");
6631
Roman Levenstein51f532f2008-04-07 10:06:32 +00006632 // TODO: Only iterate over uses of a given value of the node
6633 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006634 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006635 if (NUses == 0)
6636 return false;
6637 --NUses;
6638 }
Chris Lattner40e79822005-01-12 18:37:47 +00006639 }
6640
6641 // Found exactly the right number of uses?
6642 return NUses == 0;
6643}
6644
6645
Evan Cheng358c3d12007-08-02 05:29:38 +00006646/// hasAnyUseOfValue - Return true if there are any use of the indicated
6647/// value. This method ignores uses of other values defined by this operation.
6648bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6649 assert(Value < getNumValues() && "Bad value!");
6650
Dan Gohman7a510c22008-07-09 22:39:01 +00006651 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006652 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006653 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006654
6655 return false;
6656}
6657
6658
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006659/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006660///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006661bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006662 bool Seen = false;
6663 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006664 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006665 if (User == this)
6666 Seen = true;
6667 else
6668 return false;
6669 }
6670
6671 return Seen;
6672}
6673
Evan Cheng9456dd82006-11-03 07:31:32 +00006674/// isOperand - Return true if this node is an operand of N.
6675///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006676bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006677 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6678 if (*this == N->getOperand(i))
6679 return true;
6680 return false;
6681}
6682
Evan Cheng567d2e52008-03-04 00:41:45 +00006683bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006684 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006685 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006686 return true;
6687 return false;
6688}
Evan Chengd37645c2006-02-05 06:29:23 +00006689
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006690/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006691/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006692/// side-effecting instructions on any chain path. In practice, this looks
6693/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006694/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006695bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006696 unsigned Depth) const {
6697 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006698
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006699 // Don't search too deeply, we just want to be able to see through
6700 // TokenFactor's etc.
6701 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006702
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006703 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006704 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006705 if (getOpcode() == ISD::TokenFactor) {
6706 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006707 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6708 return false;
6709 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006710 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006711
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006712 // Loads don't have side effects, look through them.
6713 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6714 if (!Ld->isVolatile())
6715 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6716 }
6717 return false;
6718}
6719
Lang Hames5a004992011-07-07 04:31:51 +00006720/// hasPredecessor - Return true if N is a predecessor of this node.
6721/// N is either an operand of this node, or can be reached by recursively
6722/// traversing up the operands.
6723/// NOTE: This is an expensive method. Use it carefully.
6724bool SDNode::hasPredecessor(const SDNode *N) const {
6725 SmallPtrSet<const SDNode *, 32> Visited;
6726 SmallVector<const SDNode *, 16> Worklist;
6727 return hasPredecessorHelper(N, Visited, Worklist);
6728}
Dan Gohmancd139c02009-10-28 03:44:30 +00006729
Craig Topperb94011f2013-07-14 04:42:23 +00006730bool
6731SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006732 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006733 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006734 if (Visited.empty()) {
6735 Worklist.push_back(this);
6736 } else {
6737 // Take a look in the visited set. If we've already encountered this node
6738 // we needn't search further.
6739 if (Visited.count(N))
6740 return true;
6741 }
6742
6743 // Haven't visited N yet. Continue the search.
6744 while (!Worklist.empty()) {
6745 const SDNode *M = Worklist.pop_back_val();
6746 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6747 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006748 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006749 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006750 if (Op == N)
6751 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006752 }
Lang Hames5a004992011-07-07 04:31:51 +00006753 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006754
6755 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006756}
6757
Evan Cheng5d9fd972006-10-04 00:56:09 +00006758uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6759 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006760 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006761}
6762
Mon P Wang32f8bb92009-11-30 02:42:02 +00006763SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6764 assert(N->getNumValues() == 1 &&
6765 "Can't unroll a vector with multiple results!");
6766
6767 EVT VT = N->getValueType(0);
6768 unsigned NE = VT.getVectorNumElements();
6769 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006770 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006771
6772 SmallVector<SDValue, 8> Scalars;
6773 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6774
6775 // If ResNE is 0, fully unroll the vector op.
6776 if (ResNE == 0)
6777 ResNE = NE;
6778 else if (NE > ResNE)
6779 NE = ResNE;
6780
6781 unsigned i;
6782 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006783 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006784 SDValue Operand = N->getOperand(j);
6785 EVT OperandVT = Operand.getValueType();
6786 if (OperandVT.isVector()) {
6787 // A vector operand; extract a single element.
6788 EVT OperandEltVT = OperandVT.getVectorElementType();
6789 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6790 OperandEltVT,
6791 Operand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006792 getConstant(i, dl, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006793 } else {
6794 // A scalar operand; just use it as is.
6795 Operands[j] = Operand;
6796 }
6797 }
6798
6799 switch (N->getOpcode()) {
6800 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006801 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006802 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006803 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006804 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006805 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006806 case ISD::SHL:
6807 case ISD::SRA:
6808 case ISD::SRL:
6809 case ISD::ROTL:
6810 case ISD::ROTR:
6811 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006812 getShiftAmountOperand(Operands[0].getValueType(),
6813 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006814 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006815 case ISD::SIGN_EXTEND_INREG:
6816 case ISD::FP_ROUND_INREG: {
6817 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6818 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6819 Operands[0],
6820 getValueType(ExtVT)));
6821 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006822 }
6823 }
6824
6825 for (; i < ResNE; ++i)
6826 Scalars.push_back(getUNDEF(EltVT));
6827
6828 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006829 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006830}
6831
Evan Chengf5938d52009-12-09 01:36:00 +00006832
Wesley Peck527da1b2010-11-23 03:31:01 +00006833/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6834/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006835/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006836bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006837 unsigned Bytes, int Dist) const {
6838 if (LD->getChain() != Base->getChain())
6839 return false;
6840 EVT VT = LD->getValueType(0);
6841 if (VT.getSizeInBits() / 8 != Bytes)
6842 return false;
6843
6844 SDValue Loc = LD->getOperand(1);
6845 SDValue BaseLoc = Base->getOperand(1);
6846 if (Loc.getOpcode() == ISD::FrameIndex) {
6847 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6848 return false;
6849 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6850 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6851 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6852 int FS = MFI->getObjectSize(FI);
6853 int BFS = MFI->getObjectSize(BFI);
6854 if (FS != BFS || FS != (int)Bytes) return false;
6855 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6856 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006857
Sanjay Patel7129c102014-12-16 21:57:18 +00006858 // Handle X + C.
6859 if (isBaseWithConstantOffset(Loc)) {
6860 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6861 if (Loc.getOperand(0) == BaseLoc) {
6862 // If the base location is a simple address with no offset itself, then
6863 // the second load's first add operand should be the base address.
6864 if (LocOffset == Dist * (int)Bytes)
6865 return true;
6866 } else if (isBaseWithConstantOffset(BaseLoc)) {
6867 // The base location itself has an offset, so subtract that value from the
6868 // second load's offset before comparing to distance * size.
6869 int64_t BOffset =
6870 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6871 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6872 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6873 return true;
6874 }
6875 }
6876 }
Craig Topperc0196b12014-04-14 00:51:57 +00006877 const GlobalValue *GV1 = nullptr;
6878 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006879 int64_t Offset1 = 0;
6880 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006881 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6882 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006883 if (isGA1 && isGA2 && GV1 == GV2)
6884 return Offset1 == (Offset2 + Dist*Bytes);
6885 return false;
6886}
6887
6888
Evan Cheng34a23ea2009-12-09 01:04:59 +00006889/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6890/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006891unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006892 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006893 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006894 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006895 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006896 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006897 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00006898 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
6899 *TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006900 unsigned AlignBits = KnownZero.countTrailingOnes();
6901 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6902 if (Align)
6903 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006904 }
Evan Chengd938faf2009-12-09 01:53:58 +00006905
Evan Cheng34a23ea2009-12-09 01:04:59 +00006906 // If this is a direct reference to a stack slot, use information about the
6907 // stack slot's alignment.
6908 int FrameIdx = 1 << 31;
6909 int64_t FrameOffset = 0;
6910 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6911 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006912 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006913 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006914 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006915 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6916 FrameOffset = Ptr.getConstantOperandVal(1);
6917 }
6918
6919 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006920 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006921 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6922 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006923 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006924 }
6925
6926 return 0;
6927}
6928
Juergen Ributzkab3487102013-11-19 21:20:17 +00006929/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6930/// which is split (or expanded) into two not necessarily identical pieces.
6931std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6932 // Currently all types are split in half.
6933 EVT LoVT, HiVT;
6934 if (!VT.isVector()) {
6935 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6936 } else {
6937 unsigned NumElements = VT.getVectorNumElements();
6938 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6939 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6940 NumElements/2);
6941 }
6942 return std::make_pair(LoVT, HiVT);
6943}
6944
6945/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6946/// low/high part.
6947std::pair<SDValue, SDValue>
6948SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6949 const EVT &HiVT) {
6950 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6951 N.getValueType().getVectorNumElements() &&
6952 "More vector elements requested than available!");
6953 SDValue Lo, Hi;
6954 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006955 getConstant(0, DL, TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006956 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006957 getConstant(LoVT.getVectorNumElements(), DL,
6958 TLI->getVectorIdxTy()));
Juergen Ributzkab3487102013-11-19 21:20:17 +00006959 return std::make_pair(Lo, Hi);
6960}
6961
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006962void SelectionDAG::ExtractVectorElements(SDValue Op,
6963 SmallVectorImpl<SDValue> &Args,
6964 unsigned Start, unsigned Count) {
6965 EVT VT = Op.getValueType();
6966 if (Count == 0)
6967 Count = VT.getVectorNumElements();
6968
6969 EVT EltVT = VT.getVectorElementType();
6970 EVT IdxTy = TLI->getVectorIdxTy();
6971 SDLoc SL(Op);
6972 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6973 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006974 Op, getConstant(i, SL, IdxTy)));
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006975 }
6976}
6977
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006978// getAddressSpace - Return the address space this GlobalAddress belongs to.
6979unsigned GlobalAddressSDNode::getAddressSpace() const {
6980 return getGlobal()->getType()->getAddressSpace();
6981}
6982
6983
Chris Lattner229907c2011-07-18 04:54:35 +00006984Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006985 if (isMachineConstantPoolEntry())
6986 return Val.MachineCPVal->getType();
6987 return Val.ConstVal->getType();
6988}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006989
Bob Wilson85cefe82009-03-02 23:24:16 +00006990bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6991 APInt &SplatUndef,
6992 unsigned &SplatBitSize,
6993 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006994 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006995 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006996 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006997 assert(VT.isVector() && "Expected a vector type");
6998 unsigned sz = VT.getSizeInBits();
6999 if (MinSplatBits > sz)
7000 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007001
Bob Wilson85cefe82009-03-02 23:24:16 +00007002 SplatValue = APInt(sz, 0);
7003 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007004
Bob Wilson85cefe82009-03-02 23:24:16 +00007005 // Get the bits. Bits with undefined values (when the corresponding element
7006 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
7007 // in SplatValue. If any of the values are not constant, give up and return
7008 // false.
7009 unsigned int nOps = getNumOperands();
7010 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
7011 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00007012
7013 for (unsigned j = 0; j < nOps; ++j) {
7014 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007015 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00007016 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007017
Bob Wilson85cefe82009-03-02 23:24:16 +00007018 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00007019 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00007020 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00007021 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00007022 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00007023 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00007024 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00007025 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007026 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007027 }
7028
Bob Wilson85cefe82009-03-02 23:24:16 +00007029 // The build_vector is all constants or undefs. Find the smallest element
7030 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007031
Bob Wilson85cefe82009-03-02 23:24:16 +00007032 HasAnyUndefs = (SplatUndef != 0);
7033 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007034
Bob Wilson85cefe82009-03-02 23:24:16 +00007035 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00007036 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
7037 APInt LowValue = SplatValue.trunc(HalfSize);
7038 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
7039 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007040
Bob Wilson85cefe82009-03-02 23:24:16 +00007041 // If the two halves do not match (ignoring undef bits), stop here.
7042 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
7043 MinSplatBits > HalfSize)
7044 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007045
Bob Wilson85cefe82009-03-02 23:24:16 +00007046 SplatValue = HighValue | LowValue;
7047 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00007048
Bob Wilson85cefe82009-03-02 23:24:16 +00007049 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007050 }
7051
Bob Wilson85cefe82009-03-02 23:24:16 +00007052 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007053 return true;
7054}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007055
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007056SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
7057 if (UndefElements) {
7058 UndefElements->clear();
7059 UndefElements->resize(getNumOperands());
7060 }
Chandler Carruthb844e722014-07-08 07:19:55 +00007061 SDValue Splatted;
7062 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
7063 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007064 if (Op.getOpcode() == ISD::UNDEF) {
7065 if (UndefElements)
7066 (*UndefElements)[i] = true;
7067 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00007068 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007069 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00007070 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007071 }
Chandler Carruthb844e722014-07-08 07:19:55 +00007072 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00007073
Chandler Carruthb844e722014-07-08 07:19:55 +00007074 if (!Splatted) {
7075 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
7076 "Can only have a splat without a constant for all undefs.");
7077 return getOperand(0);
7078 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00007079
Chandler Carruthb844e722014-07-08 07:19:55 +00007080 return Splatted;
7081}
7082
7083ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007084BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00007085 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007086 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00007087}
7088
7089ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007090BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00007091 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00007092 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00007093}
7094
Juergen Ributzka73844052014-01-13 20:51:35 +00007095bool BuildVectorSDNode::isConstant() const {
7096 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
7097 unsigned Opc = getOperand(i).getOpcode();
7098 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
7099 return false;
7100 }
7101 return true;
7102}
7103
Owen Anderson53aa7a92009-08-10 22:56:29 +00007104bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00007105 // Find the first non-undef value in the shuffle mask.
7106 unsigned i, e;
7107 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
7108 /* search */;
7109
Nate Begeman39b59db2009-04-29 18:13:31 +00007110 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00007111
Nate Begeman5f829d82009-04-29 05:20:52 +00007112 // Make sure all remaining elements are either undef or the same as the first
7113 // non-undef value.
7114 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007115 if (Mask[i] >= 0 && Mask[i] != Idx)
7116 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007117 return true;
7118}
David Greene09851602010-01-20 20:13:31 +00007119
Adam Nemetb4690e32014-05-31 16:23:20 +00007120#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00007121static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00007122 SmallPtrSetImpl<const SDNode*> &Visited,
7123 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00007124 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007125 // If this node has already been checked, don't check it again.
7126 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00007127 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00007128
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007129 // If a node has already been visited on this depth-first walk, reject it as
7130 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00007131 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007132 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00007133 dbgs() << "Offending node:\n";
7134 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007135 abort();
David Greene09851602010-01-20 20:13:31 +00007136 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007137
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007138 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00007139 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00007140
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007141 Checked.insert(N);
7142 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00007143}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00007144#endif
David Greene09851602010-01-20 20:13:31 +00007145
Adam Nemet7d394302014-05-31 16:23:17 +00007146void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00007147 const llvm::SelectionDAG *DAG,
7148 bool force) {
7149#ifndef NDEBUG
7150 bool check = force;
David Greene09851602010-01-20 20:13:31 +00007151#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00007152 check = true;
7153#endif // XDEBUG
7154 if (check) {
7155 assert(N && "Checking nonexistent SDNode");
7156 SmallPtrSet<const SDNode*, 32> visited;
7157 SmallPtrSet<const SDNode*, 32> checked;
7158 checkForCyclesHelper(N, visited, checked, DAG);
7159 }
7160#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00007161}
7162
Adam Nemetb4690e32014-05-31 16:23:20 +00007163void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7164 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00007165}