blob: b1b8035a7d9a66a4cc9a786f47783b7d75dea31a [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendlinge36025e2009-12-21 19:34:59 +000013
Chris Lattner78ec3112003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Chenga8efe282010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthd04a8d42012-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 Carruthd04a8d42012-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 Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070029#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-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 Wendling6f287b22008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greene55d146e2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-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"
Jeff Cohenfd161e92005-01-09 20:41:56 +000049#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000050#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000051using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000052
Chris Lattner0b3e5252006-08-15 19:11:05 +000053/// makeVTList - Return an instance of the SDVTList struct initialized with the
54/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000055static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000056 SDVTList Res = {VTs, NumVTs};
57 return Res;
58}
59
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000060// Default null implementations of the callbacks.
61void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
62void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000063
Jim Laskey58b968b2005-08-17 20:08:02 +000064//===----------------------------------------------------------------------===//
65// ConstantFPSDNode Class
66//===----------------------------------------------------------------------===//
67
68/// isExactlyValue - We don't rely on operator== working on double values, as
69/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
70/// As such, this method can be used to do an exact bit-for-bit comparison of
71/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000072bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000073 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000074}
75
Owen Andersone50ed302009-08-10 22:56:29 +000076bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000077 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000078 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000079
Dale Johannesenf04afdb2007-08-30 00:23:21 +000080 // convert modifies in place, so make a copy.
81 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000082 bool losesInfo;
Tim Northover0a29cb02013-01-22 09:46:31 +000083 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
84 APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000085 &losesInfo);
86 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000087}
88
Jim Laskey58b968b2005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000090// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000092
Evan Chenga8df1662006-03-27 06:58:47 +000093/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000094/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000095bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000096 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000097 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +000098 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +000099
Evan Chenga8df1662006-03-27 06:58:47 +0000100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000101
Chris Lattner61d43992006-03-25 22:57:01 +0000102 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000103
Chris Lattner61d43992006-03-25 22:57:01 +0000104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000107
Chris Lattner61d43992006-03-25 22:57:01 +0000108 // Do not accept an all-undef vector.
109 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Chris Lattner61d43992006-03-25 22:57:01 +0000111 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000112 // elements. We have to be a bit careful here, as the type of the constant
113 // may not be the same as the type of the vector elements due to type
114 // legalization (the elements are promoted to a legal type for the target and
115 // a vector of a type may be legal when the base element type is not).
116 // We only want to check enough bits to cover the vector elements, because
117 // we care if the resultant vector is all ones, not whether the individual
118 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000119 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000120 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszak554c6762012-09-30 21:24:57 +0000121 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
122 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000123 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000124 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
125 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000126 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000127 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000128 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000129
Chris Lattner61d43992006-03-25 22:57:01 +0000130 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000131 // undefs. Even with the above element type twiddling, this should be OK, as
132 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000133 for (++i; i != e; ++i)
134 if (N->getOperand(i) != NotZero &&
135 N->getOperand(i).getOpcode() != ISD::UNDEF)
136 return false;
137 return true;
138}
139
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141/// isBuildVectorAllZeros - Return true if the specified node is a
142/// BUILD_VECTOR where all of the elements are 0 or undef.
143bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000144 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000145 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000146 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000147
Evan Cheng4a147842006-03-26 09:50:58 +0000148 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000149
Evan Chenga8df1662006-03-27 06:58:47 +0000150 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Evan Chenga8df1662006-03-27 06:58:47 +0000152 // Skip over all of the undef values.
153 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
154 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Evan Chenga8df1662006-03-27 06:58:47 +0000156 // Do not accept an all-undef vector.
157 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000158
Dan Gohman68f32cb2009-06-04 16:49:15 +0000159 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000160 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000161 SDValue Zero = N->getOperand(i);
Jakub Staszak554c6762012-09-30 21:24:57 +0000162 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
163 if (!CN->isNullValue())
Evan Chenga8df1662006-03-27 06:58:47 +0000164 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000165 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
166 if (!CFPN->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000167 return false;
168 } else
169 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000170
Dan Gohman68f32cb2009-06-04 16:49:15 +0000171 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000172 // undefs.
173 for (++i; i != e; ++i)
174 if (N->getOperand(i) != Zero &&
175 N->getOperand(i).getOpcode() != ISD::UNDEF)
176 return false;
177 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000178}
179
Stephen Hines36b56882014-04-23 16:57:46 -0700180/// \brief Return true if the specified node is a BUILD_VECTOR node of
181/// all ConstantSDNode or undef.
182bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
183 if (N->getOpcode() != ISD::BUILD_VECTOR)
184 return false;
185
186 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
187 SDValue Op = N->getOperand(i);
188 if (Op.getOpcode() == ISD::UNDEF)
189 continue;
190 if (!isa<ConstantSDNode>(Op))
191 return false;
192 }
193 return true;
194}
195
Evan Chengefec7512008-02-18 23:04:32 +0000196/// isScalarToVector - Return true if the specified node is a
197/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
198/// element is not an undef.
199bool ISD::isScalarToVector(const SDNode *N) {
200 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
201 return true;
202
203 if (N->getOpcode() != ISD::BUILD_VECTOR)
204 return false;
205 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
206 return false;
207 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000208 if (NumElems == 1)
209 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000210 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000211 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000212 if (V.getOpcode() != ISD::UNDEF)
213 return false;
214 }
215 return true;
216}
217
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000218/// allOperandsUndef - Return true if the node has at least one operand
219/// and all operands of the specified node are ISD::UNDEF.
220bool ISD::allOperandsUndef(const SDNode *N) {
221 // Return false if the node has no operands.
222 // This is "logically inconsistent" with the definition of "all" but
223 // is probably the desired behavior.
224 if (N->getNumOperands() == 0)
225 return false;
226
227 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
228 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
229 return false;
230
231 return true;
232}
233
Stephen Hines36b56882014-04-23 16:57:46 -0700234ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) {
235 switch (ExtType) {
236 case ISD::EXTLOAD:
237 return ISD::ANY_EXTEND;
238 case ISD::SEXTLOAD:
239 return ISD::SIGN_EXTEND;
240 case ISD::ZEXTLOAD:
241 return ISD::ZERO_EXTEND;
242 default:
243 break;
244 }
245
246 llvm_unreachable("Invalid LoadExtType");
247}
248
Chris Lattnerc3aae252005-01-07 07:46:32 +0000249/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
250/// when given the operation for (X op Y).
251ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
252 // To perform this operation, we just need to swap the L and G bits of the
253 // operation.
254 unsigned OldL = (Operation >> 2) & 1;
255 unsigned OldG = (Operation >> 1) & 1;
256 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
257 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000258 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000259}
260
261/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
262/// 'op' is a valid SetCC operation.
263ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
264 unsigned Operation = Op;
265 if (isInteger)
266 Operation ^= 7; // Flip L, G, E bits, but not U.
267 else
268 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000269
Chris Lattnerc3aae252005-01-07 07:46:32 +0000270 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000271 Operation &= ~8; // Don't let N and U bits get set.
272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 return ISD::CondCode(Operation);
274}
275
276
277/// isSignedOp - For an integer comparison, return 1 if the comparison is a
278/// signed operation and 2 if the result is an unsigned comparison. Return zero
279/// if the operation does not depend on the sign of the input (setne and seteq).
280static int isSignedOp(ISD::CondCode Opcode) {
281 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000282 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000283 case ISD::SETEQ:
284 case ISD::SETNE: return 0;
285 case ISD::SETLT:
286 case ISD::SETLE:
287 case ISD::SETGT:
288 case ISD::SETGE: return 1;
289 case ISD::SETULT:
290 case ISD::SETULE:
291 case ISD::SETUGT:
292 case ISD::SETUGE: return 2;
293 }
294}
295
296/// getSetCCOrOperation - Return the result of a logical OR between different
297/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
298/// returns SETCC_INVALID if it is not possible to represent the resultant
299/// comparison.
300ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
301 bool isInteger) {
302 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
303 // Cannot fold a signed integer setcc with an unsigned integer setcc.
304 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000305
Chris Lattnerc3aae252005-01-07 07:46:32 +0000306 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000307
Chris Lattnerc3aae252005-01-07 07:46:32 +0000308 // If the N and U bits get set then the resultant comparison DOES suddenly
309 // care about orderedness, and is true when ordered.
310 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000311 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000312
Chris Lattnere41102b2006-05-12 17:03:46 +0000313 // Canonicalize illegal integer setcc's.
314 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
315 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000316
Chris Lattnerc3aae252005-01-07 07:46:32 +0000317 return ISD::CondCode(Op);
318}
319
320/// getSetCCAndOperation - Return the result of a logical AND between different
321/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
322/// function returns zero if it is not possible to represent the resultant
323/// comparison.
324ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
325 bool isInteger) {
326 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
327 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000328 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000329
330 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000331 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000332
Chris Lattnera83385f2006-04-27 05:01:07 +0000333 // Canonicalize illegal integer setcc's.
334 if (isInteger) {
335 switch (Result) {
336 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000337 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000338 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000339 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
340 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
341 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000342 }
343 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000344
Chris Lattnera83385f2006-04-27 05:01:07 +0000345 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000346}
347
Jim Laskey58b968b2005-08-17 20:08:02 +0000348//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000349// SDNode Profile Support
350//===----------------------------------------------------------------------===//
351
Jim Laskeydef69b92006-10-27 23:52:51 +0000352/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
353///
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
355 ID.AddInteger(OpC);
356}
357
358/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
359/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000360static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000361 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000362}
363
Jim Laskeydef69b92006-10-27 23:52:51 +0000364/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
365///
Jim Laskey583bd472006-10-27 23:46:08 +0000366static void AddNodeIDOperands(FoldingSetNodeID &ID,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700367 ArrayRef<SDValue> Ops) {
368 for (auto& Op : Ops) {
369 ID.AddPointer(Op.getNode());
370 ID.AddInteger(Op.getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000371 }
Jim Laskey583bd472006-10-27 23:46:08 +0000372}
373
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000374/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
375///
376static void AddNodeIDOperands(FoldingSetNodeID &ID,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700377 ArrayRef<SDUse> Ops) {
378 for (auto& Op : Ops) {
379 ID.AddPointer(Op.getNode());
380 ID.AddInteger(Op.getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000381 }
382}
383
Stephen Hinesdce4a402014-05-29 02:49:00 -0700384static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
385 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000386 AddNodeIDOpcode(ID, OpC);
387 AddNodeIDValueTypes(ID, VTList);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700388 AddNodeIDOperands(ID, OpList);
Jim Laskey583bd472006-10-27 23:46:08 +0000389}
390
Duncan Sands0dc40452008-10-27 15:30:53 +0000391/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
392/// the NodeID data.
393static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000394 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000395 case ISD::TargetExternalSymbol:
396 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000397 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000398 default: break; // Normal nodes don't need extra info.
399 case ISD::TargetConstant:
Stephen Hines36b56882014-04-23 16:57:46 -0700400 case ISD::Constant: {
401 const ConstantSDNode *C = cast<ConstantSDNode>(N);
402 ID.AddPointer(C->getConstantIntValue());
403 ID.AddBoolean(C->isOpaque());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000404 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700405 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000406 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000407 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000408 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000409 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000410 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000411 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000412 case ISD::GlobalAddress:
413 case ISD::TargetGlobalTLSAddress:
414 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000415 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000416 ID.AddPointer(GA->getGlobal());
417 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000418 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000419 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000420 break;
421 }
422 case ISD::BasicBlock:
423 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
424 break;
425 case ISD::Register:
426 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
427 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000428 case ISD::RegisterMask:
429 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
430 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000431 case ISD::SRCVALUE:
432 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
433 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 case ISD::FrameIndex:
435 case ISD::TargetFrameIndex:
436 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
437 break;
438 case ISD::JumpTable:
439 case ISD::TargetJumpTable:
440 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000441 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000442 break;
443 case ISD::ConstantPool:
444 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000445 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000446 ID.AddInteger(CP->getAlignment());
447 ID.AddInteger(CP->getOffset());
448 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000449 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000450 else
451 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000452 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000453 break;
454 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000455 case ISD::TargetIndex: {
456 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
457 ID.AddInteger(TI->getIndex());
458 ID.AddInteger(TI->getOffset());
459 ID.AddInteger(TI->getTargetFlags());
460 break;
461 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000462 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000463 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000464 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000465 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000466 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000467 break;
468 }
469 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000470 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000471 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000472 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000473 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000474 break;
475 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000476 case ISD::ATOMIC_CMP_SWAP:
477 case ISD::ATOMIC_SWAP:
478 case ISD::ATOMIC_LOAD_ADD:
479 case ISD::ATOMIC_LOAD_SUB:
480 case ISD::ATOMIC_LOAD_AND:
481 case ISD::ATOMIC_LOAD_OR:
482 case ISD::ATOMIC_LOAD_XOR:
483 case ISD::ATOMIC_LOAD_NAND:
484 case ISD::ATOMIC_LOAD_MIN:
485 case ISD::ATOMIC_LOAD_MAX:
486 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000487 case ISD::ATOMIC_LOAD_UMAX:
488 case ISD::ATOMIC_LOAD:
489 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000490 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000491 ID.AddInteger(AT->getMemoryVT().getRawBits());
492 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000493 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
494 break;
495 }
496 case ISD::PREFETCH: {
497 const MemSDNode *PF = cast<MemSDNode>(N);
498 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000499 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000500 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000501 case ISD::VECTOR_SHUFFLE: {
502 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000503 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000504 i != e; ++i)
505 ID.AddInteger(SVN->getMaskElt(i));
506 break;
507 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000508 case ISD::TargetBlockAddress:
509 case ISD::BlockAddress: {
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000510 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
511 ID.AddPointer(BA->getBlockAddress());
512 ID.AddInteger(BA->getOffset());
513 ID.AddInteger(BA->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000514 break;
515 }
Mon P Wang28873102008-06-25 08:15:39 +0000516 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000517
518 // Target specific memory nodes could also have address spaces to check.
519 if (N->isTargetMemoryOpcode())
520 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000521}
522
Duncan Sands0dc40452008-10-27 15:30:53 +0000523/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
524/// data.
525static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
526 AddNodeIDOpcode(ID, N->getOpcode());
527 // Add the return value info.
528 AddNodeIDValueTypes(ID, N->getVTList());
529 // Add the operand info.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700530 AddNodeIDOperands(ID, makeArrayRef(N->op_begin(), N->op_end()));
Duncan Sands0dc40452008-10-27 15:30:53 +0000531
532 // Handle SDNode leafs with special info.
533 AddNodeIDCustom(ID, N);
534}
535
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000536/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000537/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000538/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000539///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000540static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000541encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000542 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000543 assert((ConvType & 3) == ConvType &&
544 "ConvType may not require more than 2 bits!");
545 assert((AM & 7) == AM &&
546 "AM may not require more than 3 bits!");
547 return ConvType |
548 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000549 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000550 (isNonTemporal << 6) |
551 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000552}
553
Jim Laskey583bd472006-10-27 23:46:08 +0000554//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000555// SelectionDAG Class
556//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000557
Duncan Sands0dc40452008-10-27 15:30:53 +0000558/// doNotCSE - Return true if CSE should not be performed for this node.
559static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000560 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000561 return true; // Never CSE anything that produces a flag.
562
563 switch (N->getOpcode()) {
564 default: break;
565 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000566 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000567 return true; // Never CSE these nodes.
568 }
569
570 // Check that remaining values produced are not flags.
571 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000572 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000573 return true; // Never CSE anything that produces a flag.
574
575 return false;
576}
577
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000578/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000579/// SelectionDAG.
580void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000581 // Create a dummy node (which is not added to allnodes), that adds a reference
582 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000583 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000584
Chris Lattner190a4182006-08-04 17:45:20 +0000585 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000586
Chris Lattner190a4182006-08-04 17:45:20 +0000587 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000588 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000589 if (I->use_empty())
590 DeadNodes.push_back(I);
591
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000592 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000593
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000594 // If the root changed (e.g. it was a dead load, update the root).
595 setRoot(Dummy.getValue());
596}
597
598/// RemoveDeadNodes - This method deletes the unreachable nodes in the
599/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000600void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000601
Chris Lattner190a4182006-08-04 17:45:20 +0000602 // Process the worklist, deleting the nodes and adding their uses to the
603 // worklist.
604 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000605 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000606
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000607 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700608 DUL->NodeDeleted(N, nullptr);
Scott Michelfdc40a02009-02-17 22:15:04 +0000609
Chris Lattner190a4182006-08-04 17:45:20 +0000610 // Take the node out of the appropriate CSE map.
611 RemoveNodeFromCSEMaps(N);
612
613 // Next, brutally remove the operand list. This is safe to do, as there are
614 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000615 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
616 SDUse &Use = *I++;
617 SDNode *Operand = Use.getNode();
618 Use.set(SDValue());
619
Chris Lattner190a4182006-08-04 17:45:20 +0000620 // Now that we removed this operand, see if there are no uses of it left.
621 if (Operand->use_empty())
622 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000623 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000624
Dan Gohmanc5336122009-01-19 22:39:36 +0000625 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000626 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000627}
628
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000629void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000630 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000631
632 // Create a dummy node that adds a reference to the root node, preventing
633 // it from being deleted. (This matters if the root is an operand of the
634 // dead node.)
635 HandleSDNode Dummy(getRoot());
636
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000637 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000638}
639
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000640void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000641 // First take this out of the appropriate CSE map.
642 RemoveNodeFromCSEMaps(N);
643
Scott Michelfdc40a02009-02-17 22:15:04 +0000644 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000645 // AllNodes list, and delete the node.
646 DeleteNodeNotInCSEMaps(N);
647}
648
649void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000650 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
651 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000652
Dan Gohmanf06c8352008-09-30 18:30:35 +0000653 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000654 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000655
Dan Gohmanc5336122009-01-19 22:39:36 +0000656 DeallocateNode(N);
657}
658
659void SelectionDAG::DeallocateNode(SDNode *N) {
660 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000661 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000662
Dan Gohmanc5336122009-01-19 22:39:36 +0000663 // Set the opcode to DELETED_NODE to help catch bugs when node
664 // memory is reallocated.
665 N->NodeType = ISD::DELETED_NODE;
666
Dan Gohman11467282008-08-26 01:44:34 +0000667 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000668
Evan Chengbfcb3052010-03-25 01:38:16 +0000669 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000670 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000671 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
672 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000673}
674
Chris Lattner149c58c2005-08-16 18:17:10 +0000675/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
676/// correspond to it. This is useful when we're about to delete or repurpose
677/// the node. We don't want future request for structurally identical nodes
678/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000679bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000680 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000681 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000682 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000683 case ISD::CONDCODE:
684 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
685 "Cond code doesn't exist!");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700686 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
687 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000688 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000689 case ISD::ExternalSymbol:
690 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000691 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000692 case ISD::TargetExternalSymbol: {
693 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
694 Erased = TargetExternalSymbols.erase(
695 std::pair<std::string,unsigned char>(ESN->getSymbol(),
696 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000697 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000698 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000699 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000700 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000701 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000702 Erased = ExtendedValueTypeNodes.erase(VT);
703 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700704 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
705 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsf411b832007-10-17 13:49:58 +0000706 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000707 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000708 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000709 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000710 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000711 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
712 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000713 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000714 break;
715 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000716#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000717 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000718 // flag result (which cannot be CSE'd) or is one of the special cases that are
719 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000720 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000721 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000722 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000723 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000724 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000725 }
726#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000727 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000728}
729
Dan Gohman39946102009-01-25 16:29:12 +0000730/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
731/// maps and modified in place. Add it back to the CSE maps, unless an identical
732/// node already exists, in which case transfer all its users to the existing
733/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000734///
Dan Gohman39946102009-01-25 16:29:12 +0000735void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000736SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000737 // For node types that aren't CSE'd, just act as if no identical node
738 // already exists.
739 if (!doNotCSE(N)) {
740 SDNode *Existing = CSEMap.GetOrInsertNode(N);
741 if (Existing != N) {
742 // If there was already an existing matching node, use ReplaceAllUsesWith
743 // to replace the dead one with the existing one. This can cause
744 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000745 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000746
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000747 // N is now dead. Inform the listeners and delete it.
748 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
749 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000750 DeleteNodeNotInCSEMaps(N);
751 return;
752 }
753 }
Evan Cheng71e86852008-07-08 20:06:39 +0000754
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000755 // If the node doesn't already exist, we updated it. Inform listeners.
756 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
757 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000758}
759
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000760/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000761/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000762/// return null, otherwise return a pointer to the slot it would take. If a
763/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000764SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000765 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000766 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700767 return nullptr;
Evan Cheng71e86852008-07-08 20:06:39 +0000768
Dan Gohman475871a2008-07-27 21:46:04 +0000769 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000770 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700771 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000772 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000773 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000774 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000775}
776
777/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000778/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000779/// return null, otherwise return a pointer to the slot it would take. If a
780/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000781SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000782 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000783 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000784 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700785 return nullptr;
Duncan Sands0dc40452008-10-27 15:30:53 +0000786
Dan Gohman475871a2008-07-27 21:46:04 +0000787 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000788 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700789 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000790 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000791 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000792 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000793}
794
795
796/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000797/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000798/// return null, otherwise return a pointer to the slot it would take. If a
799/// node already exists with these operands, the slot will be non-null.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700800SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattnera5682852006-08-07 23:03:03 +0000801 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000802 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700803 return nullptr;
Evan Cheng71e86852008-07-08 20:06:39 +0000804
Jim Laskey583bd472006-10-27 23:46:08 +0000805 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700806 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000807 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000808 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000809 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000810}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000811
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000812#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000813/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
814static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000815 switch (N->getOpcode()) {
816 default:
817 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000818 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000819 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000820 assert(N->getNumValues() == 1 && "Too many results!");
821 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
822 "Wrong return type!");
823 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
824 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
825 "Mismatched operand types!");
826 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
827 "Wrong operand type!");
828 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
829 "Wrong return type size");
830 break;
831 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000832 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000833 assert(N->getNumValues() == 1 && "Too many results!");
834 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000835 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000836 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000837 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000838 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000839 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000840 (EltVT.isInteger() && I->getValueType().isInteger() &&
841 EltVT.bitsLE(I->getValueType()))) &&
842 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000843 assert(I->getValueType() == N->getOperand(0).getValueType() &&
844 "Operands must all have the same type");
845 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000846 break;
847 }
848 }
849}
850
Duncan Sands59d2dad2010-11-20 11:25:00 +0000851/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
852static void VerifySDNode(SDNode *N) {
853 // The SDNode allocators cannot be used to allocate nodes with fields that are
854 // not present in an SDNode!
855 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
856 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
857 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
858 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
859 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
860 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
861 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
862 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
863 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
864 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
865 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
866 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
867 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
868 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
869 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
870 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
871 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
872 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
873 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
874
875 VerifyNodeCommon(N);
876}
877
878/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
879/// invalid.
880static void VerifyMachineNode(SDNode *N) {
881 // The MachineNode allocators cannot be used to allocate nodes with fields
882 // that are not present in a MachineNode!
883 // Currently there are no such nodes.
884
885 VerifyNodeCommon(N);
886}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000887#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000888
Owen Andersone50ed302009-08-10 22:56:29 +0000889/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000890/// given type.
891///
Owen Andersone50ed302009-08-10 22:56:29 +0000892unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000893 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000894 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000895 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000896
Bill Wendlingba54bca2013-06-19 21:36:55 +0000897 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000898}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000899
Dale Johannesen92570c42009-02-07 02:15:05 +0000900// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000901SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700902 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TLI(nullptr), OptLevel(OL),
Bill Wendlingba54bca2013-06-19 21:36:55 +0000903 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sandersea28aaf2013-11-15 12:56:49 +0000904 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700905 UpdateListeners(nullptr) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000906 AllNodes.push_back(&EntryNode);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000907 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000908}
909
Stephen Hines36b56882014-04-23 16:57:46 -0700910void SelectionDAG::init(MachineFunction &mf, const TargetLowering *tli) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000911 MF = &mf;
Bill Wendlingee287ca2013-11-22 05:17:44 +0000912 TLI = tli;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000913 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000914}
915
Chris Lattner78ec3112003-08-11 14:57:33 +0000916SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000917 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000918 allnodes_clear();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000919 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000920}
921
922void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000923 assert(&*AllNodes.begin() == &EntryNode);
924 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000925 while (!AllNodes.empty())
926 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000927}
928
Dan Gohman7c3234c2008-08-27 23:52:12 +0000929void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000930 allnodes_clear();
931 OperandAllocator.Reset();
932 CSEMap.clear();
933
934 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000935 ExternalSymbols.clear();
936 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000937 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700938 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmanf350b272008-08-23 02:25:05 +0000939 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700940 static_cast<SDNode*>(nullptr));
Dan Gohmanf350b272008-08-23 02:25:05 +0000941
Stephen Hinesdce4a402014-05-29 02:49:00 -0700942 EntryNode.UseList = nullptr;
Dan Gohmanf350b272008-08-23 02:25:05 +0000943 AllNodes.push_back(&EntryNode);
944 Root = getEntryNode();
Evan Cheng31441b72010-03-29 20:48:30 +0000945 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000946}
947
Andrew Trickac6d9be2013-05-25 02:42:55 +0000948SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotema3c42f32011-09-27 11:16:47 +0000949 return VT.bitsGT(Op.getValueType()) ?
950 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
951 getNode(ISD::TRUNCATE, DL, VT, Op);
952}
953
Andrew Trickac6d9be2013-05-25 02:42:55 +0000954SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000955 return VT.bitsGT(Op.getValueType()) ?
956 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
957 getNode(ISD::TRUNCATE, DL, VT, Op);
958}
959
Andrew Trickac6d9be2013-05-25 02:42:55 +0000960SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000961 return VT.bitsGT(Op.getValueType()) ?
962 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
963 getNode(ISD::TRUNCATE, DL, VT, Op);
964}
965
Stephen Hinesdce4a402014-05-29 02:49:00 -0700966SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT) {
967 if (VT.bitsLE(Op.getValueType()))
968 return getNode(ISD::TRUNCATE, SL, VT, Op);
969
970 TargetLowering::BooleanContent BType = TLI->getBooleanContents(VT.isVector());
971 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
972}
973
Andrew Trickac6d9be2013-05-25 02:42:55 +0000974SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000975 assert(!VT.isVector() &&
976 "getZeroExtendInReg should use the vector element type instead of "
977 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000978 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000979 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
980 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000981 VT.getSizeInBits());
982 return getNode(ISD::AND, DL, Op.getValueType(), Op,
983 getConstant(Imm, Op.getValueType()));
984}
985
Bob Wilson4c245462009-01-22 17:39:32 +0000986/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
987///
Andrew Trickac6d9be2013-05-25 02:42:55 +0000988SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000989 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000990 SDValue NegOne =
991 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000992 return getNode(ISD::XOR, DL, VT, Val, NegOne);
993}
994
Stephen Hinesdce4a402014-05-29 02:49:00 -0700995SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
996 EVT EltVT = VT.getScalarType();
997 SDValue TrueValue;
998 switch (TLI->getBooleanContents(VT.isVector())) {
999 case TargetLowering::ZeroOrOneBooleanContent:
1000 case TargetLowering::UndefinedBooleanContent:
1001 TrueValue = getConstant(1, VT);
1002 break;
1003 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1004 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1005 VT);
1006 break;
1007 }
1008 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1009}
1010
Stephen Hines36b56882014-04-23 16:57:46 -07001011SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001012 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +00001013 assert((EltVT.getSizeInBits() >= 64 ||
1014 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1015 "getConstant with a uint64_t value that doesn't fit in the type!");
Stephen Hines36b56882014-04-23 16:57:46 -07001016 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman6394b092008-02-08 22:59:30 +00001017}
1018
Stephen Hines36b56882014-04-23 16:57:46 -07001019SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1020{
1021 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001022}
1023
Stephen Hines36b56882014-04-23 16:57:46 -07001024SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1025 bool isO) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001026 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +00001027
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001028 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +00001029 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +00001030
Bill Wendlingba54bca2013-06-19 21:36:55 +00001031 const TargetLowering *TLI = TM.getTargetLowering();
1032
Duncan Sands28b77e92011-09-06 19:07:46 +00001033 // In some cases the vector type is legal but the element type is illegal and
1034 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1035 // inserted value (the type does not need to match the vector element type).
1036 // Any extra bits introduced will be truncated away.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001037 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sands28b77e92011-09-06 19:07:46 +00001038 TargetLowering::TypePromoteInteger) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001039 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sands28b77e92011-09-06 19:07:46 +00001040 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1041 Elt = ConstantInt::get(*getContext(), NewVal);
1042 }
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001043 // In other cases the element type is illegal and needs to be expanded, for
1044 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1045 // the value into n parts and use a vector type with n-times the elements.
1046 // Then bitcast to the type requested.
1047 // Legalizing constants too early makes the DAGCombiner's job harder so we
1048 // only legalize if the DAG tells us we must produce legal types.
1049 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1050 TLI->getTypeAction(*getContext(), EltVT) ==
1051 TargetLowering::TypeExpandInteger) {
1052 APInt NewVal = Elt->getValue();
1053 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1054 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1055 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1056 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1057
1058 // Check the temporary vector is the correct size. If this fails then
1059 // getTypeToTransformTo() probably returned a type whose size (in bits)
1060 // isn't a power-of-2 factor of the requested type size.
1061 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1062
1063 SmallVector<SDValue, 2> EltParts;
1064 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1065 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1066 .trunc(ViaEltSizeInBits),
Stephen Hines36b56882014-04-23 16:57:46 -07001067 ViaEltVT, isT, isO));
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001068 }
1069
1070 // EltParts is currently in little endian order. If we actually want
1071 // big-endian order then reverse it now.
1072 if (TLI->isBigEndian())
1073 std::reverse(EltParts.begin(), EltParts.end());
1074
1075 // The elements must be reversed when the element order is different
1076 // to the endianness of the elements (because the BITCAST is itself a
1077 // vector shuffle in this situation). However, we do not need any code to
1078 // perform this reversal because getConstant() is producing a vector
1079 // splat.
1080 // This situation occurs in MIPS MSA.
1081
1082 SmallVector<SDValue, 8> Ops;
1083 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1084 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1085
1086 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1087 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07001088 Ops));
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001089 return Result;
1090 }
Duncan Sands28b77e92011-09-06 19:07:46 +00001091
1092 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1093 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001094 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001095 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001096 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sands28b77e92011-09-06 19:07:46 +00001097 ID.AddPointer(Elt);
Stephen Hines36b56882014-04-23 16:57:46 -07001098 ID.AddBoolean(isO);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001099 void *IP = nullptr;
1100 SDNode *N = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001101 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001102 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001103 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001104
Dan Gohman89081322007-12-12 22:21:26 +00001105 if (!N) {
Stephen Hines36b56882014-04-23 16:57:46 -07001106 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001107 CSEMap.InsertNode(N, IP);
1108 AllNodes.push_back(N);
1109 }
1110
Dan Gohman475871a2008-07-27 21:46:04 +00001111 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001112 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001113 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001114 Ops.assign(VT.getVectorNumElements(), Result);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001115 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman89081322007-12-12 22:21:26 +00001116 }
1117 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001118}
1119
Dan Gohman475871a2008-07-27 21:46:04 +00001120SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001121 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner0bd48932008-01-17 07:00:52 +00001122}
1123
1124
Owen Andersone50ed302009-08-10 22:56:29 +00001125SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001126 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001127}
1128
Owen Andersone50ed302009-08-10 22:56:29 +00001129SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001130 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001131
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001132 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133
Chris Lattnerd8658612005-02-17 20:17:32 +00001134 // Do the map lookup using the actual bit pattern for the floating point
1135 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1136 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001137 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001138 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001139 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001140 ID.AddPointer(&V);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001141 void *IP = nullptr;
1142 SDNode *N = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001143 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001144 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001145 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001146
Evan Chengc908dcd2007-06-29 21:36:04 +00001147 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001148 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001149 CSEMap.InsertNode(N, IP);
1150 AllNodes.push_back(N);
1151 }
1152
Dan Gohman475871a2008-07-27 21:46:04 +00001153 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001154 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001155 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001156 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001157 // FIXME SDLoc info might be appropriate here
Stephen Hinesdce4a402014-05-29 02:49:00 -07001158 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7f321562007-06-25 16:23:39 +00001159 }
1160 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001161}
1162
Owen Andersone50ed302009-08-10 22:56:29 +00001163SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001164 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001165 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001166 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001167 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001168 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001169 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1170 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001171 bool ignored;
1172 APFloat apf = APFloat(Val);
Tim Northover0a29cb02013-01-22 09:46:31 +00001173 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001174 &ignored);
1175 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001176 } else
1177 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001178}
1179
Andrew Trickac6d9be2013-05-25 02:42:55 +00001180SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001181 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001182 bool isTargetGA,
1183 unsigned char TargetFlags) {
1184 assert((TargetFlags == 0 || isTargetGA) &&
1185 "Cannot set target flags on target-independent globals");
Matt Arsenault94437c92013-11-17 00:06:39 +00001186 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001187
Dan Gohman6520e202008-10-18 02:06:02 +00001188 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault94437c92013-11-17 00:06:39 +00001189 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman6520e202008-10-18 02:06:02 +00001190 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001191 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001192
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001193 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1194 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001195 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001196 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Stephen Hinesdce4a402014-05-29 02:49:00 -07001197 GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasee());
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001198 }
1199
Chris Lattner2a4ed822009-06-25 21:21:14 +00001200 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001201 if (GVar && GVar->isThreadLocal())
1202 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1203 else
1204 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001205
Jim Laskey583bd472006-10-27 23:46:08 +00001206 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001207 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001208 ID.AddPointer(GV);
1209 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001210 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001211 ID.AddInteger(GV->getType()->getAddressSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001212 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001213 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001214 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001215
Andrew Trickac6d9be2013-05-25 02:42:55 +00001216 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1217 DL.getDebugLoc(), GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001218 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001219 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001221 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001222}
1223
Owen Andersone50ed302009-08-10 22:56:29 +00001224SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001225 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001226 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001227 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001228 ID.AddInteger(FI);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001229 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001230 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001231 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001232
Dan Gohman95531882010-03-18 18:49:47 +00001233 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001234 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001235 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001236 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001237}
1238
Owen Andersone50ed302009-08-10 22:56:29 +00001239SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001240 unsigned char TargetFlags) {
1241 assert((TargetFlags == 0 || isTarget) &&
1242 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001243 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001244 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001245 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001246 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001247 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001248 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001249 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001250 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001251
Dan Gohman95531882010-03-18 18:49:47 +00001252 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1253 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001254 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001255 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001256 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001257}
1258
Dan Gohman46510a72010-04-15 01:51:59 +00001259SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001260 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001261 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001262 unsigned char TargetFlags) {
1263 assert((TargetFlags == 0 || isTarget) &&
1264 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001265 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001266 Alignment =
1267 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001268 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001269 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001270 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001271 ID.AddInteger(Alignment);
1272 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001273 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001274 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001275 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001276 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001277 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001278
Dan Gohman95531882010-03-18 18:49:47 +00001279 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1280 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001281 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001282 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001283 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001284}
1285
Chris Lattnerc3aae252005-01-07 07:46:32 +00001286
Owen Andersone50ed302009-08-10 22:56:29 +00001287SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001288 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001289 bool isTarget,
1290 unsigned char TargetFlags) {
1291 assert((TargetFlags == 0 || isTarget) &&
1292 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001293 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001294 Alignment =
1295 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001296 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001297 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001298 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Chengd6594ae2006-09-12 21:00:35 +00001299 ID.AddInteger(Alignment);
1300 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001301 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001302 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001303 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001304 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001305 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001306
Dan Gohman95531882010-03-18 18:49:47 +00001307 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1308 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001309 CSEMap.InsertNode(N, IP);
1310 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001311 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001312}
1313
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001314SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1315 unsigned char TargetFlags) {
1316 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001317 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001318 ID.AddInteger(Index);
1319 ID.AddInteger(Offset);
1320 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001321 void *IP = nullptr;
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001322 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1323 return SDValue(E, 0);
1324
1325 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1326 TargetFlags);
1327 CSEMap.InsertNode(N, IP);
1328 AllNodes.push_back(N);
1329 return SDValue(N, 0);
1330}
1331
Dan Gohman475871a2008-07-27 21:46:04 +00001332SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001333 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001334 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001335 ID.AddPointer(MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001336 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001337 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001338 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001339
Dan Gohman95531882010-03-18 18:49:47 +00001340 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001341 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001342 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001343 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001344}
1345
Owen Andersone50ed302009-08-10 22:56:29 +00001346SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001347 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1348 ValueTypeNodes.size())
1349 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001350
Duncan Sands83ec4b62008-06-06 12:08:01 +00001351 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001352 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001353
Dan Gohman475871a2008-07-27 21:46:04 +00001354 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001355 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001356 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001357 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001358}
1359
Owen Andersone50ed302009-08-10 22:56:29 +00001360SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001361 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001362 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001363 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001364 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001365 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001366}
1367
Owen Andersone50ed302009-08-10 22:56:29 +00001368SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001369 unsigned char TargetFlags) {
1370 SDNode *&N =
1371 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1372 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001373 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001374 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001375 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001376 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377}
1378
Dan Gohman475871a2008-07-27 21:46:04 +00001379SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001380 if ((unsigned)Cond >= CondCodeNodes.size())
1381 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001382
Stephen Hinesdce4a402014-05-29 02:49:00 -07001383 if (!CondCodeNodes[Cond]) {
Dan Gohman95531882010-03-18 18:49:47 +00001384 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001385 CondCodeNodes[Cond] = N;
1386 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001387 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001388
Dan Gohman475871a2008-07-27 21:46:04 +00001389 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001390}
1391
Nate Begeman5a5ca152009-04-29 05:20:52 +00001392// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1393// the shuffle mask M that point at N1 to point at N2, and indices that point
1394// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001395static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1396 std::swap(N1, N2);
1397 int NElts = M.size();
1398 for (int i = 0; i != NElts; ++i) {
1399 if (M[i] >= NElts)
1400 M[i] -= NElts;
1401 else if (M[i] >= 0)
1402 M[i] += NElts;
1403 }
1404}
1405
Andrew Trickac6d9be2013-05-25 02:42:55 +00001406SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001407 SDValue N2, const int *Mask) {
Craig Topperad445a62013-08-09 04:37:24 +00001408 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1409 "Invalid VECTOR_SHUFFLE");
Nate Begeman9008ca62009-04-27 18:41:29 +00001410
1411 // Canonicalize shuffle undef, undef -> undef
1412 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001413 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001414
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001415 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001416 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001417 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001418 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001419 for (unsigned i = 0; i != NElts; ++i) {
1420 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001421 MaskVec.push_back(Mask[i]);
1422 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001423
Nate Begeman9008ca62009-04-27 18:41:29 +00001424 // Canonicalize shuffle v, v -> v, undef
1425 if (N1 == N2) {
1426 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001427 for (unsigned i = 0; i != NElts; ++i)
1428 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001429 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001430
Nate Begeman9008ca62009-04-27 18:41:29 +00001431 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1432 if (N1.getOpcode() == ISD::UNDEF)
1433 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001434
Nate Begeman9008ca62009-04-27 18:41:29 +00001435 // Canonicalize all index into lhs, -> shuffle lhs, undef
1436 // Canonicalize all index into rhs, -> shuffle rhs, undef
1437 bool AllLHS = true, AllRHS = true;
1438 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001439 for (unsigned i = 0; i != NElts; ++i) {
1440 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001441 if (N2Undef)
1442 MaskVec[i] = -1;
1443 else
1444 AllLHS = false;
1445 } else if (MaskVec[i] >= 0) {
1446 AllRHS = false;
1447 }
1448 }
1449 if (AllLHS && AllRHS)
1450 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001451 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001452 N2 = getUNDEF(VT);
1453 if (AllRHS) {
1454 N1 = getUNDEF(VT);
1455 commuteShuffle(N1, N2, MaskVec);
1456 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001457
Craig Toppereee2a112013-08-08 08:03:12 +00001458 // If Identity shuffle return that node.
Nate Begeman9008ca62009-04-27 18:41:29 +00001459 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001460 for (unsigned i = 0; i != NElts; ++i) {
1461 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001462 }
Craig Topperad445a62013-08-09 04:37:24 +00001463 if (Identity && NElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00001464 return N1;
Nate Begeman9008ca62009-04-27 18:41:29 +00001465
Stephen Hinesdce4a402014-05-29 02:49:00 -07001466 // Shuffling a constant splat doesn't change the result.
1467 if (N2Undef && N1.getOpcode() == ISD::BUILD_VECTOR)
1468 if (cast<BuildVectorSDNode>(N1)->getConstantSplatValue())
1469 return N1;
1470
Nate Begeman9008ca62009-04-27 18:41:29 +00001471 FoldingSetNodeID ID;
1472 SDValue Ops[2] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001473 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001474 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001475 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001476
Stephen Hinesdce4a402014-05-29 02:49:00 -07001477 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001478 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001479 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001480
Nate Begeman9008ca62009-04-27 18:41:29 +00001481 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1482 // SDNode doesn't have access to it. This memory will be "leaked" when
1483 // the node is deallocated, but recovered when the NodeAllocator is released.
1484 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1485 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001486
Dan Gohman95531882010-03-18 18:49:47 +00001487 ShuffleVectorSDNode *N =
Jack Carter3af4d252013-09-09 22:02:08 +00001488 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1489 dl.getDebugLoc(), N1, N2,
1490 MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001491 CSEMap.InsertNode(N, IP);
1492 AllNodes.push_back(N);
1493 return SDValue(N, 0);
1494}
1495
Andrew Trickac6d9be2013-05-25 02:42:55 +00001496SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001497 SDValue Val, SDValue DTy,
1498 SDValue STy, SDValue Rnd, SDValue Sat,
1499 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001500 // If the src and dest types are the same and the conversion is between
1501 // integer types of the same sign or two floats, no conversion is necessary.
1502 if (DTy == STy &&
1503 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001504 return Val;
1505
1506 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001507 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001508 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
1509 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001511 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001512
Jack Carter3af4d252013-09-09 22:02:08 +00001513 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1514 dl.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07001515 Ops, Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001516 CSEMap.InsertNode(N, IP);
1517 AllNodes.push_back(N);
1518 return SDValue(N, 0);
1519}
1520
Owen Andersone50ed302009-08-10 22:56:29 +00001521SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001522 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001523 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001524 ID.AddInteger(RegNo);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001525 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001526 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001527 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001528
Dan Gohman95531882010-03-18 18:49:47 +00001529 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001530 CSEMap.InsertNode(N, IP);
1531 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001532 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001533}
1534
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001535SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1536 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001537 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001538 ID.AddPointer(RegMask);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001539 void *IP = nullptr;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001540 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1541 return SDValue(E, 0);
1542
1543 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1544 CSEMap.InsertNode(N, IP);
1545 AllNodes.push_back(N);
1546 return SDValue(N, 0);
1547}
1548
Andrew Trickac6d9be2013-05-25 02:42:55 +00001549SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001550 FoldingSetNodeID ID;
1551 SDValue Ops[] = { Root };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001552 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattner7561d482010-03-14 02:33:54 +00001553 ID.AddPointer(Label);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001554 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001555 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001556 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001557
Jack Carter3af4d252013-09-09 22:02:08 +00001558 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1559 dl.getDebugLoc(), Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001560 CSEMap.InsertNode(N, IP);
1561 AllNodes.push_back(N);
1562 return SDValue(N, 0);
1563}
1564
Chris Lattner7561d482010-03-14 02:33:54 +00001565
Dan Gohman46510a72010-04-15 01:51:59 +00001566SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001567 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001568 bool isTarget,
1569 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001570 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1571
1572 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001573 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001574 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001575 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001576 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001577 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001578 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001579 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001580
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001581 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1582 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001583 CSEMap.InsertNode(N, IP);
1584 AllNodes.push_back(N);
1585 return SDValue(N, 0);
1586}
1587
Dan Gohman475871a2008-07-27 21:46:04 +00001588SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001589 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001590 "SrcValue is not a pointer?");
1591
Jim Laskey583bd472006-10-27 23:46:08 +00001592 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001593 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001594 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001595
Stephen Hinesdce4a402014-05-29 02:49:00 -07001596 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001597 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001598 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001599
Dan Gohman95531882010-03-18 18:49:47 +00001600 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001601 CSEMap.InsertNode(N, IP);
1602 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001603 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001604}
1605
Chris Lattnerdecc2672010-04-07 05:20:54 +00001606/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1607SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1608 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001609 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattnerdecc2672010-04-07 05:20:54 +00001610 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001611
Stephen Hinesdce4a402014-05-29 02:49:00 -07001612 void *IP = nullptr;
Chris Lattnerdecc2672010-04-07 05:20:54 +00001613 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1614 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001615
Chris Lattnerdecc2672010-04-07 05:20:54 +00001616 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1617 CSEMap.InsertNode(N, IP);
1618 AllNodes.push_back(N);
1619 return SDValue(N, 0);
1620}
1621
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001622/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1623SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1624 unsigned SrcAS, unsigned DestAS) {
1625 SDValue Ops[] = {Ptr};
1626 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001627 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001628 ID.AddInteger(SrcAS);
1629 ID.AddInteger(DestAS);
1630
Stephen Hinesdce4a402014-05-29 02:49:00 -07001631 void *IP = nullptr;
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001632 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1633 return SDValue(E, 0);
1634
1635 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1636 dl.getDebugLoc(),
1637 VT, Ptr, SrcAS, DestAS);
1638 CSEMap.InsertNode(N, IP);
1639 AllNodes.push_back(N);
1640 return SDValue(N, 0);
1641}
Chris Lattnerdecc2672010-04-07 05:20:54 +00001642
Duncan Sands92abc622009-01-31 15:50:11 +00001643/// getShiftAmountOperand - Return the specified value casted to
1644/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001645SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001646 EVT OpTy = Op.getValueType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001647 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001648 if (OpTy == ShTy || OpTy.isVector()) return Op;
1649
1650 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001651 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001652}
1653
Chris Lattner37ce9df2007-10-15 17:47:20 +00001654/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1655/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001656SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001657 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001658 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001659 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001660 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang364d73d2008-07-05 20:40:31 +00001661 unsigned StackAlign =
Bill Wendlingba54bca2013-06-19 21:36:55 +00001662 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001663
David Greene3f2bf852009-11-12 20:49:22 +00001664 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001665 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner37ce9df2007-10-15 17:47:20 +00001666}
1667
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001668/// CreateStackTemporary - Create a stack temporary suitable for holding
1669/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001670SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001671 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1672 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001673 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1674 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001675 const TargetLowering *TLI = TM.getTargetLowering();
1676 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001677 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1678 TD->getPrefTypeAlignment(Ty2));
1679
1680 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001681 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001682 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001683}
1684
Owen Andersone50ed302009-08-10 22:56:29 +00001685SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001686 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001687 // These setcc operations always fold.
1688 switch (Cond) {
1689 default: break;
1690 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001691 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001692 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001693 case ISD::SETTRUE2: {
1694 const TargetLowering *TLI = TM.getTargetLowering();
1695 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1696 return getConstant(
1697 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1698 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001699
Chris Lattnera83385f2006-04-27 05:01:07 +00001700 case ISD::SETOEQ:
1701 case ISD::SETOGT:
1702 case ISD::SETOGE:
1703 case ISD::SETOLT:
1704 case ISD::SETOLE:
1705 case ISD::SETONE:
1706 case ISD::SETO:
1707 case ISD::SETUO:
1708 case ISD::SETUEQ:
1709 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001710 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001711 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001712 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001713
Gabor Greifba36cb52008-08-28 21:40:38 +00001714 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001715 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001716 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001717 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Chris Lattnerc3aae252005-01-07 07:46:32 +00001719 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001720 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001721 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1722 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001723 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1724 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1725 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1726 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1727 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1728 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1729 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1730 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001731 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001732 }
Chris Lattner67255a12005-04-07 18:14:58 +00001733 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001734 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1735 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001736 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001737 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001738 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001739 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001740 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001741 // fall through
1742 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001743 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001744 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001745 // fall through
1746 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001747 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001748 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001749 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001750 // fall through
1751 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001752 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001753 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001754 // fall through
1755 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001756 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001757 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001758 // fall through
1759 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001760 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001761 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001762 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001763 // fall through
1764 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001765 R==APFloat::cmpEqual, VT);
1766 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1767 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1768 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1769 R==APFloat::cmpEqual, VT);
1770 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1771 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1772 R==APFloat::cmpLessThan, VT);
1773 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1774 R==APFloat::cmpUnordered, VT);
1775 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1776 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001777 }
1778 } else {
1779 // Ensure that the constant occurs on the RHS.
Tom Stellard12d43f92013-09-28 02:50:38 +00001780 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1781 MVT CompVT = N1.getValueType().getSimpleVT();
1782 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1783 return SDValue();
1784
1785 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001786 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001787 }
1788
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001789 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001790 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001791}
1792
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001793/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1794/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001795bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001796 // This predicate is not safe for vector operations.
1797 if (Op.getValueType().isVector())
1798 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001799
Dan Gohman87862e72009-12-11 21:31:27 +00001800 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001801 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1802}
1803
Dan Gohmanea859be2007-06-22 14:59:07 +00001804/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1805/// this predicate to simplify operations downstream. Mask is known to be zero
1806/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001807bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001808 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001809 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001810 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001811 return (KnownZero & Mask) == Mask;
1812}
1813
Stephen Hinesdce4a402014-05-29 02:49:00 -07001814/// Determine which bits of Op are known to be either zero or one and return
1815/// them in the KnownZero/KnownOne bitsets.
1816void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1817 APInt &KnownOne, unsigned Depth) const {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001818 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001819 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001820
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001821 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001822 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001823 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001824
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001825 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001826
1827 switch (Op.getOpcode()) {
1828 case ISD::Constant:
1829 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001830 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1831 KnownZero = ~KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001832 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001833 case ISD::AND:
1834 // If either the LHS or the RHS are Zero, the result is zero.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001835 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1836 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001837
1838 // Output known-1 bits are only known if set in both the LHS & RHS.
1839 KnownOne &= KnownOne2;
1840 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1841 KnownZero |= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001842 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001843 case ISD::OR:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001844 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1845 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001846
Dan Gohmanea859be2007-06-22 14:59:07 +00001847 // Output known-0 bits are only known if clear in both the LHS & RHS.
1848 KnownZero &= KnownZero2;
1849 // Output known-1 are known to be set if set in either the LHS | RHS.
1850 KnownOne |= KnownOne2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001851 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 case ISD::XOR: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001853 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1854 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001855
Dan Gohmanea859be2007-06-22 14:59:07 +00001856 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001857 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001858 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1859 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1860 KnownZero = KnownZeroOut;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001861 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001862 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001863 case ISD::MUL: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001864 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1865 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001866
1867 // If low bits are zero in either operand, output low known-0 bits.
1868 // Also compute a conserative estimate for high known-0 bits.
1869 // More trickiness is possible, but this is sufficient for the
1870 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001871 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001872 unsigned TrailZ = KnownZero.countTrailingOnes() +
1873 KnownZero2.countTrailingOnes();
1874 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001875 KnownZero2.countLeadingOnes(),
1876 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001877
1878 TrailZ = std::min(TrailZ, BitWidth);
1879 LeadZ = std::min(LeadZ, BitWidth);
1880 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1881 APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001882 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00001883 }
1884 case ISD::UDIV: {
1885 // For the purposes of computing leading zeros we can conservatively
1886 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001887 // be less than the denominator.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001888 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001889 unsigned LeadZ = KnownZero2.countLeadingOnes();
1890
Jay Foad7a874dd2010-12-01 08:53:58 +00001891 KnownOne2.clearAllBits();
1892 KnownZero2.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001893 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001894 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1895 if (RHSUnknownLeadingOnes != BitWidth)
1896 LeadZ = std::min(BitWidth,
1897 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001898
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001899 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001900 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00001901 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001902 case ISD::SELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001903 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1904 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001905
Dan Gohmanea859be2007-06-22 14:59:07 +00001906 // Only known if known in both the LHS and RHS.
1907 KnownOne &= KnownOne2;
1908 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001909 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001910 case ISD::SELECT_CC:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001911 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1912 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001913
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 // Only known if known in both the LHS and RHS.
1915 KnownOne &= KnownOne2;
1916 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001917 break;
Bill Wendling253174b2008-11-22 07:24:01 +00001918 case ISD::SADDO:
1919 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001920 case ISD::SSUBO:
1921 case ISD::USUBO:
1922 case ISD::SMULO:
1923 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001924 if (Op.getResNo() != 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001925 break;
Duncan Sands03228082008-11-23 15:47:28 +00001926 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001927 case ISD::SETCC:
1928 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001929 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands28b77e92011-09-06 19:07:46 +00001930 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001931 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001932 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001933 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001934 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001935 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001936 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001937
1938 // If the shift count is an invalid immediate, don't do anything.
1939 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001940 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00001941
Stephen Hinesdce4a402014-05-29 02:49:00 -07001942 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmand4cf9922008-02-26 18:50:50 +00001943 KnownZero <<= ShAmt;
1944 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001945 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001946 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001947 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001948 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001949 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001950 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001951 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001952 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001953
Dan Gohmand4cf9922008-02-26 18:50:50 +00001954 // If the shift count is an invalid immediate, don't do anything.
1955 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001956 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00001957
Stephen Hinesdce4a402014-05-29 02:49:00 -07001958 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001959 KnownZero = KnownZero.lshr(ShAmt);
1960 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001961
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001962 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001963 KnownZero |= HighBits; // High bits known zero.
1964 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001965 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001966 case ISD::SRA:
1967 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001968 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001969
Dan Gohmand4cf9922008-02-26 18:50:50 +00001970 // If the shift count is an invalid immediate, don't do anything.
1971 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001972 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00001973
Dan Gohmanea859be2007-06-22 14:59:07 +00001974 // If any of the demanded bits are produced by the sign extension, we also
1975 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001976 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001977
Stephen Hinesdce4a402014-05-29 02:49:00 -07001978 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001979 KnownZero = KnownZero.lshr(ShAmt);
1980 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001981
Dan Gohmanea859be2007-06-22 14:59:07 +00001982 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001983 APInt SignBit = APInt::getSignBit(BitWidth);
1984 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Dan Gohmanca93a432008-02-20 16:30:17 +00001986 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001988 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001989 KnownOne |= HighBits; // New bits are known one.
1990 }
1991 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001992 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001993 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001994 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001995 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001996
1997 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001998 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001999 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002000
Dan Gohman977a76f2008-02-13 22:28:48 +00002001 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002002 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002003
Dan Gohmanea859be2007-06-22 14:59:07 +00002004 // If the sign extended bits are demanded, we know that the sign
2005 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00002006 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00002007 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Stephen Hinesdce4a402014-05-29 02:49:00 -07002010 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002011 KnownOne &= InputDemandedBits;
2012 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002013
Dan Gohmanea859be2007-06-22 14:59:07 +00002014 // If the sign bit of the input is known set or clear, then we know the
2015 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00002016 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00002017 KnownZero |= NewBits;
2018 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00002019 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00002020 KnownOne |= NewBits;
2021 KnownZero &= ~NewBits;
2022 } else { // Input sign bit unknown
2023 KnownZero &= ~NewBits;
2024 KnownOne &= ~NewBits;
2025 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002026 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002027 }
2028 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002029 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002030 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002031 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002032 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002033 unsigned LowBits = Log2_32(BitWidth)+1;
2034 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00002035 KnownOne.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07002036 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002037 }
2038 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00002039 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00002040 // If this is a ZEXTLoad and we are looking at the loaded value.
2041 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00002042 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002043 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002044 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00002045 } else if (const MDNode *Ranges = LD->getRanges()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002046 computeKnownBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002047 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002048 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002049 }
2050 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002051 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002052 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002053 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00002054 KnownZero = KnownZero.trunc(InBits);
2055 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002056 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002057 KnownZero = KnownZero.zext(BitWidth);
2058 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002059 KnownZero |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002060 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002061 }
2062 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002063 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002064 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002065 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002066
Jay Foad40f8f622010-12-07 08:25:19 +00002067 KnownZero = KnownZero.trunc(InBits);
2068 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002069 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00002070
2071 // Note if the sign bit is known to be zero or one.
2072 bool SignBitKnownZero = KnownZero.isNegative();
2073 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohman977a76f2008-02-13 22:28:48 +00002074
Jay Foad40f8f622010-12-07 08:25:19 +00002075 KnownZero = KnownZero.zext(BitWidth);
2076 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002077
Dan Gohman977a76f2008-02-13 22:28:48 +00002078 // If the sign bit is known zero or one, the top bits match.
2079 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00002080 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00002081 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00002082 KnownOne |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002083 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002084 }
2085 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00002086 EVT InVT = Op.getOperand(0).getValueType();
2087 unsigned InBits = InVT.getScalarType().getSizeInBits();
2088 KnownZero = KnownZero.trunc(InBits);
2089 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002090 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng2766a472012-12-06 19:13:27 +00002091 KnownZero = KnownZero.zext(BitWidth);
2092 KnownOne = KnownOne.zext(BitWidth);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002093 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002094 }
2095 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00002096 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002097 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00002098 KnownZero = KnownZero.zext(InBits);
2099 KnownOne = KnownOne.zext(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002100 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002101 KnownZero = KnownZero.trunc(BitWidth);
2102 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002103 break;
2104 }
2105 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002107 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07002108 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002109 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002110 KnownOne &= (~KnownZero);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002111 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002112 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002113 case ISD::FGETSIGN:
2114 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002115 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002116 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002117
Dan Gohman23e8b712008-04-28 17:02:21 +00002118 case ISD::SUB: {
2119 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2120 // We know that the top bits of C-X are clear if X contains less bits
2121 // than C (i.e. no wrap-around can happen). For example, 20-X is
2122 // positive if we can prove that X is >= 0 and < 16.
2123 if (CLHS->getAPIntValue().isNonNegative()) {
2124 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2125 // NLZ can't be BitWidth with no sign bit
2126 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002127 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002128
2129 // If all of the MaskV bits are known to be zero, then we know the
2130 // output top bits are zero, because we now know that the output is
2131 // from [0-C].
2132 if ((KnownZero2 & MaskV) == MaskV) {
2133 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2134 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002135 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002136 }
2137 }
2138 }
2139 }
2140 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002141 case ISD::ADD:
2142 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002143 // Output known-0 bits are known if clear or set in both the low clear bits
2144 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2145 // low 3 bits clear.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002146 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002147 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2148
Stephen Hinesdce4a402014-05-29 02:49:00 -07002149 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002150 KnownZeroOut = std::min(KnownZeroOut,
2151 KnownZero2.countTrailingOnes());
2152
Chris Lattnerda605882010-12-19 20:38:28 +00002153 if (Op.getOpcode() == ISD::ADD) {
2154 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002155 break;
Chris Lattnerda605882010-12-19 20:38:28 +00002156 }
2157
2158 // With ADDE, a carry bit may be added in, so we can only use this
2159 // information if we know (at least) that the low two bits are clear. We
2160 // then return to the caller that the low bit is unknown but that other bits
2161 // are known zero.
2162 if (KnownZeroOut >= 2) // ADDE
2163 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002164 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002165 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002166 case ISD::SREM:
2167 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002168 const APInt &RA = Rem->getAPIntValue().abs();
2169 if (RA.isPowerOf2()) {
2170 APInt LowBits = RA - 1;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002171 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002172
Duncan Sands5c2873a2010-01-29 09:45:26 +00002173 // The low bits of the first operand are unchanged by the srem.
2174 KnownZero = KnownZero2 & LowBits;
2175 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002176
Duncan Sands5c2873a2010-01-29 09:45:26 +00002177 // If the first operand is non-negative or has all low bits zero, then
2178 // the upper bits are all zero.
2179 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2180 KnownZero |= ~LowBits;
2181
2182 // If the first operand is negative and not all low bits are zero, then
2183 // the upper bits are all one.
2184 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2185 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002186 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002187 }
2188 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002189 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002190 case ISD::UREM: {
2191 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002192 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002193 if (RA.isPowerOf2()) {
2194 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002195 KnownZero |= ~LowBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002196 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002197 break;
2198 }
2199 }
2200
2201 // Since the result is less than or equal to either operand, any leading
2202 // zero bits in either operand must also exist in the result.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002203 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2204 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002205
2206 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2207 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002208 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002209 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002210 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002211 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002212 case ISD::FrameIndex:
2213 case ISD::TargetFrameIndex:
2214 if (unsigned Align = InferPtrAlignment(Op)) {
2215 // The low bits are known zero if the pointer is aligned.
2216 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002217 break;
Chris Lattner0a9481f2011-02-13 22:25:43 +00002218 }
2219 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002220
Dan Gohmanea859be2007-06-22 14:59:07 +00002221 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002222 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2223 break;
2224 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002225 case ISD::INTRINSIC_WO_CHAIN:
2226 case ISD::INTRINSIC_W_CHAIN:
2227 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002228 // Allow the target to implement this method for its nodes.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002229 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
2230 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002231 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002232
2233 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002234}
2235
2236/// ComputeNumSignBits - Return the number of times the sign bit of the
2237/// register is replicated into the other bits. We know that at least 1 bit
2238/// is always equal to the sign bit (itself), but other cases can give us
2239/// information. For example, immediately after an "SRA X, 2", we know that
2240/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002241unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlingba54bca2013-06-19 21:36:55 +00002242 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00002243 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002244 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002245 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002246 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002247 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002248
Dan Gohmanea859be2007-06-22 14:59:07 +00002249 if (Depth == 6)
2250 return 1; // Limit search depth.
2251
2252 switch (Op.getOpcode()) {
2253 default: break;
2254 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002255 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002256 return VTBits-Tmp+1;
2257 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002258 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002259 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002260
Dan Gohmanea859be2007-06-22 14:59:07 +00002261 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002262 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002263 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002264 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002265
Dan Gohmanea859be2007-06-22 14:59:07 +00002266 case ISD::SIGN_EXTEND:
Jack Carter3af4d252013-09-09 22:02:08 +00002267 Tmp =
2268 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002269 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002270
Dan Gohmanea859be2007-06-22 14:59:07 +00002271 case ISD::SIGN_EXTEND_INREG:
2272 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002273 Tmp =
2274 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002275 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002276
Dan Gohmanea859be2007-06-22 14:59:07 +00002277 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2278 return std::max(Tmp, Tmp2);
2279
2280 case ISD::SRA:
2281 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2282 // SRA X, C -> adds C sign bits.
2283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002284 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002285 if (Tmp > VTBits) Tmp = VTBits;
2286 }
2287 return Tmp;
2288 case ISD::SHL:
2289 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2290 // shl destroys sign bits.
2291 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002292 if (C->getZExtValue() >= VTBits || // Bad shift.
2293 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2294 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002295 }
2296 break;
2297 case ISD::AND:
2298 case ISD::OR:
2299 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002300 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002301 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002302 if (Tmp != 1) {
2303 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2304 FirstAnswer = std::min(Tmp, Tmp2);
2305 // We computed what we know about the sign bits as our first
2306 // answer. Now proceed to the generic code that uses
Stephen Hinesdce4a402014-05-29 02:49:00 -07002307 // computeKnownBits, and pick whichever answer is better.
Dan Gohmana332f172008-05-23 02:28:01 +00002308 }
2309 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002310
2311 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002312 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002313 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002314 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002315 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002316
2317 case ISD::SADDO:
2318 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002319 case ISD::SSUBO:
2320 case ISD::USUBO:
2321 case ISD::SMULO:
2322 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002323 if (Op.getResNo() != 1)
2324 break;
Duncan Sands03228082008-11-23 15:47:28 +00002325 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002326 case ISD::SETCC:
2327 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002328 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002329 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002330 return VTBits;
2331 break;
2332 case ISD::ROTL:
2333 case ISD::ROTR:
2334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002335 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002336
Dan Gohmanea859be2007-06-22 14:59:07 +00002337 // Handle rotate right by N like a rotate left by 32-N.
2338 if (Op.getOpcode() == ISD::ROTR)
2339 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2340
2341 // If we aren't rotating out all of the known-in sign bits, return the
2342 // number that are left. This handles rotl(sext(x), 1) for example.
2343 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2344 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2345 }
2346 break;
2347 case ISD::ADD:
2348 // Add can have at most one carry bit. Thus we know that the output
2349 // is, at worst, one more bit than the inputs.
2350 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2351 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002352
Dan Gohmanea859be2007-06-22 14:59:07 +00002353 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002354 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002355 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002356 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002357 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002358
Dan Gohmanea859be2007-06-22 14:59:07 +00002359 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2360 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002361 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002362 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002363
Dan Gohmanea859be2007-06-22 14:59:07 +00002364 // If we are subtracting one from a positive number, there is no carry
2365 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002366 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002367 return Tmp;
2368 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002369
Dan Gohmanea859be2007-06-22 14:59:07 +00002370 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2371 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002372 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002373
Dan Gohmanea859be2007-06-22 14:59:07 +00002374 case ISD::SUB:
2375 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2376 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002377
Dan Gohmanea859be2007-06-22 14:59:07 +00002378 // Handle NEG.
2379 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002380 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002381 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002382 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002383 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2384 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002385 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002386 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002387
Dan Gohmanea859be2007-06-22 14:59:07 +00002388 // If the input is known to be positive (the sign bit is known clear),
2389 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002390 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002391 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002392
Dan Gohmanea859be2007-06-22 14:59:07 +00002393 // Otherwise, we treat this like a SUB.
2394 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002395
Dan Gohmanea859be2007-06-22 14:59:07 +00002396 // Sub can have at most one carry bit. Thus we know that the output
2397 // is, at worst, one more bit than the inputs.
2398 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2399 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002400 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002401 case ISD::TRUNCATE:
2402 // FIXME: it's tricky to do anything useful for this, but it is an important
2403 // case for targets like X86.
2404 break;
2405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002406
Nadav Rotem77451752013-03-20 22:53:44 +00002407 // If we are looking at the loaded value of the SDNode.
2408 if (Op.getResNo() == 0) {
2409 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2410 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2411 unsigned ExtType = LD->getExtensionType();
2412 switch (ExtType) {
2413 default: break;
2414 case ISD::SEXTLOAD: // '17' bits known
2415 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2416 return VTBits-Tmp+1;
2417 case ISD::ZEXTLOAD: // '16' bits known
2418 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2419 return VTBits-Tmp;
2420 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002421 }
2422 }
2423
2424 // Allow the target to implement this method for its nodes.
2425 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002426 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002427 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2428 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002429 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002430 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002431 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002432
Dan Gohmanea859be2007-06-22 14:59:07 +00002433 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2434 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002435 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002436 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002437
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002438 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002439 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002440 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002441 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002442 Mask = KnownOne;
2443 } else {
2444 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002445 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002446 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002447
Dan Gohmanea859be2007-06-22 14:59:07 +00002448 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2449 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002450 Mask = ~Mask;
2451 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002452 // Return # leading zeros. We use 'min' here in case Val was zero before
2453 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002454 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002455}
2456
Chris Lattner0a9481f2011-02-13 22:25:43 +00002457/// isBaseWithConstantOffset - Return true if the specified operand is an
2458/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2459/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2460/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002461/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002462bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2463 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2464 !isa<ConstantSDNode>(Op.getOperand(1)))
2465 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002466
2467 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002468 !MaskedValueIsZero(Op.getOperand(0),
2469 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2470 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002471
Chris Lattner0a9481f2011-02-13 22:25:43 +00002472 return true;
2473}
2474
2475
Dan Gohman8d44b282009-09-03 20:34:31 +00002476bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2477 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002478 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002479 return true;
2480
2481 // If the value is a constant, we can obviously see if it is a NaN or not.
2482 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2483 return !C->getValueAPF().isNaN();
2484
2485 // TODO: Recognize more cases here.
2486
2487 return false;
2488}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002489
Dan Gohmane8326932010-02-24 06:52:40 +00002490bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2491 // If the value is a constant, we can obviously see if it is a zero or not.
2492 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2493 return !C->isZero();
2494
2495 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002496 switch (Op.getOpcode()) {
2497 default: break;
2498 case ISD::OR:
2499 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2500 return !C->isNullValue();
2501 break;
2502 }
Dan Gohmane8326932010-02-24 06:52:40 +00002503
2504 return false;
2505}
2506
2507bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2508 // Check the obvious case.
2509 if (A == B) return true;
2510
2511 // For for negative and positive zero.
2512 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2513 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2514 if (CA->isZero() && CB->isZero()) return true;
2515
2516 // Otherwise they may not be equal.
2517 return false;
2518}
2519
Chris Lattnerc3aae252005-01-07 07:46:32 +00002520/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002521///
Andrew Trickac6d9be2013-05-25 02:42:55 +00002522SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002523 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002524 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
2525 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002526 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002527 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002528
Jack Carter3af4d252013-09-09 22:02:08 +00002529 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2530 DL.getDebugLoc(), getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002531 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002532
Chris Lattner4a283e92006-08-11 18:38:11 +00002533 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002534#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002535 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002536#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002537 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002538}
2539
Andrew Trickac6d9be2013-05-25 02:42:55 +00002540SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002541 EVT VT, SDValue Operand) {
Stephen Hines36b56882014-04-23 16:57:46 -07002542 // Constant fold unary operations with an integer constant operand. Even
2543 // opaque constant will be folded, because the folding of unary operations
2544 // doesn't create new constants with different values. Nevertheless, the
2545 // opaque flag is preserved during folding to prevent future folding with
2546 // other constants.
Gabor Greifba36cb52008-08-28 21:40:38 +00002547 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002548 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002549 switch (Opcode) {
2550 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002551 case ISD::SIGN_EXTEND:
Stephen Hines36b56882014-04-23 16:57:46 -07002552 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2553 C->isTargetOpcode(), C->isOpaque());
Chris Lattner4ed11b42005-09-02 00:17:32 +00002554 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002555 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002556 case ISD::TRUNCATE:
Stephen Hines36b56882014-04-23 16:57:46 -07002557 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2558 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen73328d12007-09-19 23:55:34 +00002559 case ISD::UINT_TO_FP:
2560 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002561 APFloat apf(EVTToAPFloatSemantics(VT),
2562 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002563 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002564 Opcode==ISD::SINT_TO_FP,
2565 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002566 return getConstantFP(apf, VT);
2567 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002568 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002569 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002570 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002571 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002572 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002573 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002574 case ISD::BSWAP:
Stephen Hines36b56882014-04-23 16:57:46 -07002575 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2576 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002577 case ISD::CTPOP:
Stephen Hines36b56882014-04-23 16:57:46 -07002578 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2579 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002580 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002581 case ISD::CTLZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002582 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2583 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002584 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002585 case ISD::CTTZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002586 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2587 C->isOpaque());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002588 }
2589 }
2590
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002591 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002592 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002593 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002594 switch (Opcode) {
2595 case ISD::FNEG:
2596 V.changeSign();
2597 return getConstantFP(V, VT);
2598 case ISD::FABS:
2599 V.clearSign();
2600 return getConstantFP(V, VT);
2601 case ISD::FCEIL: {
2602 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2603 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002604 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002605 break;
2606 }
2607 case ISD::FTRUNC: {
2608 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2609 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002610 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002611 break;
2612 }
2613 case ISD::FFLOOR: {
2614 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2615 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002616 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002617 break;
2618 }
2619 case ISD::FP_EXTEND: {
2620 bool ignored;
2621 // This can return overflow, underflow, or inexact; we don't care.
2622 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002623 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002624 APFloat::rmNearestTiesToEven, &ignored);
2625 return getConstantFP(V, VT);
2626 }
2627 case ISD::FP_TO_SINT:
2628 case ISD::FP_TO_UINT: {
2629 integerPart x[2];
2630 bool ignored;
2631 assert(integerPartWidth >= 64);
2632 // FIXME need to be more flexible about rounding mode.
2633 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2634 Opcode==ISD::FP_TO_SINT,
2635 APFloat::rmTowardZero, &ignored);
2636 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002637 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002638 APInt api(VT.getSizeInBits(), x);
2639 return getConstant(api, VT);
2640 }
2641 case ISD::BITCAST:
2642 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2643 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2644 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2645 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2646 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002647 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002648 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002649
Gabor Greifba36cb52008-08-28 21:40:38 +00002650 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002651 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002652 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002653 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002654 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002655 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002656 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002657 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002658 assert(VT.isFloatingPoint() &&
2659 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002660 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002661 assert((!VT.isVector() ||
2662 VT.getVectorNumElements() ==
2663 Operand.getValueType().getVectorNumElements()) &&
2664 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002665 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002666 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002667 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002668 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002669 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002670 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002671 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002672 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2673 "Invalid sext node, dst < src!");
2674 assert((!VT.isVector() ||
2675 VT.getVectorNumElements() ==
2676 Operand.getValueType().getVectorNumElements()) &&
2677 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002678 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2679 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2680 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002681 // sext(undef) = 0, because the top bits will all be the same.
2682 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002683 break;
2684 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002685 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002686 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002687 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002688 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2689 "Invalid zext node, dst < src!");
2690 assert((!VT.isVector() ||
2691 VT.getVectorNumElements() ==
2692 Operand.getValueType().getVectorNumElements()) &&
2693 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002694 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002695 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002696 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002697 else if (OpOpcode == ISD::UNDEF)
2698 // zext(undef) = 0, because the top bits will be zero.
2699 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002700 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002701 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002702 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002703 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002704 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002705 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2706 "Invalid anyext node, dst < src!");
2707 assert((!VT.isVector() ||
2708 VT.getVectorNumElements() ==
2709 Operand.getValueType().getVectorNumElements()) &&
2710 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002711
Dan Gohman9e86a732010-06-18 00:08:30 +00002712 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2713 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002714 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002715 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002716 else if (OpOpcode == ISD::UNDEF)
2717 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002718
2719 // (ext (trunx x)) -> x
2720 if (OpOpcode == ISD::TRUNCATE) {
2721 SDValue OpOp = Operand.getNode()->getOperand(0);
2722 if (OpOp.getValueType() == VT)
2723 return OpOp;
2724 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002725 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002726 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002727 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002728 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002729 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002730 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2731 "Invalid truncate node, src < dst!");
2732 assert((!VT.isVector() ||
2733 VT.getVectorNumElements() ==
2734 Operand.getValueType().getVectorNumElements()) &&
2735 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002736 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002737 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002738 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2739 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002740 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002741 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2742 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002743 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002744 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002745 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002746 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002747 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002748 if (OpOpcode == ISD::UNDEF)
2749 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002750 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002751 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002752 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002753 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002754 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002755 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002756 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2757 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002758 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002759 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002760 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002761 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002763 (VT.getVectorElementType() == Operand.getValueType() ||
2764 (VT.getVectorElementType().isInteger() &&
2765 Operand.getValueType().isInteger() &&
2766 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002767 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002768 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002769 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002770 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2771 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2772 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2773 Operand.getConstantOperandVal(1) == 0 &&
2774 Operand.getOperand(0).getValueType() == VT)
2775 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002776 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002777 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002778 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002779 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002780 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002781 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002782 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002783 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002784 break;
2785 case ISD::FABS:
2786 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002787 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002788 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002789 }
2790
Chris Lattner43247a12005-08-25 19:12:10 +00002791 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002792 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002793 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002794 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002795 SDValue Ops[1] = { Operand };
Stephen Hinesdce4a402014-05-29 02:49:00 -07002796 AddNodeIDNode(ID, Opcode, VTs, Ops);
2797 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002798 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002799 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002800
Jack Carter3af4d252013-09-09 22:02:08 +00002801 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2802 DL.getDebugLoc(), VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002803 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002804 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00002805 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2806 DL.getDebugLoc(), VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002807 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002808
Chris Lattnerc3aae252005-01-07 07:46:32 +00002809 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002810#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002811 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002812#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002813 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002814}
2815
Benjamin Kramer49693102013-02-04 15:19:18 +00002816SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2817 SDNode *Cst1, SDNode *Cst2) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002818 // If the opcode is a target-specific ISD node, there's nothing we can
2819 // do here and the operand rules may not line up with the below, so
2820 // bail early.
2821 if (Opcode >= ISD::BUILTIN_OP_END)
2822 return SDValue();
2823
Benjamin Kramer49693102013-02-04 15:19:18 +00002824 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2825 SmallVector<SDValue, 4> Outputs;
2826 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00002827
Benjamin Kramer49693102013-02-04 15:19:18 +00002828 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2829 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Stephen Hines36b56882014-04-23 16:57:46 -07002830 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2831 return SDValue();
2832
2833 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00002834 // Scalar instruction.
2835 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Stephen Hines36b56882014-04-23 16:57:46 -07002836 else {
Benjamin Kramer49693102013-02-04 15:19:18 +00002837 // For vectors extract each constant element into Inputs so we can constant
2838 // fold them individually.
2839 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2840 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2841 if (!BV1 || !BV2)
2842 return SDValue();
2843
2844 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2845
2846 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2847 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2848 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2849 if (!V1 || !V2) // Not a constant, bail.
2850 return SDValue();
2851
Stephen Hines36b56882014-04-23 16:57:46 -07002852 if (V1->isOpaque() || V2->isOpaque())
2853 return SDValue();
2854
Benjamin Kramer49693102013-02-04 15:19:18 +00002855 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2856 // FIXME: This is valid and could be handled by truncating the APInts.
2857 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2858 return SDValue();
2859
2860 Inputs.push_back(std::make_pair(V1, V2));
2861 }
Bill Wendling688d1c42008-09-24 10:16:24 +00002862 }
2863
Benjamin Kramer49693102013-02-04 15:19:18 +00002864 // We have a number of constant values, constant fold them element by element.
2865 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2866 const APInt &C1 = Inputs[I].first->getAPIntValue();
2867 const APInt &C2 = Inputs[I].second->getAPIntValue();
2868
2869 switch (Opcode) {
2870 case ISD::ADD:
2871 Outputs.push_back(getConstant(C1 + C2, SVT));
2872 break;
2873 case ISD::SUB:
2874 Outputs.push_back(getConstant(C1 - C2, SVT));
2875 break;
2876 case ISD::MUL:
2877 Outputs.push_back(getConstant(C1 * C2, SVT));
2878 break;
2879 case ISD::UDIV:
2880 if (!C2.getBoolValue())
2881 return SDValue();
2882 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2883 break;
2884 case ISD::UREM:
2885 if (!C2.getBoolValue())
2886 return SDValue();
2887 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2888 break;
2889 case ISD::SDIV:
2890 if (!C2.getBoolValue())
2891 return SDValue();
2892 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2893 break;
2894 case ISD::SREM:
2895 if (!C2.getBoolValue())
2896 return SDValue();
2897 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2898 break;
2899 case ISD::AND:
2900 Outputs.push_back(getConstant(C1 & C2, SVT));
2901 break;
2902 case ISD::OR:
2903 Outputs.push_back(getConstant(C1 | C2, SVT));
2904 break;
2905 case ISD::XOR:
2906 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2907 break;
2908 case ISD::SHL:
2909 Outputs.push_back(getConstant(C1 << C2, SVT));
2910 break;
2911 case ISD::SRL:
2912 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2913 break;
2914 case ISD::SRA:
2915 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2916 break;
2917 case ISD::ROTL:
2918 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2919 break;
2920 case ISD::ROTR:
2921 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2922 break;
2923 default:
2924 return SDValue();
2925 }
2926 }
2927
Stephen Hinesdce4a402014-05-29 02:49:00 -07002928 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
2929 "Expected a scalar or vector!"));
2930
Benjamin Kramer49693102013-02-04 15:19:18 +00002931 // Handle the scalar case first.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002932 if (!VT.isVector())
Benjamin Kramer49693102013-02-04 15:19:18 +00002933 return Outputs.back();
2934
Stephen Hinesdce4a402014-05-29 02:49:00 -07002935 // We may have a vector type but a scalar result. Create a splat.
2936 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
2937
2938 // Build a big vector out of the scalar elements we generated.
2939 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling688d1c42008-09-24 10:16:24 +00002940}
2941
Andrew Trickac6d9be2013-05-25 02:42:55 +00002942SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer49693102013-02-04 15:19:18 +00002943 SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002944 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2945 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002946 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002947 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002948 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2950 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002951 // Fold trivial token factors.
2952 if (N1.getOpcode() == ISD::EntryToken) return N2;
2953 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002954 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002955 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002956 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002957 // Concat of UNDEFs is UNDEF.
2958 if (N1.getOpcode() == ISD::UNDEF &&
2959 N2.getOpcode() == ISD::UNDEF)
2960 return getUNDEF(VT);
2961
Dan Gohman7f8613e2008-08-14 20:04:46 +00002962 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2963 // one big BUILD_VECTOR.
2964 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2965 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002966 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2967 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002968 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07002969 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00002970 }
2971 break;
Chris Lattner76365122005-01-16 02:23:22 +00002972 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002973 assert(VT.isInteger() && "This operator does not apply to FP types!");
2974 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002975 N1.getValueType() == VT && "Binary operator types must match!");
2976 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2977 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002978 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002979 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002980 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2981 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002982 break;
Chris Lattner76365122005-01-16 02:23:22 +00002983 case ISD::OR:
2984 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002985 case ISD::ADD:
2986 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002987 assert(VT.isInteger() && "This operator does not apply to FP types!");
2988 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002989 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002990 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2991 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002992 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002993 return N1;
2994 break;
Chris Lattner76365122005-01-16 02:23:22 +00002995 case ISD::UDIV:
2996 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002997 case ISD::MULHU:
2998 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002999 case ISD::MUL:
3000 case ISD::SDIV:
3001 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00003002 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00003003 assert(N1.getValueType() == N2.getValueType() &&
3004 N1.getValueType() == VT && "Binary operator types must match!");
3005 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003006 case ISD::FADD:
3007 case ISD::FSUB:
3008 case ISD::FMUL:
3009 case ISD::FDIV:
3010 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003011 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00003012 if (Opcode == ISD::FADD) {
3013 // 0+x --> x
3014 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3015 if (CFP->getValueAPF().isZero())
3016 return N2;
3017 // x+0 --> x
3018 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3019 if (CFP->getValueAPF().isZero())
3020 return N1;
3021 } else if (Opcode == ISD::FSUB) {
3022 // x-0 --> x
3023 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3024 if (CFP->getValueAPF().isZero())
3025 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00003026 } else if (Opcode == ISD::FMUL) {
3027 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3028 SDValue V = N2;
3029
3030 // If the first operand isn't the constant, try the second
3031 if (!CFP) {
3032 CFP = dyn_cast<ConstantFPSDNode>(N2);
3033 V = N1;
3034 }
3035
3036 if (CFP) {
3037 // 0*x --> 0
3038 if (CFP->isZero())
3039 return SDValue(CFP,0);
3040 // 1*x --> x
3041 if (CFP->isExactlyValue(1.0))
3042 return V;
3043 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00003044 }
Dan Gohman760f86f2009-01-22 21:58:43 +00003045 }
Dale Johannesen78995512010-05-15 18:38:02 +00003046 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00003047 assert(N1.getValueType() == N2.getValueType() &&
3048 N1.getValueType() == VT && "Binary operator types must match!");
3049 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003050 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3051 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003052 N1.getValueType().isFloatingPoint() &&
3053 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00003054 "Invalid FCOPYSIGN!");
3055 break;
Chris Lattner76365122005-01-16 02:23:22 +00003056 case ISD::SHL:
3057 case ISD::SRA:
3058 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00003059 case ISD::ROTL:
3060 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00003061 assert(VT == N1.getValueType() &&
3062 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003063 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00003064 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00003065 assert((!VT.isVector() || VT == N2.getValueType()) &&
3066 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00003067 // Verify that the shift amount VT is bit enough to hold valid shift
3068 // amounts. This catches things like trying to shift an i1024 value by an
3069 // i8, which is easy to fall into in generic code that uses
3070 // TLI.getShiftAmount().
3071 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00003072 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00003073 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00003074
3075 // Always fold shifts of i1 values so the code generator doesn't need to
3076 // handle them. Since we know the size of the shift has to be less than the
3077 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00003078 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00003079 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00003080 if (N2C && N2C->isNullValue())
3081 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00003082 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00003083 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003084 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003085 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003086 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003087 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00003088 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003089 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003090 "type is vector!");
3091 assert((!EVT.isVector() ||
3092 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3093 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003094 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00003095 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003096 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00003097 break;
3098 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003099 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003100 assert(VT.isFloatingPoint() &&
3101 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003102 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00003103 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003104 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00003105 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00003106 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003107 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00003108 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003109 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003110 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003111 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00003112 assert(!EVT.isVector() &&
3113 "AssertSExt/AssertZExt type should be the vector element type "
3114 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003115 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00003116 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003117 break;
3118 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003119 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003120 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003121 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003122 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003123 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00003124 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003125 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003126 "type is vector!");
3127 assert((!EVT.isVector() ||
3128 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3129 "Vector element counts must match in SIGN_EXTEND_INREG");
3130 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003131 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00003132
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003133 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00003134 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003135 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00003136 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00003137 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00003138 return getConstant(Val, VT);
3139 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003140 break;
3141 }
3142 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00003143 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3144 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00003145 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003146
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003147 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3148 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003149 if (N2C &&
3150 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003151 N1.getNumOperands() > 0) {
3152 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003153 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003154 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003155 N1.getOperand(N2C->getZExtValue() / Factor),
3156 getConstant(N2C->getZExtValue() % Factor,
3157 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003158 }
3159
3160 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3161 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003162 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3163 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003164
3165 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003166 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003167 // are promoted and implicitly truncated, and the result implicitly
3168 // extended. Make that explicit here.
3169 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003170
Bob Wilsonb1303d02009-04-13 22:05:19 +00003171 return Elt;
3172 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003173
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003174 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3175 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003176 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003177 // If the indices are the same, return the inserted element else
3178 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003179 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003180 SDValue N1Op2 = N1.getOperand(2);
3181 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3182
3183 if (N1Op2C && N2C) {
3184 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3185 if (VT == N1.getOperand(1).getValueType())
3186 return N1.getOperand(1);
3187 else
3188 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3189 }
3190
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003191 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003192 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003193 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003194 break;
3195 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003196 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003197 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3198 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003199 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003200 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003201
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003202 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3203 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003204 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003205 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003206 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003207
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003208 // EXTRACT_ELEMENT of a constant int is also very common.
3209 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003210 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003211 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003212 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3213 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003214 }
3215 break;
David Greene91585092011-01-26 15:38:49 +00003216 case ISD::EXTRACT_SUBVECTOR: {
3217 SDValue Index = N2;
3218 if (VT.isSimple() && N1.getValueType().isSimple()) {
3219 assert(VT.isVector() && N1.getValueType().isVector() &&
3220 "Extract subvector VTs must be a vectors!");
Jack Carter3af4d252013-09-09 22:02:08 +00003221 assert(VT.getVectorElementType() ==
3222 N1.getValueType().getVectorElementType() &&
David Greene91585092011-01-26 15:38:49 +00003223 "Extract subvector VTs must have the same element type!");
Craig Topper0ff11902013-08-15 02:44:19 +00003224 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greene91585092011-01-26 15:38:49 +00003225 "Extract subvector must be from larger vector to smaller vector!");
3226
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003227 if (isa<ConstantSDNode>(Index.getNode())) {
3228 assert((VT.getVectorNumElements() +
3229 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003230 <= N1.getValueType().getVectorNumElements())
3231 && "Extract subvector overflow!");
3232 }
3233
3234 // Trivial extraction.
Craig Topper0ff11902013-08-15 02:44:19 +00003235 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greene91585092011-01-26 15:38:49 +00003236 return N1;
3237 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003238 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003239 }
David Greene91585092011-01-26 15:38:49 +00003240 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003241
Benjamin Kramer49693102013-02-04 15:19:18 +00003242 // Perform trivial constant folding.
3243 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3244 if (SV.getNode()) return SV;
3245
3246 // Canonicalize constant to RHS if commutative.
3247 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3248 std::swap(N1C, N2C);
3249 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003250 }
3251
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003252 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003253 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3254 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003255 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003256 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003257 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003258 std::swap(N1CFP, N2CFP);
3259 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003260 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003261 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3262 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003263 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003264 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003265 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003266 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003267 return getConstantFP(V1, VT);
3268 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003269 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003270 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3271 if (s!=APFloat::opInvalidOp)
3272 return getConstantFP(V1, VT);
3273 break;
3274 case ISD::FMUL:
3275 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3276 if (s!=APFloat::opInvalidOp)
3277 return getConstantFP(V1, VT);
3278 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003279 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003280 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3281 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3282 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003283 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003284 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003285 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3286 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3287 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003288 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003289 case ISD::FCOPYSIGN:
3290 V1.copySign(V2);
3291 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003292 default: break;
3293 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003294 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003295
3296 if (Opcode == ISD::FP_ROUND) {
3297 APFloat V = N1CFP->getValueAPF(); // make copy
3298 bool ignored;
3299 // This can return overflow, underflow, or inexact; we don't care.
3300 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003301 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003302 APFloat::rmNearestTiesToEven, &ignored);
3303 return getConstantFP(V, VT);
3304 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003306
Chris Lattner62b57722006-04-20 05:39:12 +00003307 // Canonicalize an UNDEF to the RHS, even over a constant.
3308 if (N1.getOpcode() == ISD::UNDEF) {
3309 if (isCommutativeBinOp(Opcode)) {
3310 std::swap(N1, N2);
3311 } else {
3312 switch (Opcode) {
3313 case ISD::FP_ROUND_INREG:
3314 case ISD::SIGN_EXTEND_INREG:
3315 case ISD::SUB:
3316 case ISD::FSUB:
3317 case ISD::FDIV:
3318 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003319 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003320 return N1; // fold op(undef, arg2) -> undef
3321 case ISD::UDIV:
3322 case ISD::SDIV:
3323 case ISD::UREM:
3324 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003325 case ISD::SRL:
3326 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003327 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003328 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3329 // For vectors, we can't easily build an all zero vector, just return
3330 // the LHS.
3331 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003332 }
3333 }
3334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003335
3336 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003337 if (N2.getOpcode() == ISD::UNDEF) {
3338 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003339 case ISD::XOR:
3340 if (N1.getOpcode() == ISD::UNDEF)
3341 // Handle undef ^ undef -> 0 special case. This is a common
3342 // idiom (misuse).
3343 return getConstant(0, VT);
3344 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003345 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003346 case ISD::ADDC:
3347 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003348 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003349 case ISD::UDIV:
3350 case ISD::SDIV:
3351 case ISD::UREM:
3352 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003353 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003354 case ISD::FADD:
3355 case ISD::FSUB:
3356 case ISD::FMUL:
3357 case ISD::FDIV:
3358 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003359 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003360 return N2;
3361 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003362 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003363 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003364 case ISD::SRL:
3365 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003366 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003367 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3368 // For vectors, we can't easily build an all zero vector, just return
3369 // the LHS.
3370 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003371 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003372 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003373 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003374 // For vectors, we can't easily build an all one vector, just return
3375 // the LHS.
3376 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003377 case ISD::SRA:
3378 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003379 }
3380 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003381
Chris Lattner27e9b412005-05-11 18:57:39 +00003382 // Memoize this node if possible.
3383 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003384 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003385 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003386 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003387 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003388 AddNodeIDNode(ID, Opcode, VTs, Ops);
3389 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003390 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003391 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003392
Jack Carter3af4d252013-09-09 22:02:08 +00003393 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3394 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003395 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003396 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003397 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3398 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003399 }
3400
Chris Lattnerc3aae252005-01-07 07:46:32 +00003401 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003402#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003403 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003404#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003405 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003406}
3407
Andrew Trickac6d9be2013-05-25 02:42:55 +00003408SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003409 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003410 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003411 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003412 switch (Opcode) {
Owen Anderson58dcd202013-05-09 22:27:13 +00003413 case ISD::FMA: {
3414 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3415 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3416 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3417 if (N1CFP && N2CFP && N3CFP) {
3418 APFloat V1 = N1CFP->getValueAPF();
3419 const APFloat &V2 = N2CFP->getValueAPF();
3420 const APFloat &V3 = N3CFP->getValueAPF();
3421 APFloat::opStatus s =
3422 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3423 if (s != APFloat::opInvalidOp)
3424 return getConstantFP(V1, VT);
3425 }
3426 break;
3427 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00003428 case ISD::CONCAT_VECTORS:
3429 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3430 // one big BUILD_VECTOR.
3431 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3432 N2.getOpcode() == ISD::BUILD_VECTOR &&
3433 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003434 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3435 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003436 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3437 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07003438 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00003439 }
3440 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003441 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003442 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003443 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003444 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003445 break;
3446 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003447 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003448 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003449 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003450 return N2; // select true, X, Y -> X
3451 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003452 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003453
3454 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003455 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003456 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003457 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003458 case ISD::INSERT_SUBVECTOR: {
3459 SDValue Index = N3;
3460 if (VT.isSimple() && N1.getValueType().isSimple()
3461 && N2.getValueType().isSimple()) {
3462 assert(VT.isVector() && N1.getValueType().isVector() &&
3463 N2.getValueType().isVector() &&
3464 "Insert subvector VTs must be a vectors");
3465 assert(VT == N1.getValueType() &&
3466 "Dest and insert subvector source types must match!");
Craig Topper0ff11902013-08-15 02:44:19 +00003467 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenecfe33c42011-01-26 19:13:22 +00003468 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003469 if (isa<ConstantSDNode>(Index.getNode())) {
3470 assert((N2.getValueType().getVectorNumElements() +
3471 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003472 <= VT.getVectorNumElements())
3473 && "Insert subvector overflow!");
3474 }
3475
3476 // Trivial insertion.
Craig Topper0ff11902013-08-15 02:44:19 +00003477 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenecfe33c42011-01-26 19:13:22 +00003478 return N2;
3479 }
3480 break;
3481 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003482 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003483 // Fold bit_convert nodes from a type to themselves.
3484 if (N1.getValueType() == VT)
3485 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003486 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003487 }
3488
Chris Lattner43247a12005-08-25 19:12:10 +00003489 // Memoize node if it doesn't produce a flag.
3490 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003491 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003492 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003493 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003494 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003495 AddNodeIDNode(ID, Opcode, VTs, Ops);
3496 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003497 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003498 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003499
Jack Carter3af4d252013-09-09 22:02:08 +00003500 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3501 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003502 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003503 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003504 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3505 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003506 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003507
Chris Lattnerc3aae252005-01-07 07:46:32 +00003508 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003509#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003510 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003511#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003512 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003513}
3514
Andrew Trickac6d9be2013-05-25 02:42:55 +00003515SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003516 SDValue N1, SDValue N2, SDValue N3,
3517 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003518 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003519 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003520}
3521
Andrew Trickac6d9be2013-05-25 02:42:55 +00003522SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003523 SDValue N1, SDValue N2, SDValue N3,
3524 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003525 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003526 return getNode(Opcode, DL, VT, Ops);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003527}
3528
Dan Gohman98ca4f22009-08-05 01:29:28 +00003529/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3530/// the incoming stack arguments to be loaded from the stack.
3531SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3532 SmallVector<SDValue, 8> ArgChains;
3533
3534 // Include the original chain at the beginning of the list. When this is
3535 // used by target LowerCall hooks, this helps legalize find the
3536 // CALLSEQ_BEGIN node.
3537 ArgChains.push_back(Chain);
3538
3539 // Add a chain value for each stack argument.
3540 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3541 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3542 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3543 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3544 if (FI->getIndex() < 0)
3545 ArgChains.push_back(SDValue(L, 1));
3546
3547 // Build a tokenfactor for all the chains.
Stephen Hinesdce4a402014-05-29 02:49:00 -07003548 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003549}
3550
Dan Gohman707e0182008-04-12 04:36:06 +00003551/// getMemsetValue - Vectorized representation of the memset value
3552/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003553static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003554 SDLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003555 assert(Value.getOpcode() != ISD::UNDEF);
3556
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003557 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003558 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003559 assert(C->getAPIntValue().getBitWidth() == 8);
3560 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003561 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003562 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003563 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003564 }
Evan Chengf0df0312008-05-15 08:39:06 +00003565
Dale Johannesen0f502f62009-02-03 22:26:09 +00003566 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003567 if (NumBits > 8) {
3568 // Use a multiplication with 0x010101... to extend the input to the
3569 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003570 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003571 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003572 }
3573
3574 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003575}
3576
Dan Gohman707e0182008-04-12 04:36:06 +00003577/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3578/// used when a memcpy is turned into a memset when the source is a constant
3579/// string ptr.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003580static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003581 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003582 // Handle vector with all elements zero.
3583 if (Str.empty()) {
3584 if (VT.isInteger())
3585 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003586 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003587 return DAG.getConstantFP(0.0, VT);
3588 else if (VT.isVector()) {
3589 unsigned NumElts = VT.getVectorNumElements();
3590 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003591 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003592 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3593 EltVT, NumElts)));
3594 } else
3595 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003596 }
3597
Duncan Sands83ec4b62008-06-06 12:08:01 +00003598 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003599 unsigned NumVTBits = VT.getSizeInBits();
3600 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003601 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3602
Evan Cheng4ff23d02013-01-10 22:13:27 +00003603 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003604 if (TLI.isLittleEndian()) {
3605 for (unsigned i = 0; i != NumBytes; ++i)
3606 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3607 } else {
3608 for (unsigned i = 0; i != NumBytes; ++i)
3609 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003610 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003611
Stephen Hines36b56882014-04-23 16:57:46 -07003612 // If the "cost" of materializing the integer immediate is less than the cost
3613 // of a load, then it is cost effective to turn the load into the immediate.
3614 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3615 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng376642e2012-12-10 23:21:26 +00003616 return DAG.getConstant(Val, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07003617 return SDValue(nullptr, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003618}
3619
Scott Michelfdc40a02009-02-17 22:15:04 +00003620/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003621///
Andrew Trickdd0fb012013-05-25 03:08:10 +00003622static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman707e0182008-04-12 04:36:06 +00003623 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003624 EVT VT = Base.getValueType();
Andrew Trickdd0fb012013-05-25 03:08:10 +00003625 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003626 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003627}
3628
Evan Chengf0df0312008-05-15 08:39:06 +00003629/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3630///
Chris Lattner18c7f802012-02-05 02:29:43 +00003631static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003632 unsigned SrcDelta = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003633 GlobalAddressSDNode *G = nullptr;
Evan Chengf0df0312008-05-15 08:39:06 +00003634 if (Src.getOpcode() == ISD::GlobalAddress)
3635 G = cast<GlobalAddressSDNode>(Src);
3636 else if (Src.getOpcode() == ISD::ADD &&
3637 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3638 Src.getOperand(1).getOpcode() == ISD::Constant) {
3639 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003640 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003641 }
3642 if (!G)
3643 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003644
Chris Lattner18c7f802012-02-05 02:29:43 +00003645 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003646}
Dan Gohman707e0182008-04-12 04:36:06 +00003647
Evan Cheng255f20f2010-04-01 06:04:33 +00003648/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3649/// to replace the memset / memcpy. Return true if the number of memory ops
3650/// is below the threshold. It returns the types of the sequence of
3651/// memory ops to perform memset / memcpy by reference.
3652static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003653 unsigned Limit, uint64_t Size,
3654 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003655 bool IsMemset,
3656 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003657 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003658 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003659 SelectionDAG &DAG,
3660 const TargetLowering &TLI) {
3661 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3662 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003663 // If 'SrcAlign' is zero, that means the memory operation does not need to
3664 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3665 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3666 // is the specified alignment of the memory operation. If it is zero, that
3667 // means it's possible to change the alignment of the destination.
3668 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3669 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003670 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003671 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003672 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003673
Chris Lattneracee6472010-03-07 07:45:08 +00003674 if (VT == MVT::Other) {
Stephen Hines36b56882014-04-23 16:57:46 -07003675 unsigned AS = 0;
3676 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
3677 TLI.allowsUnalignedMemoryAccesses(VT, AS)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003678 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003679 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003680 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003681 case 0: VT = MVT::i64; break;
3682 case 4: VT = MVT::i32; break;
3683 case 2: VT = MVT::i16; break;
3684 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003685 }
3686 }
3687
Owen Anderson766b5ef2009-08-11 21:59:30 +00003688 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003689 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003690 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003691 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003692
Duncan Sands8e4eb092008-06-08 20:54:56 +00003693 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003694 VT = LVT;
3695 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003696
Dan Gohman707e0182008-04-12 04:36:06 +00003697 unsigned NumMemOps = 0;
3698 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003699 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003700 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003701 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003702 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003703 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003704
3705 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003706 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003707 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003708 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003709 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003710 Found = true;
3711 else if (NewVT == MVT::i64 &&
3712 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003713 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003714 // i64 is usually not legal on 32-bit targets, but f64 may be.
3715 NewVT = MVT::f64;
3716 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003717 }
Evan Cheng376642e2012-12-10 23:21:26 +00003718 }
3719
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003720 if (!Found) {
3721 do {
3722 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3723 if (NewVT == MVT::i8)
3724 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003725 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003726 }
3727 NewVTSize = NewVT.getSizeInBits() / 8;
3728
Evan Cheng376642e2012-12-10 23:21:26 +00003729 // If the new VT cannot cover all of the remaining bits, then consider
3730 // issuing a (or a pair of) unaligned and overlapping load / store.
3731 // FIXME: Only does this for 64-bit or more since we don't have proper
3732 // cost model for unaligned load / store.
3733 bool Fast;
Stephen Hines36b56882014-04-23 16:57:46 -07003734 unsigned AS = 0;
Evan Chenga16e49d2012-12-12 20:43:23 +00003735 if (NumMemOps && AllowOverlap &&
3736 VTSize >= 8 && NewVTSize < Size &&
Stephen Hines36b56882014-04-23 16:57:46 -07003737 TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast)
Evan Cheng376642e2012-12-10 23:21:26 +00003738 VTSize = Size;
3739 else {
3740 VT = NewVT;
3741 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003742 }
Dan Gohman707e0182008-04-12 04:36:06 +00003743 }
Dan Gohman707e0182008-04-12 04:36:06 +00003744
Evan Chenga16e49d2012-12-12 20:43:23 +00003745 if (++NumMemOps > Limit)
3746 return false;
3747
Dan Gohman707e0182008-04-12 04:36:06 +00003748 MemOps.push_back(VT);
3749 Size -= VTSize;
3750 }
3751
3752 return true;
3753}
3754
Andrew Trickac6d9be2013-05-25 02:42:55 +00003755static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003756 SDValue Chain, SDValue Dst,
3757 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003758 unsigned Align, bool isVol,
3759 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003760 MachinePointerInfo DstPtrInfo,
3761 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003762 // Turn a memcpy of undef to nop.
3763 if (Src.getOpcode() == ISD::UNDEF)
3764 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003765
Dan Gohman21323f32008-05-29 19:42:22 +00003766 // Expand memcpy to a series of load and store ops if the size operand falls
3767 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003768 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3769 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003770 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003771 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003772 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003773 MachineFunction &MF = DAG.getMachineFunction();
3774 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003775 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003776 MF.getFunction()->getAttributes().
3777 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003778 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3779 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3780 DstAlignCanChange = true;
3781 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3782 if (Align > SrcAlign)
3783 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003784 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003785 bool CopyFromStr = isMemSrcFromString(Src, Str);
3786 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003787 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003788
Evan Cheng94107ba2010-04-01 18:19:11 +00003789 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003790 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003791 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003792 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003793 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003794
Evan Cheng255f20f2010-04-01 06:04:33 +00003795 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003796 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003797 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00003798
3799 // Don't promote to an alignment that would require dynamic stack
Stephen Lin155615d2013-07-08 00:37:03 +00003800 // realignment.
Lang Hames2d95e432013-01-31 20:23:43 +00003801 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3802 if (!TRI->needsStackRealignment(MF))
3803 while (NewAlign > Align &&
3804 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3805 NewAlign /= 2;
3806
Evan Cheng255f20f2010-04-01 06:04:33 +00003807 if (NewAlign > Align) {
3808 // Give the stack frame object a larger alignment if needed.
3809 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3810 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3811 Align = NewAlign;
3812 }
3813 }
Dan Gohman707e0182008-04-12 04:36:06 +00003814
Dan Gohman475871a2008-07-27 21:46:04 +00003815 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003816 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003817 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003818 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003819 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003820 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003821 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003822
Evan Cheng376642e2012-12-10 23:21:26 +00003823 if (VTSize > Size) {
3824 // Issuing an unaligned load / store pair that overlaps with the previous
3825 // pair. Adjust the offset accordingly.
3826 assert(i == NumMemOps-1 && i != 0);
3827 SrcOff -= VTSize - Size;
3828 DstOff -= VTSize - Size;
3829 }
3830
Evan Cheng255f20f2010-04-01 06:04:33 +00003831 if (CopyFromStr &&
3832 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003833 // It's unlikely a store of a vector immediate can be done in a single
3834 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003835 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003836 // FIXME: Handle other cases where store of vector immediate is done in
3837 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003838 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003839 if (Value.getNode())
3840 Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003841 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng376642e2012-12-10 23:21:26 +00003842 DstPtrInfo.getWithOffset(DstOff), isVol,
3843 false, Align);
3844 }
3845
3846 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003847 // The type might not be legal for the target. This should only happen
3848 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003849 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3850 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003851 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003852 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003853 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003854 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003855 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003856 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003857 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003858 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003859 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003860 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3861 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003862 }
3863 OutChains.push_back(Store);
3864 SrcOff += VTSize;
3865 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003866 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003867 }
3868
Stephen Hinesdce4a402014-05-29 02:49:00 -07003869 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00003870}
3871
Andrew Trickac6d9be2013-05-25 02:42:55 +00003872static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003873 SDValue Chain, SDValue Dst,
3874 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003875 unsigned Align, bool isVol,
3876 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003877 MachinePointerInfo DstPtrInfo,
3878 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003879 // Turn a memmove of undef to nop.
3880 if (Src.getOpcode() == ISD::UNDEF)
3881 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003882
3883 // Expand memmove to a series of load and store ops if the size operand falls
3884 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003885 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003886 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003887 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003888 MachineFunction &MF = DAG.getMachineFunction();
3889 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003890 bool OptSize = MF.getFunction()->getAttributes().
3891 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003892 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3893 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3894 DstAlignCanChange = true;
3895 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3896 if (Align > SrcAlign)
3897 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003898 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003899
Evan Cheng94107ba2010-04-01 18:19:11 +00003900 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00003901 (DstAlignCanChange ? 0 : Align), SrcAlign,
3902 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003903 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003904
Evan Cheng255f20f2010-04-01 06:04:33 +00003905 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003906 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003907 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003908 if (NewAlign > Align) {
3909 // Give the stack frame object a larger alignment if needed.
3910 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3911 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3912 Align = NewAlign;
3913 }
3914 }
Dan Gohman21323f32008-05-29 19:42:22 +00003915
Evan Cheng255f20f2010-04-01 06:04:33 +00003916 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003917 SmallVector<SDValue, 8> LoadValues;
3918 SmallVector<SDValue, 8> LoadChains;
3919 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003920 unsigned NumMemOps = MemOps.size();
3921 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003922 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003923 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003924 SDValue Value;
Dan Gohman21323f32008-05-29 19:42:22 +00003925
Dale Johannesen0f502f62009-02-03 22:26:09 +00003926 Value = DAG.getLoad(VT, dl, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003927 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003928 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003929 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003930 LoadValues.push_back(Value);
3931 LoadChains.push_back(Value.getValue(1));
3932 SrcOff += VTSize;
3933 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07003934 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman21323f32008-05-29 19:42:22 +00003935 OutChains.clear();
3936 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003937 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003938 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003939 SDValue Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003940
Dale Johannesen0f502f62009-02-03 22:26:09 +00003941 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Trickdd0fb012013-05-25 03:08:10 +00003942 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003943 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003944 OutChains.push_back(Store);
3945 DstOff += VTSize;
3946 }
3947
Stephen Hinesdce4a402014-05-29 02:49:00 -07003948 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman21323f32008-05-29 19:42:22 +00003949}
3950
Serge Pavlov496f0242013-09-17 16:24:42 +00003951/// \brief Lower the call to 'memset' intrinsic function into a series of store
3952/// operations.
3953///
3954/// \param DAG Selection DAG where lowered code is placed.
3955/// \param dl Link to corresponding IR location.
3956/// \param Chain Control flow dependency.
3957/// \param Dst Pointer to destination memory location.
3958/// \param Src Value of byte to write into the memory.
3959/// \param Size Number of bytes to write.
3960/// \param Align Alignment of the destination in bytes.
3961/// \param isVol True if destination is volatile.
3962/// \param DstPtrInfo IR information on the memory pointer.
3963/// \returns New head in the control flow, if lowering was successful, empty
3964/// SDValue otherwise.
3965///
3966/// The function tries to replace 'llvm.memset' intrinsic with several store
3967/// operations and value calculation code. This is usually profitable for small
3968/// memory size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003969static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003970 SDValue Chain, SDValue Dst,
3971 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003972 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003973 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003974 // Turn a memset of undef to nop.
3975 if (Src.getOpcode() == ISD::UNDEF)
3976 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003977
3978 // Expand memset to a series of load/store ops if the size operand
3979 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003980 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003981 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003982 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003983 MachineFunction &MF = DAG.getMachineFunction();
3984 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003985 bool OptSize = MF.getFunction()->getAttributes().
3986 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003987 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3988 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3989 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003990 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003991 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003992 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003993 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00003994 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003995 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003996
Evan Cheng255f20f2010-04-01 06:04:33 +00003997 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003998 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003999 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00004000 if (NewAlign > Align) {
4001 // Give the stack frame object a larger alignment if needed.
4002 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4003 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4004 Align = NewAlign;
4005 }
4006 }
4007
Dan Gohman475871a2008-07-27 21:46:04 +00004008 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00004009 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00004010 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00004011
4012 // Find the largest store and generate the bit pattern for it.
4013 EVT LargestVT = MemOps[0];
4014 for (unsigned i = 1; i < NumMemOps; i++)
4015 if (MemOps[i].bitsGT(LargestVT))
4016 LargestVT = MemOps[i];
4017 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4018
Dan Gohman707e0182008-04-12 04:36:06 +00004019 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004020 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00004021 unsigned VTSize = VT.getSizeInBits() / 8;
4022 if (VTSize > Size) {
4023 // Issuing an unaligned load / store pair that overlaps with the previous
4024 // pair. Adjust the offset accordingly.
4025 assert(i == NumMemOps-1 && i != 0);
4026 DstOff -= VTSize - Size;
4027 }
Benjamin Kramer80220362011-01-02 19:57:05 +00004028
4029 // If this store is smaller than the largest store see whether we can get
4030 // the smaller value for free with a truncate.
4031 SDValue Value = MemSetValue;
4032 if (VT.bitsLT(LargestVT)) {
4033 if (!LargestVT.isVector() && !VT.isVector() &&
4034 TLI.isTruncateFree(LargestVT, VT))
4035 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4036 else
4037 Value = getMemsetValue(Src, VT, DAG, dl);
4038 }
4039 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004040 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004041 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004042 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00004043 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00004044 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00004045 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00004046 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00004047 }
4048
Stephen Hinesdce4a402014-05-29 02:49:00 -07004049 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00004050}
4051
Andrew Trickac6d9be2013-05-25 02:42:55 +00004052SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004053 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004054 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004055 MachinePointerInfo DstPtrInfo,
4056 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004057 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004058
4059 // Check to see if we should lower the memcpy to loads and stores first.
4060 // For cases within the target-specified limits, this is the best choice.
4061 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4062 if (ConstantSize) {
4063 // Memcpy with size zero? Just return the original chain.
4064 if (ConstantSize->isNullValue())
4065 return Chain;
4066
Evan Cheng255f20f2010-04-01 06:04:33 +00004067 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4068 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00004069 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004070 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004071 return Result;
4072 }
4073
4074 // Then check to see if we should lower the memcpy with target-specific
4075 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004076 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004077 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004078 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004079 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004080 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004081 return Result;
4082
4083 // If we really need inline code and the target declined to provide it,
4084 // use a (potentially long) sequence of loads and stores.
4085 if (AlwaysInline) {
4086 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004087 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004088 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004089 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00004090 }
4091
Dan Gohman7b55d362010-04-05 20:24:08 +00004092 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4093 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4094 // respect volatile, so they may do things like read or write memory
4095 // beyond the given memory regions. But fixing this isn't easy, and most
4096 // people don't care.
4097
Bill Wendlingba54bca2013-06-19 21:36:55 +00004098 const TargetLowering *TLI = TM.getTargetLowering();
4099
Dan Gohman707e0182008-04-12 04:36:06 +00004100 // Emit a library call.
4101 TargetLowering::ArgListTy Args;
4102 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004103 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004104 Entry.Node = Dst; Args.push_back(Entry);
4105 Entry.Node = Src; Args.push_back(Entry);
4106 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004107 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004108 TargetLowering::CallLoweringInfo CLI(*this);
4109 CLI.setDebugLoc(dl).setChain(Chain)
4110 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4111 Type::getVoidTy(*getContext()),
4112 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4113 TLI->getPointerTy()), &Args, 0)
4114 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004115 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004116
Dan Gohman707e0182008-04-12 04:36:06 +00004117 return CallResult.second;
4118}
4119
Andrew Trickac6d9be2013-05-25 02:42:55 +00004120SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004122 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004123 MachinePointerInfo DstPtrInfo,
4124 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004125 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004126
Dan Gohman21323f32008-05-29 19:42:22 +00004127 // Check to see if we should lower the memmove to loads and stores first.
4128 // For cases within the target-specified limits, this is the best choice.
4129 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4130 if (ConstantSize) {
4131 // Memmove with size zero? Just return the original chain.
4132 if (ConstantSize->isNullValue())
4133 return Chain;
4134
Dan Gohman475871a2008-07-27 21:46:04 +00004135 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00004136 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004137 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004138 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004139 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00004140 return Result;
4141 }
Dan Gohman707e0182008-04-12 04:36:06 +00004142
4143 // Then check to see if we should lower the memmove with target-specific
4144 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004145 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004146 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004147 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004148 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004149 return Result;
4150
Mon P Wang01f0e852010-04-06 08:27:51 +00004151 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4152 // not be safe. See memcpy above for more details.
4153
Bill Wendlingba54bca2013-06-19 21:36:55 +00004154 const TargetLowering *TLI = TM.getTargetLowering();
4155
Dan Gohman707e0182008-04-12 04:36:06 +00004156 // Emit a library call.
4157 TargetLowering::ArgListTy Args;
4158 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004159 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004160 Entry.Node = Dst; Args.push_back(Entry);
4161 Entry.Node = Src; Args.push_back(Entry);
4162 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004163 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004164 TargetLowering::CallLoweringInfo CLI(*this);
4165 CLI.setDebugLoc(dl).setChain(Chain)
4166 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4167 Type::getVoidTy(*getContext()),
4168 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4169 TLI->getPointerTy()), &Args, 0)
4170 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004171 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004172
Dan Gohman707e0182008-04-12 04:36:06 +00004173 return CallResult.second;
4174}
4175
Andrew Trickac6d9be2013-05-25 02:42:55 +00004176SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004177 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004178 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004179 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004180 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004181
4182 // Check to see if we should lower the memset to stores first.
4183 // For cases within the target-specified limits, this is the best choice.
4184 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4185 if (ConstantSize) {
4186 // Memset with size zero? Just return the original chain.
4187 if (ConstantSize->isNullValue())
4188 return Chain;
4189
Mon P Wang20adc9d2010-04-04 03:10:48 +00004190 SDValue Result =
4191 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004192 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004193
Gabor Greifba36cb52008-08-28 21:40:38 +00004194 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004195 return Result;
4196 }
4197
4198 // Then check to see if we should lower the memset with target-specific
4199 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004200 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004201 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004202 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004203 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004204 return Result;
4205
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004206 // Emit a library call.
Bill Wendlingba54bca2013-06-19 21:36:55 +00004207 const TargetLowering *TLI = TM.getTargetLowering();
4208 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004209 TargetLowering::ArgListTy Args;
4210 TargetLowering::ArgListEntry Entry;
4211 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4212 Args.push_back(Entry);
4213 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00004214 if (Src.getValueType().bitsGT(MVT::i32))
4215 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00004216 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004217 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00004218 Entry.Node = Src;
4219 Entry.Ty = Type::getInt32Ty(*getContext());
4220 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00004221 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004222 Entry.Node = Size;
4223 Entry.Ty = IntPtrTy;
4224 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00004225 Args.push_back(Entry);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004226
Stephen Hinesdce4a402014-05-29 02:49:00 -07004227 // FIXME: pass in SDLoc
4228 TargetLowering::CallLoweringInfo CLI(*this);
4229 CLI.setDebugLoc(dl).setChain(Chain)
4230 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4231 Type::getVoidTy(*getContext()),
4232 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4233 TLI->getPointerTy()), &Args, 0)
4234 .setDiscardResult();
4235
4236 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman707e0182008-04-12 04:36:06 +00004237 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004238}
4239
Andrew Trickac6d9be2013-05-25 02:42:55 +00004240SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004241 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emerson268c7432013-09-26 12:22:36 +00004242 MachineMemOperand *MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004243 AtomicOrdering SuccessOrdering,
4244 AtomicOrdering FailureOrdering,
Amara Emerson268c7432013-09-26 12:22:36 +00004245 SynchronizationScope SynchScope) {
4246 FoldingSetNodeID ID;
4247 ID.AddInteger(MemVT.getRawBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004248 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emerson268c7432013-09-26 12:22:36 +00004249 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004250 void* IP = nullptr;
Amara Emerson268c7432013-09-26 12:22:36 +00004251 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4252 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4253 return SDValue(E, 0);
4254 }
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004255
4256 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4257 // SDNode doesn't have access to it. This memory will be "leaked" when
4258 // the node is deallocated, but recovered when the allocator is released.
4259 // If the number of operands is less than 5 we use AtomicSDNode's internal
4260 // storage.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004261 unsigned NumOps = Ops.size();
4262 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4263 : nullptr;
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004264
Amara Emerson268c7432013-09-26 12:22:36 +00004265 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4266 dl.getDebugLoc(), VTList, MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004267 Ops.data(), DynOps, NumOps, MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004268 SuccessOrdering, FailureOrdering,
4269 SynchScope);
Amara Emerson268c7432013-09-26 12:22:36 +00004270 CSEMap.InsertNode(N, IP);
4271 AllNodes.push_back(N);
4272 return SDValue(N, 0);
4273}
4274
4275SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004276 SDVTList VTList, ArrayRef<SDValue> Ops,
Stephen Hines36b56882014-04-23 16:57:46 -07004277 MachineMemOperand *MMO,
4278 AtomicOrdering Ordering,
4279 SynchronizationScope SynchScope) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07004280 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Stephen Hines36b56882014-04-23 16:57:46 -07004281 Ordering, SynchScope);
4282}
4283
4284SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00004285 SDValue Chain, SDValue Ptr, SDValue Cmp,
4286 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00004287 unsigned Alignment,
Stephen Hines36b56882014-04-23 16:57:46 -07004288 AtomicOrdering SuccessOrdering,
4289 AtomicOrdering FailureOrdering,
Michael Ilseman06b96902012-09-10 16:56:31 +00004290 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004291 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4292 Alignment = getEVTAlignment(MemVT);
4293
Evan Chengff89dcb2009-10-18 18:16:27 +00004294 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004295
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004296 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004297 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004298 // FIXME: Volatile isn't really correct; we should keep track of atomic
4299 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004300 unsigned Flags = MachineMemOperand::MOVolatile;
4301 if (Opcode != ISD::ATOMIC_STORE)
4302 Flags |= MachineMemOperand::MOLoad;
4303 if (Opcode != ISD::ATOMIC_LOAD)
4304 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004305
4306 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004307 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004308
Eli Friedman55ba8162011-07-29 03:05:32 +00004309 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004310 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004311}
4312
Andrew Trickac6d9be2013-05-25 02:42:55 +00004313SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004314 SDValue Chain,
4315 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00004316 SDValue Swp, MachineMemOperand *MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004317 AtomicOrdering SuccessOrdering,
4318 AtomicOrdering FailureOrdering,
Eli Friedman55ba8162011-07-29 03:05:32 +00004319 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004320 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4321 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4322
Owen Andersone50ed302009-08-10 22:56:29 +00004323 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004324
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004326 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004327 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, SuccessOrdering,
Stephen Hines36b56882014-04-23 16:57:46 -07004328 FailureOrdering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004329}
4330
Andrew Trickac6d9be2013-05-25 02:42:55 +00004331SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004332 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004333 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004334 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004335 unsigned Alignment,
4336 AtomicOrdering Ordering,
4337 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004338 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4339 Alignment = getEVTAlignment(MemVT);
4340
Evan Chengff89dcb2009-10-18 18:16:27 +00004341 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004342 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004343 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004344 // For now, atomics are considered to be volatile always, and they are
4345 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004346 // FIXME: Volatile isn't really correct; we should keep track of atomic
4347 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004348 unsigned Flags = MachineMemOperand::MOVolatile;
4349 if (Opcode != ISD::ATOMIC_STORE)
4350 Flags |= MachineMemOperand::MOLoad;
4351 if (Opcode != ISD::ATOMIC_LOAD)
4352 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004353
4354 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004355 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004356 MemVT.getStoreSize(), Alignment);
4357
Eli Friedman55ba8162011-07-29 03:05:32 +00004358 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4359 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004360}
4361
Andrew Trickac6d9be2013-05-25 02:42:55 +00004362SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004363 SDValue Chain,
4364 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004365 MachineMemOperand *MMO,
4366 AtomicOrdering Ordering,
4367 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004368 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4369 Opcode == ISD::ATOMIC_LOAD_SUB ||
4370 Opcode == ISD::ATOMIC_LOAD_AND ||
4371 Opcode == ISD::ATOMIC_LOAD_OR ||
4372 Opcode == ISD::ATOMIC_LOAD_XOR ||
4373 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004374 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004375 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004376 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004377 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004378 Opcode == ISD::ATOMIC_SWAP ||
4379 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004380 "Invalid Atomic Op");
4381
Owen Andersone50ed302009-08-10 22:56:29 +00004382 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004383
Eli Friedman327236c2011-08-24 20:50:09 +00004384 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4385 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004386 SDValue Ops[] = {Chain, Ptr, Val};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004387 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004388}
4389
Andrew Trickac6d9be2013-05-25 02:42:55 +00004390SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004391 EVT VT, SDValue Chain,
4392 SDValue Ptr,
4393 MachineMemOperand *MMO,
4394 AtomicOrdering Ordering,
4395 SynchronizationScope SynchScope) {
4396 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4397
4398 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman327236c2011-08-24 20:50:09 +00004399 SDValue Ops[] = {Chain, Ptr};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004400 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004401}
4402
Duncan Sands4bdcb612008-07-02 17:40:58 +00004403/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004404SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4405 if (Ops.size() == 1)
Dale Johannesen54c94522009-02-02 20:47:48 +00004406 return Ops[0];
4407
Owen Andersone50ed302009-08-10 22:56:29 +00004408 SmallVector<EVT, 4> VTs;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004409 VTs.reserve(Ops.size());
4410 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesen54c94522009-02-02 20:47:48 +00004411 VTs.push_back(Ops[i].getValueType());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004412 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004413}
4414
4415SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004416SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004417 ArrayRef<SDValue> Ops,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004418 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004419 unsigned Align, bool Vol,
4420 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004421 if (Align == 0) // Ensure that codegen never sees alignment 0
4422 Align = getEVTAlignment(MemVT);
4423
4424 MachineFunction &MF = getMachineFunction();
4425 unsigned Flags = 0;
4426 if (WriteMem)
4427 Flags |= MachineMemOperand::MOStore;
4428 if (ReadMem)
4429 Flags |= MachineMemOperand::MOLoad;
4430 if (Vol)
4431 Flags |= MachineMemOperand::MOVolatile;
4432 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004433 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004434
Stephen Hinesdce4a402014-05-29 02:49:00 -07004435 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004436}
4437
4438SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004439SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004440 ArrayRef<SDValue> Ops, EVT MemVT,
4441 MachineMemOperand *MMO) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004442 assert((Opcode == ISD::INTRINSIC_VOID ||
4443 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004444 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004445 Opcode == ISD::LIFETIME_START ||
4446 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004447 (Opcode <= INT_MAX &&
4448 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4449 "Opcode is not a memory-accessing opcode!");
4450
Dale Johannesene8c17332009-01-29 00:47:48 +00004451 // Memoize the node unless it returns a flag.
4452 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004453 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004454 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004455 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004456 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004457 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004458 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4459 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004460 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004461 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004462
Jack Carter3af4d252013-09-09 22:02:08 +00004463 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4464 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004465 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004466 CSEMap.InsertNode(N, IP);
4467 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004468 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4469 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004470 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004471 }
4472 AllNodes.push_back(N);
4473 return SDValue(N, 0);
4474}
4475
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004476/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4477/// MachinePointerInfo record from it. This is particularly useful because the
4478/// code generator has many cases where it doesn't bother passing in a
4479/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4480static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4481 // If this is FI+Offset, we can model it.
4482 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4483 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4484
4485 // If this is (FI+Offset1)+Offset2, we can model it.
4486 if (Ptr.getOpcode() != ISD::ADD ||
4487 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4488 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4489 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004490
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004491 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4492 return MachinePointerInfo::getFixedStack(FI, Offset+
4493 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4494}
4495
4496/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4497/// MachinePointerInfo record from it. This is particularly useful because the
4498/// code generator has many cases where it doesn't bother passing in a
4499/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4500static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4501 // If the 'Offset' value isn't a constant, we can't handle this.
4502 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4503 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4504 if (OffsetOp.getOpcode() == ISD::UNDEF)
4505 return InferPointerInfo(Ptr);
4506 return MachinePointerInfo();
4507}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004508
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004509
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004510SDValue
4511SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004512 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004513 SDValue Ptr, SDValue Offset,
4514 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004515 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004516 unsigned Alignment, const MDNode *TBAAInfo,
4517 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004518 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004519 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004520 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4521 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004522
Dan Gohmanc76909a2009-09-25 20:36:54 +00004523 unsigned Flags = MachineMemOperand::MOLoad;
4524 if (isVolatile)
4525 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004526 if (isNonTemporal)
4527 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004528 if (isInvariant)
4529 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004530
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004531 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4532 // clients.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004533 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004534 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004535
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004536 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004537 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004538 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004539 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004540 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004541}
4542
4543SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004544SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004545 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004546 SDValue Ptr, SDValue Offset, EVT MemVT,
4547 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004548 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004549 ExtType = ISD::NON_EXTLOAD;
4550 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004551 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004552 } else {
4553 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004554 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4555 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004556 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004557 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004558 assert(VT.isVector() == MemVT.isVector() &&
4559 "Cannot use trunc store to convert to or from a vector!");
4560 assert((!VT.isVector() ||
4561 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4562 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004563 }
4564
4565 bool Indexed = AM != ISD::UNINDEXED;
4566 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4567 "Unindexed load with an offset!");
4568
4569 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004570 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004571 SDValue Ops[] = { Chain, Ptr, Offset };
4572 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004573 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004574 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004575 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004576 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004577 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004578 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004579 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004580 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4581 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004582 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004583 }
Jack Carter3af4d252013-09-09 22:02:08 +00004584 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4585 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman95531882010-03-18 18:49:47 +00004586 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004587 CSEMap.InsertNode(N, IP);
4588 AllNodes.push_back(N);
4589 return SDValue(N, 0);
4590}
4591
Andrew Trickac6d9be2013-05-25 02:42:55 +00004592SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004593 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004594 MachinePointerInfo PtrInfo,
4595 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004596 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004597 const MDNode *TBAAInfo,
4598 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004599 SDValue Undef = getUNDEF(Ptr.getValueType());
4600 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004601 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4602 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004603}
Dale Johannesene8c17332009-01-29 00:47:48 +00004604
Richard Sandiford66589dc2013-10-28 11:17:59 +00004605SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4606 SDValue Chain, SDValue Ptr,
4607 MachineMemOperand *MMO) {
4608 SDValue Undef = getUNDEF(Ptr.getValueType());
4609 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4610 VT, MMO);
4611}
4612
Andrew Trickac6d9be2013-05-25 02:42:55 +00004613SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004614 SDValue Chain, SDValue Ptr,
4615 MachinePointerInfo PtrInfo, EVT MemVT,
4616 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004617 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004618 SDValue Undef = getUNDEF(Ptr.getValueType());
4619 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004620 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004621 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004622}
4623
4624
Richard Sandiford66589dc2013-10-28 11:17:59 +00004625SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4626 SDValue Chain, SDValue Ptr, EVT MemVT,
4627 MachineMemOperand *MMO) {
4628 SDValue Undef = getUNDEF(Ptr.getValueType());
4629 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4630 MemVT, MMO);
4631}
4632
Dan Gohman475871a2008-07-27 21:46:04 +00004633SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004634SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004635 SDValue Offset, ISD::MemIndexedMode AM) {
4636 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4637 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4638 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004639 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004640 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004641 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004642 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004643}
4644
Andrew Trickac6d9be2013-05-25 02:42:55 +00004645SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004646 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004647 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004648 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004649 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004650 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004651 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004652 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004653
Dan Gohmanc76909a2009-09-25 20:36:54 +00004654 unsigned Flags = MachineMemOperand::MOStore;
4655 if (isVolatile)
4656 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004657 if (isNonTemporal)
4658 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004659
Stephen Hinesdce4a402014-05-29 02:49:00 -07004660 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004661 PtrInfo = InferPointerInfo(Ptr);
4662
4663 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004664 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004665 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004666 Val.getValueType().getStoreSize(), Alignment,
4667 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004668
4669 return getStore(Chain, dl, Val, Ptr, MMO);
4670}
4671
Andrew Trickac6d9be2013-05-25 02:42:55 +00004672SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004673 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004674 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004675 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004676 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004677 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004678 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004679 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4680 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004681 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004682 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004683 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004684 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004685 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004686 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004687 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4688 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004689 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004690 }
Jack Carter3af4d252013-09-09 22:02:08 +00004691 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4692 dl.getDebugLoc(), VTs,
4693 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004694 CSEMap.InsertNode(N, IP);
4695 AllNodes.push_back(N);
4696 return SDValue(N, 0);
4697}
4698
Andrew Trickac6d9be2013-05-25 02:42:55 +00004699SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004700 SDValue Ptr, MachinePointerInfo PtrInfo,
4701 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004702 unsigned Alignment,
4703 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004704 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004705 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004706 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4707 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004708
Dan Gohmanc76909a2009-09-25 20:36:54 +00004709 unsigned Flags = MachineMemOperand::MOStore;
4710 if (isVolatile)
4711 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004712 if (isNonTemporal)
4713 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004714
Stephen Hinesdce4a402014-05-29 02:49:00 -07004715 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004716 PtrInfo = InferPointerInfo(Ptr);
4717
4718 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004719 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004720 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4721 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004722
4723 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4724}
4725
Andrew Trickac6d9be2013-05-25 02:42:55 +00004726SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004727 SDValue Ptr, EVT SVT,
4728 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004729 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004730
Michael Ilseman06b96902012-09-10 16:56:31 +00004731 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004732 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004733 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004734 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004735
Dan Gohman2e141d72009-12-14 23:40:38 +00004736 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4737 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004738 assert(VT.isInteger() == SVT.isInteger() &&
4739 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004740 assert(VT.isVector() == SVT.isVector() &&
4741 "Cannot use trunc store to convert to or from a vector!");
4742 assert((!VT.isVector() ||
4743 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4744 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004745
Owen Anderson825b72b2009-08-11 20:47:22 +00004746 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004747 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004748 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4749 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004750 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004751 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004752 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004753 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004754 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004755 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004756 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4757 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004758 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004759 }
Jack Carter3af4d252013-09-09 22:02:08 +00004760 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4761 dl.getDebugLoc(), VTs,
4762 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004763 CSEMap.InsertNode(N, IP);
4764 AllNodes.push_back(N);
4765 return SDValue(N, 0);
4766}
4767
Dan Gohman475871a2008-07-27 21:46:04 +00004768SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004769SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004770 SDValue Offset, ISD::MemIndexedMode AM) {
4771 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4772 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4773 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004774 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004775 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4776 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004777 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004778 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004779 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004780 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004781 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004782 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004783 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004784
Jack Carter3af4d252013-09-09 22:02:08 +00004785 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4786 dl.getDebugLoc(), VTs, AM,
Dan Gohman95531882010-03-18 18:49:47 +00004787 ST->isTruncatingStore(),
4788 ST->getMemoryVT(),
4789 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004790 CSEMap.InsertNode(N, IP);
4791 AllNodes.push_back(N);
4792 return SDValue(N, 0);
4793}
4794
Andrew Trickac6d9be2013-05-25 02:42:55 +00004795SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004796 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004797 SDValue SV,
4798 unsigned Align) {
4799 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004800 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004801}
4802
Andrew Trickac6d9be2013-05-25 02:42:55 +00004803SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004804 ArrayRef<SDUse> Ops) {
4805 switch (Ops.size()) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004806 case 0: return getNode(Opcode, DL, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07004807 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling7ade28c2009-01-28 22:17:52 +00004808 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4809 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004810 default: break;
4811 }
4812
Dan Gohman475871a2008-07-27 21:46:04 +00004813 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004814 // the regular getNode logic.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004815 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
4816 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004817}
4818
Andrew Trickac6d9be2013-05-25 02:42:55 +00004819SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004820 ArrayRef<SDValue> Ops) {
4821 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004822 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004823 case 0: return getNode(Opcode, DL, VT);
4824 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4825 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4826 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004827 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004828 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004829
Chris Lattneref847df2005-04-09 03:27:28 +00004830 switch (Opcode) {
4831 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004832 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004833 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004834 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4835 "LHS and RHS of condition must have same type!");
4836 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4837 "True and False arms of SelectCC must have same type!");
4838 assert(Ops[2].getValueType() == VT &&
4839 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004840 break;
4841 }
4842 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004843 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004844 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4845 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004846 break;
4847 }
Chris Lattneref847df2005-04-09 03:27:28 +00004848 }
4849
Chris Lattner385328c2005-05-14 07:42:29 +00004850 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004851 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004852 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004853
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004854 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004855 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004856 AddNodeIDNode(ID, Opcode, VTs, Ops);
4857 void *IP = nullptr;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004858
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004859 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004860 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004861
Jack Carter3af4d252013-09-09 22:02:08 +00004862 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004863 VTs, Ops);
Chris Lattnera5682852006-08-07 23:03:03 +00004864 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004865 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004866 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004867 VTs, Ops);
Chris Lattner43247a12005-08-25 19:12:10 +00004868 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004869
Chris Lattneref847df2005-04-09 03:27:28 +00004870 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004871#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004872 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004873#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004874 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004875}
4876
Andrew Trickac6d9be2013-05-25 02:42:55 +00004877SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004878 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
4879 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Scott Michelfdc40a02009-02-17 22:15:04 +00004880}
4881
Andrew Trickac6d9be2013-05-25 02:42:55 +00004882SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004883 ArrayRef<SDValue> Ops) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004884 if (VTList.NumVTs == 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004885 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattner89c34632005-05-14 06:20:26 +00004886
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004887#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004888 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004889 // FIXME: figure out how to safely handle things like
4890 // int foo(int x) { return 1 << (x & 255); }
4891 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004892 case ISD::SRA_PARTS:
4893 case ISD::SRL_PARTS:
4894 case ISD::SHL_PARTS:
4895 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004897 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004898 else if (N3.getOpcode() == ISD::AND)
4899 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4900 // If the and is only masking out bits that cannot effect the shift,
4901 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004902 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004903 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004904 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004905 }
4906 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004907 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004908#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004909
Chris Lattner43247a12005-08-25 19:12:10 +00004910 // Memoize the node unless it returns a flag.
4911 SDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004912 unsigned NumOps = Ops.size();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004913 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004914 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004915 AddNodeIDNode(ID, Opcode, VTList, Ops);
4916 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004917 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004918 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004919
Dan Gohman0e5f1302008-07-07 23:02:41 +00004920 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004921 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4922 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004923 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004924 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4925 DL.getDebugLoc(), VTList, Ops[0],
4926 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004927 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004928 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4929 DL.getDebugLoc(), VTList, Ops[0],
4930 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004931 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004932 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004933 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004934 }
Chris Lattnera5682852006-08-07 23:03:03 +00004935 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004936 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004937 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004938 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4939 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004940 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004941 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4942 DL.getDebugLoc(), VTList, Ops[0],
4943 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004944 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004945 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4946 DL.getDebugLoc(), VTList, Ops[0],
4947 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004948 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004949 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004950 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004951 }
Chris Lattner43247a12005-08-25 19:12:10 +00004952 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004953 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004954#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004955 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004956#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004958}
4959
Andrew Trickac6d9be2013-05-25 02:42:55 +00004960SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07004961 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
Dan Gohman08ce9762007-10-08 15:49:58 +00004962}
4963
Andrew Trickac6d9be2013-05-25 02:42:55 +00004964SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004965 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004966 SDValue Ops[] = { N1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004967 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00004968}
4969
Andrew Trickac6d9be2013-05-25 02:42:55 +00004970SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004971 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004972 SDValue Ops[] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004973 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00004974}
4975
Andrew Trickac6d9be2013-05-25 02:42:55 +00004976SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004977 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004978 SDValue Ops[] = { N1, N2, N3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004979 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00004980}
4981
Andrew Trickac6d9be2013-05-25 02:42:55 +00004982SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004983 SDValue N1, SDValue N2, SDValue N3,
4984 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004985 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004986 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00004987}
4988
Andrew Trickac6d9be2013-05-25 02:42:55 +00004989SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004990 SDValue N1, SDValue N2, SDValue N3,
4991 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004992 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004993 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00004994}
4995
Owen Andersone50ed302009-08-10 22:56:29 +00004996SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004997 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004998}
4999
Owen Andersone50ed302009-08-10 22:56:29 +00005000SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005001 FoldingSetNodeID ID;
5002 ID.AddInteger(2U);
5003 ID.AddInteger(VT1.getRawBits());
5004 ID.AddInteger(VT2.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005005
Stephen Hinesdce4a402014-05-29 02:49:00 -07005006 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005007 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005008 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005009 EVT *Array = Allocator.Allocate<EVT>(2);
5010 Array[0] = VT1;
5011 Array[1] = VT2;
5012 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5013 VTListMap.InsertNode(Result, IP);
5014 }
5015 return Result->getSDVTList();
Chris Lattnera3255112005-11-08 23:30:28 +00005016}
Dan Gohmane8be6c62008-07-17 19:10:17 +00005017
Owen Andersone50ed302009-08-10 22:56:29 +00005018SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005019 FoldingSetNodeID ID;
5020 ID.AddInteger(3U);
5021 ID.AddInteger(VT1.getRawBits());
5022 ID.AddInteger(VT2.getRawBits());
5023 ID.AddInteger(VT3.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005024
Stephen Hinesdce4a402014-05-29 02:49:00 -07005025 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005026 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005027 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005028 EVT *Array = Allocator.Allocate<EVT>(3);
5029 Array[0] = VT1;
5030 Array[1] = VT2;
5031 Array[2] = VT3;
5032 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5033 VTListMap.InsertNode(Result, IP);
5034 }
5035 return Result->getSDVTList();
Chris Lattner70046e92006-08-15 17:46:01 +00005036}
5037
Owen Andersone50ed302009-08-10 22:56:29 +00005038SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005039 FoldingSetNodeID ID;
5040 ID.AddInteger(4U);
5041 ID.AddInteger(VT1.getRawBits());
5042 ID.AddInteger(VT2.getRawBits());
5043 ID.AddInteger(VT3.getRawBits());
5044 ID.AddInteger(VT4.getRawBits());
Bill Wendling13d6d442008-12-01 23:28:22 +00005045
Stephen Hinesdce4a402014-05-29 02:49:00 -07005046 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005047 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005048 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005049 EVT *Array = Allocator.Allocate<EVT>(4);
5050 Array[0] = VT1;
5051 Array[1] = VT2;
5052 Array[2] = VT3;
5053 Array[3] = VT4;
5054 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5055 VTListMap.InsertNode(Result, IP);
5056 }
5057 return Result->getSDVTList();
Bill Wendling13d6d442008-12-01 23:28:22 +00005058}
5059
Stephen Hinesdce4a402014-05-29 02:49:00 -07005060SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5061 unsigned NumVTs = VTs.size();
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005062 FoldingSetNodeID ID;
5063 ID.AddInteger(NumVTs);
5064 for (unsigned index = 0; index < NumVTs; index++) {
5065 ID.AddInteger(VTs[index].getRawBits());
Chris Lattner70046e92006-08-15 17:46:01 +00005066 }
5067
Stephen Hinesdce4a402014-05-29 02:49:00 -07005068 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005069 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005070 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005071 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005072 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005073 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5074 VTListMap.InsertNode(Result, IP);
Chris Lattner70046e92006-08-15 17:46:01 +00005075 }
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005076 return Result->getSDVTList();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00005077}
5078
5079
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005080/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5081/// specified operands. If the resultant node already exists in the DAG,
5082/// this does not modify the specified node, instead it returns the node that
5083/// already exists. If the resultant node does not exist in the DAG, the
5084/// input node is returned. As a degenerate case, if you specify the same
5085/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00005086SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005087 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005088
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005089 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00005090 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005091
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005092 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005093 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005094 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005095 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005096
Dan Gohman79acd2b2008-07-21 22:38:59 +00005097 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005098 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005099 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005100 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005101
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005102 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005103 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00005104
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005105 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005106 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005107 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005108}
5109
Dan Gohman027657d2010-06-18 15:30:29 +00005110SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005111 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005112
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005113 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005114 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00005115 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00005116
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005117 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005118 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005119 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005120 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005121
Dan Gohman79acd2b2008-07-21 22:38:59 +00005122 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005123 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005124 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005125 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005126
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005127 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005128 if (N->OperandList[0] != Op1)
5129 N->OperandList[0].set(Op1);
5130 if (N->OperandList[1] != Op2)
5131 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005132
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005133 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005134 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005135 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005136}
5137
Dan Gohman027657d2010-06-18 15:30:29 +00005138SDNode *SelectionDAG::
5139UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005140 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005141 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005142}
5143
Dan Gohman027657d2010-06-18 15:30:29 +00005144SDNode *SelectionDAG::
5145UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005146 SDValue Op3, SDValue Op4) {
5147 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005148 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005149}
5150
Dan Gohman027657d2010-06-18 15:30:29 +00005151SDNode *SelectionDAG::
5152UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005153 SDValue Op3, SDValue Op4, SDValue Op5) {
5154 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005155 return UpdateNodeOperands(N, Ops);
Chris Lattner809ec112006-01-28 10:09:25 +00005156}
5157
Dan Gohman027657d2010-06-18 15:30:29 +00005158SDNode *SelectionDAG::
Stephen Hinesdce4a402014-05-29 02:49:00 -07005159UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5160 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005161 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005162 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005163
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005164 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005165 bool AnyChange = false;
5166 for (unsigned i = 0; i != NumOps; ++i) {
5167 if (Ops[i] != N->getOperand(i)) {
5168 AnyChange = true;
5169 break;
5170 }
5171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005172
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005173 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00005174 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005175
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005176 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005177 void *InsertPos = nullptr;
5178 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005179 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005180
Dan Gohman7ceda162008-05-02 00:05:03 +00005181 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005182 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005183 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005184 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005185
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005186 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005187 for (unsigned i = 0; i != NumOps; ++i)
5188 if (N->OperandList[i] != Ops[i])
5189 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005190
5191 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005192 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005193 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005194}
5195
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005196/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005197/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005198void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005199 // Unlike the code in MorphNodeTo that does this, we don't need to
5200 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005201 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5202 SDUse &Use = *I++;
5203 Use.set(SDValue());
5204 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005205}
Chris Lattner149c58c2005-08-16 18:17:10 +00005206
Dan Gohmane8be6c62008-07-17 19:10:17 +00005207/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5208/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005209///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005210SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005211 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005212 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005213 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner7651fa42005-08-24 23:00:29 +00005214}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005215
Dan Gohmane8be6c62008-07-17 19:10:17 +00005216SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005217 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005218 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005220 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005221}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005222
Dan Gohmane8be6c62008-07-17 19:10:17 +00005223SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005224 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005225 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005226 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005227 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005228 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005229}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005230
Dan Gohmane8be6c62008-07-17 19:10:17 +00005231SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005232 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005233 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005234 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005235 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005236 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005237}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005238
Dan Gohmane8be6c62008-07-17 19:10:17 +00005239SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005240 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005241 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005242 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005243}
5244
Dan Gohmane8be6c62008-07-17 19:10:17 +00005245SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005246 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005247 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005248 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005249}
5250
Dan Gohmane8be6c62008-07-17 19:10:17 +00005251SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005252 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005253 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005254 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohmancd920d92008-07-02 23:23:19 +00005255}
5256
Dan Gohmane8be6c62008-07-17 19:10:17 +00005257SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005258 EVT VT1, EVT VT2, EVT VT3,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005259 ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005260 SDVTList VTs = getVTList(VT1, VT2, VT3);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005261 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005262}
5263
Bill Wendling13d6d442008-12-01 23:28:22 +00005264SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005265 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005266 ArrayRef<SDValue> Ops) {
Bill Wendling13d6d442008-12-01 23:28:22 +00005267 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005268 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005269}
5270
Scott Michelfdc40a02009-02-17 22:15:04 +00005271SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005272 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005273 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005274 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005275 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005276 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005277}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005278
Scott Michelfdc40a02009-02-17 22:15:04 +00005279SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005280 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005281 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005282 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005283 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005284 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005285}
5286
Dan Gohmane8be6c62008-07-17 19:10:17 +00005287SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005288 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005289 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005291 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005292 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005293 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005294}
5295
Dan Gohmane8be6c62008-07-17 19:10:17 +00005296SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005297 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005298 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005299 SDValue Op3) {
5300 SDVTList VTs = getVTList(VT1, VT2, VT3);
5301 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005302 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005303}
5304
5305SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005306 SDVTList VTs,ArrayRef<SDValue> Ops) {
5307 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner5857e0a2010-02-23 23:01:35 +00005308 // Reset the NodeID to -1.
5309 N->setNodeId(-1);
5310 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005311}
5312
Andrew Trickac6d9be2013-05-25 02:42:55 +00005313/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel0508d042011-12-15 18:21:18 +00005314/// the line number information on the merged node since it is not possible to
5315/// preserve the information that operation is associated with multiple lines.
5316/// This will make the debugger working better at -O0, were there is a higher
5317/// probability having other instructions associated with that line.
5318///
Andrew Trickac6d9be2013-05-25 02:42:55 +00005319/// For IROrder, we keep the smaller of the two
5320SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel0508d042011-12-15 18:21:18 +00005321 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005322 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5323 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel0508d042011-12-15 18:21:18 +00005324 N->setDebugLoc(DebugLoc());
5325 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005326 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5327 N->setIROrder(Order);
Devang Patel0508d042011-12-15 18:21:18 +00005328 return N;
5329}
5330
Chris Lattner21221e32010-02-28 21:36:14 +00005331/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005332/// return type, opcode, and operands.
5333///
5334/// Note that MorphNodeTo returns the resultant node. If there is already a
5335/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickac6d9be2013-05-25 02:42:55 +00005336/// the current one. Note that the SDLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005337///
5338/// Using MorphNodeTo is faster than creating a new node and swapping it in
5339/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005340/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005341/// the node's users.
5342///
5343SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005344 SDVTList VTs, ArrayRef<SDValue> Ops) {
5345 unsigned NumOps = Ops.size();
Dan Gohmancd920d92008-07-02 23:23:19 +00005346 // If an identical node already exists, use it.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005347 void *IP = nullptr;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005348 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005349 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005350 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005351 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005352 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohmane8be6c62008-07-17 19:10:17 +00005353 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005354
Dan Gohman095cc292008-09-13 01:54:27 +00005355 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005356 IP = nullptr;
Chris Lattner67612a12007-02-04 02:32:44 +00005357
Dan Gohmane8be6c62008-07-17 19:10:17 +00005358 // Start the morphing.
5359 N->NodeType = Opc;
5360 N->ValueList = VTs.VTs;
5361 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005362
Dan Gohmane8be6c62008-07-17 19:10:17 +00005363 // Clear the operands list, updating used nodes to remove this from their
5364 // use list. Keep track of any operands that become dead as a result.
5365 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005366 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5367 SDUse &Use = *I++;
5368 SDNode *Used = Use.getNode();
5369 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005370 if (Used->use_empty())
5371 DeadNodeSet.insert(Used);
5372 }
5373
Dan Gohmanc76909a2009-09-25 20:36:54 +00005374 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5375 // Initialize the memory references information.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005376 MN->setMemRefs(nullptr, nullptr);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005377 // If NumOps is larger than the # of operands we can have in a
5378 // MachineSDNode, reallocate the operand list.
5379 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5380 if (MN->OperandsNeedDelete)
5381 delete[] MN->OperandList;
5382 if (NumOps > array_lengthof(MN->LocalOperands))
5383 // We're creating a final node that will live unmorphed for the
5384 // remainder of the current SelectionDAG iteration, so we can allocate
5385 // the operands directly out of a pool with no recycling metadata.
5386 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005387 Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005388 else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005389 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005390 MN->OperandsNeedDelete = false;
5391 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005392 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005393 } else {
5394 // If NumOps is larger than the # of operands we currently have, reallocate
5395 // the operand list.
5396 if (NumOps > N->NumOperands) {
5397 if (N->OperandsNeedDelete)
5398 delete[] N->OperandList;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005399 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005400 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005401 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005402 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005403 }
5404
5405 // Delete any nodes that are still dead after adding the uses for the
5406 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005407 if (!DeadNodeSet.empty()) {
5408 SmallVector<SDNode *, 16> DeadNodes;
5409 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5410 E = DeadNodeSet.end(); I != E; ++I)
5411 if ((*I)->use_empty())
5412 DeadNodes.push_back(*I);
5413 RemoveDeadNodes(DeadNodes);
5414 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005415
Dan Gohmane8be6c62008-07-17 19:10:17 +00005416 if (IP)
5417 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005418 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005419}
5420
Chris Lattner0fb094f2005-11-19 01:44:53 +00005421
Dan Gohman602b0c82009-09-25 18:54:59 +00005422/// getMachineNode - These are used for target selectors to create a new node
5423/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005424///
Dan Gohman602b0c82009-09-25 18:54:59 +00005425/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005426/// node of the specified opcode and operands, it returns that node instead of
5427/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005428MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005429SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005430 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005431 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005432}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005433
Dan Gohmanc81b7832009-10-10 01:29:16 +00005434MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005435SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005436 SDVTList VTs = getVTList(VT);
5437 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005438 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005439}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005440
Dan Gohmanc81b7832009-10-10 01:29:16 +00005441MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005442SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005443 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005444 SDVTList VTs = getVTList(VT);
5445 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005446 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005447}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005448
Dan Gohmanc81b7832009-10-10 01:29:16 +00005449MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005450SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005451 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005452 SDVTList VTs = getVTList(VT);
5453 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005454 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005455}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005456
Dan Gohmanc81b7832009-10-10 01:29:16 +00005457MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005458SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005459 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005460 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005461 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005462}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005463
Dan Gohmanc81b7832009-10-10 01:29:16 +00005464MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005465SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005466 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005467 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005468}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005469
Dan Gohmanc81b7832009-10-10 01:29:16 +00005470MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005471SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005472 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005473 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005474 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005475 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005476}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005477
Dan Gohmanc81b7832009-10-10 01:29:16 +00005478MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005479SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005480 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005481 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005482 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005483 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005484}
5485
Dan Gohmanc81b7832009-10-10 01:29:16 +00005486MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005487SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005488 EVT VT1, EVT VT2, SDValue Op1,
5489 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005490 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005491 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005492 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005493}
5494
Dan Gohmanc81b7832009-10-10 01:29:16 +00005495MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005496SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005497 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005498 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005499 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005500 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005501}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005502
Dan Gohmanc81b7832009-10-10 01:29:16 +00005503MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005504SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005505 EVT VT1, EVT VT2, EVT VT3,
5506 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005507 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005508 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005509 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005510}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005511
Dan Gohmanc81b7832009-10-10 01:29:16 +00005512MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005513SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005514 EVT VT1, EVT VT2, EVT VT3,
5515 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005516 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005517 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005518 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005519}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005520
Dan Gohmanc81b7832009-10-10 01:29:16 +00005521MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005522SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005523 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005524 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005525 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005526 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005527}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005528
Dan Gohmanc81b7832009-10-10 01:29:16 +00005529MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005530SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005531 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005532 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005533 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005534 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005535}
5536
Dan Gohmanc81b7832009-10-10 01:29:16 +00005537MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005538SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005539 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005540 ArrayRef<SDValue> Ops) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07005541 SDVTList VTs = getVTList(ResultTys);
Michael Liao2a8bea72013-04-19 22:22:57 +00005542 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005543}
5544
Dan Gohmanc81b7832009-10-10 01:29:16 +00005545MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005546SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005547 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005548 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005549 MachineSDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005550 void *IP = nullptr;
Michael Liao2a8bea72013-04-19 22:22:57 +00005551 const SDValue *Ops = OpsArray.data();
5552 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005553
5554 if (DoCSE) {
5555 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005556 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
5557 IP = nullptr;
Devang Patel0508d042011-12-15 18:21:18 +00005558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005559 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel0508d042011-12-15 18:21:18 +00005560 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005561 }
5562
5563 // Allocate a new MachineSDNode.
Jack Carter3af4d252013-09-09 22:02:08 +00005564 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5565 DL.getDebugLoc(), VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005566
5567 // Initialize the operands list.
5568 if (NumOps > array_lengthof(N->LocalOperands))
5569 // We're creating a final node that will live unmorphed for the
5570 // remainder of the current SelectionDAG iteration, so we can allocate
5571 // the operands directly out of a pool with no recycling metadata.
5572 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5573 Ops, NumOps);
5574 else
5575 N->InitOperands(N->LocalOperands, Ops, NumOps);
5576 N->OperandsNeedDelete = false;
5577
5578 if (DoCSE)
5579 CSEMap.InsertNode(N, IP);
5580
5581 AllNodes.push_back(N);
5582#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005583 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005584#endif
5585 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005586}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005587
Dan Gohman6a402dc2009-08-19 18:16:17 +00005588/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005589/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005590SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005591SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohman6a402dc2009-08-19 18:16:17 +00005592 SDValue Operand) {
5593 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005594 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005595 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005596 return SDValue(Subreg, 0);
5597}
5598
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005599/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005600/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005601SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005602SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005603 SDValue Operand, SDValue Subreg) {
5604 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005605 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005606 VT, Operand, Subreg, SRIdxVal);
5607 return SDValue(Result, 0);
5608}
5609
Evan Cheng08b11732008-03-22 01:55:50 +00005610/// getNodeIfExists - Get the specified node if it's already available, or
5611/// else return NULL.
5612SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005613 ArrayRef<SDValue> Ops) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005614 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005615 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005616 AddNodeIDNode(ID, Opcode, VTList, Ops);
5617 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005618 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005619 return E;
5620 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07005621 return nullptr;
Evan Cheng08b11732008-03-22 01:55:50 +00005622}
5623
Evan Cheng31441b72010-03-29 20:48:30 +00005624/// getDbgValue - Creates a SDDbgValue node.
5625///
Stephen Hinesdce4a402014-05-29 02:49:00 -07005626/// SDNode
Evan Cheng31441b72010-03-29 20:48:30 +00005627SDDbgValue *
Stephen Hinesdce4a402014-05-29 02:49:00 -07005628SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
5629 bool IsIndirect, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005630 DebugLoc DL, unsigned O) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07005631 return new (Allocator) SDDbgValue(MDPtr, N, R, IsIndirect, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005632}
5633
Stephen Hinesdce4a402014-05-29 02:49:00 -07005634/// Constant
Evan Cheng31441b72010-03-29 20:48:30 +00005635SDDbgValue *
Stephen Hinesdce4a402014-05-29 02:49:00 -07005636SelectionDAG::getConstantDbgValue(MDNode *MDPtr, const Value *C,
5637 uint64_t Off,
5638 DebugLoc DL, unsigned O) {
Evan Cheng31441b72010-03-29 20:48:30 +00005639 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5640}
5641
Stephen Hinesdce4a402014-05-29 02:49:00 -07005642/// FrameIndex
Evan Cheng31441b72010-03-29 20:48:30 +00005643SDDbgValue *
Stephen Hinesdce4a402014-05-29 02:49:00 -07005644SelectionDAG::getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5645 DebugLoc DL, unsigned O) {
Evan Cheng31441b72010-03-29 20:48:30 +00005646 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5647}
5648
Dan Gohmana72d2a22010-03-03 21:33:37 +00005649namespace {
5650
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005651/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005652/// pointed to by a use iterator is deleted, increment the use iterator
5653/// so that it doesn't dangle.
5654///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005655class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005656 SDNode::use_iterator &UI;
5657 SDNode::use_iterator &UE;
5658
Stephen Hines36b56882014-04-23 16:57:46 -07005659 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005660 // Increment the iterator as needed.
5661 while (UI != UE && N == *UI)
5662 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005663 }
5664
5665public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005666 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005667 SDNode::use_iterator &ui,
5668 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005669 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005670};
5671
5672}
5673
Evan Cheng99157a02006-08-07 22:13:29 +00005674/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005675/// This can cause recursive merging of nodes in the DAG.
5676///
Chris Lattner11d049c2008-02-03 03:35:22 +00005677/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005678///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005679void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005680 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005681 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005682 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005683 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005684
Dan Gohman39946102009-01-25 16:29:12 +00005685 // Iterate over all the existing uses of From. New uses will be added
5686 // to the beginning of the use list, which we avoid visiting.
5687 // This specifically avoids visiting uses of From that arise while the
5688 // replacement is happening, because any such uses would be the result
5689 // of CSE: If an existing node looks like From after one of its operands
5690 // is replaced by To, we don't want to replace of all its users with To
5691 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005692 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005693 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005694 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005695 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005696
Chris Lattner8b8749f2005-08-17 19:00:20 +00005697 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005698 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005699
Dan Gohman39946102009-01-25 16:29:12 +00005700 // A user can appear in a use list multiple times, and when this
5701 // happens the uses are usually next to each other in the list.
5702 // To help reduce the number of CSE recomputations, process all
5703 // the uses of this user that we can find this way.
5704 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005705 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005706 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005707 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005708 } while (UI != UE && *UI == User);
5709
5710 // Now that we have modified User, add it back to the CSE maps. If it
5711 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005712 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005713 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005714
5715 // If we just RAUW'd the root, take note.
5716 if (FromN == getRoot())
5717 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005718}
5719
5720/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5721/// This can cause recursive merging of nodes in the DAG.
5722///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005723/// This version assumes that for each value of From, there is a
5724/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005725///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005726void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005727#ifndef NDEBUG
5728 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5729 assert((!From->hasAnyUseOfValue(i) ||
5730 From->getValueType(i) == To->getValueType(i)) &&
5731 "Cannot use this version of ReplaceAllUsesWith!");
5732#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005733
5734 // Handle the trivial case.
5735 if (From == To)
5736 return;
5737
Dan Gohmandbe664a2009-01-19 21:44:21 +00005738 // Iterate over just the existing users of From. See the comments in
5739 // the ReplaceAllUsesWith above.
5740 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005741 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005742 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005743 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005744
Chris Lattner8b52f212005-08-26 18:36:28 +00005745 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005746 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005747
Dan Gohman39946102009-01-25 16:29:12 +00005748 // A user can appear in a use list multiple times, and when this
5749 // happens the uses are usually next to each other in the list.
5750 // To help reduce the number of CSE recomputations, process all
5751 // the uses of this user that we can find this way.
5752 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005753 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005754 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005755 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005756 } while (UI != UE && *UI == User);
5757
5758 // Now that we have modified User, add it back to the CSE maps. If it
5759 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005760 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005761 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005762
5763 // If we just RAUW'd the root, take note.
5764 if (From == getRoot().getNode())
5765 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005766}
5767
Chris Lattner8b52f212005-08-26 18:36:28 +00005768/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5769/// This can cause recursive merging of nodes in the DAG.
5770///
5771/// This version can replace From with any result values. To must match the
5772/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005773void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005774 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005775 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005776
Dan Gohmandbe664a2009-01-19 21:44:21 +00005777 // Iterate over just the existing users of From. See the comments in
5778 // the ReplaceAllUsesWith above.
5779 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005780 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005781 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005782 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005783
Chris Lattner7b2880c2005-08-24 22:44:39 +00005784 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005785 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005786
Dan Gohman39946102009-01-25 16:29:12 +00005787 // A user can appear in a use list multiple times, and when this
5788 // happens the uses are usually next to each other in the list.
5789 // To help reduce the number of CSE recomputations, process all
5790 // the uses of this user that we can find this way.
5791 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005792 SDUse &Use = UI.getUse();
5793 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005794 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005795 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005796 } while (UI != UE && *UI == User);
5797
5798 // Now that we have modified User, add it back to the CSE maps. If it
5799 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005800 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005801 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005802
5803 // If we just RAUW'd the root, take note.
5804 if (From == getRoot().getNode())
5805 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005806}
5807
Chris Lattner012f2412006-02-17 21:58:01 +00005808/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005809/// uses of other values produced by From.getNode() alone. The Deleted
5810/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005811void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005812 // Handle the really simple, really trivial case efficiently.
5813 if (From == To) return;
5814
Chris Lattner012f2412006-02-17 21:58:01 +00005815 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005816 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005817 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005818 return;
5819 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005820
Dan Gohman39946102009-01-25 16:29:12 +00005821 // Iterate over just the existing users of From. See the comments in
5822 // the ReplaceAllUsesWith above.
5823 SDNode::use_iterator UI = From.getNode()->use_begin(),
5824 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005825 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005826 while (UI != UE) {
5827 SDNode *User = *UI;
5828 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005829
Dan Gohman39946102009-01-25 16:29:12 +00005830 // A user can appear in a use list multiple times, and when this
5831 // happens the uses are usually next to each other in the list.
5832 // To help reduce the number of CSE recomputations, process all
5833 // the uses of this user that we can find this way.
5834 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005835 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005836
5837 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005838 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005839 ++UI;
5840 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005841 }
Dan Gohman39946102009-01-25 16:29:12 +00005842
5843 // If this node hasn't been modified yet, it's still in the CSE maps,
5844 // so remove its old self from the CSE maps.
5845 if (!UserRemovedFromCSEMaps) {
5846 RemoveNodeFromCSEMaps(User);
5847 UserRemovedFromCSEMaps = true;
5848 }
5849
5850 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005851 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005852 } while (UI != UE && *UI == User);
5853
5854 // We are iterating over all uses of the From node, so if a use
5855 // doesn't use the specific value, no changes are made.
5856 if (!UserRemovedFromCSEMaps)
5857 continue;
5858
Chris Lattner01d029b2007-10-15 06:10:22 +00005859 // Now that we have modified User, add it back to the CSE maps. If it
5860 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005861 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005862 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005863
5864 // If we just RAUW'd the root, take note.
5865 if (From == getRoot())
5866 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005867}
5868
Dan Gohman39946102009-01-25 16:29:12 +00005869namespace {
5870 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5871 /// to record information about a use.
5872 struct UseMemo {
5873 SDNode *User;
5874 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005875 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005876 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005877
5878 /// operator< - Sort Memos by User.
5879 bool operator<(const UseMemo &L, const UseMemo &R) {
5880 return (intptr_t)L.User < (intptr_t)R.User;
5881 }
Dan Gohman39946102009-01-25 16:29:12 +00005882}
5883
Dan Gohmane8be6c62008-07-17 19:10:17 +00005884/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005885/// uses of other values produced by From.getNode() alone. The same value
5886/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005887/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005888void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5889 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005890 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005891 // Handle the simple, trivial case efficiently.
5892 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005893 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005894
Dan Gohman39946102009-01-25 16:29:12 +00005895 // Read up all the uses and make records of them. This helps
5896 // processing new uses that are introduced during the
5897 // replacement process.
5898 SmallVector<UseMemo, 4> Uses;
5899 for (unsigned i = 0; i != Num; ++i) {
5900 unsigned FromResNo = From[i].getResNo();
5901 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005902 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005903 E = FromNode->use_end(); UI != E; ++UI) {
5904 SDUse &Use = UI.getUse();
5905 if (Use.getResNo() == FromResNo) {
5906 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005907 Uses.push_back(Memo);
5908 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005909 }
Dan Gohman39946102009-01-25 16:29:12 +00005910 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005911
Dan Gohmane7852d02009-01-26 04:35:06 +00005912 // Sort the uses, so that all the uses from a given User are together.
5913 std::sort(Uses.begin(), Uses.end());
5914
Dan Gohman39946102009-01-25 16:29:12 +00005915 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5916 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005917 // We know that this user uses some value of From. If it is the right
5918 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005919 SDNode *User = Uses[UseIndex].User;
5920
5921 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005922 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005923
Dan Gohmane7852d02009-01-26 04:35:06 +00005924 // The Uses array is sorted, so all the uses for a given User
5925 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005926 // To help reduce the number of CSE recomputations, process all
5927 // the uses of this user that we can find this way.
5928 do {
5929 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005930 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005931 ++UseIndex;
5932
Dan Gohmane7852d02009-01-26 04:35:06 +00005933 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005934 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5935
Dan Gohmane8be6c62008-07-17 19:10:17 +00005936 // Now that we have modified User, add it back to the CSE maps. If it
5937 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005938 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005939 }
5940}
5941
Evan Chenge6f35d82006-08-01 08:20:41 +00005942/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005943/// based on their topological order. It returns the maximum id and a vector
5944/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005945unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005946
Dan Gohmanf06c8352008-09-30 18:30:35 +00005947 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005948
Dan Gohmanf06c8352008-09-30 18:30:35 +00005949 // SortedPos tracks the progress of the algorithm. Nodes before it are
5950 // sorted, nodes after it are unsorted. When the algorithm completes
5951 // it is at the end of the list.
5952 allnodes_iterator SortedPos = allnodes_begin();
5953
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005954 // Visit all the nodes. Move nodes with no operands to the front of
5955 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005956 // operand count. Before we do this, the Node Id fields of the nodes
5957 // may contain arbitrary values. After, the Node Id fields for nodes
5958 // before SortedPos will contain the topological sort index, and the
5959 // Node Id fields for nodes At SortedPos and after will contain the
5960 // count of outstanding operands.
5961 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5962 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005963 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005964 unsigned Degree = N->getNumOperands();
5965 if (Degree == 0) {
5966 // A node with no uses, add it to the result array immediately.
5967 N->setNodeId(DAGSize++);
5968 allnodes_iterator Q = N;
5969 if (Q != SortedPos)
5970 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005971 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005972 ++SortedPos;
5973 } else {
5974 // Temporarily use the Node Id as scratch space for the degree count.
5975 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005976 }
5977 }
5978
Chad Rosierf496dea2012-05-21 17:13:41 +00005979 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005980 // such that by the time the end is reached all nodes will be sorted.
5981 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5982 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005983 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005984 // N is in sorted position, so all its uses have one less operand
5985 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005986 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5987 UI != UE; ++UI) {
5988 SDNode *P = *UI;
5989 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005990 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005991 --Degree;
5992 if (Degree == 0) {
5993 // All of P's operands are sorted, so P may sorted now.
5994 P->setNodeId(DAGSize++);
5995 if (P != SortedPos)
5996 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005997 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005998 ++SortedPos;
5999 } else {
6000 // Update P's outstanding operand count.
6001 P->setNodeId(Degree);
6002 }
6003 }
David Greene221925e2010-01-20 00:59:23 +00006004 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00006005#ifndef NDEBUG
6006 SDNode *S = ++I;
6007 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00006008 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00006009#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -07006010 llvm_unreachable(nullptr);
David Greene221925e2010-01-20 00:59:23 +00006011 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00006012 }
6013
6014 assert(SortedPos == AllNodes.end() &&
6015 "Topological sort incomplete!");
6016 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6017 "First node in topological sort is not the entry token!");
6018 assert(AllNodes.front().getNodeId() == 0 &&
6019 "First node in topological sort has non-zero id!");
6020 assert(AllNodes.front().getNumOperands() == 0 &&
6021 "First node in topological sort has operands!");
6022 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6023 "Last node in topologic sort has unexpected id!");
6024 assert(AllNodes.back().use_empty() &&
6025 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006026 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006027 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00006028}
6029
Evan Chengbfcb3052010-03-25 01:38:16 +00006030/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6031/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00006032void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6033 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00006034 if (SD)
6035 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00006036}
Evan Cheng091cba12006-07-27 06:39:06 +00006037
Devang Patela2e868d2011-01-25 23:27:42 +00006038/// TransferDbgValues - Transfer SDDbgValues.
6039void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6040 if (From == To || !From.getNode()->getHasDebugValue())
6041 return;
6042 SDNode *FromNode = From.getNode();
6043 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006044 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00006045 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006046 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00006047 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00006048 SDDbgValue *Dbg = *I;
6049 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6050 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07006051 Dbg->isIndirect(),
Devang Patela778f5c2011-02-18 22:43:42 +00006052 Dbg->getOffset(), Dbg->getDebugLoc(),
6053 Dbg->getOrder());
6054 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00006055 }
6056 }
Craig Topperf22fd3f2013-07-03 05:11:49 +00006057 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patela778f5c2011-02-18 22:43:42 +00006058 E = ClonedDVs.end(); I != E; ++I)
6059 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00006060}
6061
Jim Laskey58b968b2005-08-17 20:08:02 +00006062//===----------------------------------------------------------------------===//
6063// SDNode Class
6064//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00006065
Chris Lattner48b85922007-02-04 02:41:42 +00006066HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00006067 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00006068}
6069
Andrew Trickac6d9be2013-05-25 02:42:55 +00006070GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6071 DebugLoc DL, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006072 EVT VT, int64_t o, unsigned char TF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006073 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00006074 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00006075}
Chris Lattner48b85922007-02-04 02:41:42 +00006076
Matt Arsenault59d3ae62013-11-15 01:34:59 +00006077AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6078 SDValue X, unsigned SrcAS,
6079 unsigned DestAS)
6080 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6081 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6082
Andrew Trickac6d9be2013-05-25 02:42:55 +00006083MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6084 EVT memvt, MachineMemOperand *mmo)
6085 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006086 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006087 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006088 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00006089 assert(isNonTemporal() == MMO->isNonTemporal() &&
6090 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00006091 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00006092}
6093
Andrew Trickac6d9be2013-05-25 02:42:55 +00006094MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006095 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6096 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohmanc76909a2009-09-25 20:36:54 +00006097 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006098 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006099 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006100 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6101 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00006102}
6103
Jim Laskey583bd472006-10-27 23:46:08 +00006104/// Profile - Gather unique data for the node.
6105///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00006106void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00006107 AddNodeIDNode(ID, this);
6108}
6109
Owen Andersond8110fb2009-08-25 22:27:22 +00006110namespace {
6111 struct EVTArray {
6112 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006113
Owen Andersond8110fb2009-08-25 22:27:22 +00006114 EVTArray() {
6115 VTs.reserve(MVT::LAST_VALUETYPE);
6116 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6117 VTs.push_back(MVT((MVT::SimpleValueType)i));
6118 }
6119 };
6120}
6121
Owen Andersone50ed302009-08-10 22:56:29 +00006122static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00006123static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00006124static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00006125
Chris Lattnera3255112005-11-08 23:30:28 +00006126/// getValueTypeList - Return a pointer to the specified value type.
6127///
Owen Andersone50ed302009-08-10 22:56:29 +00006128const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006129 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00006130 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00006131 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00006132 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00006133 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00006134 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00006135 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00006136 }
Chris Lattnera3255112005-11-08 23:30:28 +00006137}
Duncan Sandsaf47b112007-10-16 09:56:48 +00006138
Chris Lattner5c884562005-01-12 18:37:47 +00006139/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6140/// indicated value. This method ignores uses of other values defined by this
6141/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00006142bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00006143 assert(Value < getNumValues() && "Bad value!");
6144
Roman Levensteindc1adac2008-04-07 10:06:32 +00006145 // TODO: Only iterate over uses of a given value of the node
6146 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00006147 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00006148 if (NUses == 0)
6149 return false;
6150 --NUses;
6151 }
Chris Lattner5c884562005-01-12 18:37:47 +00006152 }
6153
6154 // Found exactly the right number of uses?
6155 return NUses == 0;
6156}
6157
6158
Evan Cheng33d55952007-08-02 05:29:38 +00006159/// hasAnyUseOfValue - Return true if there are any use of the indicated
6160/// value. This method ignores uses of other values defined by this operation.
6161bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6162 assert(Value < getNumValues() && "Bad value!");
6163
Dan Gohman1373c1c2008-07-09 22:39:01 +00006164 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00006165 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00006166 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00006167
6168 return false;
6169}
6170
6171
Dan Gohman2a629952008-07-27 18:06:42 +00006172/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00006173///
Dan Gohman2a629952008-07-27 18:06:42 +00006174bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00006175 bool Seen = false;
6176 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006177 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006178 if (User == this)
6179 Seen = true;
6180 else
6181 return false;
6182 }
6183
6184 return Seen;
6185}
6186
Evan Chenge6e97e62006-11-03 07:31:32 +00006187/// isOperand - Return true if this node is an operand of N.
6188///
Dan Gohman475871a2008-07-27 21:46:04 +00006189bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006190 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6191 if (*this == N->getOperand(i))
6192 return true;
6193 return false;
6194}
6195
Evan Cheng917be682008-03-04 00:41:45 +00006196bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006197 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006198 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006199 return true;
6200 return false;
6201}
Evan Cheng4ee62112006-02-05 06:29:23 +00006202
Chris Lattner572dee72008-01-16 05:49:24 +00006203/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006204/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006205/// side-effecting instructions on any chain path. In practice, this looks
6206/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006207/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006208bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006209 unsigned Depth) const {
6210 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006211
Chris Lattner572dee72008-01-16 05:49:24 +00006212 // Don't search too deeply, we just want to be able to see through
6213 // TokenFactor's etc.
6214 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006215
Chris Lattner572dee72008-01-16 05:49:24 +00006216 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006217 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006218 if (getOpcode() == ISD::TokenFactor) {
6219 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006220 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6221 return false;
6222 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006223 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006224
Chris Lattner572dee72008-01-16 05:49:24 +00006225 // Loads don't have side effects, look through them.
6226 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6227 if (!Ld->isVolatile())
6228 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6229 }
6230 return false;
6231}
6232
Lang Hames944520f2011-07-07 04:31:51 +00006233/// hasPredecessor - Return true if N is a predecessor of this node.
6234/// N is either an operand of this node, or can be reached by recursively
6235/// traversing up the operands.
6236/// NOTE: This is an expensive method. Use it carefully.
6237bool SDNode::hasPredecessor(const SDNode *N) const {
6238 SmallPtrSet<const SDNode *, 32> Visited;
6239 SmallVector<const SDNode *, 16> Worklist;
6240 return hasPredecessorHelper(N, Visited, Worklist);
6241}
Dan Gohman6688d612009-10-28 03:44:30 +00006242
Craig Toppera0ec3f92013-07-14 04:42:23 +00006243bool
6244SDNode::hasPredecessorHelper(const SDNode *N,
6245 SmallPtrSet<const SDNode *, 32> &Visited,
6246 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames944520f2011-07-07 04:31:51 +00006247 if (Visited.empty()) {
6248 Worklist.push_back(this);
6249 } else {
6250 // Take a look in the visited set. If we've already encountered this node
6251 // we needn't search further.
6252 if (Visited.count(N))
6253 return true;
6254 }
6255
6256 // Haven't visited N yet. Continue the search.
6257 while (!Worklist.empty()) {
6258 const SDNode *M = Worklist.pop_back_val();
6259 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6260 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006261 if (Visited.insert(Op))
6262 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006263 if (Op == N)
6264 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006265 }
Lang Hames944520f2011-07-07 04:31:51 +00006266 }
Dan Gohman6688d612009-10-28 03:44:30 +00006267
6268 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006269}
6270
Evan Chengc5484282006-10-04 00:56:09 +00006271uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6272 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006273 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006274}
6275
Mon P Wangcd6e7252009-11-30 02:42:02 +00006276SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6277 assert(N->getNumValues() == 1 &&
6278 "Can't unroll a vector with multiple results!");
6279
6280 EVT VT = N->getValueType(0);
6281 unsigned NE = VT.getVectorNumElements();
6282 EVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006283 SDLoc dl(N);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006284
6285 SmallVector<SDValue, 8> Scalars;
6286 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6287
6288 // If ResNE is 0, fully unroll the vector op.
6289 if (ResNE == 0)
6290 ResNE = NE;
6291 else if (NE > ResNE)
6292 NE = ResNE;
6293
6294 unsigned i;
6295 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006296 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006297 SDValue Operand = N->getOperand(j);
6298 EVT OperandVT = Operand.getValueType();
6299 if (OperandVT.isVector()) {
6300 // A vector operand; extract a single element.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006301 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wangcd6e7252009-11-30 02:42:02 +00006302 EVT OperandEltVT = OperandVT.getVectorElementType();
6303 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6304 OperandEltVT,
6305 Operand,
Tom Stellard425b76c2013-08-05 22:22:01 +00006306 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006307 } else {
6308 // A scalar operand; just use it as is.
6309 Operands[j] = Operand;
6310 }
6311 }
6312
6313 switch (N->getOpcode()) {
6314 default:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006315 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006316 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006317 case ISD::VSELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006318 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotemaec58612011-09-13 19:17:42 +00006319 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006320 case ISD::SHL:
6321 case ISD::SRA:
6322 case ISD::SRL:
6323 case ISD::ROTL:
6324 case ISD::ROTR:
6325 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter3af4d252013-09-09 22:02:08 +00006326 getShiftAmountOperand(Operands[0].getValueType(),
6327 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006328 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006329 case ISD::SIGN_EXTEND_INREG:
6330 case ISD::FP_ROUND_INREG: {
6331 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6332 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6333 Operands[0],
6334 getValueType(ExtVT)));
6335 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006336 }
6337 }
6338
6339 for (; i < ResNE; ++i)
6340 Scalars.push_back(getUNDEF(EltVT));
6341
6342 return getNode(ISD::BUILD_VECTOR, dl,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006343 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006344}
6345
Evan Cheng64fa4a92009-12-09 01:36:00 +00006346
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006347/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6348/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006349/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006350bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006351 unsigned Bytes, int Dist) const {
6352 if (LD->getChain() != Base->getChain())
6353 return false;
6354 EVT VT = LD->getValueType(0);
6355 if (VT.getSizeInBits() / 8 != Bytes)
6356 return false;
6357
6358 SDValue Loc = LD->getOperand(1);
6359 SDValue BaseLoc = Base->getOperand(1);
6360 if (Loc.getOpcode() == ISD::FrameIndex) {
6361 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6362 return false;
6363 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6364 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6365 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6366 int FS = MFI->getObjectSize(FI);
6367 int BFS = MFI->getObjectSize(BFI);
6368 if (FS != BFS || FS != (int)Bytes) return false;
6369 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6370 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006371
6372 // Handle X+C
6373 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6374 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6375 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006376
Stephen Hinesdce4a402014-05-29 02:49:00 -07006377 const GlobalValue *GV1 = nullptr;
6378 const GlobalValue *GV2 = nullptr;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006379 int64_t Offset1 = 0;
6380 int64_t Offset2 = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006381 const TargetLowering *TLI = TM.getTargetLowering();
6382 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6383 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Cheng64fa4a92009-12-09 01:36:00 +00006384 if (isGA1 && isGA2 && GV1 == GV2)
6385 return Offset1 == (Offset2 + Dist*Bytes);
6386 return false;
6387}
6388
6389
Evan Chengf2dc5c72009-12-09 01:04:59 +00006390/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6391/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006392unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006393 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006394 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006395 int64_t GVOffset = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006396 const TargetLowering *TLI = TM.getTargetLowering();
6397 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaulte6e81122013-11-16 20:50:54 +00006398 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006399 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Stephen Hinesdce4a402014-05-29 02:49:00 -07006400 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6401 TLI->getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006402 unsigned AlignBits = KnownZero.countTrailingOnes();
6403 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6404 if (Align)
6405 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006406 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006407
Evan Chengf2dc5c72009-12-09 01:04:59 +00006408 // If this is a direct reference to a stack slot, use information about the
6409 // stack slot's alignment.
6410 int FrameIdx = 1 << 31;
6411 int64_t FrameOffset = 0;
6412 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6413 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006414 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006415 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006416 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006417 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6418 FrameOffset = Ptr.getConstantOperandVal(1);
6419 }
6420
6421 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006422 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006423 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6424 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006425 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006426 }
6427
6428 return 0;
6429}
6430
Bill Wendlingee287ca2013-11-22 05:17:44 +00006431/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6432/// which is split (or expanded) into two not necessarily identical pieces.
6433std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6434 // Currently all types are split in half.
6435 EVT LoVT, HiVT;
6436 if (!VT.isVector()) {
6437 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6438 } else {
6439 unsigned NumElements = VT.getVectorNumElements();
6440 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6441 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6442 NumElements/2);
6443 }
6444 return std::make_pair(LoVT, HiVT);
6445}
6446
6447/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6448/// low/high part.
6449std::pair<SDValue, SDValue>
6450SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6451 const EVT &HiVT) {
6452 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6453 N.getValueType().getVectorNumElements() &&
6454 "More vector elements requested than available!");
6455 SDValue Lo, Hi;
6456 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6457 getConstant(0, TLI->getVectorIdxTy()));
6458 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6459 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6460 return std::make_pair(Lo, Hi);
6461}
6462
Stephen Hinesdce4a402014-05-29 02:49:00 -07006463void SelectionDAG::ExtractVectorElements(SDValue Op,
6464 SmallVectorImpl<SDValue> &Args,
6465 unsigned Start, unsigned Count) {
6466 EVT VT = Op.getValueType();
6467 if (Count == 0)
6468 Count = VT.getVectorNumElements();
6469
6470 EVT EltVT = VT.getVectorElementType();
6471 EVT IdxTy = TLI->getVectorIdxTy();
6472 SDLoc SL(Op);
6473 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6474 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6475 Op, getConstant(i, IdxTy)));
6476 }
6477}
6478
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006479// getAddressSpace - Return the address space this GlobalAddress belongs to.
6480unsigned GlobalAddressSDNode::getAddressSpace() const {
6481 return getGlobal()->getType()->getAddressSpace();
6482}
6483
6484
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006485Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006486 if (isMachineConstantPoolEntry())
6487 return Val.MachineCPVal->getType();
6488 return Val.ConstVal->getType();
6489}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006490
Bob Wilson24e338e2009-03-02 23:24:16 +00006491bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6492 APInt &SplatUndef,
6493 unsigned &SplatBitSize,
6494 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006495 unsigned MinSplatBits,
Stephen Hines36b56882014-04-23 16:57:46 -07006496 bool isBigEndian) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006497 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006498 assert(VT.isVector() && "Expected a vector type");
6499 unsigned sz = VT.getSizeInBits();
6500 if (MinSplatBits > sz)
6501 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006502
Bob Wilson24e338e2009-03-02 23:24:16 +00006503 SplatValue = APInt(sz, 0);
6504 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006505
Bob Wilson24e338e2009-03-02 23:24:16 +00006506 // Get the bits. Bits with undefined values (when the corresponding element
6507 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6508 // in SplatValue. If any of the values are not constant, give up and return
6509 // false.
6510 unsigned int nOps = getNumOperands();
6511 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6512 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006513
6514 for (unsigned j = 0; j < nOps; ++j) {
6515 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006516 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006517 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006518
Bob Wilson24e338e2009-03-02 23:24:16 +00006519 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006520 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006521 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006522 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006523 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006524 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006525 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006526 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006527 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006528 }
6529
Bob Wilson24e338e2009-03-02 23:24:16 +00006530 // The build_vector is all constants or undefs. Find the smallest element
6531 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006532
Bob Wilson24e338e2009-03-02 23:24:16 +00006533 HasAnyUndefs = (SplatUndef != 0);
6534 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006535
Bob Wilson24e338e2009-03-02 23:24:16 +00006536 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006537 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6538 APInt LowValue = SplatValue.trunc(HalfSize);
6539 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6540 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006541
Bob Wilson24e338e2009-03-02 23:24:16 +00006542 // If the two halves do not match (ignoring undef bits), stop here.
6543 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6544 MinSplatBits > HalfSize)
6545 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006546
Bob Wilson24e338e2009-03-02 23:24:16 +00006547 SplatValue = HighValue | LowValue;
6548 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006549
Bob Wilson24e338e2009-03-02 23:24:16 +00006550 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006551 }
6552
Bob Wilson24e338e2009-03-02 23:24:16 +00006553 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006554 return true;
6555}
Nate Begeman9008ca62009-04-27 18:41:29 +00006556
Stephen Hines36b56882014-04-23 16:57:46 -07006557ConstantSDNode *BuildVectorSDNode::getConstantSplatValue() const {
6558 SDValue Op0 = getOperand(0);
6559 if (Op0.getOpcode() != ISD::Constant)
6560 return nullptr;
6561
6562 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
6563 if (getOperand(i) != Op0)
6564 return nullptr;
6565
6566 return cast<ConstantSDNode>(Op0);
6567}
6568
6569bool BuildVectorSDNode::isConstant() const {
6570 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6571 unsigned Opc = getOperand(i).getOpcode();
6572 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6573 return false;
6574 }
6575 return true;
6576}
6577
Owen Andersone50ed302009-08-10 22:56:29 +00006578bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006579 // Find the first non-undef value in the shuffle mask.
6580 unsigned i, e;
6581 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6582 /* search */;
6583
Nate Begemana6415752009-04-29 18:13:31 +00006584 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006585
Nate Begeman5a5ca152009-04-29 05:20:52 +00006586 // Make sure all remaining elements are either undef or the same as the first
6587 // non-undef value.
6588 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006589 if (Mask[i] >= 0 && Mask[i] != Idx)
6590 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006591 return true;
6592}
David Greenecf495bc2010-01-20 20:13:31 +00006593
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006594#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006595static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006596 SmallPtrSet<const SDNode*, 32> &Visited,
6597 SmallPtrSet<const SDNode*, 32> &Checked) {
6598 // If this node has already been checked, don't check it again.
6599 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006600 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006601
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006602 // If a node has already been visited on this depth-first walk, reject it as
6603 // a cycle.
6604 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006605 dbgs() << "Offending node:\n";
6606 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006607 errs() << "Detected cycle in SelectionDAG\n";
6608 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006609 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006610
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006611 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6612 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006613
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006614 Checked.insert(N);
6615 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006616}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006617#endif
David Greenecf495bc2010-01-20 20:13:31 +00006618
6619void llvm::checkForCycles(const llvm::SDNode *N) {
6620#ifdef XDEBUG
Alp Toker18a988e2013-10-29 02:35:28 +00006621 assert(N && "Checking nonexistent SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006622 SmallPtrSet<const SDNode*, 32> visited;
6623 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006624 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006625#endif
6626}
6627
6628void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6629 checkForCycles(DAG->getRoot().getNode());
6630}