blob: daff1f228c71096da94c8215d9399caf2c5a640d [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>
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070051
Chris Lattnere25738c2004-06-02 04:28:06 +000052using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000053
Chris Lattner0b3e5252006-08-15 19:11:05 +000054/// makeVTList - Return an instance of the SDVTList struct initialized with the
55/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000056static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000057 SDVTList Res = {VTs, NumVTs};
58 return Res;
59}
60
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000061// Default null implementations of the callbacks.
62void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
63void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000064
Jim Laskey58b968b2005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
66// ConstantFPSDNode Class
67//===----------------------------------------------------------------------===//
68
69/// isExactlyValue - We don't rely on operator== working on double values, as
70/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
71/// As such, this method can be used to do an exact bit-for-bit comparison of
72/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000073bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000074 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000075}
76
Owen Andersone50ed302009-08-10 22:56:29 +000077bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000078 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000079 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000080
Dale Johannesenf04afdb2007-08-30 00:23:21 +000081 // convert modifies in place, so make a copy.
82 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000083 bool losesInfo;
Tim Northover0a29cb02013-01-22 09:46:31 +000084 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
85 APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000086 &losesInfo);
87 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088}
89
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000093
Evan Chenga8df1662006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000097 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000098 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +000099 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000100
Evan Chenga8df1662006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000102
Chris Lattner61d43992006-03-25 22:57:01 +0000103 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000104
Chris Lattner61d43992006-03-25 22:57:01 +0000105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000108
Chris Lattner61d43992006-03-25 22:57:01 +0000109 // Do not accept an all-undef vector.
110 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000111
Chris Lattner61d43992006-03-25 22:57:01 +0000112 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000113 // elements. We have to be a bit careful here, as the type of the constant
114 // may not be the same as the type of the vector elements due to type
115 // legalization (the elements are promoted to a legal type for the target and
116 // a vector of a type may be legal when the base element type is not).
117 // We only want to check enough bits to cover the vector elements, because
118 // we care if the resultant vector is all ones, not whether the individual
119 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000120 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000121 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszak554c6762012-09-30 21:24:57 +0000122 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
123 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000124 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000125 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
126 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000127 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000128 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000129 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000130
Chris Lattner61d43992006-03-25 22:57:01 +0000131 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000132 // undefs. Even with the above element type twiddling, this should be OK, as
133 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000134 for (++i; i != e; ++i)
135 if (N->getOperand(i) != NotZero &&
136 N->getOperand(i).getOpcode() != ISD::UNDEF)
137 return false;
138 return true;
139}
140
141
Evan Cheng4a147842006-03-26 09:50:58 +0000142/// isBuildVectorAllZeros - Return true if the specified node is a
143/// BUILD_VECTOR where all of the elements are 0 or undef.
144bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000145 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000146 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000147 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000148
Evan Cheng4a147842006-03-26 09:50:58 +0000149 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000150
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700151 bool IsAllUndef = true;
152 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
153 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
154 continue;
155 IsAllUndef = false;
156 // Do not accept build_vectors that aren't all constants or which have non-0
157 // elements. We have to be a bit careful here, as the type of the constant
158 // may not be the same as the type of the vector elements due to type
159 // legalization (the elements are promoted to a legal type for the target
160 // and a vector of a type may be legal when the base element type is not).
161 // We only want to check enough bits to cover the vector elements, because
162 // we care if the resultant vector is all zeros, not whether the individual
163 // constants are.
164 SDValue Zero = N->getOperand(i);
165 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
166 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
167 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
168 return false;
169 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
170 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
171 return false;
172 } else
173 return false;
174 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000175
Evan Chenga8df1662006-03-27 06:58:47 +0000176 // Do not accept an all-undef vector.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700177 if (IsAllUndef)
Evan Chenga8df1662006-03-27 06:58:47 +0000178 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000179 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000180}
181
Stephen Hines36b56882014-04-23 16:57:46 -0700182/// \brief Return true if the specified node is a BUILD_VECTOR node of
183/// all ConstantSDNode or undef.
184bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
185 if (N->getOpcode() != ISD::BUILD_VECTOR)
186 return false;
187
188 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
189 SDValue Op = N->getOperand(i);
190 if (Op.getOpcode() == ISD::UNDEF)
191 continue;
192 if (!isa<ConstantSDNode>(Op))
193 return false;
194 }
195 return true;
196}
197
Evan Chengefec7512008-02-18 23:04:32 +0000198/// isScalarToVector - Return true if the specified node is a
199/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
200/// element is not an undef.
201bool ISD::isScalarToVector(const SDNode *N) {
202 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
203 return true;
204
205 if (N->getOpcode() != ISD::BUILD_VECTOR)
206 return false;
207 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
208 return false;
209 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000210 if (NumElems == 1)
211 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000212 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000213 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000214 if (V.getOpcode() != ISD::UNDEF)
215 return false;
216 }
217 return true;
218}
219
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000220/// allOperandsUndef - Return true if the node has at least one operand
221/// and all operands of the specified node are ISD::UNDEF.
222bool ISD::allOperandsUndef(const SDNode *N) {
223 // Return false if the node has no operands.
224 // This is "logically inconsistent" with the definition of "all" but
225 // is probably the desired behavior.
226 if (N->getNumOperands() == 0)
227 return false;
228
229 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
230 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
231 return false;
232
233 return true;
234}
235
Stephen Hines36b56882014-04-23 16:57:46 -0700236ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) {
237 switch (ExtType) {
238 case ISD::EXTLOAD:
239 return ISD::ANY_EXTEND;
240 case ISD::SEXTLOAD:
241 return ISD::SIGN_EXTEND;
242 case ISD::ZEXTLOAD:
243 return ISD::ZERO_EXTEND;
244 default:
245 break;
246 }
247
248 llvm_unreachable("Invalid LoadExtType");
249}
250
Chris Lattnerc3aae252005-01-07 07:46:32 +0000251/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
252/// when given the operation for (X op Y).
253ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
254 // To perform this operation, we just need to swap the L and G bits of the
255 // operation.
256 unsigned OldL = (Operation >> 2) & 1;
257 unsigned OldG = (Operation >> 1) & 1;
258 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
259 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000260 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261}
262
263/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
264/// 'op' is a valid SetCC operation.
265ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
266 unsigned Operation = Op;
267 if (isInteger)
268 Operation ^= 7; // Flip L, G, E bits, but not U.
269 else
270 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000273 Operation &= ~8; // Don't let N and U bits get set.
274
Chris Lattnerc3aae252005-01-07 07:46:32 +0000275 return ISD::CondCode(Operation);
276}
277
278
279/// isSignedOp - For an integer comparison, return 1 if the comparison is a
280/// signed operation and 2 if the result is an unsigned comparison. Return zero
281/// if the operation does not depend on the sign of the input (setne and seteq).
282static int isSignedOp(ISD::CondCode Opcode) {
283 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000284 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000285 case ISD::SETEQ:
286 case ISD::SETNE: return 0;
287 case ISD::SETLT:
288 case ISD::SETLE:
289 case ISD::SETGT:
290 case ISD::SETGE: return 1;
291 case ISD::SETULT:
292 case ISD::SETULE:
293 case ISD::SETUGT:
294 case ISD::SETUGE: return 2;
295 }
296}
297
298/// getSetCCOrOperation - Return the result of a logical OR between different
299/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
300/// returns SETCC_INVALID if it is not possible to represent the resultant
301/// comparison.
302ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
303 bool isInteger) {
304 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
305 // Cannot fold a signed integer setcc with an unsigned integer setcc.
306 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000307
Chris Lattnerc3aae252005-01-07 07:46:32 +0000308 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000309
Chris Lattnerc3aae252005-01-07 07:46:32 +0000310 // If the N and U bits get set then the resultant comparison DOES suddenly
311 // care about orderedness, and is true when ordered.
312 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000313 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000314
Chris Lattnere41102b2006-05-12 17:03:46 +0000315 // Canonicalize illegal integer setcc's.
316 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
317 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000318
Chris Lattnerc3aae252005-01-07 07:46:32 +0000319 return ISD::CondCode(Op);
320}
321
322/// getSetCCAndOperation - Return the result of a logical AND between different
323/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
324/// function returns zero if it is not possible to represent the resultant
325/// comparison.
326ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
327 bool isInteger) {
328 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
329 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000330 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000331
332 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000333 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000334
Chris Lattnera83385f2006-04-27 05:01:07 +0000335 // Canonicalize illegal integer setcc's.
336 if (isInteger) {
337 switch (Result) {
338 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000339 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000340 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000341 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
342 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
343 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000344 }
345 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000346
Chris Lattnera83385f2006-04-27 05:01:07 +0000347 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000348}
349
Jim Laskey58b968b2005-08-17 20:08:02 +0000350//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000351// SDNode Profile Support
352//===----------------------------------------------------------------------===//
353
Jim Laskeydef69b92006-10-27 23:52:51 +0000354/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
355///
Jim Laskey583bd472006-10-27 23:46:08 +0000356static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
357 ID.AddInteger(OpC);
358}
359
360/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
361/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000362static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000363 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000364}
365
Jim Laskeydef69b92006-10-27 23:52:51 +0000366/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
367///
Jim Laskey583bd472006-10-27 23:46:08 +0000368static void AddNodeIDOperands(FoldingSetNodeID &ID,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700369 ArrayRef<SDValue> Ops) {
370 for (auto& Op : Ops) {
371 ID.AddPointer(Op.getNode());
372 ID.AddInteger(Op.getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000373 }
Jim Laskey583bd472006-10-27 23:46:08 +0000374}
375
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000376/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
377///
378static void AddNodeIDOperands(FoldingSetNodeID &ID,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700379 ArrayRef<SDUse> Ops) {
380 for (auto& Op : Ops) {
381 ID.AddPointer(Op.getNode());
382 ID.AddInteger(Op.getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000383 }
384}
385
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700386static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
387 bool exact) {
388 ID.AddBoolean(nuw);
389 ID.AddBoolean(nsw);
390 ID.AddBoolean(exact);
391}
392
393/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
394static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
395 bool nuw, bool nsw, bool exact) {
396 if (isBinOpWithFlags(Opcode))
397 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
398}
399
Stephen Hinesdce4a402014-05-29 02:49:00 -0700400static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
401 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000402 AddNodeIDOpcode(ID, OpC);
403 AddNodeIDValueTypes(ID, VTList);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700404 AddNodeIDOperands(ID, OpList);
Jim Laskey583bd472006-10-27 23:46:08 +0000405}
406
Duncan Sands0dc40452008-10-27 15:30:53 +0000407/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
408/// the NodeID data.
409static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000410 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000411 case ISD::TargetExternalSymbol:
412 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000413 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000414 default: break; // Normal nodes don't need extra info.
415 case ISD::TargetConstant:
Stephen Hines36b56882014-04-23 16:57:46 -0700416 case ISD::Constant: {
417 const ConstantSDNode *C = cast<ConstantSDNode>(N);
418 ID.AddPointer(C->getConstantIntValue());
419 ID.AddBoolean(C->isOpaque());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000420 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700421 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000422 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000423 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000424 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000425 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000426 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000427 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000428 case ISD::GlobalAddress:
429 case ISD::TargetGlobalTLSAddress:
430 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 ID.AddPointer(GA->getGlobal());
433 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000434 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000435 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 break;
437 }
438 case ISD::BasicBlock:
439 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
440 break;
441 case ISD::Register:
442 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
443 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000444 case ISD::RegisterMask:
445 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
446 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000447 case ISD::SRCVALUE:
448 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
449 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000450 case ISD::FrameIndex:
451 case ISD::TargetFrameIndex:
452 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
453 break;
454 case ISD::JumpTable:
455 case ISD::TargetJumpTable:
456 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000457 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000458 break;
459 case ISD::ConstantPool:
460 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000461 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000462 ID.AddInteger(CP->getAlignment());
463 ID.AddInteger(CP->getOffset());
464 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000465 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000466 else
467 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000468 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000469 break;
470 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000471 case ISD::TargetIndex: {
472 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
473 ID.AddInteger(TI->getIndex());
474 ID.AddInteger(TI->getOffset());
475 ID.AddInteger(TI->getTargetFlags());
476 break;
477 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000478 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000479 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000480 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000481 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000482 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000483 break;
484 }
485 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000486 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000487 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000488 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000489 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000490 break;
491 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700492 case ISD::SDIV:
493 case ISD::UDIV:
494 case ISD::SRA:
495 case ISD::SRL:
496 case ISD::MUL:
497 case ISD::ADD:
498 case ISD::SUB:
499 case ISD::SHL: {
500 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
501 AddBinaryNodeIDCustom(ID, N->getOpcode(), BinNode->hasNoUnsignedWrap(),
502 BinNode->hasNoSignedWrap(), BinNode->isExact());
503 break;
504 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000505 case ISD::ATOMIC_CMP_SWAP:
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700506 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000507 case ISD::ATOMIC_SWAP:
508 case ISD::ATOMIC_LOAD_ADD:
509 case ISD::ATOMIC_LOAD_SUB:
510 case ISD::ATOMIC_LOAD_AND:
511 case ISD::ATOMIC_LOAD_OR:
512 case ISD::ATOMIC_LOAD_XOR:
513 case ISD::ATOMIC_LOAD_NAND:
514 case ISD::ATOMIC_LOAD_MIN:
515 case ISD::ATOMIC_LOAD_MAX:
516 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000517 case ISD::ATOMIC_LOAD_UMAX:
518 case ISD::ATOMIC_LOAD:
519 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000520 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000521 ID.AddInteger(AT->getMemoryVT().getRawBits());
522 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000523 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
524 break;
525 }
526 case ISD::PREFETCH: {
527 const MemSDNode *PF = cast<MemSDNode>(N);
528 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000529 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000530 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000531 case ISD::VECTOR_SHUFFLE: {
532 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000533 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000534 i != e; ++i)
535 ID.AddInteger(SVN->getMaskElt(i));
536 break;
537 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000538 case ISD::TargetBlockAddress:
539 case ISD::BlockAddress: {
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000540 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
541 ID.AddPointer(BA->getBlockAddress());
542 ID.AddInteger(BA->getOffset());
543 ID.AddInteger(BA->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000544 break;
545 }
Mon P Wang28873102008-06-25 08:15:39 +0000546 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000547
548 // Target specific memory nodes could also have address spaces to check.
549 if (N->isTargetMemoryOpcode())
550 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000551}
552
Duncan Sands0dc40452008-10-27 15:30:53 +0000553/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
554/// data.
555static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
556 AddNodeIDOpcode(ID, N->getOpcode());
557 // Add the return value info.
558 AddNodeIDValueTypes(ID, N->getVTList());
559 // Add the operand info.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700560 AddNodeIDOperands(ID, N->ops());
Duncan Sands0dc40452008-10-27 15:30:53 +0000561
562 // Handle SDNode leafs with special info.
563 AddNodeIDCustom(ID, N);
564}
565
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000566/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000567/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000568/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000569///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000570static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000571encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000572 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000573 assert((ConvType & 3) == ConvType &&
574 "ConvType may not require more than 2 bits!");
575 assert((AM & 7) == AM &&
576 "AM may not require more than 3 bits!");
577 return ConvType |
578 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000579 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000580 (isNonTemporal << 6) |
581 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000582}
583
Jim Laskey583bd472006-10-27 23:46:08 +0000584//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000585// SelectionDAG Class
586//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000587
Duncan Sands0dc40452008-10-27 15:30:53 +0000588/// doNotCSE - Return true if CSE should not be performed for this node.
589static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000590 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000591 return true; // Never CSE anything that produces a flag.
592
593 switch (N->getOpcode()) {
594 default: break;
595 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000596 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000597 return true; // Never CSE these nodes.
598 }
599
600 // Check that remaining values produced are not flags.
601 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000602 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000603 return true; // Never CSE anything that produces a flag.
604
605 return false;
606}
607
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000608/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000609/// SelectionDAG.
610void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000611 // Create a dummy node (which is not added to allnodes), that adds a reference
612 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000613 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000614
Chris Lattner190a4182006-08-04 17:45:20 +0000615 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000616
Chris Lattner190a4182006-08-04 17:45:20 +0000617 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000618 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000619 if (I->use_empty())
620 DeadNodes.push_back(I);
621
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000622 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000623
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000624 // If the root changed (e.g. it was a dead load, update the root).
625 setRoot(Dummy.getValue());
626}
627
628/// RemoveDeadNodes - This method deletes the unreachable nodes in the
629/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000630void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000631
Chris Lattner190a4182006-08-04 17:45:20 +0000632 // Process the worklist, deleting the nodes and adding their uses to the
633 // worklist.
634 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000635 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000636
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000637 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700638 DUL->NodeDeleted(N, nullptr);
Scott Michelfdc40a02009-02-17 22:15:04 +0000639
Chris Lattner190a4182006-08-04 17:45:20 +0000640 // Take the node out of the appropriate CSE map.
641 RemoveNodeFromCSEMaps(N);
642
643 // Next, brutally remove the operand list. This is safe to do, as there are
644 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000645 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
646 SDUse &Use = *I++;
647 SDNode *Operand = Use.getNode();
648 Use.set(SDValue());
649
Chris Lattner190a4182006-08-04 17:45:20 +0000650 // Now that we removed this operand, see if there are no uses of it left.
651 if (Operand->use_empty())
652 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000653 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000654
Dan Gohmanc5336122009-01-19 22:39:36 +0000655 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000656 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000657}
658
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000659void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000660 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000661
662 // Create a dummy node that adds a reference to the root node, preventing
663 // it from being deleted. (This matters if the root is an operand of the
664 // dead node.)
665 HandleSDNode Dummy(getRoot());
666
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000667 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000668}
669
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000670void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000671 // First take this out of the appropriate CSE map.
672 RemoveNodeFromCSEMaps(N);
673
Scott Michelfdc40a02009-02-17 22:15:04 +0000674 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000675 // AllNodes list, and delete the node.
676 DeleteNodeNotInCSEMaps(N);
677}
678
679void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000680 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
681 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000682
Dan Gohmanf06c8352008-09-30 18:30:35 +0000683 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000684 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000685
Dan Gohmanc5336122009-01-19 22:39:36 +0000686 DeallocateNode(N);
687}
688
689void SelectionDAG::DeallocateNode(SDNode *N) {
690 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000691 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000692
Dan Gohmanc5336122009-01-19 22:39:36 +0000693 // Set the opcode to DELETED_NODE to help catch bugs when node
694 // memory is reallocated.
695 N->NodeType = ISD::DELETED_NODE;
696
Dan Gohman11467282008-08-26 01:44:34 +0000697 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000698
Evan Chengbfcb3052010-03-25 01:38:16 +0000699 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000700 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000701 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
702 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000703}
704
Chris Lattner149c58c2005-08-16 18:17:10 +0000705/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
706/// correspond to it. This is useful when we're about to delete or repurpose
707/// the node. We don't want future request for structurally identical nodes
708/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000709bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000710 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000711 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000712 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000713 case ISD::CONDCODE:
714 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
715 "Cond code doesn't exist!");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700716 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
717 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000718 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000719 case ISD::ExternalSymbol:
720 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000721 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000722 case ISD::TargetExternalSymbol: {
723 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
724 Erased = TargetExternalSymbols.erase(
725 std::pair<std::string,unsigned char>(ESN->getSymbol(),
726 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000727 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000728 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000729 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000730 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000731 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000732 Erased = ExtendedValueTypeNodes.erase(VT);
733 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700734 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
735 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsf411b832007-10-17 13:49:58 +0000736 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000737 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000738 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000739 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000740 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000741 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
742 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000743 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000744 break;
745 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000746#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000747 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000748 // flag result (which cannot be CSE'd) or is one of the special cases that are
749 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000750 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000751 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000752 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000753 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000754 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000755 }
756#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000757 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000758}
759
Dan Gohman39946102009-01-25 16:29:12 +0000760/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
761/// maps and modified in place. Add it back to the CSE maps, unless an identical
762/// node already exists, in which case transfer all its users to the existing
763/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000764///
Dan Gohman39946102009-01-25 16:29:12 +0000765void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000766SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000767 // For node types that aren't CSE'd, just act as if no identical node
768 // already exists.
769 if (!doNotCSE(N)) {
770 SDNode *Existing = CSEMap.GetOrInsertNode(N);
771 if (Existing != N) {
772 // If there was already an existing matching node, use ReplaceAllUsesWith
773 // to replace the dead one with the existing one. This can cause
774 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000775 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000776
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000777 // N is now dead. Inform the listeners and delete it.
778 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
779 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000780 DeleteNodeNotInCSEMaps(N);
781 return;
782 }
783 }
Evan Cheng71e86852008-07-08 20:06:39 +0000784
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000785 // If the node doesn't already exist, we updated it. Inform listeners.
786 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
787 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000788}
789
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000790/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000791/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000792/// return null, otherwise return a pointer to the slot it would take. If a
793/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000794SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000795 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000796 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700797 return nullptr;
Evan Cheng71e86852008-07-08 20:06:39 +0000798
Dan Gohman475871a2008-07-27 21:46:04 +0000799 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000800 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700801 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000802 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000803 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000804 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000805}
806
807/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000808/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000809/// return null, otherwise return a pointer to the slot it would take. If a
810/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000811SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000812 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000813 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000814 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700815 return nullptr;
Duncan Sands0dc40452008-10-27 15:30:53 +0000816
Dan Gohman475871a2008-07-27 21:46:04 +0000817 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000818 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700819 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000820 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000821 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000822 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000823}
824
825
826/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000827/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000828/// return null, otherwise return a pointer to the slot it would take. If a
829/// node already exists with these operands, the slot will be non-null.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700830SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattnera5682852006-08-07 23:03:03 +0000831 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000832 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700833 return nullptr;
Evan Cheng71e86852008-07-08 20:06:39 +0000834
Jim Laskey583bd472006-10-27 23:46:08 +0000835 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700836 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000837 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000838 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000839 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000840}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000841
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000842#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000843/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
844static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000845 switch (N->getOpcode()) {
846 default:
847 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000848 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000849 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000850 assert(N->getNumValues() == 1 && "Too many results!");
851 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
852 "Wrong return type!");
853 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
854 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
855 "Mismatched operand types!");
856 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
857 "Wrong operand type!");
858 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
859 "Wrong return type size");
860 break;
861 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000862 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000863 assert(N->getNumValues() == 1 && "Too many results!");
864 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000865 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000866 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000867 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000868 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000869 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000870 (EltVT.isInteger() && I->getValueType().isInteger() &&
871 EltVT.bitsLE(I->getValueType()))) &&
872 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000873 assert(I->getValueType() == N->getOperand(0).getValueType() &&
874 "Operands must all have the same type");
875 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000876 break;
877 }
878 }
879}
880
Duncan Sands59d2dad2010-11-20 11:25:00 +0000881/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
882static void VerifySDNode(SDNode *N) {
883 // The SDNode allocators cannot be used to allocate nodes with fields that are
884 // not present in an SDNode!
885 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
886 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
887 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
888 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
889 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
890 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
891 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
892 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
893 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
894 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
895 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
896 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
897 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
898 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
899 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
900 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
901 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
902 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
903 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
904
905 VerifyNodeCommon(N);
906}
907
908/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
909/// invalid.
910static void VerifyMachineNode(SDNode *N) {
911 // The MachineNode allocators cannot be used to allocate nodes with fields
912 // that are not present in a MachineNode!
913 // Currently there are no such nodes.
914
915 VerifyNodeCommon(N);
916}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000917#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000918
Owen Andersone50ed302009-08-10 22:56:29 +0000919/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000920/// given type.
921///
Owen Andersone50ed302009-08-10 22:56:29 +0000922unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000923 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000924 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000925 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000926
Bill Wendlingba54bca2013-06-19 21:36:55 +0000927 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000928}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000929
Dale Johannesen92570c42009-02-07 02:15:05 +0000930// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000931SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700932 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TLI(nullptr), OptLevel(OL),
Bill Wendlingba54bca2013-06-19 21:36:55 +0000933 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sandersea28aaf2013-11-15 12:56:49 +0000934 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700935 UpdateListeners(nullptr) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000936 AllNodes.push_back(&EntryNode);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000937 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000938}
939
Stephen Hines36b56882014-04-23 16:57:46 -0700940void SelectionDAG::init(MachineFunction &mf, const TargetLowering *tli) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000941 MF = &mf;
Bill Wendlingee287ca2013-11-22 05:17:44 +0000942 TLI = tli;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000943 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000944}
945
Chris Lattner78ec3112003-08-11 14:57:33 +0000946SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000947 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000948 allnodes_clear();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000949 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000950}
951
952void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000953 assert(&*AllNodes.begin() == &EntryNode);
954 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000955 while (!AllNodes.empty())
956 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000957}
958
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700959BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
960 SDVTList VTs, SDValue N1,
961 SDValue N2, bool nuw, bool nsw,
962 bool exact) {
963 if (isBinOpWithFlags(Opcode)) {
964 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
965 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
966 FN->setHasNoUnsignedWrap(nuw);
967 FN->setHasNoSignedWrap(nsw);
968 FN->setIsExact(exact);
969
970 return FN;
971 }
972
973 BinarySDNode *N = new (NodeAllocator)
974 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
975 return N;
976}
977
Dan Gohman7c3234c2008-08-27 23:52:12 +0000978void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000979 allnodes_clear();
980 OperandAllocator.Reset();
981 CSEMap.clear();
982
983 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000984 ExternalSymbols.clear();
985 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000986 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700987 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmanf350b272008-08-23 02:25:05 +0000988 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700989 static_cast<SDNode*>(nullptr));
Dan Gohmanf350b272008-08-23 02:25:05 +0000990
Stephen Hinesdce4a402014-05-29 02:49:00 -0700991 EntryNode.UseList = nullptr;
Dan Gohmanf350b272008-08-23 02:25:05 +0000992 AllNodes.push_back(&EntryNode);
993 Root = getEntryNode();
Evan Cheng31441b72010-03-29 20:48:30 +0000994 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000995}
996
Andrew Trickac6d9be2013-05-25 02:42:55 +0000997SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotema3c42f32011-09-27 11:16:47 +0000998 return VT.bitsGT(Op.getValueType()) ?
999 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1000 getNode(ISD::TRUNCATE, DL, VT, Op);
1001}
1002
Andrew Trickac6d9be2013-05-25 02:42:55 +00001003SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +00001004 return VT.bitsGT(Op.getValueType()) ?
1005 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1006 getNode(ISD::TRUNCATE, DL, VT, Op);
1007}
1008
Andrew Trickac6d9be2013-05-25 02:42:55 +00001009SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +00001010 return VT.bitsGT(Op.getValueType()) ?
1011 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1012 getNode(ISD::TRUNCATE, DL, VT, Op);
1013}
1014
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001015SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1016 EVT OpVT) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001017 if (VT.bitsLE(Op.getValueType()))
1018 return getNode(ISD::TRUNCATE, SL, VT, Op);
1019
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001020 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001021 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1022}
1023
Andrew Trickac6d9be2013-05-25 02:42:55 +00001024SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +00001025 assert(!VT.isVector() &&
1026 "getZeroExtendInReg should use the vector element type instead of "
1027 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +00001028 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +00001029 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1030 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +00001031 VT.getSizeInBits());
1032 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1033 getConstant(Imm, Op.getValueType()));
1034}
1035
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001036SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1037 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1038 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1039 "The sizes of the input and result must match in order to perform the "
1040 "extend in-register.");
1041 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1042 "The destination vector type must have fewer lanes than the input.");
1043 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1044}
1045
1046SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1047 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1048 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1049 "The sizes of the input and result must match in order to perform the "
1050 "extend in-register.");
1051 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1052 "The destination vector type must have fewer lanes than the input.");
1053 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1054}
1055
1056SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1057 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1058 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1059 "The sizes of the input and result must match in order to perform the "
1060 "extend in-register.");
1061 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1062 "The destination vector type must have fewer lanes than the input.");
1063 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1064}
1065
Bob Wilson4c245462009-01-22 17:39:32 +00001066/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1067///
Andrew Trickac6d9be2013-05-25 02:42:55 +00001068SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001069 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +00001070 SDValue NegOne =
1071 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +00001072 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1073}
1074
Stephen Hinesdce4a402014-05-29 02:49:00 -07001075SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1076 EVT EltVT = VT.getScalarType();
1077 SDValue TrueValue;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001078 switch (TLI->getBooleanContents(VT)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001079 case TargetLowering::ZeroOrOneBooleanContent:
1080 case TargetLowering::UndefinedBooleanContent:
1081 TrueValue = getConstant(1, VT);
1082 break;
1083 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1084 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1085 VT);
1086 break;
1087 }
1088 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1089}
1090
Stephen Hines36b56882014-04-23 16:57:46 -07001091SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001092 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +00001093 assert((EltVT.getSizeInBits() >= 64 ||
1094 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1095 "getConstant with a uint64_t value that doesn't fit in the type!");
Stephen Hines36b56882014-04-23 16:57:46 -07001096 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman6394b092008-02-08 22:59:30 +00001097}
1098
Stephen Hines36b56882014-04-23 16:57:46 -07001099SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1100{
1101 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001102}
1103
Stephen Hines36b56882014-04-23 16:57:46 -07001104SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1105 bool isO) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001106 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +00001107
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001108 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +00001109 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +00001110
Bill Wendlingba54bca2013-06-19 21:36:55 +00001111 const TargetLowering *TLI = TM.getTargetLowering();
1112
Duncan Sands28b77e92011-09-06 19:07:46 +00001113 // In some cases the vector type is legal but the element type is illegal and
1114 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1115 // inserted value (the type does not need to match the vector element type).
1116 // Any extra bits introduced will be truncated away.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001117 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sands28b77e92011-09-06 19:07:46 +00001118 TargetLowering::TypePromoteInteger) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001119 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sands28b77e92011-09-06 19:07:46 +00001120 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1121 Elt = ConstantInt::get(*getContext(), NewVal);
1122 }
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001123 // In other cases the element type is illegal and needs to be expanded, for
1124 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1125 // the value into n parts and use a vector type with n-times the elements.
1126 // Then bitcast to the type requested.
1127 // Legalizing constants too early makes the DAGCombiner's job harder so we
1128 // only legalize if the DAG tells us we must produce legal types.
1129 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1130 TLI->getTypeAction(*getContext(), EltVT) ==
1131 TargetLowering::TypeExpandInteger) {
1132 APInt NewVal = Elt->getValue();
1133 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1134 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1135 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1136 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1137
1138 // Check the temporary vector is the correct size. If this fails then
1139 // getTypeToTransformTo() probably returned a type whose size (in bits)
1140 // isn't a power-of-2 factor of the requested type size.
1141 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1142
1143 SmallVector<SDValue, 2> EltParts;
1144 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1145 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1146 .trunc(ViaEltSizeInBits),
Stephen Hines36b56882014-04-23 16:57:46 -07001147 ViaEltVT, isT, isO));
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001148 }
1149
1150 // EltParts is currently in little endian order. If we actually want
1151 // big-endian order then reverse it now.
1152 if (TLI->isBigEndian())
1153 std::reverse(EltParts.begin(), EltParts.end());
1154
1155 // The elements must be reversed when the element order is different
1156 // to the endianness of the elements (because the BITCAST is itself a
1157 // vector shuffle in this situation). However, we do not need any code to
1158 // perform this reversal because getConstant() is producing a vector
1159 // splat.
1160 // This situation occurs in MIPS MSA.
1161
1162 SmallVector<SDValue, 8> Ops;
1163 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1164 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1165
1166 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1167 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07001168 Ops));
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001169 return Result;
1170 }
Duncan Sands28b77e92011-09-06 19:07:46 +00001171
1172 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1173 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001174 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001175 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001176 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sands28b77e92011-09-06 19:07:46 +00001177 ID.AddPointer(Elt);
Stephen Hines36b56882014-04-23 16:57:46 -07001178 ID.AddBoolean(isO);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001179 void *IP = nullptr;
1180 SDNode *N = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001181 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001182 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001183 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001184
Dan Gohman89081322007-12-12 22:21:26 +00001185 if (!N) {
Stephen Hines36b56882014-04-23 16:57:46 -07001186 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001187 CSEMap.InsertNode(N, IP);
1188 AllNodes.push_back(N);
1189 }
1190
Dan Gohman475871a2008-07-27 21:46:04 +00001191 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001192 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001193 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001194 Ops.assign(VT.getVectorNumElements(), Result);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001195 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman89081322007-12-12 22:21:26 +00001196 }
1197 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001198}
1199
Dan Gohman475871a2008-07-27 21:46:04 +00001200SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001201 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner0bd48932008-01-17 07:00:52 +00001202}
1203
1204
Owen Andersone50ed302009-08-10 22:56:29 +00001205SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001206 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001207}
1208
Owen Andersone50ed302009-08-10 22:56:29 +00001209SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001210 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001211
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001212 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001213
Chris Lattnerd8658612005-02-17 20:17:32 +00001214 // Do the map lookup using the actual bit pattern for the floating point
1215 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1216 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001217 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001218 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001219 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001220 ID.AddPointer(&V);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001221 void *IP = nullptr;
1222 SDNode *N = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001223 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001224 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001225 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001226
Evan Chengc908dcd2007-06-29 21:36:04 +00001227 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001228 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001229 CSEMap.InsertNode(N, IP);
1230 AllNodes.push_back(N);
1231 }
1232
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001234 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001236 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001237 // FIXME SDLoc info might be appropriate here
Stephen Hinesdce4a402014-05-29 02:49:00 -07001238 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7f321562007-06-25 16:23:39 +00001239 }
1240 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241}
1242
Owen Andersone50ed302009-08-10 22:56:29 +00001243SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001244 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001245 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001246 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001247 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001248 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001249 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1250 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001251 bool ignored;
1252 APFloat apf = APFloat(Val);
Tim Northover0a29cb02013-01-22 09:46:31 +00001253 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001254 &ignored);
1255 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001256 } else
1257 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001258}
1259
Andrew Trickac6d9be2013-05-25 02:42:55 +00001260SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001261 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001262 bool isTargetGA,
1263 unsigned char TargetFlags) {
1264 assert((TargetFlags == 0 || isTargetGA) &&
1265 "Cannot set target flags on target-independent globals");
Matt Arsenault94437c92013-11-17 00:06:39 +00001266 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001267
Dan Gohman6520e202008-10-18 02:06:02 +00001268 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault94437c92013-11-17 00:06:39 +00001269 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman6520e202008-10-18 02:06:02 +00001270 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001271 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001272
Chris Lattner2a4ed822009-06-25 21:21:14 +00001273 unsigned Opc;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001274 if (GV->isThreadLocal())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001275 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1276 else
1277 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001278
Jim Laskey583bd472006-10-27 23:46:08 +00001279 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001280 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001281 ID.AddPointer(GV);
1282 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001283 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001284 ID.AddInteger(GV->getType()->getAddressSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001285 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001286 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001287 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001288
Andrew Trickac6d9be2013-05-25 02:42:55 +00001289 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1290 DL.getDebugLoc(), GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001291 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001292 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001293 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001294 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001295}
1296
Owen Andersone50ed302009-08-10 22:56:29 +00001297SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001298 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001299 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001300 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001301 ID.AddInteger(FI);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001302 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001304 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001305
Dan Gohman95531882010-03-18 18:49:47 +00001306 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001307 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001308 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001309 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001310}
1311
Owen Andersone50ed302009-08-10 22:56:29 +00001312SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001313 unsigned char TargetFlags) {
1314 assert((TargetFlags == 0 || isTarget) &&
1315 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001316 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001317 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001318 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001319 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001320 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001321 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001322 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001323 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001324
Dan Gohman95531882010-03-18 18:49:47 +00001325 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1326 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001327 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001328 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001329 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001330}
1331
Dan Gohman46510a72010-04-15 01:51:59 +00001332SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001333 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001334 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001335 unsigned char TargetFlags) {
1336 assert((TargetFlags == 0 || isTarget) &&
1337 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001338 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001339 Alignment =
1340 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001341 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001342 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001343 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001344 ID.AddInteger(Alignment);
1345 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001346 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001347 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001348 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001349 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001350 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001351
Dan Gohman95531882010-03-18 18:49:47 +00001352 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1353 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001354 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001355 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001356 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001357}
1358
Chris Lattnerc3aae252005-01-07 07:46:32 +00001359
Owen Andersone50ed302009-08-10 22:56:29 +00001360SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001361 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001362 bool isTarget,
1363 unsigned char TargetFlags) {
1364 assert((TargetFlags == 0 || isTarget) &&
1365 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001366 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001367 Alignment =
1368 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001369 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001370 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001371 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Chengd6594ae2006-09-12 21:00:35 +00001372 ID.AddInteger(Alignment);
1373 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001374 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001375 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001376 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001377 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001378 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001379
Dan Gohman95531882010-03-18 18:49:47 +00001380 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1381 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001382 CSEMap.InsertNode(N, IP);
1383 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001384 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001385}
1386
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001387SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1388 unsigned char TargetFlags) {
1389 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001390 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001391 ID.AddInteger(Index);
1392 ID.AddInteger(Offset);
1393 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001394 void *IP = nullptr;
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1396 return SDValue(E, 0);
1397
1398 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1399 TargetFlags);
1400 CSEMap.InsertNode(N, IP);
1401 AllNodes.push_back(N);
1402 return SDValue(N, 0);
1403}
1404
Dan Gohman475871a2008-07-27 21:46:04 +00001405SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001406 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001407 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001408 ID.AddPointer(MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001409 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001410 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001411 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001412
Dan Gohman95531882010-03-18 18:49:47 +00001413 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001414 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001415 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001416 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001417}
1418
Owen Andersone50ed302009-08-10 22:56:29 +00001419SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1421 ValueTypeNodes.size())
1422 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001423
Duncan Sands83ec4b62008-06-06 12:08:01 +00001424 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001425 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001426
Dan Gohman475871a2008-07-27 21:46:04 +00001427 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001428 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001429 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001430 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001431}
1432
Owen Andersone50ed302009-08-10 22:56:29 +00001433SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001434 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001435 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001436 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001437 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001438 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001439}
1440
Owen Andersone50ed302009-08-10 22:56:29 +00001441SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001442 unsigned char TargetFlags) {
1443 SDNode *&N =
1444 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1445 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001446 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001447 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001448 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001449 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001450}
1451
Dan Gohman475871a2008-07-27 21:46:04 +00001452SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001453 if ((unsigned)Cond >= CondCodeNodes.size())
1454 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001455
Stephen Hinesdce4a402014-05-29 02:49:00 -07001456 if (!CondCodeNodes[Cond]) {
Dan Gohman95531882010-03-18 18:49:47 +00001457 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001458 CondCodeNodes[Cond] = N;
1459 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001460 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001461
Dan Gohman475871a2008-07-27 21:46:04 +00001462 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001463}
1464
Nate Begeman5a5ca152009-04-29 05:20:52 +00001465// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1466// the shuffle mask M that point at N1 to point at N2, and indices that point
1467// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001468static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1469 std::swap(N1, N2);
1470 int NElts = M.size();
1471 for (int i = 0; i != NElts; ++i) {
1472 if (M[i] >= NElts)
1473 M[i] -= NElts;
1474 else if (M[i] >= 0)
1475 M[i] += NElts;
1476 }
1477}
1478
Andrew Trickac6d9be2013-05-25 02:42:55 +00001479SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001480 SDValue N2, const int *Mask) {
Craig Topperad445a62013-08-09 04:37:24 +00001481 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1482 "Invalid VECTOR_SHUFFLE");
Nate Begeman9008ca62009-04-27 18:41:29 +00001483
1484 // Canonicalize shuffle undef, undef -> undef
1485 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001486 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001487
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001488 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001489 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001490 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001491 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001492 for (unsigned i = 0; i != NElts; ++i) {
1493 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001494 MaskVec.push_back(Mask[i]);
1495 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001496
Nate Begeman9008ca62009-04-27 18:41:29 +00001497 // Canonicalize shuffle v, v -> v, undef
1498 if (N1 == N2) {
1499 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001500 for (unsigned i = 0; i != NElts; ++i)
1501 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001502 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001503
Nate Begeman9008ca62009-04-27 18:41:29 +00001504 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1505 if (N1.getOpcode() == ISD::UNDEF)
1506 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001507
Nate Begeman9008ca62009-04-27 18:41:29 +00001508 // Canonicalize all index into lhs, -> shuffle lhs, undef
1509 // Canonicalize all index into rhs, -> shuffle rhs, undef
1510 bool AllLHS = true, AllRHS = true;
1511 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001512 for (unsigned i = 0; i != NElts; ++i) {
1513 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001514 if (N2Undef)
1515 MaskVec[i] = -1;
1516 else
1517 AllLHS = false;
1518 } else if (MaskVec[i] >= 0) {
1519 AllRHS = false;
1520 }
1521 }
1522 if (AllLHS && AllRHS)
1523 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001524 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001525 N2 = getUNDEF(VT);
1526 if (AllRHS) {
1527 N1 = getUNDEF(VT);
1528 commuteShuffle(N1, N2, MaskVec);
1529 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001530 // Reset our undef status after accounting for the mask.
1531 N2Undef = N2.getOpcode() == ISD::UNDEF;
1532 // Re-check whether both sides ended up undef.
1533 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1534 return getUNDEF(VT);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001535
Craig Toppereee2a112013-08-08 08:03:12 +00001536 // If Identity shuffle return that node.
Nate Begeman9008ca62009-04-27 18:41:29 +00001537 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001538 for (unsigned i = 0; i != NElts; ++i) {
1539 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001540 }
Craig Topperad445a62013-08-09 04:37:24 +00001541 if (Identity && NElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00001542 return N1;
Nate Begeman9008ca62009-04-27 18:41:29 +00001543
Stephen Hinesdce4a402014-05-29 02:49:00 -07001544 // Shuffling a constant splat doesn't change the result.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001545 if (N2Undef) {
1546 SDValue V = N1;
1547
1548 // Look through any bitcasts. We check that these don't change the number
1549 // (and size) of elements and just changes their types.
1550 while (V.getOpcode() == ISD::BITCAST)
1551 V = V->getOperand(0);
1552
1553 // A splat should always show up as a build vector node.
1554 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1555 BitVector UndefElements;
1556 SDValue Splat = BV->getSplatValue(&UndefElements);
1557 // If this is a splat of an undef, shuffling it is also undef.
1558 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1559 return getUNDEF(VT);
1560
1561 // We only have a splat which can skip shuffles if there is a splatted
1562 // value and no undef lanes rearranged by the shuffle.
1563 if (Splat && UndefElements.none()) {
1564 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1565 // number of elements match or the value splatted is a zero constant.
1566 if (V.getValueType().getVectorNumElements() ==
1567 VT.getVectorNumElements())
1568 return N1;
1569 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1570 if (C->isNullValue())
1571 return N1;
1572 }
1573 }
1574 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001575
Nate Begeman9008ca62009-04-27 18:41:29 +00001576 FoldingSetNodeID ID;
1577 SDValue Ops[2] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001578 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001579 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001580 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001581
Stephen Hinesdce4a402014-05-29 02:49:00 -07001582 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001583 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001584 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001585
Nate Begeman9008ca62009-04-27 18:41:29 +00001586 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1587 // SDNode doesn't have access to it. This memory will be "leaked" when
1588 // the node is deallocated, but recovered when the NodeAllocator is released.
1589 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1590 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001591
Dan Gohman95531882010-03-18 18:49:47 +00001592 ShuffleVectorSDNode *N =
Jack Carter3af4d252013-09-09 22:02:08 +00001593 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1594 dl.getDebugLoc(), N1, N2,
1595 MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001596 CSEMap.InsertNode(N, IP);
1597 AllNodes.push_back(N);
1598 return SDValue(N, 0);
1599}
1600
Andrew Trickac6d9be2013-05-25 02:42:55 +00001601SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001602 SDValue Val, SDValue DTy,
1603 SDValue STy, SDValue Rnd, SDValue Sat,
1604 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001605 // If the src and dest types are the same and the conversion is between
1606 // integer types of the same sign or two floats, no conversion is necessary.
1607 if (DTy == STy &&
1608 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001609 return Val;
1610
1611 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001612 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001613 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
1614 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001616 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001617
Jack Carter3af4d252013-09-09 22:02:08 +00001618 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1619 dl.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07001620 Ops, Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001621 CSEMap.InsertNode(N, IP);
1622 AllNodes.push_back(N);
1623 return SDValue(N, 0);
1624}
1625
Owen Andersone50ed302009-08-10 22:56:29 +00001626SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001627 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001628 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001629 ID.AddInteger(RegNo);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001630 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001631 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001632 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001633
Dan Gohman95531882010-03-18 18:49:47 +00001634 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001635 CSEMap.InsertNode(N, IP);
1636 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001637 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001638}
1639
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001640SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1641 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001642 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001643 ID.AddPointer(RegMask);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001644 void *IP = nullptr;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001645 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1646 return SDValue(E, 0);
1647
1648 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1649 CSEMap.InsertNode(N, IP);
1650 AllNodes.push_back(N);
1651 return SDValue(N, 0);
1652}
1653
Andrew Trickac6d9be2013-05-25 02:42:55 +00001654SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001655 FoldingSetNodeID ID;
1656 SDValue Ops[] = { Root };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001657 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattner7561d482010-03-14 02:33:54 +00001658 ID.AddPointer(Label);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001659 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001661 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001662
Jack Carter3af4d252013-09-09 22:02:08 +00001663 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1664 dl.getDebugLoc(), Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001665 CSEMap.InsertNode(N, IP);
1666 AllNodes.push_back(N);
1667 return SDValue(N, 0);
1668}
1669
Chris Lattner7561d482010-03-14 02:33:54 +00001670
Dan Gohman46510a72010-04-15 01:51:59 +00001671SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001672 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001673 bool isTarget,
1674 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001675 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1676
1677 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001678 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001679 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001680 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001681 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001682 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001683 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001684 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001685
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001686 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1687 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001688 CSEMap.InsertNode(N, IP);
1689 AllNodes.push_back(N);
1690 return SDValue(N, 0);
1691}
1692
Dan Gohman475871a2008-07-27 21:46:04 +00001693SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001694 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001695 "SrcValue is not a pointer?");
1696
Jim Laskey583bd472006-10-27 23:46:08 +00001697 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001698 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001699 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001700
Stephen Hinesdce4a402014-05-29 02:49:00 -07001701 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001702 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001703 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001704
Dan Gohman95531882010-03-18 18:49:47 +00001705 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001706 CSEMap.InsertNode(N, IP);
1707 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001708 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001709}
1710
Chris Lattnerdecc2672010-04-07 05:20:54 +00001711/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1712SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1713 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001714 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattnerdecc2672010-04-07 05:20:54 +00001715 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001716
Stephen Hinesdce4a402014-05-29 02:49:00 -07001717 void *IP = nullptr;
Chris Lattnerdecc2672010-04-07 05:20:54 +00001718 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1719 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001720
Chris Lattnerdecc2672010-04-07 05:20:54 +00001721 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1722 CSEMap.InsertNode(N, IP);
1723 AllNodes.push_back(N);
1724 return SDValue(N, 0);
1725}
1726
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001727/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1728SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1729 unsigned SrcAS, unsigned DestAS) {
1730 SDValue Ops[] = {Ptr};
1731 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001732 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001733 ID.AddInteger(SrcAS);
1734 ID.AddInteger(DestAS);
1735
Stephen Hinesdce4a402014-05-29 02:49:00 -07001736 void *IP = nullptr;
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001737 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1738 return SDValue(E, 0);
1739
1740 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1741 dl.getDebugLoc(),
1742 VT, Ptr, SrcAS, DestAS);
1743 CSEMap.InsertNode(N, IP);
1744 AllNodes.push_back(N);
1745 return SDValue(N, 0);
1746}
Chris Lattnerdecc2672010-04-07 05:20:54 +00001747
Duncan Sands92abc622009-01-31 15:50:11 +00001748/// getShiftAmountOperand - Return the specified value casted to
1749/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001750SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001751 EVT OpTy = Op.getValueType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001752 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001753 if (OpTy == ShTy || OpTy.isVector()) return Op;
1754
1755 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001756 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001757}
1758
Chris Lattner37ce9df2007-10-15 17:47:20 +00001759/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1760/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001761SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001762 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001763 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001764 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001765 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang364d73d2008-07-05 20:40:31 +00001766 unsigned StackAlign =
Bill Wendlingba54bca2013-06-19 21:36:55 +00001767 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001768
David Greene3f2bf852009-11-12 20:49:22 +00001769 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001770 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner37ce9df2007-10-15 17:47:20 +00001771}
1772
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001773/// CreateStackTemporary - Create a stack temporary suitable for holding
1774/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001775SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001776 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1777 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001778 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1779 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001780 const TargetLowering *TLI = TM.getTargetLowering();
1781 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001782 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1783 TD->getPrefTypeAlignment(Ty2));
1784
1785 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001786 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001787 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001788}
1789
Owen Andersone50ed302009-08-10 22:56:29 +00001790SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001791 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001792 // These setcc operations always fold.
1793 switch (Cond) {
1794 default: break;
1795 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001796 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001797 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001798 case ISD::SETTRUE2: {
1799 const TargetLowering *TLI = TM.getTargetLowering();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001800 TargetLowering::BooleanContent Cnt =
1801 TLI->getBooleanContents(N1->getValueType(0));
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001802 return getConstant(
1803 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1804 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001805
Chris Lattnera83385f2006-04-27 05:01:07 +00001806 case ISD::SETOEQ:
1807 case ISD::SETOGT:
1808 case ISD::SETOGE:
1809 case ISD::SETOLT:
1810 case ISD::SETOLE:
1811 case ISD::SETONE:
1812 case ISD::SETO:
1813 case ISD::SETUO:
1814 case ISD::SETUEQ:
1815 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001816 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001817 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001818 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001819
Gabor Greifba36cb52008-08-28 21:40:38 +00001820 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001821 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001822 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001823 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001824
Chris Lattnerc3aae252005-01-07 07:46:32 +00001825 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001826 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001827 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1828 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001829 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1830 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1831 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1832 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1833 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1834 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1835 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1836 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001837 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001838 }
Chris Lattner67255a12005-04-07 18:14:58 +00001839 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001840 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1841 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001842 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001843 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001844 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001845 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001846 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001847 // fall through
1848 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001849 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001850 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001851 // fall through
1852 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001853 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001854 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001855 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001856 // fall through
1857 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001858 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001859 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001860 // fall through
1861 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001862 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001863 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001864 // fall through
1865 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001866 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001867 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001868 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001869 // fall through
1870 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001871 R==APFloat::cmpEqual, VT);
1872 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1873 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1874 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1875 R==APFloat::cmpEqual, VT);
1876 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1877 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1878 R==APFloat::cmpLessThan, VT);
1879 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1880 R==APFloat::cmpUnordered, VT);
1881 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1882 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001883 }
1884 } else {
1885 // Ensure that the constant occurs on the RHS.
Tom Stellard12d43f92013-09-28 02:50:38 +00001886 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1887 MVT CompVT = N1.getValueType().getSimpleVT();
1888 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1889 return SDValue();
1890
1891 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001892 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001893 }
1894
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001895 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001896 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001897}
1898
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001899/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1900/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001901bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001902 // This predicate is not safe for vector operations.
1903 if (Op.getValueType().isVector())
1904 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001905
Dan Gohman87862e72009-12-11 21:31:27 +00001906 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001907 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1908}
1909
Dan Gohmanea859be2007-06-22 14:59:07 +00001910/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1911/// this predicate to simplify operations downstream. Mask is known to be zero
1912/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001913bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001915 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001916 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001917 return (KnownZero & Mask) == Mask;
1918}
1919
Stephen Hinesdce4a402014-05-29 02:49:00 -07001920/// Determine which bits of Op are known to be either zero or one and return
1921/// them in the KnownZero/KnownOne bitsets.
1922void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1923 APInt &KnownOne, unsigned Depth) const {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001924 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001925 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001926
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001927 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001928 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001929 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001930
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001931 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001932
1933 switch (Op.getOpcode()) {
1934 case ISD::Constant:
1935 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001936 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1937 KnownZero = ~KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001938 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001939 case ISD::AND:
1940 // If either the LHS or the RHS are Zero, the result is zero.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001941 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1942 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001943
1944 // Output known-1 bits are only known if set in both the LHS & RHS.
1945 KnownOne &= KnownOne2;
1946 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1947 KnownZero |= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001948 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001949 case ISD::OR:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001950 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1951 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001952
Dan Gohmanea859be2007-06-22 14:59:07 +00001953 // Output known-0 bits are only known if clear in both the LHS & RHS.
1954 KnownZero &= KnownZero2;
1955 // Output known-1 are known to be set if set in either the LHS | RHS.
1956 KnownOne |= KnownOne2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001957 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001958 case ISD::XOR: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001959 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1960 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001961
Dan Gohmanea859be2007-06-22 14:59:07 +00001962 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001963 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001964 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1965 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1966 KnownZero = KnownZeroOut;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001967 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001968 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001969 case ISD::MUL: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001970 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1971 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001972
1973 // If low bits are zero in either operand, output low known-0 bits.
1974 // Also compute a conserative estimate for high known-0 bits.
1975 // More trickiness is possible, but this is sufficient for the
1976 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001977 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001978 unsigned TrailZ = KnownZero.countTrailingOnes() +
1979 KnownZero2.countTrailingOnes();
1980 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001981 KnownZero2.countLeadingOnes(),
1982 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001983
1984 TrailZ = std::min(TrailZ, BitWidth);
1985 LeadZ = std::min(LeadZ, BitWidth);
1986 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1987 APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001988 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00001989 }
1990 case ISD::UDIV: {
1991 // For the purposes of computing leading zeros we can conservatively
1992 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001993 // be less than the denominator.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001994 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001995 unsigned LeadZ = KnownZero2.countLeadingOnes();
1996
Jay Foad7a874dd2010-12-01 08:53:58 +00001997 KnownOne2.clearAllBits();
1998 KnownZero2.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001999 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00002000 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2001 if (RHSUnknownLeadingOnes != BitWidth)
2002 LeadZ = std::min(BitWidth,
2003 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002004
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002005 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002006 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002007 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 case ISD::SELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07002009 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2010 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Dan Gohmanea859be2007-06-22 14:59:07 +00002012 // Only known if known in both the LHS and RHS.
2013 KnownOne &= KnownOne2;
2014 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002015 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002016 case ISD::SELECT_CC:
Stephen Hinesdce4a402014-05-29 02:49:00 -07002017 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2018 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002019
Dan Gohmanea859be2007-06-22 14:59:07 +00002020 // Only known if known in both the LHS and RHS.
2021 KnownOne &= KnownOne2;
2022 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002023 break;
Bill Wendling253174b2008-11-22 07:24:01 +00002024 case ISD::SADDO:
2025 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002026 case ISD::SSUBO:
2027 case ISD::USUBO:
2028 case ISD::SMULO:
2029 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002030 if (Op.getResNo() != 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002031 break;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002032 // The boolean result conforms to getBooleanContents.
2033 // If we know the result of a setcc has the top bits zero, use this info.
2034 // We know that we have an integer-based boolean since these operations
2035 // are only available for integer.
2036 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2037 TargetLowering::ZeroOrOneBooleanContent &&
2038 BitWidth > 1)
2039 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2040 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002041 case ISD::SETCC:
2042 // If we know the result of a setcc has the top bits zero, use this info.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002043 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2044 TargetLowering::ZeroOrOneBooleanContent &&
2045 BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002046 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002047 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002048 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002049 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002050 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002051 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00002052
2053 // If the shift count is an invalid immediate, don't do anything.
2054 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002055 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002056
Stephen Hinesdce4a402014-05-29 02:49:00 -07002057 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmand4cf9922008-02-26 18:50:50 +00002058 KnownZero <<= ShAmt;
2059 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002060 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00002061 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002062 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002063 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002064 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002065 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002066 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002067 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002068
Dan Gohmand4cf9922008-02-26 18:50:50 +00002069 // If the shift count is an invalid immediate, don't do anything.
2070 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002071 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002072
Stephen Hinesdce4a402014-05-29 02:49:00 -07002073 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002074 KnownZero = KnownZero.lshr(ShAmt);
2075 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002076
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002077 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002078 KnownZero |= HighBits; // High bits known zero.
2079 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002080 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002081 case ISD::SRA:
2082 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002083 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002084
Dan Gohmand4cf9922008-02-26 18:50:50 +00002085 // If the shift count is an invalid immediate, don't do anything.
2086 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002087 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002088
Dan Gohmanea859be2007-06-22 14:59:07 +00002089 // If any of the demanded bits are produced by the sign extension, we also
2090 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002091 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Stephen Hinesdce4a402014-05-29 02:49:00 -07002093 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002094 KnownZero = KnownZero.lshr(ShAmt);
2095 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002096
Dan Gohmanea859be2007-06-22 14:59:07 +00002097 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002098 APInt SignBit = APInt::getSignBit(BitWidth);
2099 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00002100
Dan Gohmanca93a432008-02-20 16:30:17 +00002101 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002102 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00002103 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002104 KnownOne |= HighBits; // New bits are known one.
2105 }
2106 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002107 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002108 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002109 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002110 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002111
2112 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00002113 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002114 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002115
Dan Gohman977a76f2008-02-13 22:28:48 +00002116 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002117 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002118
Dan Gohmanea859be2007-06-22 14:59:07 +00002119 // If the sign extended bits are demanded, we know that the sign
2120 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00002121 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00002122 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002123 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00002124
Stephen Hinesdce4a402014-05-29 02:49:00 -07002125 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002126 KnownOne &= InputDemandedBits;
2127 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002128
Dan Gohmanea859be2007-06-22 14:59:07 +00002129 // If the sign bit of the input is known set or clear, then we know the
2130 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00002131 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00002132 KnownZero |= NewBits;
2133 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00002134 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00002135 KnownOne |= NewBits;
2136 KnownZero &= ~NewBits;
2137 } else { // Input sign bit unknown
2138 KnownZero &= ~NewBits;
2139 KnownOne &= ~NewBits;
2140 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002141 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002142 }
2143 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002144 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002145 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002146 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002147 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002148 unsigned LowBits = Log2_32(BitWidth)+1;
2149 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00002150 KnownOne.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07002151 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002152 }
2153 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00002154 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00002155 // If this is a ZEXTLoad and we are looking at the loaded value.
2156 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00002157 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002158 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002159 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00002160 } else if (const MDNode *Ranges = LD->getRanges()) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002161 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002162 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002163 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002164 }
2165 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002166 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002167 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002168 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00002169 KnownZero = KnownZero.trunc(InBits);
2170 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002171 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002172 KnownZero = KnownZero.zext(BitWidth);
2173 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002174 KnownZero |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002175 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002176 }
2177 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002178 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002179 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002180 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002181
Jay Foad40f8f622010-12-07 08:25:19 +00002182 KnownZero = KnownZero.trunc(InBits);
2183 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002184 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00002185
2186 // Note if the sign bit is known to be zero or one.
2187 bool SignBitKnownZero = KnownZero.isNegative();
2188 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohman977a76f2008-02-13 22:28:48 +00002189
Jay Foad40f8f622010-12-07 08:25:19 +00002190 KnownZero = KnownZero.zext(BitWidth);
2191 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002192
Dan Gohman977a76f2008-02-13 22:28:48 +00002193 // If the sign bit is known zero or one, the top bits match.
2194 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00002195 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00002196 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00002197 KnownOne |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002198 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002199 }
2200 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00002201 EVT InVT = Op.getOperand(0).getValueType();
2202 unsigned InBits = InVT.getScalarType().getSizeInBits();
2203 KnownZero = KnownZero.trunc(InBits);
2204 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002205 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng2766a472012-12-06 19:13:27 +00002206 KnownZero = KnownZero.zext(BitWidth);
2207 KnownOne = KnownOne.zext(BitWidth);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002208 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002209 }
2210 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00002211 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002212 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00002213 KnownZero = KnownZero.zext(InBits);
2214 KnownOne = KnownOne.zext(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002215 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002216 KnownZero = KnownZero.trunc(BitWidth);
2217 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002218 break;
2219 }
2220 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002221 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002222 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07002223 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002224 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002225 KnownOne &= (~KnownZero);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002226 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002227 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002228 case ISD::FGETSIGN:
2229 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002230 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002231 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002232
Dan Gohman23e8b712008-04-28 17:02:21 +00002233 case ISD::SUB: {
2234 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2235 // We know that the top bits of C-X are clear if X contains less bits
2236 // than C (i.e. no wrap-around can happen). For example, 20-X is
2237 // positive if we can prove that X is >= 0 and < 16.
2238 if (CLHS->getAPIntValue().isNonNegative()) {
2239 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2240 // NLZ can't be BitWidth with no sign bit
2241 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002242 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002243
2244 // If all of the MaskV bits are known to be zero, then we know the
2245 // output top bits are zero, because we now know that the output is
2246 // from [0-C].
2247 if ((KnownZero2 & MaskV) == MaskV) {
2248 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2249 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002250 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002251 }
2252 }
2253 }
2254 }
2255 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002256 case ISD::ADD:
2257 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 // Output known-0 bits are known if clear or set in both the low clear bits
2259 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2260 // low 3 bits clear.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002261 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002262 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2263
Stephen Hinesdce4a402014-05-29 02:49:00 -07002264 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002265 KnownZeroOut = std::min(KnownZeroOut,
2266 KnownZero2.countTrailingOnes());
2267
Chris Lattnerda605882010-12-19 20:38:28 +00002268 if (Op.getOpcode() == ISD::ADD) {
2269 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002270 break;
Chris Lattnerda605882010-12-19 20:38:28 +00002271 }
2272
2273 // With ADDE, a carry bit may be added in, so we can only use this
2274 // information if we know (at least) that the low two bits are clear. We
2275 // then return to the caller that the low bit is unknown but that other bits
2276 // are known zero.
2277 if (KnownZeroOut >= 2) // ADDE
2278 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002279 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002280 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002281 case ISD::SREM:
2282 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002283 const APInt &RA = Rem->getAPIntValue().abs();
2284 if (RA.isPowerOf2()) {
2285 APInt LowBits = RA - 1;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002286 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002287
Duncan Sands5c2873a2010-01-29 09:45:26 +00002288 // The low bits of the first operand are unchanged by the srem.
2289 KnownZero = KnownZero2 & LowBits;
2290 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002291
Duncan Sands5c2873a2010-01-29 09:45:26 +00002292 // If the first operand is non-negative or has all low bits zero, then
2293 // the upper bits are all zero.
2294 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2295 KnownZero |= ~LowBits;
2296
2297 // If the first operand is negative and not all low bits are zero, then
2298 // the upper bits are all one.
2299 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2300 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002301 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002302 }
2303 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002304 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002305 case ISD::UREM: {
2306 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002307 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002308 if (RA.isPowerOf2()) {
2309 APInt LowBits = (RA - 1);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002310 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2311
2312 // The upper bits are all zero, the lower ones are unchanged.
2313 KnownZero = KnownZero2 | ~LowBits;
2314 KnownOne = KnownOne2 & LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002315 break;
2316 }
2317 }
2318
2319 // Since the result is less than or equal to either operand, any leading
2320 // zero bits in either operand must also exist in the result.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002321 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2322 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002323
2324 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2325 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002326 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002327 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002328 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002329 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002330 case ISD::FrameIndex:
2331 case ISD::TargetFrameIndex:
2332 if (unsigned Align = InferPtrAlignment(Op)) {
2333 // The low bits are known zero if the pointer is aligned.
2334 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002335 break;
Chris Lattner0a9481f2011-02-13 22:25:43 +00002336 }
2337 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002338
Dan Gohmanea859be2007-06-22 14:59:07 +00002339 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002340 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2341 break;
2342 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002343 case ISD::INTRINSIC_WO_CHAIN:
2344 case ISD::INTRINSIC_W_CHAIN:
2345 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002346 // Allow the target to implement this method for its nodes.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002347 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
2348 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002349 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002350
2351 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002352}
2353
2354/// ComputeNumSignBits - Return the number of times the sign bit of the
2355/// register is replicated into the other bits. We know that at least 1 bit
2356/// is always equal to the sign bit (itself), but other cases can give us
2357/// information. For example, immediately after an "SRA X, 2", we know that
2358/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002359unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlingba54bca2013-06-19 21:36:55 +00002360 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00002361 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002362 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002363 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002364 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002365 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Dan Gohmanea859be2007-06-22 14:59:07 +00002367 if (Depth == 6)
2368 return 1; // Limit search depth.
2369
2370 switch (Op.getOpcode()) {
2371 default: break;
2372 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002373 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002374 return VTBits-Tmp+1;
2375 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002376 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002377 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002378
Dan Gohmanea859be2007-06-22 14:59:07 +00002379 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002380 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002381 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002382 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002383
Dan Gohmanea859be2007-06-22 14:59:07 +00002384 case ISD::SIGN_EXTEND:
Jack Carter3af4d252013-09-09 22:02:08 +00002385 Tmp =
2386 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002387 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002388
Dan Gohmanea859be2007-06-22 14:59:07 +00002389 case ISD::SIGN_EXTEND_INREG:
2390 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002391 Tmp =
2392 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002393 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002394
Dan Gohmanea859be2007-06-22 14:59:07 +00002395 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2396 return std::max(Tmp, Tmp2);
2397
2398 case ISD::SRA:
2399 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2400 // SRA X, C -> adds C sign bits.
2401 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002402 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002403 if (Tmp > VTBits) Tmp = VTBits;
2404 }
2405 return Tmp;
2406 case ISD::SHL:
2407 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2408 // shl destroys sign bits.
2409 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002410 if (C->getZExtValue() >= VTBits || // Bad shift.
2411 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2412 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002413 }
2414 break;
2415 case ISD::AND:
2416 case ISD::OR:
2417 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002418 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002419 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002420 if (Tmp != 1) {
2421 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2422 FirstAnswer = std::min(Tmp, Tmp2);
2423 // We computed what we know about the sign bits as our first
2424 // answer. Now proceed to the generic code that uses
Stephen Hinesdce4a402014-05-29 02:49:00 -07002425 // computeKnownBits, and pick whichever answer is better.
Dan Gohmana332f172008-05-23 02:28:01 +00002426 }
2427 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002428
2429 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002430 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002431 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002432 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002433 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002434
2435 case ISD::SADDO:
2436 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002437 case ISD::SSUBO:
2438 case ISD::USUBO:
2439 case ISD::SMULO:
2440 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002441 if (Op.getResNo() != 1)
2442 break;
Duncan Sands03228082008-11-23 15:47:28 +00002443 // The boolean result conforms to getBooleanContents. Fall through.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002444 // If setcc returns 0/-1, all bits are sign bits.
2445 // We know that we have an integer-based boolean since these operations
2446 // are only available for integer.
2447 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2448 TargetLowering::ZeroOrNegativeOneBooleanContent)
2449 return VTBits;
2450 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002451 case ISD::SETCC:
2452 // If setcc returns 0/-1, all bits are sign bits.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002453 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002454 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002455 return VTBits;
2456 break;
2457 case ISD::ROTL:
2458 case ISD::ROTR:
2459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002460 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002461
Dan Gohmanea859be2007-06-22 14:59:07 +00002462 // Handle rotate right by N like a rotate left by 32-N.
2463 if (Op.getOpcode() == ISD::ROTR)
2464 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2465
2466 // If we aren't rotating out all of the known-in sign bits, return the
2467 // number that are left. This handles rotl(sext(x), 1) for example.
2468 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2469 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2470 }
2471 break;
2472 case ISD::ADD:
2473 // Add can have at most one carry bit. Thus we know that the output
2474 // is, at worst, one more bit than the inputs.
2475 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2476 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002477
Dan Gohmanea859be2007-06-22 14:59:07 +00002478 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002479 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002480 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002481 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002482 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002483
Dan Gohmanea859be2007-06-22 14:59:07 +00002484 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2485 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002486 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002487 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002488
Dan Gohmanea859be2007-06-22 14:59:07 +00002489 // If we are subtracting one from a positive number, there is no carry
2490 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002491 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002492 return Tmp;
2493 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002494
Dan Gohmanea859be2007-06-22 14:59:07 +00002495 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2496 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002497 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002498
Dan Gohmanea859be2007-06-22 14:59:07 +00002499 case ISD::SUB:
2500 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2501 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002502
Dan Gohmanea859be2007-06-22 14:59:07 +00002503 // Handle NEG.
2504 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002505 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002506 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002507 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002508 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2509 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002510 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002511 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002512
Dan Gohmanea859be2007-06-22 14:59:07 +00002513 // If the input is known to be positive (the sign bit is known clear),
2514 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002515 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002516 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002517
Dan Gohmanea859be2007-06-22 14:59:07 +00002518 // Otherwise, we treat this like a SUB.
2519 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002520
Dan Gohmanea859be2007-06-22 14:59:07 +00002521 // Sub can have at most one carry bit. Thus we know that the output
2522 // is, at worst, one more bit than the inputs.
2523 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2524 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002525 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002526 case ISD::TRUNCATE:
2527 // FIXME: it's tricky to do anything useful for this, but it is an important
2528 // case for targets like X86.
2529 break;
2530 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002531
Nadav Rotem77451752013-03-20 22:53:44 +00002532 // If we are looking at the loaded value of the SDNode.
2533 if (Op.getResNo() == 0) {
2534 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2535 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2536 unsigned ExtType = LD->getExtensionType();
2537 switch (ExtType) {
2538 default: break;
2539 case ISD::SEXTLOAD: // '17' bits known
2540 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2541 return VTBits-Tmp+1;
2542 case ISD::ZEXTLOAD: // '16' bits known
2543 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2544 return VTBits-Tmp;
2545 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002546 }
2547 }
2548
2549 // Allow the target to implement this method for its nodes.
2550 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002551 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002552 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2553 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002554 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002555 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002557
Dan Gohmanea859be2007-06-22 14:59:07 +00002558 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2559 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002560 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002561 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002562
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002563 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002564 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002565 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002566 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002567 Mask = KnownOne;
2568 } else {
2569 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002570 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002571 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002572
Dan Gohmanea859be2007-06-22 14:59:07 +00002573 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2574 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002575 Mask = ~Mask;
2576 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002577 // Return # leading zeros. We use 'min' here in case Val was zero before
2578 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002579 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002580}
2581
Chris Lattner0a9481f2011-02-13 22:25:43 +00002582/// isBaseWithConstantOffset - Return true if the specified operand is an
2583/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2584/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2585/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002586/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002587bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2588 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2589 !isa<ConstantSDNode>(Op.getOperand(1)))
2590 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002591
2592 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002593 !MaskedValueIsZero(Op.getOperand(0),
2594 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2595 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002596
Chris Lattner0a9481f2011-02-13 22:25:43 +00002597 return true;
2598}
2599
2600
Dan Gohman8d44b282009-09-03 20:34:31 +00002601bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2602 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002603 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002604 return true;
2605
2606 // If the value is a constant, we can obviously see if it is a NaN or not.
2607 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2608 return !C->getValueAPF().isNaN();
2609
2610 // TODO: Recognize more cases here.
2611
2612 return false;
2613}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002614
Dan Gohmane8326932010-02-24 06:52:40 +00002615bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2616 // If the value is a constant, we can obviously see if it is a zero or not.
2617 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2618 return !C->isZero();
2619
2620 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002621 switch (Op.getOpcode()) {
2622 default: break;
2623 case ISD::OR:
2624 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2625 return !C->isNullValue();
2626 break;
2627 }
Dan Gohmane8326932010-02-24 06:52:40 +00002628
2629 return false;
2630}
2631
2632bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2633 // Check the obvious case.
2634 if (A == B) return true;
2635
2636 // For for negative and positive zero.
2637 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2638 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2639 if (CA->isZero() && CB->isZero()) return true;
2640
2641 // Otherwise they may not be equal.
2642 return false;
2643}
2644
Chris Lattnerc3aae252005-01-07 07:46:32 +00002645/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002646///
Andrew Trickac6d9be2013-05-25 02:42:55 +00002647SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002648 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002649 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
2650 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002651 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002652 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002653
Jack Carter3af4d252013-09-09 22:02:08 +00002654 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2655 DL.getDebugLoc(), getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002656 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002657
Chris Lattner4a283e92006-08-11 18:38:11 +00002658 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002659#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002660 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002661#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002662 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002663}
2664
Andrew Trickac6d9be2013-05-25 02:42:55 +00002665SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002666 EVT VT, SDValue Operand) {
Stephen Hines36b56882014-04-23 16:57:46 -07002667 // Constant fold unary operations with an integer constant operand. Even
2668 // opaque constant will be folded, because the folding of unary operations
2669 // doesn't create new constants with different values. Nevertheless, the
2670 // opaque flag is preserved during folding to prevent future folding with
2671 // other constants.
Gabor Greifba36cb52008-08-28 21:40:38 +00002672 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002673 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002674 switch (Opcode) {
2675 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002676 case ISD::SIGN_EXTEND:
Stephen Hines36b56882014-04-23 16:57:46 -07002677 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2678 C->isTargetOpcode(), C->isOpaque());
Chris Lattner4ed11b42005-09-02 00:17:32 +00002679 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002680 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002681 case ISD::TRUNCATE:
Stephen Hines36b56882014-04-23 16:57:46 -07002682 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2683 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen73328d12007-09-19 23:55:34 +00002684 case ISD::UINT_TO_FP:
2685 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002686 APFloat apf(EVTToAPFloatSemantics(VT),
2687 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002688 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002689 Opcode==ISD::SINT_TO_FP,
2690 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002691 return getConstantFP(apf, VT);
2692 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002693 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002694 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002695 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002696 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002697 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002698 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002699 case ISD::BSWAP:
Stephen Hines36b56882014-04-23 16:57:46 -07002700 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2701 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002702 case ISD::CTPOP:
Stephen Hines36b56882014-04-23 16:57:46 -07002703 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2704 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002705 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002706 case ISD::CTLZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002707 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2708 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002709 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002710 case ISD::CTTZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002711 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2712 C->isOpaque());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002713 }
2714 }
2715
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002716 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002717 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002718 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002719 switch (Opcode) {
2720 case ISD::FNEG:
2721 V.changeSign();
2722 return getConstantFP(V, VT);
2723 case ISD::FABS:
2724 V.clearSign();
2725 return getConstantFP(V, VT);
2726 case ISD::FCEIL: {
2727 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2728 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002729 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002730 break;
2731 }
2732 case ISD::FTRUNC: {
2733 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2734 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002735 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002736 break;
2737 }
2738 case ISD::FFLOOR: {
2739 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2740 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002741 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002742 break;
2743 }
2744 case ISD::FP_EXTEND: {
2745 bool ignored;
2746 // This can return overflow, underflow, or inexact; we don't care.
2747 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002748 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002749 APFloat::rmNearestTiesToEven, &ignored);
2750 return getConstantFP(V, VT);
2751 }
2752 case ISD::FP_TO_SINT:
2753 case ISD::FP_TO_UINT: {
2754 integerPart x[2];
2755 bool ignored;
2756 assert(integerPartWidth >= 64);
2757 // FIXME need to be more flexible about rounding mode.
2758 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2759 Opcode==ISD::FP_TO_SINT,
2760 APFloat::rmTowardZero, &ignored);
2761 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002762 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002763 APInt api(VT.getSizeInBits(), x);
2764 return getConstant(api, VT);
2765 }
2766 case ISD::BITCAST:
2767 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2768 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2769 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2770 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2771 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002772 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002773 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002774
Gabor Greifba36cb52008-08-28 21:40:38 +00002775 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002776 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002777 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002778 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002779 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002780 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002781 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002782 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002783 assert(VT.isFloatingPoint() &&
2784 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002785 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002786 assert((!VT.isVector() ||
2787 VT.getVectorNumElements() ==
2788 Operand.getValueType().getVectorNumElements()) &&
2789 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002790 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002791 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002792 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002793 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002794 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002795 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002796 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002797 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2798 "Invalid sext node, dst < src!");
2799 assert((!VT.isVector() ||
2800 VT.getVectorNumElements() ==
2801 Operand.getValueType().getVectorNumElements()) &&
2802 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002803 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2804 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2805 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002806 // sext(undef) = 0, because the top bits will all be the same.
2807 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002808 break;
2809 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002810 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002811 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002812 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002813 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2814 "Invalid zext node, dst < src!");
2815 assert((!VT.isVector() ||
2816 VT.getVectorNumElements() ==
2817 Operand.getValueType().getVectorNumElements()) &&
2818 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002819 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002820 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002821 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002822 else if (OpOpcode == ISD::UNDEF)
2823 // zext(undef) = 0, because the top bits will be zero.
2824 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002825 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002826 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002828 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002829 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002830 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2831 "Invalid anyext node, dst < src!");
2832 assert((!VT.isVector() ||
2833 VT.getVectorNumElements() ==
2834 Operand.getValueType().getVectorNumElements()) &&
2835 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002836
Dan Gohman9e86a732010-06-18 00:08:30 +00002837 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2838 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002839 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002840 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002841 else if (OpOpcode == ISD::UNDEF)
2842 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002843
2844 // (ext (trunx x)) -> x
2845 if (OpOpcode == ISD::TRUNCATE) {
2846 SDValue OpOp = Operand.getNode()->getOperand(0);
2847 if (OpOp.getValueType() == VT)
2848 return OpOp;
2849 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002850 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002851 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002852 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002853 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002854 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002855 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2856 "Invalid truncate node, src < dst!");
2857 assert((!VT.isVector() ||
2858 VT.getVectorNumElements() ==
2859 Operand.getValueType().getVectorNumElements()) &&
2860 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002861 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002862 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002863 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2864 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002865 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002866 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2867 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002868 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002869 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002870 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002871 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002872 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002873 if (OpOpcode == ISD::UNDEF)
2874 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002875 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002876 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002877 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002878 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002879 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002880 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002881 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2882 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002883 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002884 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002885 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002886 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002887 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002888 (VT.getVectorElementType() == Operand.getValueType() ||
2889 (VT.getVectorElementType().isInteger() &&
2890 Operand.getValueType().isInteger() &&
2891 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002892 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002893 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002894 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002895 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2896 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2897 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2898 Operand.getConstantOperandVal(1) == 0 &&
2899 Operand.getOperand(0).getValueType() == VT)
2900 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002901 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002902 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002903 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002904 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002905 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002906 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002907 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002908 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002909 break;
2910 case ISD::FABS:
2911 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002912 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002913 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002914 }
2915
Chris Lattner43247a12005-08-25 19:12:10 +00002916 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002917 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002918 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002919 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002920 SDValue Ops[1] = { Operand };
Stephen Hinesdce4a402014-05-29 02:49:00 -07002921 AddNodeIDNode(ID, Opcode, VTs, Ops);
2922 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002923 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002924 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002925
Jack Carter3af4d252013-09-09 22:02:08 +00002926 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2927 DL.getDebugLoc(), VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002928 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002929 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00002930 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2931 DL.getDebugLoc(), VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002932 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002933
Chris Lattnerc3aae252005-01-07 07:46:32 +00002934 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002935#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002936 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002937#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002938 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002939}
2940
Benjamin Kramer49693102013-02-04 15:19:18 +00002941SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2942 SDNode *Cst1, SDNode *Cst2) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002943 // If the opcode is a target-specific ISD node, there's nothing we can
2944 // do here and the operand rules may not line up with the below, so
2945 // bail early.
2946 if (Opcode >= ISD::BUILTIN_OP_END)
2947 return SDValue();
2948
Benjamin Kramer49693102013-02-04 15:19:18 +00002949 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2950 SmallVector<SDValue, 4> Outputs;
2951 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00002952
Benjamin Kramer49693102013-02-04 15:19:18 +00002953 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2954 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Stephen Hines36b56882014-04-23 16:57:46 -07002955 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2956 return SDValue();
2957
2958 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00002959 // Scalar instruction.
2960 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Stephen Hines36b56882014-04-23 16:57:46 -07002961 else {
Benjamin Kramer49693102013-02-04 15:19:18 +00002962 // For vectors extract each constant element into Inputs so we can constant
2963 // fold them individually.
2964 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2965 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2966 if (!BV1 || !BV2)
2967 return SDValue();
2968
2969 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2970
2971 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2972 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2973 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2974 if (!V1 || !V2) // Not a constant, bail.
2975 return SDValue();
2976
Stephen Hines36b56882014-04-23 16:57:46 -07002977 if (V1->isOpaque() || V2->isOpaque())
2978 return SDValue();
2979
Benjamin Kramer49693102013-02-04 15:19:18 +00002980 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2981 // FIXME: This is valid and could be handled by truncating the APInts.
2982 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2983 return SDValue();
2984
2985 Inputs.push_back(std::make_pair(V1, V2));
2986 }
Bill Wendling688d1c42008-09-24 10:16:24 +00002987 }
2988
Benjamin Kramer49693102013-02-04 15:19:18 +00002989 // We have a number of constant values, constant fold them element by element.
2990 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2991 const APInt &C1 = Inputs[I].first->getAPIntValue();
2992 const APInt &C2 = Inputs[I].second->getAPIntValue();
2993
2994 switch (Opcode) {
2995 case ISD::ADD:
2996 Outputs.push_back(getConstant(C1 + C2, SVT));
2997 break;
2998 case ISD::SUB:
2999 Outputs.push_back(getConstant(C1 - C2, SVT));
3000 break;
3001 case ISD::MUL:
3002 Outputs.push_back(getConstant(C1 * C2, SVT));
3003 break;
3004 case ISD::UDIV:
3005 if (!C2.getBoolValue())
3006 return SDValue();
3007 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3008 break;
3009 case ISD::UREM:
3010 if (!C2.getBoolValue())
3011 return SDValue();
3012 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3013 break;
3014 case ISD::SDIV:
3015 if (!C2.getBoolValue())
3016 return SDValue();
3017 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3018 break;
3019 case ISD::SREM:
3020 if (!C2.getBoolValue())
3021 return SDValue();
3022 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3023 break;
3024 case ISD::AND:
3025 Outputs.push_back(getConstant(C1 & C2, SVT));
3026 break;
3027 case ISD::OR:
3028 Outputs.push_back(getConstant(C1 | C2, SVT));
3029 break;
3030 case ISD::XOR:
3031 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3032 break;
3033 case ISD::SHL:
3034 Outputs.push_back(getConstant(C1 << C2, SVT));
3035 break;
3036 case ISD::SRL:
3037 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3038 break;
3039 case ISD::SRA:
3040 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3041 break;
3042 case ISD::ROTL:
3043 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3044 break;
3045 case ISD::ROTR:
3046 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3047 break;
3048 default:
3049 return SDValue();
3050 }
3051 }
3052
Stephen Hinesdce4a402014-05-29 02:49:00 -07003053 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3054 "Expected a scalar or vector!"));
3055
Benjamin Kramer49693102013-02-04 15:19:18 +00003056 // Handle the scalar case first.
Stephen Hinesdce4a402014-05-29 02:49:00 -07003057 if (!VT.isVector())
Benjamin Kramer49693102013-02-04 15:19:18 +00003058 return Outputs.back();
3059
Stephen Hinesdce4a402014-05-29 02:49:00 -07003060 // We may have a vector type but a scalar result. Create a splat.
3061 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3062
3063 // Build a big vector out of the scalar elements we generated.
3064 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling688d1c42008-09-24 10:16:24 +00003065}
3066
Andrew Trickac6d9be2013-05-25 02:42:55 +00003067SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003068 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003069 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3070 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00003071 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003072 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00003073 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3075 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003076 // Fold trivial token factors.
3077 if (N1.getOpcode() == ISD::EntryToken) return N2;
3078 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00003079 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00003080 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003081 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00003082 // Concat of UNDEFs is UNDEF.
3083 if (N1.getOpcode() == ISD::UNDEF &&
3084 N2.getOpcode() == ISD::UNDEF)
3085 return getUNDEF(VT);
3086
Dan Gohman7f8613e2008-08-14 20:04:46 +00003087 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3088 // one big BUILD_VECTOR.
3089 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3090 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003091 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3092 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003093 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07003094 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00003095 }
3096 break;
Chris Lattner76365122005-01-16 02:23:22 +00003097 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00003098 assert(VT.isInteger() && "This operator does not apply to FP types!");
3099 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003100 N1.getValueType() == VT && "Binary operator types must match!");
3101 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3102 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00003103 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003104 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00003105 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3106 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003107 break;
Chris Lattner76365122005-01-16 02:23:22 +00003108 case ISD::OR:
3109 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00003110 case ISD::ADD:
3111 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00003112 assert(VT.isInteger() && "This operator does not apply to FP types!");
3113 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003114 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00003115 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3116 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00003117 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003118 return N1;
3119 break;
Chris Lattner76365122005-01-16 02:23:22 +00003120 case ISD::UDIV:
3121 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00003122 case ISD::MULHU:
3123 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00003124 case ISD::MUL:
3125 case ISD::SDIV:
3126 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00003127 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00003128 assert(N1.getValueType() == N2.getValueType() &&
3129 N1.getValueType() == VT && "Binary operator types must match!");
3130 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003131 case ISD::FADD:
3132 case ISD::FSUB:
3133 case ISD::FMUL:
3134 case ISD::FDIV:
3135 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003136 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00003137 if (Opcode == ISD::FADD) {
3138 // 0+x --> x
3139 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3140 if (CFP->getValueAPF().isZero())
3141 return N2;
3142 // x+0 --> x
3143 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3144 if (CFP->getValueAPF().isZero())
3145 return N1;
3146 } else if (Opcode == ISD::FSUB) {
3147 // x-0 --> x
3148 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3149 if (CFP->getValueAPF().isZero())
3150 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00003151 } else if (Opcode == ISD::FMUL) {
3152 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3153 SDValue V = N2;
3154
3155 // If the first operand isn't the constant, try the second
3156 if (!CFP) {
3157 CFP = dyn_cast<ConstantFPSDNode>(N2);
3158 V = N1;
3159 }
3160
3161 if (CFP) {
3162 // 0*x --> 0
3163 if (CFP->isZero())
3164 return SDValue(CFP,0);
3165 // 1*x --> x
3166 if (CFP->isExactlyValue(1.0))
3167 return V;
3168 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00003169 }
Dan Gohman760f86f2009-01-22 21:58:43 +00003170 }
Dale Johannesen78995512010-05-15 18:38:02 +00003171 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00003172 assert(N1.getValueType() == N2.getValueType() &&
3173 N1.getValueType() == VT && "Binary operator types must match!");
3174 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003175 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3176 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003177 N1.getValueType().isFloatingPoint() &&
3178 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00003179 "Invalid FCOPYSIGN!");
3180 break;
Chris Lattner76365122005-01-16 02:23:22 +00003181 case ISD::SHL:
3182 case ISD::SRA:
3183 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00003184 case ISD::ROTL:
3185 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00003186 assert(VT == N1.getValueType() &&
3187 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003188 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00003189 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00003190 assert((!VT.isVector() || VT == N2.getValueType()) &&
3191 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00003192 // Verify that the shift amount VT is bit enough to hold valid shift
3193 // amounts. This catches things like trying to shift an i1024 value by an
3194 // i8, which is easy to fall into in generic code that uses
3195 // TLI.getShiftAmount().
3196 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00003197 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00003198 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00003199
3200 // Always fold shifts of i1 values so the code generator doesn't need to
3201 // handle them. Since we know the size of the shift has to be less than the
3202 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00003204 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00003205 if (N2C && N2C->isNullValue())
3206 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00003207 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00003208 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003209 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003210 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003211 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003212 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00003213 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003214 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003215 "type is vector!");
3216 assert((!EVT.isVector() ||
3217 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3218 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003219 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00003220 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003221 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00003222 break;
3223 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003224 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003225 assert(VT.isFloatingPoint() &&
3226 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003227 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00003228 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003229 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00003230 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00003231 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003232 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00003233 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003234 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003235 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003236 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00003237 assert(!EVT.isVector() &&
3238 "AssertSExt/AssertZExt type should be the vector element type "
3239 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003240 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00003241 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003242 break;
3243 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003244 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003245 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003246 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003247 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003248 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00003249 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003250 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003251 "type is vector!");
3252 assert((!EVT.isVector() ||
3253 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3254 "Vector element counts must match in SIGN_EXTEND_INREG");
3255 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003256 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00003257
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003258 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00003259 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003260 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00003261 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00003262 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00003263 return getConstant(Val, VT);
3264 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003265 break;
3266 }
3267 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00003268 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3269 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00003270 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003271
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003272 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3273 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003274 if (N2C &&
3275 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003276 N1.getNumOperands() > 0) {
3277 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003278 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003279 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003280 N1.getOperand(N2C->getZExtValue() / Factor),
3281 getConstant(N2C->getZExtValue() % Factor,
3282 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003283 }
3284
3285 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3286 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003287 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3288 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003289
3290 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003291 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003292 // are promoted and implicitly truncated, and the result implicitly
3293 // extended. Make that explicit here.
3294 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003295
Bob Wilsonb1303d02009-04-13 22:05:19 +00003296 return Elt;
3297 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003298
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003299 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3300 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003301 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003302 // If the indices are the same, return the inserted element else
3303 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003304 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003305 SDValue N1Op2 = N1.getOperand(2);
3306 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3307
3308 if (N1Op2C && N2C) {
3309 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3310 if (VT == N1.getOperand(1).getValueType())
3311 return N1.getOperand(1);
3312 else
3313 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3314 }
3315
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003316 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003317 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003318 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003319 break;
3320 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003321 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003322 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3323 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003324 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003325 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003326
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003327 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3328 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003329 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003330 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003331 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003332
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003333 // EXTRACT_ELEMENT of a constant int is also very common.
3334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003335 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003336 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003337 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3338 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003339 }
3340 break;
David Greene91585092011-01-26 15:38:49 +00003341 case ISD::EXTRACT_SUBVECTOR: {
3342 SDValue Index = N2;
3343 if (VT.isSimple() && N1.getValueType().isSimple()) {
3344 assert(VT.isVector() && N1.getValueType().isVector() &&
3345 "Extract subvector VTs must be a vectors!");
Jack Carter3af4d252013-09-09 22:02:08 +00003346 assert(VT.getVectorElementType() ==
3347 N1.getValueType().getVectorElementType() &&
David Greene91585092011-01-26 15:38:49 +00003348 "Extract subvector VTs must have the same element type!");
Craig Topper0ff11902013-08-15 02:44:19 +00003349 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greene91585092011-01-26 15:38:49 +00003350 "Extract subvector must be from larger vector to smaller vector!");
3351
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003352 if (isa<ConstantSDNode>(Index.getNode())) {
3353 assert((VT.getVectorNumElements() +
3354 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003355 <= N1.getValueType().getVectorNumElements())
3356 && "Extract subvector overflow!");
3357 }
3358
3359 // Trivial extraction.
Craig Topper0ff11902013-08-15 02:44:19 +00003360 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greene91585092011-01-26 15:38:49 +00003361 return N1;
3362 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003363 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003364 }
David Greene91585092011-01-26 15:38:49 +00003365 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003366
Benjamin Kramer49693102013-02-04 15:19:18 +00003367 // Perform trivial constant folding.
3368 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3369 if (SV.getNode()) return SV;
3370
3371 // Canonicalize constant to RHS if commutative.
3372 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3373 std::swap(N1C, N2C);
3374 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003375 }
3376
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003377 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003378 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3379 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003380 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003381 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003382 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003383 std::swap(N1CFP, N2CFP);
3384 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003385 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003386 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3387 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003388 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003389 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003390 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003391 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003392 return getConstantFP(V1, VT);
3393 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003394 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003395 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3396 if (s!=APFloat::opInvalidOp)
3397 return getConstantFP(V1, VT);
3398 break;
3399 case ISD::FMUL:
3400 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3401 if (s!=APFloat::opInvalidOp)
3402 return getConstantFP(V1, VT);
3403 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003404 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003405 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3406 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3407 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003408 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003409 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003410 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3411 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3412 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003413 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003414 case ISD::FCOPYSIGN:
3415 V1.copySign(V2);
3416 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003417 default: break;
3418 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003419 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003420
3421 if (Opcode == ISD::FP_ROUND) {
3422 APFloat V = N1CFP->getValueAPF(); // make copy
3423 bool ignored;
3424 // This can return overflow, underflow, or inexact; we don't care.
3425 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003426 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003427 APFloat::rmNearestTiesToEven, &ignored);
3428 return getConstantFP(V, VT);
3429 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003430 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003431
Chris Lattner62b57722006-04-20 05:39:12 +00003432 // Canonicalize an UNDEF to the RHS, even over a constant.
3433 if (N1.getOpcode() == ISD::UNDEF) {
3434 if (isCommutativeBinOp(Opcode)) {
3435 std::swap(N1, N2);
3436 } else {
3437 switch (Opcode) {
3438 case ISD::FP_ROUND_INREG:
3439 case ISD::SIGN_EXTEND_INREG:
3440 case ISD::SUB:
3441 case ISD::FSUB:
3442 case ISD::FDIV:
3443 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003444 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003445 return N1; // fold op(undef, arg2) -> undef
3446 case ISD::UDIV:
3447 case ISD::SDIV:
3448 case ISD::UREM:
3449 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003450 case ISD::SRL:
3451 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003452 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003453 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3454 // For vectors, we can't easily build an all zero vector, just return
3455 // the LHS.
3456 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003457 }
3458 }
3459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003460
3461 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003462 if (N2.getOpcode() == ISD::UNDEF) {
3463 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003464 case ISD::XOR:
3465 if (N1.getOpcode() == ISD::UNDEF)
3466 // Handle undef ^ undef -> 0 special case. This is a common
3467 // idiom (misuse).
3468 return getConstant(0, VT);
3469 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003470 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003471 case ISD::ADDC:
3472 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003473 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003474 case ISD::UDIV:
3475 case ISD::SDIV:
3476 case ISD::UREM:
3477 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003478 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003479 case ISD::FADD:
3480 case ISD::FSUB:
3481 case ISD::FMUL:
3482 case ISD::FDIV:
3483 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003484 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003485 return N2;
3486 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003487 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003488 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003489 case ISD::SRL:
3490 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003491 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003492 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3493 // For vectors, we can't easily build an all zero vector, just return
3494 // the LHS.
3495 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003496 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003498 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003499 // For vectors, we can't easily build an all one vector, just return
3500 // the LHS.
3501 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003502 case ISD::SRA:
3503 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003504 }
3505 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003506
Chris Lattner27e9b412005-05-11 18:57:39 +00003507 // Memoize this node if possible.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003508 BinarySDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003509 SDVTList VTs = getVTList(VT);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003510 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003511 if (VT != MVT::Glue) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003512 SDValue Ops[] = {N1, N2};
Jim Laskey583bd472006-10-27 23:46:08 +00003513 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003514 AddNodeIDNode(ID, Opcode, VTs, Ops);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003515 if (BinOpHasFlags)
3516 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Stephen Hinesdce4a402014-05-29 02:49:00 -07003517 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003518 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003519 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003520
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003521 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3522
Chris Lattnera5682852006-08-07 23:03:03 +00003523 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003524 } else {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003525
3526 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner27e9b412005-05-11 18:57:39 +00003527 }
3528
Chris Lattnerc3aae252005-01-07 07:46:32 +00003529 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003530#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003531 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003532#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003533 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003534}
3535
Andrew Trickac6d9be2013-05-25 02:42:55 +00003536SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003537 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003538 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003539 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003540 switch (Opcode) {
Owen Anderson58dcd202013-05-09 22:27:13 +00003541 case ISD::FMA: {
3542 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3543 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3544 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3545 if (N1CFP && N2CFP && N3CFP) {
3546 APFloat V1 = N1CFP->getValueAPF();
3547 const APFloat &V2 = N2CFP->getValueAPF();
3548 const APFloat &V3 = N3CFP->getValueAPF();
3549 APFloat::opStatus s =
3550 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3551 if (s != APFloat::opInvalidOp)
3552 return getConstantFP(V1, VT);
3553 }
3554 break;
3555 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00003556 case ISD::CONCAT_VECTORS:
3557 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3558 // one big BUILD_VECTOR.
3559 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3560 N2.getOpcode() == ISD::BUILD_VECTOR &&
3561 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003562 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3563 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003564 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3565 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07003566 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00003567 }
3568 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003569 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003570 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003571 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003572 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003573 break;
3574 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003575 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003576 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003577 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003578 return N2; // select true, X, Y -> X
3579 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003580 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003581
3582 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003583 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003584 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003585 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003586 case ISD::INSERT_SUBVECTOR: {
3587 SDValue Index = N3;
3588 if (VT.isSimple() && N1.getValueType().isSimple()
3589 && N2.getValueType().isSimple()) {
3590 assert(VT.isVector() && N1.getValueType().isVector() &&
3591 N2.getValueType().isVector() &&
3592 "Insert subvector VTs must be a vectors");
3593 assert(VT == N1.getValueType() &&
3594 "Dest and insert subvector source types must match!");
Craig Topper0ff11902013-08-15 02:44:19 +00003595 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenecfe33c42011-01-26 19:13:22 +00003596 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003597 if (isa<ConstantSDNode>(Index.getNode())) {
3598 assert((N2.getValueType().getVectorNumElements() +
3599 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003600 <= VT.getVectorNumElements())
3601 && "Insert subvector overflow!");
3602 }
3603
3604 // Trivial insertion.
Craig Topper0ff11902013-08-15 02:44:19 +00003605 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenecfe33c42011-01-26 19:13:22 +00003606 return N2;
3607 }
3608 break;
3609 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003610 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003611 // Fold bit_convert nodes from a type to themselves.
3612 if (N1.getValueType() == VT)
3613 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003614 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003615 }
3616
Chris Lattner43247a12005-08-25 19:12:10 +00003617 // Memoize node if it doesn't produce a flag.
3618 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003619 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003620 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003621 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003622 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003623 AddNodeIDNode(ID, Opcode, VTs, Ops);
3624 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003625 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003626 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003627
Jack Carter3af4d252013-09-09 22:02:08 +00003628 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3629 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003630 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003631 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003632 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3633 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003634 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003635
Chris Lattnerc3aae252005-01-07 07:46:32 +00003636 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003637#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003638 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003639#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003640 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003641}
3642
Andrew Trickac6d9be2013-05-25 02:42:55 +00003643SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003644 SDValue N1, SDValue N2, SDValue N3,
3645 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003646 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003647 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003648}
3649
Andrew Trickac6d9be2013-05-25 02:42:55 +00003650SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003651 SDValue N1, SDValue N2, SDValue N3,
3652 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003653 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003654 return getNode(Opcode, DL, VT, Ops);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003655}
3656
Dan Gohman98ca4f22009-08-05 01:29:28 +00003657/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3658/// the incoming stack arguments to be loaded from the stack.
3659SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3660 SmallVector<SDValue, 8> ArgChains;
3661
3662 // Include the original chain at the beginning of the list. When this is
3663 // used by target LowerCall hooks, this helps legalize find the
3664 // CALLSEQ_BEGIN node.
3665 ArgChains.push_back(Chain);
3666
3667 // Add a chain value for each stack argument.
3668 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3669 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3670 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3671 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3672 if (FI->getIndex() < 0)
3673 ArgChains.push_back(SDValue(L, 1));
3674
3675 // Build a tokenfactor for all the chains.
Stephen Hinesdce4a402014-05-29 02:49:00 -07003676 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003677}
3678
Dan Gohman707e0182008-04-12 04:36:06 +00003679/// getMemsetValue - Vectorized representation of the memset value
3680/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003681static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003682 SDLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003683 assert(Value.getOpcode() != ISD::UNDEF);
3684
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003685 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003686 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003687 assert(C->getAPIntValue().getBitWidth() == 8);
3688 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003689 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003690 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003691 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003692 }
Evan Chengf0df0312008-05-15 08:39:06 +00003693
Dale Johannesen0f502f62009-02-03 22:26:09 +00003694 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003695 if (NumBits > 8) {
3696 // Use a multiplication with 0x010101... to extend the input to the
3697 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003698 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003699 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003700 }
3701
3702 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003703}
3704
Dan Gohman707e0182008-04-12 04:36:06 +00003705/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3706/// used when a memcpy is turned into a memset when the source is a constant
3707/// string ptr.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003708static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003709 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003710 // Handle vector with all elements zero.
3711 if (Str.empty()) {
3712 if (VT.isInteger())
3713 return DAG.getConstant(0, VT);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003714 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng255f20f2010-04-01 06:04:33 +00003715 return DAG.getConstantFP(0.0, VT);
3716 else if (VT.isVector()) {
3717 unsigned NumElts = VT.getVectorNumElements();
3718 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003719 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003720 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3721 EltVT, NumElts)));
3722 } else
3723 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003724 }
3725
Duncan Sands83ec4b62008-06-06 12:08:01 +00003726 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003727 unsigned NumVTBits = VT.getSizeInBits();
3728 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003729 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3730
Evan Cheng4ff23d02013-01-10 22:13:27 +00003731 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003732 if (TLI.isLittleEndian()) {
3733 for (unsigned i = 0; i != NumBytes; ++i)
3734 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3735 } else {
3736 for (unsigned i = 0; i != NumBytes; ++i)
3737 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003738 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003739
Stephen Hines36b56882014-04-23 16:57:46 -07003740 // If the "cost" of materializing the integer immediate is less than the cost
3741 // of a load, then it is cost effective to turn the load into the immediate.
3742 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3743 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng376642e2012-12-10 23:21:26 +00003744 return DAG.getConstant(Val, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07003745 return SDValue(nullptr, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003746}
3747
Scott Michelfdc40a02009-02-17 22:15:04 +00003748/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003749///
Andrew Trickdd0fb012013-05-25 03:08:10 +00003750static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman707e0182008-04-12 04:36:06 +00003751 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003752 EVT VT = Base.getValueType();
Andrew Trickdd0fb012013-05-25 03:08:10 +00003753 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003754 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003755}
3756
Evan Chengf0df0312008-05-15 08:39:06 +00003757/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3758///
Chris Lattner18c7f802012-02-05 02:29:43 +00003759static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003760 unsigned SrcDelta = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003761 GlobalAddressSDNode *G = nullptr;
Evan Chengf0df0312008-05-15 08:39:06 +00003762 if (Src.getOpcode() == ISD::GlobalAddress)
3763 G = cast<GlobalAddressSDNode>(Src);
3764 else if (Src.getOpcode() == ISD::ADD &&
3765 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3766 Src.getOperand(1).getOpcode() == ISD::Constant) {
3767 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003768 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003769 }
3770 if (!G)
3771 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003772
Chris Lattner18c7f802012-02-05 02:29:43 +00003773 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003774}
Dan Gohman707e0182008-04-12 04:36:06 +00003775
Evan Cheng255f20f2010-04-01 06:04:33 +00003776/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3777/// to replace the memset / memcpy. Return true if the number of memory ops
3778/// is below the threshold. It returns the types of the sequence of
3779/// memory ops to perform memset / memcpy by reference.
3780static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003781 unsigned Limit, uint64_t Size,
3782 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003783 bool IsMemset,
3784 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003785 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003786 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003787 SelectionDAG &DAG,
3788 const TargetLowering &TLI) {
3789 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3790 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003791 // If 'SrcAlign' is zero, that means the memory operation does not need to
3792 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3793 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3794 // is the specified alignment of the memory operation. If it is zero, that
3795 // means it's possible to change the alignment of the destination.
3796 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3797 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003798 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003799 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003800 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003801
Chris Lattneracee6472010-03-07 07:45:08 +00003802 if (VT == MVT::Other) {
Stephen Hines36b56882014-04-23 16:57:46 -07003803 unsigned AS = 0;
3804 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
3805 TLI.allowsUnalignedMemoryAccesses(VT, AS)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003806 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003807 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003808 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003809 case 0: VT = MVT::i64; break;
3810 case 4: VT = MVT::i32; break;
3811 case 2: VT = MVT::i16; break;
3812 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003813 }
3814 }
3815
Owen Anderson766b5ef2009-08-11 21:59:30 +00003816 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003817 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003818 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003819 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003820
Duncan Sands8e4eb092008-06-08 20:54:56 +00003821 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003822 VT = LVT;
3823 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003824
Dan Gohman707e0182008-04-12 04:36:06 +00003825 unsigned NumMemOps = 0;
3826 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003827 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003828 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003829 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003830 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003831 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003832
3833 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003834 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003835 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003836 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003837 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003838 Found = true;
3839 else if (NewVT == MVT::i64 &&
3840 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003841 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003842 // i64 is usually not legal on 32-bit targets, but f64 may be.
3843 NewVT = MVT::f64;
3844 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003845 }
Evan Cheng376642e2012-12-10 23:21:26 +00003846 }
3847
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003848 if (!Found) {
3849 do {
3850 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3851 if (NewVT == MVT::i8)
3852 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003853 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003854 }
3855 NewVTSize = NewVT.getSizeInBits() / 8;
3856
Evan Cheng376642e2012-12-10 23:21:26 +00003857 // If the new VT cannot cover all of the remaining bits, then consider
3858 // issuing a (or a pair of) unaligned and overlapping load / store.
3859 // FIXME: Only does this for 64-bit or more since we don't have proper
3860 // cost model for unaligned load / store.
3861 bool Fast;
Stephen Hines36b56882014-04-23 16:57:46 -07003862 unsigned AS = 0;
Evan Chenga16e49d2012-12-12 20:43:23 +00003863 if (NumMemOps && AllowOverlap &&
3864 VTSize >= 8 && NewVTSize < Size &&
Stephen Hines36b56882014-04-23 16:57:46 -07003865 TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast)
Evan Cheng376642e2012-12-10 23:21:26 +00003866 VTSize = Size;
3867 else {
3868 VT = NewVT;
3869 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003870 }
Dan Gohman707e0182008-04-12 04:36:06 +00003871 }
Dan Gohman707e0182008-04-12 04:36:06 +00003872
Evan Chenga16e49d2012-12-12 20:43:23 +00003873 if (++NumMemOps > Limit)
3874 return false;
3875
Dan Gohman707e0182008-04-12 04:36:06 +00003876 MemOps.push_back(VT);
3877 Size -= VTSize;
3878 }
3879
3880 return true;
3881}
3882
Andrew Trickac6d9be2013-05-25 02:42:55 +00003883static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003884 SDValue Chain, SDValue Dst,
3885 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003886 unsigned Align, bool isVol,
3887 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003888 MachinePointerInfo DstPtrInfo,
3889 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003890 // Turn a memcpy of undef to nop.
3891 if (Src.getOpcode() == ISD::UNDEF)
3892 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003893
Dan Gohman21323f32008-05-29 19:42:22 +00003894 // Expand memcpy to a series of load and store ops if the size operand falls
3895 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003896 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3897 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003898 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003899 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003900 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003901 MachineFunction &MF = DAG.getMachineFunction();
3902 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003903 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003904 MF.getFunction()->getAttributes().
3905 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003906 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3907 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3908 DstAlignCanChange = true;
3909 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3910 if (Align > SrcAlign)
3911 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003912 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003913 bool CopyFromStr = isMemSrcFromString(Src, Str);
3914 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003915 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003916
Evan Cheng94107ba2010-04-01 18:19:11 +00003917 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003918 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003919 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003920 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003921 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003922
Evan Cheng255f20f2010-04-01 06:04:33 +00003923 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003924 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003925 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00003926
3927 // Don't promote to an alignment that would require dynamic stack
Stephen Lin155615d2013-07-08 00:37:03 +00003928 // realignment.
Lang Hames2d95e432013-01-31 20:23:43 +00003929 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3930 if (!TRI->needsStackRealignment(MF))
3931 while (NewAlign > Align &&
3932 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3933 NewAlign /= 2;
3934
Evan Cheng255f20f2010-04-01 06:04:33 +00003935 if (NewAlign > Align) {
3936 // Give the stack frame object a larger alignment if needed.
3937 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3938 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3939 Align = NewAlign;
3940 }
3941 }
Dan Gohman707e0182008-04-12 04:36:06 +00003942
Dan Gohman475871a2008-07-27 21:46:04 +00003943 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003944 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003945 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003946 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003947 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003948 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003949 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003950
Evan Cheng376642e2012-12-10 23:21:26 +00003951 if (VTSize > Size) {
3952 // Issuing an unaligned load / store pair that overlaps with the previous
3953 // pair. Adjust the offset accordingly.
3954 assert(i == NumMemOps-1 && i != 0);
3955 SrcOff -= VTSize - Size;
3956 DstOff -= VTSize - Size;
3957 }
3958
Evan Cheng255f20f2010-04-01 06:04:33 +00003959 if (CopyFromStr &&
3960 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003961 // It's unlikely a store of a vector immediate can be done in a single
3962 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003963 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003964 // FIXME: Handle other cases where store of vector immediate is done in
3965 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003966 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003967 if (Value.getNode())
3968 Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003969 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng376642e2012-12-10 23:21:26 +00003970 DstPtrInfo.getWithOffset(DstOff), isVol,
3971 false, Align);
3972 }
3973
3974 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003975 // The type might not be legal for the target. This should only happen
3976 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003977 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3978 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003979 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003980 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003981 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003982 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003983 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003984 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003985 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003986 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003987 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003988 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3989 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003990 }
3991 OutChains.push_back(Store);
3992 SrcOff += VTSize;
3993 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003994 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003995 }
3996
Stephen Hinesdce4a402014-05-29 02:49:00 -07003997 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00003998}
3999
Andrew Trickac6d9be2013-05-25 02:42:55 +00004000static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00004001 SDValue Chain, SDValue Dst,
4002 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004003 unsigned Align, bool isVol,
4004 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004005 MachinePointerInfo DstPtrInfo,
4006 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00004007 // Turn a memmove of undef to nop.
4008 if (Src.getOpcode() == ISD::UNDEF)
4009 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00004010
4011 // Expand memmove to a series of load and store ops if the size operand falls
4012 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00004013 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004014 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00004015 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00004016 MachineFunction &MF = DAG.getMachineFunction();
4017 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00004018 bool OptSize = MF.getFunction()->getAttributes().
4019 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004020 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4021 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4022 DstAlignCanChange = true;
4023 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4024 if (Align > SrcAlign)
4025 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00004026 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004027
Evan Cheng94107ba2010-04-01 18:19:11 +00004028 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00004029 (DstAlignCanChange ? 0 : Align), SrcAlign,
4030 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00004031 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00004032
Evan Cheng255f20f2010-04-01 06:04:33 +00004033 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004034 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00004035 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00004036 if (NewAlign > Align) {
4037 // Give the stack frame object a larger alignment if needed.
4038 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4039 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4040 Align = NewAlign;
4041 }
4042 }
Dan Gohman21323f32008-05-29 19:42:22 +00004043
Evan Cheng255f20f2010-04-01 06:04:33 +00004044 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004045 SmallVector<SDValue, 8> LoadValues;
4046 SmallVector<SDValue, 8> LoadChains;
4047 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00004048 unsigned NumMemOps = MemOps.size();
4049 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004050 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00004051 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00004052 SDValue Value;
Dan Gohman21323f32008-05-29 19:42:22 +00004053
Dale Johannesen0f502f62009-02-03 22:26:09 +00004054 Value = DAG.getLoad(VT, dl, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004055 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004056 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004057 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00004058 LoadValues.push_back(Value);
4059 LoadChains.push_back(Value.getValue(1));
4060 SrcOff += VTSize;
4061 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07004062 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman21323f32008-05-29 19:42:22 +00004063 OutChains.clear();
4064 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004065 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00004066 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00004067 SDValue Store;
Dan Gohman21323f32008-05-29 19:42:22 +00004068
Dale Johannesen0f502f62009-02-03 22:26:09 +00004069 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Trickdd0fb012013-05-25 03:08:10 +00004070 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004071 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00004072 OutChains.push_back(Store);
4073 DstOff += VTSize;
4074 }
4075
Stephen Hinesdce4a402014-05-29 02:49:00 -07004076 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman21323f32008-05-29 19:42:22 +00004077}
4078
Serge Pavlov496f0242013-09-17 16:24:42 +00004079/// \brief Lower the call to 'memset' intrinsic function into a series of store
4080/// operations.
4081///
4082/// \param DAG Selection DAG where lowered code is placed.
4083/// \param dl Link to corresponding IR location.
4084/// \param Chain Control flow dependency.
4085/// \param Dst Pointer to destination memory location.
4086/// \param Src Value of byte to write into the memory.
4087/// \param Size Number of bytes to write.
4088/// \param Align Alignment of the destination in bytes.
4089/// \param isVol True if destination is volatile.
4090/// \param DstPtrInfo IR information on the memory pointer.
4091/// \returns New head in the control flow, if lowering was successful, empty
4092/// SDValue otherwise.
4093///
4094/// The function tries to replace 'llvm.memset' intrinsic with several store
4095/// operations and value calculation code. This is usually profitable for small
4096/// memory size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004097static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00004098 SDValue Chain, SDValue Dst,
4099 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004100 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004101 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00004102 // Turn a memset of undef to nop.
4103 if (Src.getOpcode() == ISD::UNDEF)
4104 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00004105
4106 // Expand memset to a series of load/store ops if the size operand
4107 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00004108 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004109 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00004110 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00004111 MachineFunction &MF = DAG.getMachineFunction();
4112 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00004113 bool OptSize = MF.getFunction()->getAttributes().
4114 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004115 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4116 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4117 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00004118 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00004119 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00004120 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00004121 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00004122 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00004123 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00004124
Evan Cheng255f20f2010-04-01 06:04:33 +00004125 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004126 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00004127 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00004128 if (NewAlign > Align) {
4129 // Give the stack frame object a larger alignment if needed.
4130 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4131 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4132 Align = NewAlign;
4133 }
4134 }
4135
Dan Gohman475871a2008-07-27 21:46:04 +00004136 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00004137 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00004138 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00004139
4140 // Find the largest store and generate the bit pattern for it.
4141 EVT LargestVT = MemOps[0];
4142 for (unsigned i = 1; i < NumMemOps; i++)
4143 if (MemOps[i].bitsGT(LargestVT))
4144 LargestVT = MemOps[i];
4145 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4146
Dan Gohman707e0182008-04-12 04:36:06 +00004147 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004148 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00004149 unsigned VTSize = VT.getSizeInBits() / 8;
4150 if (VTSize > Size) {
4151 // Issuing an unaligned load / store pair that overlaps with the previous
4152 // pair. Adjust the offset accordingly.
4153 assert(i == NumMemOps-1 && i != 0);
4154 DstOff -= VTSize - Size;
4155 }
Benjamin Kramer80220362011-01-02 19:57:05 +00004156
4157 // If this store is smaller than the largest store see whether we can get
4158 // the smaller value for free with a truncate.
4159 SDValue Value = MemSetValue;
4160 if (VT.bitsLT(LargestVT)) {
4161 if (!LargestVT.isVector() && !VT.isVector() &&
4162 TLI.isTruncateFree(LargestVT, VT))
4163 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4164 else
4165 Value = getMemsetValue(Src, VT, DAG, dl);
4166 }
4167 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004168 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004169 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004170 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00004171 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00004172 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00004173 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00004174 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00004175 }
4176
Stephen Hinesdce4a402014-05-29 02:49:00 -07004177 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00004178}
4179
Andrew Trickac6d9be2013-05-25 02:42:55 +00004180SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004181 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004182 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004183 MachinePointerInfo DstPtrInfo,
4184 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004185 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004186
4187 // Check to see if we should lower the memcpy to loads and stores first.
4188 // For cases within the target-specified limits, this is the best choice.
4189 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4190 if (ConstantSize) {
4191 // Memcpy with size zero? Just return the original chain.
4192 if (ConstantSize->isNullValue())
4193 return Chain;
4194
Evan Cheng255f20f2010-04-01 06:04:33 +00004195 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4196 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00004197 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004198 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004199 return Result;
4200 }
4201
4202 // Then check to see if we should lower the memcpy with target-specific
4203 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004204 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004205 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004206 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004207 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004208 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004209 return Result;
4210
4211 // If we really need inline code and the target declined to provide it,
4212 // use a (potentially long) sequence of loads and stores.
4213 if (AlwaysInline) {
4214 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004215 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004216 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004217 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00004218 }
4219
Dan Gohman7b55d362010-04-05 20:24:08 +00004220 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4221 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4222 // respect volatile, so they may do things like read or write memory
4223 // beyond the given memory regions. But fixing this isn't easy, and most
4224 // people don't care.
4225
Bill Wendlingba54bca2013-06-19 21:36:55 +00004226 const TargetLowering *TLI = TM.getTargetLowering();
4227
Dan Gohman707e0182008-04-12 04:36:06 +00004228 // Emit a library call.
4229 TargetLowering::ArgListTy Args;
4230 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004231 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004232 Entry.Node = Dst; Args.push_back(Entry);
4233 Entry.Node = Src; Args.push_back(Entry);
4234 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004235 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004236 TargetLowering::CallLoweringInfo CLI(*this);
4237 CLI.setDebugLoc(dl).setChain(Chain)
4238 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4239 Type::getVoidTy(*getContext()),
4240 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004241 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004242 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004243 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004244
Dan Gohman707e0182008-04-12 04:36:06 +00004245 return CallResult.second;
4246}
4247
Andrew Trickac6d9be2013-05-25 02:42:55 +00004248SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004249 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004250 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004251 MachinePointerInfo DstPtrInfo,
4252 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004253 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004254
Dan Gohman21323f32008-05-29 19:42:22 +00004255 // Check to see if we should lower the memmove to loads and stores first.
4256 // For cases within the target-specified limits, this is the best choice.
4257 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4258 if (ConstantSize) {
4259 // Memmove with size zero? Just return the original chain.
4260 if (ConstantSize->isNullValue())
4261 return Chain;
4262
Dan Gohman475871a2008-07-27 21:46:04 +00004263 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00004264 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004265 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004266 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004267 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00004268 return Result;
4269 }
Dan Gohman707e0182008-04-12 04:36:06 +00004270
4271 // Then check to see if we should lower the memmove with target-specific
4272 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004273 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004274 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004275 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004276 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004277 return Result;
4278
Mon P Wang01f0e852010-04-06 08:27:51 +00004279 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4280 // not be safe. See memcpy above for more details.
4281
Bill Wendlingba54bca2013-06-19 21:36:55 +00004282 const TargetLowering *TLI = TM.getTargetLowering();
4283
Dan Gohman707e0182008-04-12 04:36:06 +00004284 // Emit a library call.
4285 TargetLowering::ArgListTy Args;
4286 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004287 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004288 Entry.Node = Dst; Args.push_back(Entry);
4289 Entry.Node = Src; Args.push_back(Entry);
4290 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004291 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004292 TargetLowering::CallLoweringInfo CLI(*this);
4293 CLI.setDebugLoc(dl).setChain(Chain)
4294 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4295 Type::getVoidTy(*getContext()),
4296 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004297 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004298 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004299 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004300
Dan Gohman707e0182008-04-12 04:36:06 +00004301 return CallResult.second;
4302}
4303
Andrew Trickac6d9be2013-05-25 02:42:55 +00004304SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004306 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004307 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004308 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004309
4310 // Check to see if we should lower the memset to stores first.
4311 // For cases within the target-specified limits, this is the best choice.
4312 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4313 if (ConstantSize) {
4314 // Memset with size zero? Just return the original chain.
4315 if (ConstantSize->isNullValue())
4316 return Chain;
4317
Mon P Wang20adc9d2010-04-04 03:10:48 +00004318 SDValue Result =
4319 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004320 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004321
Gabor Greifba36cb52008-08-28 21:40:38 +00004322 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004323 return Result;
4324 }
4325
4326 // Then check to see if we should lower the memset with target-specific
4327 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004328 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004329 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004330 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004331 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004332 return Result;
4333
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004334 // Emit a library call.
Bill Wendlingba54bca2013-06-19 21:36:55 +00004335 const TargetLowering *TLI = TM.getTargetLowering();
4336 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004337 TargetLowering::ArgListTy Args;
4338 TargetLowering::ArgListEntry Entry;
4339 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4340 Args.push_back(Entry);
4341 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 if (Src.getValueType().bitsGT(MVT::i32))
4343 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00004344 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004345 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00004346 Entry.Node = Src;
4347 Entry.Ty = Type::getInt32Ty(*getContext());
4348 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00004349 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004350 Entry.Node = Size;
4351 Entry.Ty = IntPtrTy;
4352 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00004353 Args.push_back(Entry);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004354
Stephen Hinesdce4a402014-05-29 02:49:00 -07004355 // FIXME: pass in SDLoc
4356 TargetLowering::CallLoweringInfo CLI(*this);
4357 CLI.setDebugLoc(dl).setChain(Chain)
4358 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4359 Type::getVoidTy(*getContext()),
4360 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004361 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004362 .setDiscardResult();
4363
4364 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman707e0182008-04-12 04:36:06 +00004365 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004366}
4367
Andrew Trickac6d9be2013-05-25 02:42:55 +00004368SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004369 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emerson268c7432013-09-26 12:22:36 +00004370 MachineMemOperand *MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004371 AtomicOrdering SuccessOrdering,
4372 AtomicOrdering FailureOrdering,
Amara Emerson268c7432013-09-26 12:22:36 +00004373 SynchronizationScope SynchScope) {
4374 FoldingSetNodeID ID;
4375 ID.AddInteger(MemVT.getRawBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004376 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emerson268c7432013-09-26 12:22:36 +00004377 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004378 void* IP = nullptr;
Amara Emerson268c7432013-09-26 12:22:36 +00004379 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4380 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4381 return SDValue(E, 0);
4382 }
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004383
4384 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4385 // SDNode doesn't have access to it. This memory will be "leaked" when
4386 // the node is deallocated, but recovered when the allocator is released.
4387 // If the number of operands is less than 5 we use AtomicSDNode's internal
4388 // storage.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004389 unsigned NumOps = Ops.size();
4390 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4391 : nullptr;
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004392
Amara Emerson268c7432013-09-26 12:22:36 +00004393 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4394 dl.getDebugLoc(), VTList, MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004395 Ops.data(), DynOps, NumOps, MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004396 SuccessOrdering, FailureOrdering,
4397 SynchScope);
Amara Emerson268c7432013-09-26 12:22:36 +00004398 CSEMap.InsertNode(N, IP);
4399 AllNodes.push_back(N);
4400 return SDValue(N, 0);
4401}
4402
4403SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004404 SDVTList VTList, ArrayRef<SDValue> Ops,
Stephen Hines36b56882014-04-23 16:57:46 -07004405 MachineMemOperand *MMO,
4406 AtomicOrdering Ordering,
4407 SynchronizationScope SynchScope) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07004408 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Stephen Hines36b56882014-04-23 16:57:46 -07004409 Ordering, SynchScope);
4410}
4411
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004412SDValue SelectionDAG::getAtomicCmpSwap(
4413 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4414 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4415 unsigned Alignment, AtomicOrdering SuccessOrdering,
4416 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4417 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4418 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4419 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4420
Dan Gohmanc76909a2009-09-25 20:36:54 +00004421 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4422 Alignment = getEVTAlignment(MemVT);
4423
Evan Chengff89dcb2009-10-18 18:16:27 +00004424 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004425
Eli Friedman981a0102011-09-07 02:23:42 +00004426 // FIXME: Volatile isn't really correct; we should keep track of atomic
4427 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004428 unsigned Flags = MachineMemOperand::MOVolatile;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004429 Flags |= MachineMemOperand::MOLoad;
4430 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004431
4432 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004433 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004434
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004435 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4436 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004437}
4438
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004439SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4440 SDVTList VTs, SDValue Chain, SDValue Ptr,
4441 SDValue Cmp, SDValue Swp,
4442 MachineMemOperand *MMO,
4443 AtomicOrdering SuccessOrdering,
4444 AtomicOrdering FailureOrdering,
4445 SynchronizationScope SynchScope) {
4446 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4447 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesene8c17332009-01-29 00:47:48 +00004448 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4449
Dale Johannesene8c17332009-01-29 00:47:48 +00004450 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004451 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4452 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004453}
4454
Andrew Trickac6d9be2013-05-25 02:42:55 +00004455SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004456 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004457 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004458 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004459 unsigned Alignment,
4460 AtomicOrdering Ordering,
4461 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004462 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4463 Alignment = getEVTAlignment(MemVT);
4464
Evan Chengff89dcb2009-10-18 18:16:27 +00004465 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004466 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004467 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004468 // For now, atomics are considered to be volatile always, and they are
4469 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004470 // FIXME: Volatile isn't really correct; we should keep track of atomic
4471 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004472 unsigned Flags = MachineMemOperand::MOVolatile;
4473 if (Opcode != ISD::ATOMIC_STORE)
4474 Flags |= MachineMemOperand::MOLoad;
4475 if (Opcode != ISD::ATOMIC_LOAD)
4476 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004477
4478 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004479 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004480 MemVT.getStoreSize(), Alignment);
4481
Eli Friedman55ba8162011-07-29 03:05:32 +00004482 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4483 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004484}
4485
Andrew Trickac6d9be2013-05-25 02:42:55 +00004486SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004487 SDValue Chain,
4488 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004489 MachineMemOperand *MMO,
4490 AtomicOrdering Ordering,
4491 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004492 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4493 Opcode == ISD::ATOMIC_LOAD_SUB ||
4494 Opcode == ISD::ATOMIC_LOAD_AND ||
4495 Opcode == ISD::ATOMIC_LOAD_OR ||
4496 Opcode == ISD::ATOMIC_LOAD_XOR ||
4497 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004498 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004499 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004500 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004501 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004502 Opcode == ISD::ATOMIC_SWAP ||
4503 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004504 "Invalid Atomic Op");
4505
Owen Andersone50ed302009-08-10 22:56:29 +00004506 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004507
Eli Friedman327236c2011-08-24 20:50:09 +00004508 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4509 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004510 SDValue Ops[] = {Chain, Ptr, Val};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004511 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004512}
4513
Andrew Trickac6d9be2013-05-25 02:42:55 +00004514SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004515 EVT VT, SDValue Chain,
4516 SDValue Ptr,
4517 MachineMemOperand *MMO,
4518 AtomicOrdering Ordering,
4519 SynchronizationScope SynchScope) {
4520 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4521
4522 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman327236c2011-08-24 20:50:09 +00004523 SDValue Ops[] = {Chain, Ptr};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004524 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004525}
4526
Duncan Sands4bdcb612008-07-02 17:40:58 +00004527/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004528SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4529 if (Ops.size() == 1)
Dale Johannesen54c94522009-02-02 20:47:48 +00004530 return Ops[0];
4531
Owen Andersone50ed302009-08-10 22:56:29 +00004532 SmallVector<EVT, 4> VTs;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004533 VTs.reserve(Ops.size());
4534 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesen54c94522009-02-02 20:47:48 +00004535 VTs.push_back(Ops[i].getValueType());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004536 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004537}
4538
4539SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004540SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004541 ArrayRef<SDValue> Ops,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004542 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004543 unsigned Align, bool Vol,
4544 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004545 if (Align == 0) // Ensure that codegen never sees alignment 0
4546 Align = getEVTAlignment(MemVT);
4547
4548 MachineFunction &MF = getMachineFunction();
4549 unsigned Flags = 0;
4550 if (WriteMem)
4551 Flags |= MachineMemOperand::MOStore;
4552 if (ReadMem)
4553 Flags |= MachineMemOperand::MOLoad;
4554 if (Vol)
4555 Flags |= MachineMemOperand::MOVolatile;
4556 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004557 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004558
Stephen Hinesdce4a402014-05-29 02:49:00 -07004559 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004560}
4561
4562SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004563SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004564 ArrayRef<SDValue> Ops, EVT MemVT,
4565 MachineMemOperand *MMO) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004566 assert((Opcode == ISD::INTRINSIC_VOID ||
4567 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004568 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004569 Opcode == ISD::LIFETIME_START ||
4570 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004571 (Opcode <= INT_MAX &&
4572 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4573 "Opcode is not a memory-accessing opcode!");
4574
Dale Johannesene8c17332009-01-29 00:47:48 +00004575 // Memoize the node unless it returns a flag.
4576 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004577 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004578 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004579 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004580 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004581 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004582 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4583 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004584 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004586
Jack Carter3af4d252013-09-09 22:02:08 +00004587 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4588 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004589 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004590 CSEMap.InsertNode(N, IP);
4591 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004592 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4593 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004594 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004595 }
4596 AllNodes.push_back(N);
4597 return SDValue(N, 0);
4598}
4599
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004600/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4601/// MachinePointerInfo record from it. This is particularly useful because the
4602/// code generator has many cases where it doesn't bother passing in a
4603/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4604static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4605 // If this is FI+Offset, we can model it.
4606 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4607 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4608
4609 // If this is (FI+Offset1)+Offset2, we can model it.
4610 if (Ptr.getOpcode() != ISD::ADD ||
4611 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4612 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4613 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004614
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004615 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4616 return MachinePointerInfo::getFixedStack(FI, Offset+
4617 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4618}
4619
4620/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4621/// MachinePointerInfo record from it. This is particularly useful because the
4622/// code generator has many cases where it doesn't bother passing in a
4623/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4624static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4625 // If the 'Offset' value isn't a constant, we can't handle this.
4626 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4627 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4628 if (OffsetOp.getOpcode() == ISD::UNDEF)
4629 return InferPointerInfo(Ptr);
4630 return MachinePointerInfo();
4631}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004632
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004633
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004634SDValue
4635SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004636 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004637 SDValue Ptr, SDValue Offset,
4638 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004639 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004640 unsigned Alignment, const MDNode *TBAAInfo,
4641 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004642 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004643 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004644 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4645 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004646
Dan Gohmanc76909a2009-09-25 20:36:54 +00004647 unsigned Flags = MachineMemOperand::MOLoad;
4648 if (isVolatile)
4649 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004650 if (isNonTemporal)
4651 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004652 if (isInvariant)
4653 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004654
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004655 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4656 // clients.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004657 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004658 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004659
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004660 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004661 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004662 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004663 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004664 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004665}
4666
4667SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004668SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004669 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004670 SDValue Ptr, SDValue Offset, EVT MemVT,
4671 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004672 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004673 ExtType = ISD::NON_EXTLOAD;
4674 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004675 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004676 } else {
4677 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004678 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4679 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004680 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004681 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004682 assert(VT.isVector() == MemVT.isVector() &&
4683 "Cannot use trunc store to convert to or from a vector!");
4684 assert((!VT.isVector() ||
4685 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4686 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004687 }
4688
4689 bool Indexed = AM != ISD::UNINDEXED;
4690 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4691 "Unindexed load with an offset!");
4692
4693 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004694 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004695 SDValue Ops[] = { Chain, Ptr, Offset };
4696 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004697 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004698 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004699 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004700 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004701 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004702 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004703 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004704 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4705 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004706 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004707 }
Jack Carter3af4d252013-09-09 22:02:08 +00004708 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4709 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman95531882010-03-18 18:49:47 +00004710 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004711 CSEMap.InsertNode(N, IP);
4712 AllNodes.push_back(N);
4713 return SDValue(N, 0);
4714}
4715
Andrew Trickac6d9be2013-05-25 02:42:55 +00004716SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004717 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004718 MachinePointerInfo PtrInfo,
4719 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004720 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004721 const MDNode *TBAAInfo,
4722 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004723 SDValue Undef = getUNDEF(Ptr.getValueType());
4724 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004725 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4726 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004727}
Dale Johannesene8c17332009-01-29 00:47:48 +00004728
Richard Sandiford66589dc2013-10-28 11:17:59 +00004729SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4730 SDValue Chain, SDValue Ptr,
4731 MachineMemOperand *MMO) {
4732 SDValue Undef = getUNDEF(Ptr.getValueType());
4733 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4734 VT, MMO);
4735}
4736
Andrew Trickac6d9be2013-05-25 02:42:55 +00004737SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004738 SDValue Chain, SDValue Ptr,
4739 MachinePointerInfo PtrInfo, EVT MemVT,
4740 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004741 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004742 SDValue Undef = getUNDEF(Ptr.getValueType());
4743 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004744 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004745 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004746}
4747
4748
Richard Sandiford66589dc2013-10-28 11:17:59 +00004749SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4750 SDValue Chain, SDValue Ptr, EVT MemVT,
4751 MachineMemOperand *MMO) {
4752 SDValue Undef = getUNDEF(Ptr.getValueType());
4753 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4754 MemVT, MMO);
4755}
4756
Dan Gohman475871a2008-07-27 21:46:04 +00004757SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004758SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004759 SDValue Offset, ISD::MemIndexedMode AM) {
4760 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4761 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4762 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004763 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004764 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004765 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004766 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004767}
4768
Andrew Trickac6d9be2013-05-25 02:42:55 +00004769SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004770 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004771 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004772 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004773 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004774 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004775 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004776 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004777
Dan Gohmanc76909a2009-09-25 20:36:54 +00004778 unsigned Flags = MachineMemOperand::MOStore;
4779 if (isVolatile)
4780 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004781 if (isNonTemporal)
4782 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004783
Stephen Hinesdce4a402014-05-29 02:49:00 -07004784 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004785 PtrInfo = InferPointerInfo(Ptr);
4786
4787 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004788 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004789 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004790 Val.getValueType().getStoreSize(), Alignment,
4791 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004792
4793 return getStore(Chain, dl, Val, Ptr, MMO);
4794}
4795
Andrew Trickac6d9be2013-05-25 02:42:55 +00004796SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004797 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004798 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004799 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004800 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004801 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004802 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004803 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4804 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004805 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004806 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004807 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004808 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004809 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004810 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004811 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4812 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004813 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004814 }
Jack Carter3af4d252013-09-09 22:02:08 +00004815 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4816 dl.getDebugLoc(), VTs,
4817 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004818 CSEMap.InsertNode(N, IP);
4819 AllNodes.push_back(N);
4820 return SDValue(N, 0);
4821}
4822
Andrew Trickac6d9be2013-05-25 02:42:55 +00004823SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004824 SDValue Ptr, MachinePointerInfo PtrInfo,
4825 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004826 unsigned Alignment,
4827 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004828 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004829 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004830 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4831 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004832
Dan Gohmanc76909a2009-09-25 20:36:54 +00004833 unsigned Flags = MachineMemOperand::MOStore;
4834 if (isVolatile)
4835 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004836 if (isNonTemporal)
4837 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004838
Stephen Hinesdce4a402014-05-29 02:49:00 -07004839 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004840 PtrInfo = InferPointerInfo(Ptr);
4841
4842 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004843 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004844 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4845 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004846
4847 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4848}
4849
Andrew Trickac6d9be2013-05-25 02:42:55 +00004850SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004851 SDValue Ptr, EVT SVT,
4852 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004853 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004854
Michael Ilseman06b96902012-09-10 16:56:31 +00004855 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004856 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004857 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004858 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004859
Dan Gohman2e141d72009-12-14 23:40:38 +00004860 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4861 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004862 assert(VT.isInteger() == SVT.isInteger() &&
4863 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004864 assert(VT.isVector() == SVT.isVector() &&
4865 "Cannot use trunc store to convert to or from a vector!");
4866 assert((!VT.isVector() ||
4867 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4868 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004869
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004871 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004872 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4873 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004874 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004875 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004876 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004877 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004878 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004879 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004880 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4881 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004882 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004883 }
Jack Carter3af4d252013-09-09 22:02:08 +00004884 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4885 dl.getDebugLoc(), VTs,
4886 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004887 CSEMap.InsertNode(N, IP);
4888 AllNodes.push_back(N);
4889 return SDValue(N, 0);
4890}
4891
Dan Gohman475871a2008-07-27 21:46:04 +00004892SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004893SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004894 SDValue Offset, ISD::MemIndexedMode AM) {
4895 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4896 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4897 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004898 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004899 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4900 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004901 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004902 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004903 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004904 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004905 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004906 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004907 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004908
Jack Carter3af4d252013-09-09 22:02:08 +00004909 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4910 dl.getDebugLoc(), VTs, AM,
Dan Gohman95531882010-03-18 18:49:47 +00004911 ST->isTruncatingStore(),
4912 ST->getMemoryVT(),
4913 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004914 CSEMap.InsertNode(N, IP);
4915 AllNodes.push_back(N);
4916 return SDValue(N, 0);
4917}
4918
Andrew Trickac6d9be2013-05-25 02:42:55 +00004919SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004920 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004921 SDValue SV,
4922 unsigned Align) {
4923 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004924 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004925}
4926
Andrew Trickac6d9be2013-05-25 02:42:55 +00004927SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004928 ArrayRef<SDUse> Ops) {
4929 switch (Ops.size()) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004930 case 0: return getNode(Opcode, DL, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07004931 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling7ade28c2009-01-28 22:17:52 +00004932 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4933 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004934 default: break;
4935 }
4936
Dan Gohman475871a2008-07-27 21:46:04 +00004937 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004938 // the regular getNode logic.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004939 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
4940 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004941}
4942
Andrew Trickac6d9be2013-05-25 02:42:55 +00004943SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004944 ArrayRef<SDValue> Ops) {
4945 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004946 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004947 case 0: return getNode(Opcode, DL, VT);
4948 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4949 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4950 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004951 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004953
Chris Lattneref847df2005-04-09 03:27:28 +00004954 switch (Opcode) {
4955 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004956 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004957 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004958 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4959 "LHS and RHS of condition must have same type!");
4960 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4961 "True and False arms of SelectCC must have same type!");
4962 assert(Ops[2].getValueType() == VT &&
4963 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004964 break;
4965 }
4966 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004967 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004968 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4969 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004970 break;
4971 }
Chris Lattneref847df2005-04-09 03:27:28 +00004972 }
4973
Chris Lattner385328c2005-05-14 07:42:29 +00004974 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004975 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004976 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004977
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004978 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004979 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004980 AddNodeIDNode(ID, Opcode, VTs, Ops);
4981 void *IP = nullptr;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004982
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004983 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004984 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004985
Jack Carter3af4d252013-09-09 22:02:08 +00004986 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004987 VTs, Ops);
Chris Lattnera5682852006-08-07 23:03:03 +00004988 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004989 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004990 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004991 VTs, Ops);
Chris Lattner43247a12005-08-25 19:12:10 +00004992 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004993
Chris Lattneref847df2005-04-09 03:27:28 +00004994 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004995#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004996 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004997#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004998 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004999}
5000
Andrew Trickac6d9be2013-05-25 02:42:55 +00005001SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005002 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5003 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Scott Michelfdc40a02009-02-17 22:15:04 +00005004}
5005
Andrew Trickac6d9be2013-05-25 02:42:55 +00005006SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005007 ArrayRef<SDValue> Ops) {
Chris Lattnerbe384162006-08-16 22:57:46 +00005008 if (VTList.NumVTs == 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07005009 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattner89c34632005-05-14 06:20:26 +00005010
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00005011#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00005012 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00005013 // FIXME: figure out how to safely handle things like
5014 // int foo(int x) { return 1 << (x & 255); }
5015 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00005016 case ISD::SRA_PARTS:
5017 case ISD::SRL_PARTS:
5018 case ISD::SHL_PARTS:
5019 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005020 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00005021 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00005022 else if (N3.getOpcode() == ISD::AND)
5023 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5024 // If the and is only masking out bits that cannot effect the shift,
5025 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00005026 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00005027 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00005028 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00005029 }
5030 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00005031 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00005032#endif
Chris Lattner89c34632005-05-14 06:20:26 +00005033
Chris Lattner43247a12005-08-25 19:12:10 +00005034 // Memoize the node unless it returns a flag.
5035 SDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005036 unsigned NumOps = Ops.size();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005037 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00005038 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005039 AddNodeIDNode(ID, Opcode, VTList, Ops);
5040 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005041 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00005042 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005043
Dan Gohman0e5f1302008-07-07 23:02:41 +00005044 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00005045 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5046 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005047 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00005048 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5049 DL.getDebugLoc(), VTList, Ops[0],
5050 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005051 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00005052 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5053 DL.getDebugLoc(), VTList, Ops[0],
5054 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005055 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00005056 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005057 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005058 }
Chris Lattnera5682852006-08-07 23:03:03 +00005059 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00005060 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00005061 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00005062 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5063 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005064 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00005065 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5066 DL.getDebugLoc(), VTList, Ops[0],
5067 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005068 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00005069 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5070 DL.getDebugLoc(), VTList, Ops[0],
5071 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005072 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00005073 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005074 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005075 }
Chris Lattner43247a12005-08-25 19:12:10 +00005076 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00005077 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00005078#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005079 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00005080#endif
Dan Gohman475871a2008-07-27 21:46:04 +00005081 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00005082}
5083
Andrew Trickac6d9be2013-05-25 02:42:55 +00005084SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07005085 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
Dan Gohman08ce9762007-10-08 15:49:58 +00005086}
5087
Andrew Trickac6d9be2013-05-25 02:42:55 +00005088SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005089 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00005090 SDValue Ops[] = { N1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005091 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005092}
5093
Andrew Trickac6d9be2013-05-25 02:42:55 +00005094SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005095 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00005096 SDValue Ops[] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005097 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005098}
5099
Andrew Trickac6d9be2013-05-25 02:42:55 +00005100SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005101 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005102 SDValue Ops[] = { N1, N2, N3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005103 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005104}
5105
Andrew Trickac6d9be2013-05-25 02:42:55 +00005106SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005107 SDValue N1, SDValue N2, SDValue N3,
5108 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00005109 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005110 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005111}
5112
Andrew Trickac6d9be2013-05-25 02:42:55 +00005113SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005114 SDValue N1, SDValue N2, SDValue N3,
5115 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00005116 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005117 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005118}
5119
Owen Andersone50ed302009-08-10 22:56:29 +00005120SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00005121 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00005122}
5123
Owen Andersone50ed302009-08-10 22:56:29 +00005124SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005125 FoldingSetNodeID ID;
5126 ID.AddInteger(2U);
5127 ID.AddInteger(VT1.getRawBits());
5128 ID.AddInteger(VT2.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005129
Stephen Hinesdce4a402014-05-29 02:49:00 -07005130 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005131 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005132 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005133 EVT *Array = Allocator.Allocate<EVT>(2);
5134 Array[0] = VT1;
5135 Array[1] = VT2;
5136 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5137 VTListMap.InsertNode(Result, IP);
5138 }
5139 return Result->getSDVTList();
Chris Lattnera3255112005-11-08 23:30:28 +00005140}
Dan Gohmane8be6c62008-07-17 19:10:17 +00005141
Owen Andersone50ed302009-08-10 22:56:29 +00005142SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005143 FoldingSetNodeID ID;
5144 ID.AddInteger(3U);
5145 ID.AddInteger(VT1.getRawBits());
5146 ID.AddInteger(VT2.getRawBits());
5147 ID.AddInteger(VT3.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005148
Stephen Hinesdce4a402014-05-29 02:49:00 -07005149 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005150 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005151 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005152 EVT *Array = Allocator.Allocate<EVT>(3);
5153 Array[0] = VT1;
5154 Array[1] = VT2;
5155 Array[2] = VT3;
5156 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5157 VTListMap.InsertNode(Result, IP);
5158 }
5159 return Result->getSDVTList();
Chris Lattner70046e92006-08-15 17:46:01 +00005160}
5161
Owen Andersone50ed302009-08-10 22:56:29 +00005162SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005163 FoldingSetNodeID ID;
5164 ID.AddInteger(4U);
5165 ID.AddInteger(VT1.getRawBits());
5166 ID.AddInteger(VT2.getRawBits());
5167 ID.AddInteger(VT3.getRawBits());
5168 ID.AddInteger(VT4.getRawBits());
Bill Wendling13d6d442008-12-01 23:28:22 +00005169
Stephen Hinesdce4a402014-05-29 02:49:00 -07005170 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005171 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005172 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005173 EVT *Array = Allocator.Allocate<EVT>(4);
5174 Array[0] = VT1;
5175 Array[1] = VT2;
5176 Array[2] = VT3;
5177 Array[3] = VT4;
5178 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5179 VTListMap.InsertNode(Result, IP);
5180 }
5181 return Result->getSDVTList();
Bill Wendling13d6d442008-12-01 23:28:22 +00005182}
5183
Stephen Hinesdce4a402014-05-29 02:49:00 -07005184SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5185 unsigned NumVTs = VTs.size();
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005186 FoldingSetNodeID ID;
5187 ID.AddInteger(NumVTs);
5188 for (unsigned index = 0; index < NumVTs; index++) {
5189 ID.AddInteger(VTs[index].getRawBits());
Chris Lattner70046e92006-08-15 17:46:01 +00005190 }
5191
Stephen Hinesdce4a402014-05-29 02:49:00 -07005192 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005193 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005194 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005195 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005196 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005197 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5198 VTListMap.InsertNode(Result, IP);
Chris Lattner70046e92006-08-15 17:46:01 +00005199 }
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005200 return Result->getSDVTList();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00005201}
5202
5203
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005204/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5205/// specified operands. If the resultant node already exists in the DAG,
5206/// this does not modify the specified node, instead it returns the node that
5207/// already exists. If the resultant node does not exist in the DAG, the
5208/// input node is returned. As a degenerate case, if you specify the same
5209/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00005210SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005211 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005212
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005213 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00005214 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005215
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005216 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005217 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005218 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005219 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005220
Dan Gohman79acd2b2008-07-21 22:38:59 +00005221 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005222 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005223 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005224 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005225
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005226 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005227 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00005228
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005229 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005230 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005231 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005232}
5233
Dan Gohman027657d2010-06-18 15:30:29 +00005234SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005235 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005236
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005237 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005238 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00005239 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00005240
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005241 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005242 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005243 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005244 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005245
Dan Gohman79acd2b2008-07-21 22:38:59 +00005246 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005247 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005248 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005249 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005250
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005251 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005252 if (N->OperandList[0] != Op1)
5253 N->OperandList[0].set(Op1);
5254 if (N->OperandList[1] != Op2)
5255 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005256
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005257 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005258 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005259 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005260}
5261
Dan Gohman027657d2010-06-18 15:30:29 +00005262SDNode *SelectionDAG::
5263UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005264 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005265 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005266}
5267
Dan Gohman027657d2010-06-18 15:30:29 +00005268SDNode *SelectionDAG::
5269UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005270 SDValue Op3, SDValue Op4) {
5271 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005272 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005273}
5274
Dan Gohman027657d2010-06-18 15:30:29 +00005275SDNode *SelectionDAG::
5276UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005277 SDValue Op3, SDValue Op4, SDValue Op5) {
5278 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005279 return UpdateNodeOperands(N, Ops);
Chris Lattner809ec112006-01-28 10:09:25 +00005280}
5281
Dan Gohman027657d2010-06-18 15:30:29 +00005282SDNode *SelectionDAG::
Stephen Hinesdce4a402014-05-29 02:49:00 -07005283UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5284 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005285 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005286 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005287
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005288 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005289 bool AnyChange = false;
5290 for (unsigned i = 0; i != NumOps; ++i) {
5291 if (Ops[i] != N->getOperand(i)) {
5292 AnyChange = true;
5293 break;
5294 }
5295 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005296
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005297 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00005298 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005299
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005300 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005301 void *InsertPos = nullptr;
5302 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005303 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005304
Dan Gohman7ceda162008-05-02 00:05:03 +00005305 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005306 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005307 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005308 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005309
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005310 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005311 for (unsigned i = 0; i != NumOps; ++i)
5312 if (N->OperandList[i] != Ops[i])
5313 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005314
5315 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005316 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005317 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005318}
5319
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005320/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005321/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005322void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005323 // Unlike the code in MorphNodeTo that does this, we don't need to
5324 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005325 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5326 SDUse &Use = *I++;
5327 Use.set(SDValue());
5328 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005329}
Chris Lattner149c58c2005-08-16 18:17:10 +00005330
Dan Gohmane8be6c62008-07-17 19:10:17 +00005331/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5332/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005333///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005334SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005335 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005336 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005337 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner7651fa42005-08-24 23:00:29 +00005338}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005339
Dan Gohmane8be6c62008-07-17 19:10:17 +00005340SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005341 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005342 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005343 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005344 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005345}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005346
Dan Gohmane8be6c62008-07-17 19:10:17 +00005347SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005348 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005349 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005350 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005351 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005352 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005353}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005354
Dan Gohmane8be6c62008-07-17 19:10:17 +00005355SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005356 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005357 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005358 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005359 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005360 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005361}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005362
Dan Gohmane8be6c62008-07-17 19:10:17 +00005363SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005364 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005365 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005366 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005367}
5368
Dan Gohmane8be6c62008-07-17 19:10:17 +00005369SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005370 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005371 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005372 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005373}
5374
Dan Gohmane8be6c62008-07-17 19:10:17 +00005375SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005376 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005377 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005378 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohmancd920d92008-07-02 23:23:19 +00005379}
5380
Dan Gohmane8be6c62008-07-17 19:10:17 +00005381SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005382 EVT VT1, EVT VT2, EVT VT3,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005383 ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005384 SDVTList VTs = getVTList(VT1, VT2, VT3);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005385 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005386}
5387
Bill Wendling13d6d442008-12-01 23:28:22 +00005388SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005389 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005390 ArrayRef<SDValue> Ops) {
Bill Wendling13d6d442008-12-01 23:28:22 +00005391 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005392 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005393}
5394
Scott Michelfdc40a02009-02-17 22:15:04 +00005395SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005396 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005397 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005398 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005399 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005400 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005401}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005402
Scott Michelfdc40a02009-02-17 22:15:04 +00005403SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005404 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005405 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005406 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005407 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005408 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005409}
5410
Dan Gohmane8be6c62008-07-17 19:10:17 +00005411SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005412 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005413 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005415 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005416 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005417 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005418}
5419
Dan Gohmane8be6c62008-07-17 19:10:17 +00005420SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005421 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005422 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005423 SDValue Op3) {
5424 SDVTList VTs = getVTList(VT1, VT2, VT3);
5425 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005426 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005427}
5428
5429SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005430 SDVTList VTs,ArrayRef<SDValue> Ops) {
5431 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner5857e0a2010-02-23 23:01:35 +00005432 // Reset the NodeID to -1.
5433 N->setNodeId(-1);
5434 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005435}
5436
Andrew Trickac6d9be2013-05-25 02:42:55 +00005437/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel0508d042011-12-15 18:21:18 +00005438/// the line number information on the merged node since it is not possible to
5439/// preserve the information that operation is associated with multiple lines.
5440/// This will make the debugger working better at -O0, were there is a higher
5441/// probability having other instructions associated with that line.
5442///
Andrew Trickac6d9be2013-05-25 02:42:55 +00005443/// For IROrder, we keep the smaller of the two
5444SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel0508d042011-12-15 18:21:18 +00005445 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005446 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5447 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel0508d042011-12-15 18:21:18 +00005448 N->setDebugLoc(DebugLoc());
5449 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005450 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5451 N->setIROrder(Order);
Devang Patel0508d042011-12-15 18:21:18 +00005452 return N;
5453}
5454
Chris Lattner21221e32010-02-28 21:36:14 +00005455/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005456/// return type, opcode, and operands.
5457///
5458/// Note that MorphNodeTo returns the resultant node. If there is already a
5459/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickac6d9be2013-05-25 02:42:55 +00005460/// the current one. Note that the SDLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005461///
5462/// Using MorphNodeTo is faster than creating a new node and swapping it in
5463/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005464/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005465/// the node's users.
5466///
5467SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005468 SDVTList VTs, ArrayRef<SDValue> Ops) {
5469 unsigned NumOps = Ops.size();
Dan Gohmancd920d92008-07-02 23:23:19 +00005470 // If an identical node already exists, use it.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005471 void *IP = nullptr;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005472 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005473 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005474 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005475 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005476 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohmane8be6c62008-07-17 19:10:17 +00005477 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005478
Dan Gohman095cc292008-09-13 01:54:27 +00005479 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005480 IP = nullptr;
Chris Lattner67612a12007-02-04 02:32:44 +00005481
Dan Gohmane8be6c62008-07-17 19:10:17 +00005482 // Start the morphing.
5483 N->NodeType = Opc;
5484 N->ValueList = VTs.VTs;
5485 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005486
Dan Gohmane8be6c62008-07-17 19:10:17 +00005487 // Clear the operands list, updating used nodes to remove this from their
5488 // use list. Keep track of any operands that become dead as a result.
5489 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005490 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5491 SDUse &Use = *I++;
5492 SDNode *Used = Use.getNode();
5493 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005494 if (Used->use_empty())
5495 DeadNodeSet.insert(Used);
5496 }
5497
Dan Gohmanc76909a2009-09-25 20:36:54 +00005498 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5499 // Initialize the memory references information.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005500 MN->setMemRefs(nullptr, nullptr);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005501 // If NumOps is larger than the # of operands we can have in a
5502 // MachineSDNode, reallocate the operand list.
5503 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5504 if (MN->OperandsNeedDelete)
5505 delete[] MN->OperandList;
5506 if (NumOps > array_lengthof(MN->LocalOperands))
5507 // We're creating a final node that will live unmorphed for the
5508 // remainder of the current SelectionDAG iteration, so we can allocate
5509 // the operands directly out of a pool with no recycling metadata.
5510 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005511 Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005512 else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005513 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005514 MN->OperandsNeedDelete = false;
5515 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005516 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005517 } else {
5518 // If NumOps is larger than the # of operands we currently have, reallocate
5519 // the operand list.
5520 if (NumOps > N->NumOperands) {
5521 if (N->OperandsNeedDelete)
5522 delete[] N->OperandList;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005523 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005524 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005525 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005526 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005527 }
5528
5529 // Delete any nodes that are still dead after adding the uses for the
5530 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005531 if (!DeadNodeSet.empty()) {
5532 SmallVector<SDNode *, 16> DeadNodes;
5533 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5534 E = DeadNodeSet.end(); I != E; ++I)
5535 if ((*I)->use_empty())
5536 DeadNodes.push_back(*I);
5537 RemoveDeadNodes(DeadNodes);
5538 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005539
Dan Gohmane8be6c62008-07-17 19:10:17 +00005540 if (IP)
5541 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005542 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005543}
5544
Chris Lattner0fb094f2005-11-19 01:44:53 +00005545
Dan Gohman602b0c82009-09-25 18:54:59 +00005546/// getMachineNode - These are used for target selectors to create a new node
5547/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005548///
Dan Gohman602b0c82009-09-25 18:54:59 +00005549/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005550/// node of the specified opcode and operands, it returns that node instead of
5551/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005552MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005553SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005554 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005555 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005556}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005557
Dan Gohmanc81b7832009-10-10 01:29:16 +00005558MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005559SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005560 SDVTList VTs = getVTList(VT);
5561 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005562 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005563}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005564
Dan Gohmanc81b7832009-10-10 01:29:16 +00005565MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005566SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005567 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005568 SDVTList VTs = getVTList(VT);
5569 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005570 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005571}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005572
Dan Gohmanc81b7832009-10-10 01:29:16 +00005573MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005574SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005575 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005576 SDVTList VTs = getVTList(VT);
5577 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005578 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005579}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005580
Dan Gohmanc81b7832009-10-10 01:29:16 +00005581MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005582SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005583 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005584 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005585 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005586}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005587
Dan Gohmanc81b7832009-10-10 01:29:16 +00005588MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005589SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005590 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005591 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005592}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005593
Dan Gohmanc81b7832009-10-10 01:29:16 +00005594MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005595SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005596 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005597 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005598 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005599 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005600}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005601
Dan Gohmanc81b7832009-10-10 01:29:16 +00005602MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005603SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005604 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005605 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005606 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005607 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005608}
5609
Dan Gohmanc81b7832009-10-10 01:29:16 +00005610MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005611SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005612 EVT VT1, EVT VT2, SDValue Op1,
5613 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005614 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005615 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005616 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005617}
5618
Dan Gohmanc81b7832009-10-10 01:29:16 +00005619MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005620SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005621 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005622 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005623 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005624 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005625}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005626
Dan Gohmanc81b7832009-10-10 01:29:16 +00005627MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005628SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005629 EVT VT1, EVT VT2, EVT VT3,
5630 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005631 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005632 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005633 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005634}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005635
Dan Gohmanc81b7832009-10-10 01:29:16 +00005636MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005637SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005638 EVT VT1, EVT VT2, EVT VT3,
5639 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005640 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005641 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005642 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005643}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005644
Dan Gohmanc81b7832009-10-10 01:29:16 +00005645MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005646SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005647 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005648 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005649 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005650 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005651}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005652
Dan Gohmanc81b7832009-10-10 01:29:16 +00005653MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005654SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005655 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005656 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005657 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005658 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005659}
5660
Dan Gohmanc81b7832009-10-10 01:29:16 +00005661MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005662SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005663 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005664 ArrayRef<SDValue> Ops) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07005665 SDVTList VTs = getVTList(ResultTys);
Michael Liao2a8bea72013-04-19 22:22:57 +00005666 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005667}
5668
Dan Gohmanc81b7832009-10-10 01:29:16 +00005669MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005670SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005671 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005672 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005673 MachineSDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005674 void *IP = nullptr;
Michael Liao2a8bea72013-04-19 22:22:57 +00005675 const SDValue *Ops = OpsArray.data();
5676 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005677
5678 if (DoCSE) {
5679 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005680 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
5681 IP = nullptr;
Devang Patel0508d042011-12-15 18:21:18 +00005682 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005683 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel0508d042011-12-15 18:21:18 +00005684 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005685 }
5686
5687 // Allocate a new MachineSDNode.
Jack Carter3af4d252013-09-09 22:02:08 +00005688 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5689 DL.getDebugLoc(), VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005690
5691 // Initialize the operands list.
5692 if (NumOps > array_lengthof(N->LocalOperands))
5693 // We're creating a final node that will live unmorphed for the
5694 // remainder of the current SelectionDAG iteration, so we can allocate
5695 // the operands directly out of a pool with no recycling metadata.
5696 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5697 Ops, NumOps);
5698 else
5699 N->InitOperands(N->LocalOperands, Ops, NumOps);
5700 N->OperandsNeedDelete = false;
5701
5702 if (DoCSE)
5703 CSEMap.InsertNode(N, IP);
5704
5705 AllNodes.push_back(N);
5706#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005707 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005708#endif
5709 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005710}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005711
Dan Gohman6a402dc2009-08-19 18:16:17 +00005712/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005713/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005714SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005715SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohman6a402dc2009-08-19 18:16:17 +00005716 SDValue Operand) {
5717 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005718 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005719 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005720 return SDValue(Subreg, 0);
5721}
5722
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005723/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005724/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005725SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005726SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005727 SDValue Operand, SDValue Subreg) {
5728 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005729 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005730 VT, Operand, Subreg, SRIdxVal);
5731 return SDValue(Result, 0);
5732}
5733
Evan Cheng08b11732008-03-22 01:55:50 +00005734/// getNodeIfExists - Get the specified node if it's already available, or
5735/// else return NULL.
5736SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07005737 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5738 bool exact) {
5739 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005740 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005741 AddNodeIDNode(ID, Opcode, VTList, Ops);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07005742 if (isBinOpWithFlags(Opcode))
5743 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005744 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005745 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005746 return E;
5747 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07005748 return nullptr;
Evan Cheng08b11732008-03-22 01:55:50 +00005749}
5750
Evan Cheng31441b72010-03-29 20:48:30 +00005751/// getDbgValue - Creates a SDDbgValue node.
5752///
Stephen Hinesdce4a402014-05-29 02:49:00 -07005753/// SDNode
Evan Cheng31441b72010-03-29 20:48:30 +00005754SDDbgValue *
Stephen Hinesdce4a402014-05-29 02:49:00 -07005755SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
5756 bool IsIndirect, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005757 DebugLoc DL, unsigned O) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07005758 return new (Allocator) SDDbgValue(MDPtr, N, R, IsIndirect, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005759}
5760
Stephen Hinesdce4a402014-05-29 02:49:00 -07005761/// Constant
Evan Cheng31441b72010-03-29 20:48:30 +00005762SDDbgValue *
Stephen Hinesdce4a402014-05-29 02:49:00 -07005763SelectionDAG::getConstantDbgValue(MDNode *MDPtr, const Value *C,
5764 uint64_t Off,
5765 DebugLoc DL, unsigned O) {
Evan Cheng31441b72010-03-29 20:48:30 +00005766 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5767}
5768
Stephen Hinesdce4a402014-05-29 02:49:00 -07005769/// FrameIndex
Evan Cheng31441b72010-03-29 20:48:30 +00005770SDDbgValue *
Stephen Hinesdce4a402014-05-29 02:49:00 -07005771SelectionDAG::getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5772 DebugLoc DL, unsigned O) {
Evan Cheng31441b72010-03-29 20:48:30 +00005773 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5774}
5775
Dan Gohmana72d2a22010-03-03 21:33:37 +00005776namespace {
5777
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005778/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005779/// pointed to by a use iterator is deleted, increment the use iterator
5780/// so that it doesn't dangle.
5781///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005782class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005783 SDNode::use_iterator &UI;
5784 SDNode::use_iterator &UE;
5785
Stephen Hines36b56882014-04-23 16:57:46 -07005786 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005787 // Increment the iterator as needed.
5788 while (UI != UE && N == *UI)
5789 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005790 }
5791
5792public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005793 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005794 SDNode::use_iterator &ui,
5795 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005796 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005797};
5798
5799}
5800
Evan Cheng99157a02006-08-07 22:13:29 +00005801/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005802/// This can cause recursive merging of nodes in the DAG.
5803///
Chris Lattner11d049c2008-02-03 03:35:22 +00005804/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005805///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005806void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005807 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005808 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005809 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005810 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005811
Dan Gohman39946102009-01-25 16:29:12 +00005812 // Iterate over all the existing uses of From. New uses will be added
5813 // to the beginning of the use list, which we avoid visiting.
5814 // This specifically avoids visiting uses of From that arise while the
5815 // replacement is happening, because any such uses would be the result
5816 // of CSE: If an existing node looks like From after one of its operands
5817 // is replaced by To, we don't want to replace of all its users with To
5818 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005819 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005820 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005821 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005822 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005823
Chris Lattner8b8749f2005-08-17 19:00:20 +00005824 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005825 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005826
Dan Gohman39946102009-01-25 16:29:12 +00005827 // A user can appear in a use list multiple times, and when this
5828 // happens the uses are usually next to each other in the list.
5829 // To help reduce the number of CSE recomputations, process all
5830 // the uses of this user that we can find this way.
5831 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005832 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005833 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005834 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005835 } while (UI != UE && *UI == User);
5836
5837 // Now that we have modified User, add it back to the CSE maps. If it
5838 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005839 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005840 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005841
5842 // If we just RAUW'd the root, take note.
5843 if (FromN == getRoot())
5844 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005845}
5846
5847/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5848/// This can cause recursive merging of nodes in the DAG.
5849///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005850/// This version assumes that for each value of From, there is a
5851/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005852///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005853void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005854#ifndef NDEBUG
5855 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5856 assert((!From->hasAnyUseOfValue(i) ||
5857 From->getValueType(i) == To->getValueType(i)) &&
5858 "Cannot use this version of ReplaceAllUsesWith!");
5859#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005860
5861 // Handle the trivial case.
5862 if (From == To)
5863 return;
5864
Dan Gohmandbe664a2009-01-19 21:44:21 +00005865 // Iterate over just the existing users of From. See the comments in
5866 // the ReplaceAllUsesWith above.
5867 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005868 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005869 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005870 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005871
Chris Lattner8b52f212005-08-26 18:36:28 +00005872 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005873 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005874
Dan Gohman39946102009-01-25 16:29:12 +00005875 // A user can appear in a use list multiple times, and when this
5876 // happens the uses are usually next to each other in the list.
5877 // To help reduce the number of CSE recomputations, process all
5878 // the uses of this user that we can find this way.
5879 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005880 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005881 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005882 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005883 } while (UI != UE && *UI == User);
5884
5885 // Now that we have modified User, add it back to the CSE maps. If it
5886 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005887 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005888 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005889
5890 // If we just RAUW'd the root, take note.
5891 if (From == getRoot().getNode())
5892 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005893}
5894
Chris Lattner8b52f212005-08-26 18:36:28 +00005895/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5896/// This can cause recursive merging of nodes in the DAG.
5897///
5898/// This version can replace From with any result values. To must match the
5899/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005900void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005901 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005902 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005903
Dan Gohmandbe664a2009-01-19 21:44:21 +00005904 // Iterate over just the existing users of From. See the comments in
5905 // the ReplaceAllUsesWith above.
5906 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005907 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005908 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005909 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005910
Chris Lattner7b2880c2005-08-24 22:44:39 +00005911 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005912 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005913
Dan Gohman39946102009-01-25 16:29:12 +00005914 // A user can appear in a use list multiple times, and when this
5915 // happens the uses are usually next to each other in the list.
5916 // To help reduce the number of CSE recomputations, process all
5917 // the uses of this user that we can find this way.
5918 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005919 SDUse &Use = UI.getUse();
5920 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005921 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005922 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005923 } while (UI != UE && *UI == User);
5924
5925 // Now that we have modified User, add it back to the CSE maps. If it
5926 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005927 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005928 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005929
5930 // If we just RAUW'd the root, take note.
5931 if (From == getRoot().getNode())
5932 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005933}
5934
Chris Lattner012f2412006-02-17 21:58:01 +00005935/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005936/// uses of other values produced by From.getNode() alone. The Deleted
5937/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005938void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005939 // Handle the really simple, really trivial case efficiently.
5940 if (From == To) return;
5941
Chris Lattner012f2412006-02-17 21:58:01 +00005942 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005943 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005944 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005945 return;
5946 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005947
Dan Gohman39946102009-01-25 16:29:12 +00005948 // Iterate over just the existing users of From. See the comments in
5949 // the ReplaceAllUsesWith above.
5950 SDNode::use_iterator UI = From.getNode()->use_begin(),
5951 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005952 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005953 while (UI != UE) {
5954 SDNode *User = *UI;
5955 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005956
Dan Gohman39946102009-01-25 16:29:12 +00005957 // A user can appear in a use list multiple times, and when this
5958 // happens the uses are usually next to each other in the list.
5959 // To help reduce the number of CSE recomputations, process all
5960 // the uses of this user that we can find this way.
5961 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005962 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005963
5964 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005965 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005966 ++UI;
5967 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005968 }
Dan Gohman39946102009-01-25 16:29:12 +00005969
5970 // If this node hasn't been modified yet, it's still in the CSE maps,
5971 // so remove its old self from the CSE maps.
5972 if (!UserRemovedFromCSEMaps) {
5973 RemoveNodeFromCSEMaps(User);
5974 UserRemovedFromCSEMaps = true;
5975 }
5976
5977 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005978 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005979 } while (UI != UE && *UI == User);
5980
5981 // We are iterating over all uses of the From node, so if a use
5982 // doesn't use the specific value, no changes are made.
5983 if (!UserRemovedFromCSEMaps)
5984 continue;
5985
Chris Lattner01d029b2007-10-15 06:10:22 +00005986 // Now that we have modified User, add it back to the CSE maps. If it
5987 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005988 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005989 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005990
5991 // If we just RAUW'd the root, take note.
5992 if (From == getRoot())
5993 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005994}
5995
Dan Gohman39946102009-01-25 16:29:12 +00005996namespace {
5997 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5998 /// to record information about a use.
5999 struct UseMemo {
6000 SDNode *User;
6001 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00006002 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00006003 };
Dan Gohmane7852d02009-01-26 04:35:06 +00006004
6005 /// operator< - Sort Memos by User.
6006 bool operator<(const UseMemo &L, const UseMemo &R) {
6007 return (intptr_t)L.User < (intptr_t)R.User;
6008 }
Dan Gohman39946102009-01-25 16:29:12 +00006009}
6010
Dan Gohmane8be6c62008-07-17 19:10:17 +00006011/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00006012/// uses of other values produced by From.getNode() alone. The same value
6013/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00006014/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00006015void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6016 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006017 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00006018 // Handle the simple, trivial case efficiently.
6019 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006020 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00006021
Dan Gohman39946102009-01-25 16:29:12 +00006022 // Read up all the uses and make records of them. This helps
6023 // processing new uses that are introduced during the
6024 // replacement process.
6025 SmallVector<UseMemo, 4> Uses;
6026 for (unsigned i = 0; i != Num; ++i) {
6027 unsigned FromResNo = From[i].getResNo();
6028 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00006029 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00006030 E = FromNode->use_end(); UI != E; ++UI) {
6031 SDUse &Use = UI.getUse();
6032 if (Use.getResNo() == FromResNo) {
6033 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00006034 Uses.push_back(Memo);
6035 }
Dan Gohmane7852d02009-01-26 04:35:06 +00006036 }
Dan Gohman39946102009-01-25 16:29:12 +00006037 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00006038
Dan Gohmane7852d02009-01-26 04:35:06 +00006039 // Sort the uses, so that all the uses from a given User are together.
6040 std::sort(Uses.begin(), Uses.end());
6041
Dan Gohman39946102009-01-25 16:29:12 +00006042 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6043 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00006044 // We know that this user uses some value of From. If it is the right
6045 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00006046 SDNode *User = Uses[UseIndex].User;
6047
6048 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00006049 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00006050
Dan Gohmane7852d02009-01-26 04:35:06 +00006051 // The Uses array is sorted, so all the uses for a given User
6052 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00006053 // To help reduce the number of CSE recomputations, process all
6054 // the uses of this user that we can find this way.
6055 do {
6056 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00006057 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00006058 ++UseIndex;
6059
Dan Gohmane7852d02009-01-26 04:35:06 +00006060 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00006061 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6062
Dan Gohmane8be6c62008-07-17 19:10:17 +00006063 // Now that we have modified User, add it back to the CSE maps. If it
6064 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006065 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00006066 }
6067}
6068
Evan Chenge6f35d82006-08-01 08:20:41 +00006069/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00006070/// based on their topological order. It returns the maximum id and a vector
6071/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00006072unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00006073
Dan Gohmanf06c8352008-09-30 18:30:35 +00006074 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00006075
Dan Gohmanf06c8352008-09-30 18:30:35 +00006076 // SortedPos tracks the progress of the algorithm. Nodes before it are
6077 // sorted, nodes after it are unsorted. When the algorithm completes
6078 // it is at the end of the list.
6079 allnodes_iterator SortedPos = allnodes_begin();
6080
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006081 // Visit all the nodes. Move nodes with no operands to the front of
6082 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00006083 // operand count. Before we do this, the Node Id fields of the nodes
6084 // may contain arbitrary values. After, the Node Id fields for nodes
6085 // before SortedPos will contain the topological sort index, and the
6086 // Node Id fields for nodes At SortedPos and after will contain the
6087 // count of outstanding operands.
6088 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6089 SDNode *N = I++;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006090 checkForCycles(N, this);
Dan Gohmanf06c8352008-09-30 18:30:35 +00006091 unsigned Degree = N->getNumOperands();
6092 if (Degree == 0) {
6093 // A node with no uses, add it to the result array immediately.
6094 N->setNodeId(DAGSize++);
6095 allnodes_iterator Q = N;
6096 if (Q != SortedPos)
6097 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00006098 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006099 ++SortedPos;
6100 } else {
6101 // Temporarily use the Node Id as scratch space for the degree count.
6102 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00006103 }
6104 }
6105
Chad Rosierf496dea2012-05-21 17:13:41 +00006106 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00006107 // such that by the time the end is reached all nodes will be sorted.
6108 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6109 SDNode *N = I;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006110 checkForCycles(N, this);
David Greene221925e2010-01-20 00:59:23 +00006111 // N is in sorted position, so all its uses have one less operand
6112 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00006113 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6114 UI != UE; ++UI) {
6115 SDNode *P = *UI;
6116 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00006117 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006118 --Degree;
6119 if (Degree == 0) {
6120 // All of P's operands are sorted, so P may sorted now.
6121 P->setNodeId(DAGSize++);
6122 if (P != SortedPos)
6123 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00006124 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006125 ++SortedPos;
6126 } else {
6127 // Update P's outstanding operand count.
6128 P->setNodeId(Degree);
6129 }
6130 }
David Greene221925e2010-01-20 00:59:23 +00006131 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00006132#ifndef NDEBUG
6133 SDNode *S = ++I;
6134 dbgs() << "Overran sorted position:\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006135 S->dumprFull(this); dbgs() << "\n";
6136 dbgs() << "Checking if this is due to cycles\n";
6137 checkForCycles(this, true);
David Greene39143702010-02-09 23:03:05 +00006138#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -07006139 llvm_unreachable(nullptr);
David Greene221925e2010-01-20 00:59:23 +00006140 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00006141 }
6142
6143 assert(SortedPos == AllNodes.end() &&
6144 "Topological sort incomplete!");
6145 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6146 "First node in topological sort is not the entry token!");
6147 assert(AllNodes.front().getNodeId() == 0 &&
6148 "First node in topological sort has non-zero id!");
6149 assert(AllNodes.front().getNumOperands() == 0 &&
6150 "First node in topological sort has operands!");
6151 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6152 "Last node in topologic sort has unexpected id!");
6153 assert(AllNodes.back().use_empty() &&
6154 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006155 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006156 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00006157}
6158
Evan Chengbfcb3052010-03-25 01:38:16 +00006159/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6160/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00006161void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6162 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00006163 if (SD)
6164 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00006165}
Evan Cheng091cba12006-07-27 06:39:06 +00006166
Devang Patela2e868d2011-01-25 23:27:42 +00006167/// TransferDbgValues - Transfer SDDbgValues.
6168void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6169 if (From == To || !From.getNode()->getHasDebugValue())
6170 return;
6171 SDNode *FromNode = From.getNode();
6172 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006173 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00006174 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006175 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00006176 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00006177 SDDbgValue *Dbg = *I;
6178 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6179 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07006180 Dbg->isIndirect(),
Devang Patela778f5c2011-02-18 22:43:42 +00006181 Dbg->getOffset(), Dbg->getDebugLoc(),
6182 Dbg->getOrder());
6183 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00006184 }
6185 }
Craig Topperf22fd3f2013-07-03 05:11:49 +00006186 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patela778f5c2011-02-18 22:43:42 +00006187 E = ClonedDVs.end(); I != E; ++I)
6188 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00006189}
6190
Jim Laskey58b968b2005-08-17 20:08:02 +00006191//===----------------------------------------------------------------------===//
6192// SDNode Class
6193//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00006194
Chris Lattner48b85922007-02-04 02:41:42 +00006195HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00006196 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00006197}
6198
Andrew Trickac6d9be2013-05-25 02:42:55 +00006199GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6200 DebugLoc DL, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006201 EVT VT, int64_t o, unsigned char TF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006202 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00006203 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00006204}
Chris Lattner48b85922007-02-04 02:41:42 +00006205
Matt Arsenault59d3ae62013-11-15 01:34:59 +00006206AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6207 SDValue X, unsigned SrcAS,
6208 unsigned DestAS)
6209 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6210 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6211
Andrew Trickac6d9be2013-05-25 02:42:55 +00006212MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6213 EVT memvt, MachineMemOperand *mmo)
6214 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006215 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006216 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006217 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00006218 assert(isNonTemporal() == MMO->isNonTemporal() &&
6219 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00006220 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00006221}
6222
Andrew Trickac6d9be2013-05-25 02:42:55 +00006223MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006224 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6225 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohmanc76909a2009-09-25 20:36:54 +00006226 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006227 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006228 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006229 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6230 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00006231}
6232
Jim Laskey583bd472006-10-27 23:46:08 +00006233/// Profile - Gather unique data for the node.
6234///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00006235void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00006236 AddNodeIDNode(ID, this);
6237}
6238
Owen Andersond8110fb2009-08-25 22:27:22 +00006239namespace {
6240 struct EVTArray {
6241 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006242
Owen Andersond8110fb2009-08-25 22:27:22 +00006243 EVTArray() {
6244 VTs.reserve(MVT::LAST_VALUETYPE);
6245 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6246 VTs.push_back(MVT((MVT::SimpleValueType)i));
6247 }
6248 };
6249}
6250
Owen Andersone50ed302009-08-10 22:56:29 +00006251static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00006252static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00006253static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00006254
Chris Lattnera3255112005-11-08 23:30:28 +00006255/// getValueTypeList - Return a pointer to the specified value type.
6256///
Owen Andersone50ed302009-08-10 22:56:29 +00006257const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006258 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00006259 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00006260 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00006261 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00006262 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00006263 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00006264 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00006265 }
Chris Lattnera3255112005-11-08 23:30:28 +00006266}
Duncan Sandsaf47b112007-10-16 09:56:48 +00006267
Chris Lattner5c884562005-01-12 18:37:47 +00006268/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6269/// indicated value. This method ignores uses of other values defined by this
6270/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00006271bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00006272 assert(Value < getNumValues() && "Bad value!");
6273
Roman Levensteindc1adac2008-04-07 10:06:32 +00006274 // TODO: Only iterate over uses of a given value of the node
6275 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00006276 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00006277 if (NUses == 0)
6278 return false;
6279 --NUses;
6280 }
Chris Lattner5c884562005-01-12 18:37:47 +00006281 }
6282
6283 // Found exactly the right number of uses?
6284 return NUses == 0;
6285}
6286
6287
Evan Cheng33d55952007-08-02 05:29:38 +00006288/// hasAnyUseOfValue - Return true if there are any use of the indicated
6289/// value. This method ignores uses of other values defined by this operation.
6290bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6291 assert(Value < getNumValues() && "Bad value!");
6292
Dan Gohman1373c1c2008-07-09 22:39:01 +00006293 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00006294 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00006295 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00006296
6297 return false;
6298}
6299
6300
Dan Gohman2a629952008-07-27 18:06:42 +00006301/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00006302///
Dan Gohman2a629952008-07-27 18:06:42 +00006303bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00006304 bool Seen = false;
6305 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006306 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006307 if (User == this)
6308 Seen = true;
6309 else
6310 return false;
6311 }
6312
6313 return Seen;
6314}
6315
Evan Chenge6e97e62006-11-03 07:31:32 +00006316/// isOperand - Return true if this node is an operand of N.
6317///
Dan Gohman475871a2008-07-27 21:46:04 +00006318bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006319 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6320 if (*this == N->getOperand(i))
6321 return true;
6322 return false;
6323}
6324
Evan Cheng917be682008-03-04 00:41:45 +00006325bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006326 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006327 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006328 return true;
6329 return false;
6330}
Evan Cheng4ee62112006-02-05 06:29:23 +00006331
Chris Lattner572dee72008-01-16 05:49:24 +00006332/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006333/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006334/// side-effecting instructions on any chain path. In practice, this looks
6335/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006336/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006337bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006338 unsigned Depth) const {
6339 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006340
Chris Lattner572dee72008-01-16 05:49:24 +00006341 // Don't search too deeply, we just want to be able to see through
6342 // TokenFactor's etc.
6343 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006344
Chris Lattner572dee72008-01-16 05:49:24 +00006345 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006346 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006347 if (getOpcode() == ISD::TokenFactor) {
6348 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006349 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6350 return false;
6351 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006353
Chris Lattner572dee72008-01-16 05:49:24 +00006354 // Loads don't have side effects, look through them.
6355 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6356 if (!Ld->isVolatile())
6357 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6358 }
6359 return false;
6360}
6361
Lang Hames944520f2011-07-07 04:31:51 +00006362/// hasPredecessor - Return true if N is a predecessor of this node.
6363/// N is either an operand of this node, or can be reached by recursively
6364/// traversing up the operands.
6365/// NOTE: This is an expensive method. Use it carefully.
6366bool SDNode::hasPredecessor(const SDNode *N) const {
6367 SmallPtrSet<const SDNode *, 32> Visited;
6368 SmallVector<const SDNode *, 16> Worklist;
6369 return hasPredecessorHelper(N, Visited, Worklist);
6370}
Dan Gohman6688d612009-10-28 03:44:30 +00006371
Craig Toppera0ec3f92013-07-14 04:42:23 +00006372bool
6373SDNode::hasPredecessorHelper(const SDNode *N,
6374 SmallPtrSet<const SDNode *, 32> &Visited,
6375 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames944520f2011-07-07 04:31:51 +00006376 if (Visited.empty()) {
6377 Worklist.push_back(this);
6378 } else {
6379 // Take a look in the visited set. If we've already encountered this node
6380 // we needn't search further.
6381 if (Visited.count(N))
6382 return true;
6383 }
6384
6385 // Haven't visited N yet. Continue the search.
6386 while (!Worklist.empty()) {
6387 const SDNode *M = Worklist.pop_back_val();
6388 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6389 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006390 if (Visited.insert(Op))
6391 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006392 if (Op == N)
6393 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006394 }
Lang Hames944520f2011-07-07 04:31:51 +00006395 }
Dan Gohman6688d612009-10-28 03:44:30 +00006396
6397 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006398}
6399
Evan Chengc5484282006-10-04 00:56:09 +00006400uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6401 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006402 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006403}
6404
Mon P Wangcd6e7252009-11-30 02:42:02 +00006405SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6406 assert(N->getNumValues() == 1 &&
6407 "Can't unroll a vector with multiple results!");
6408
6409 EVT VT = N->getValueType(0);
6410 unsigned NE = VT.getVectorNumElements();
6411 EVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006412 SDLoc dl(N);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006413
6414 SmallVector<SDValue, 8> Scalars;
6415 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6416
6417 // If ResNE is 0, fully unroll the vector op.
6418 if (ResNE == 0)
6419 ResNE = NE;
6420 else if (NE > ResNE)
6421 NE = ResNE;
6422
6423 unsigned i;
6424 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006425 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006426 SDValue Operand = N->getOperand(j);
6427 EVT OperandVT = Operand.getValueType();
6428 if (OperandVT.isVector()) {
6429 // A vector operand; extract a single element.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006430 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wangcd6e7252009-11-30 02:42:02 +00006431 EVT OperandEltVT = OperandVT.getVectorElementType();
6432 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6433 OperandEltVT,
6434 Operand,
Tom Stellard425b76c2013-08-05 22:22:01 +00006435 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006436 } else {
6437 // A scalar operand; just use it as is.
6438 Operands[j] = Operand;
6439 }
6440 }
6441
6442 switch (N->getOpcode()) {
6443 default:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006444 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006445 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006446 case ISD::VSELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006447 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotemaec58612011-09-13 19:17:42 +00006448 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006449 case ISD::SHL:
6450 case ISD::SRA:
6451 case ISD::SRL:
6452 case ISD::ROTL:
6453 case ISD::ROTR:
6454 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter3af4d252013-09-09 22:02:08 +00006455 getShiftAmountOperand(Operands[0].getValueType(),
6456 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006457 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006458 case ISD::SIGN_EXTEND_INREG:
6459 case ISD::FP_ROUND_INREG: {
6460 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6461 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6462 Operands[0],
6463 getValueType(ExtVT)));
6464 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006465 }
6466 }
6467
6468 for (; i < ResNE; ++i)
6469 Scalars.push_back(getUNDEF(EltVT));
6470
6471 return getNode(ISD::BUILD_VECTOR, dl,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006472 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006473}
6474
Evan Cheng64fa4a92009-12-09 01:36:00 +00006475
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006476/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6477/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006478/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006479bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006480 unsigned Bytes, int Dist) const {
6481 if (LD->getChain() != Base->getChain())
6482 return false;
6483 EVT VT = LD->getValueType(0);
6484 if (VT.getSizeInBits() / 8 != Bytes)
6485 return false;
6486
6487 SDValue Loc = LD->getOperand(1);
6488 SDValue BaseLoc = Base->getOperand(1);
6489 if (Loc.getOpcode() == ISD::FrameIndex) {
6490 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6491 return false;
6492 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6493 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6494 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6495 int FS = MFI->getObjectSize(FI);
6496 int BFS = MFI->getObjectSize(BFI);
6497 if (FS != BFS || FS != (int)Bytes) return false;
6498 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6499 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006500
6501 // Handle X+C
6502 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6503 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6504 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006505
Stephen Hinesdce4a402014-05-29 02:49:00 -07006506 const GlobalValue *GV1 = nullptr;
6507 const GlobalValue *GV2 = nullptr;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006508 int64_t Offset1 = 0;
6509 int64_t Offset2 = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006510 const TargetLowering *TLI = TM.getTargetLowering();
6511 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6512 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Cheng64fa4a92009-12-09 01:36:00 +00006513 if (isGA1 && isGA2 && GV1 == GV2)
6514 return Offset1 == (Offset2 + Dist*Bytes);
6515 return false;
6516}
6517
6518
Evan Chengf2dc5c72009-12-09 01:04:59 +00006519/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6520/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006521unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006522 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006523 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006524 int64_t GVOffset = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006525 const TargetLowering *TLI = TM.getTargetLowering();
6526 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaulte6e81122013-11-16 20:50:54 +00006527 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006528 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Stephen Hinesdce4a402014-05-29 02:49:00 -07006529 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6530 TLI->getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006531 unsigned AlignBits = KnownZero.countTrailingOnes();
6532 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6533 if (Align)
6534 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006535 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006536
Evan Chengf2dc5c72009-12-09 01:04:59 +00006537 // If this is a direct reference to a stack slot, use information about the
6538 // stack slot's alignment.
6539 int FrameIdx = 1 << 31;
6540 int64_t FrameOffset = 0;
6541 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6542 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006543 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006544 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006545 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006546 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6547 FrameOffset = Ptr.getConstantOperandVal(1);
6548 }
6549
6550 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006551 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006552 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6553 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006554 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006555 }
6556
6557 return 0;
6558}
6559
Bill Wendlingee287ca2013-11-22 05:17:44 +00006560/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6561/// which is split (or expanded) into two not necessarily identical pieces.
6562std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6563 // Currently all types are split in half.
6564 EVT LoVT, HiVT;
6565 if (!VT.isVector()) {
6566 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6567 } else {
6568 unsigned NumElements = VT.getVectorNumElements();
6569 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6570 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6571 NumElements/2);
6572 }
6573 return std::make_pair(LoVT, HiVT);
6574}
6575
6576/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6577/// low/high part.
6578std::pair<SDValue, SDValue>
6579SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6580 const EVT &HiVT) {
6581 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6582 N.getValueType().getVectorNumElements() &&
6583 "More vector elements requested than available!");
6584 SDValue Lo, Hi;
6585 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6586 getConstant(0, TLI->getVectorIdxTy()));
6587 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6588 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6589 return std::make_pair(Lo, Hi);
6590}
6591
Stephen Hinesdce4a402014-05-29 02:49:00 -07006592void SelectionDAG::ExtractVectorElements(SDValue Op,
6593 SmallVectorImpl<SDValue> &Args,
6594 unsigned Start, unsigned Count) {
6595 EVT VT = Op.getValueType();
6596 if (Count == 0)
6597 Count = VT.getVectorNumElements();
6598
6599 EVT EltVT = VT.getVectorElementType();
6600 EVT IdxTy = TLI->getVectorIdxTy();
6601 SDLoc SL(Op);
6602 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6603 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6604 Op, getConstant(i, IdxTy)));
6605 }
6606}
6607
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006608// getAddressSpace - Return the address space this GlobalAddress belongs to.
6609unsigned GlobalAddressSDNode::getAddressSpace() const {
6610 return getGlobal()->getType()->getAddressSpace();
6611}
6612
6613
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006614Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006615 if (isMachineConstantPoolEntry())
6616 return Val.MachineCPVal->getType();
6617 return Val.ConstVal->getType();
6618}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006619
Bob Wilson24e338e2009-03-02 23:24:16 +00006620bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6621 APInt &SplatUndef,
6622 unsigned &SplatBitSize,
6623 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006624 unsigned MinSplatBits,
Stephen Hines36b56882014-04-23 16:57:46 -07006625 bool isBigEndian) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006626 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006627 assert(VT.isVector() && "Expected a vector type");
6628 unsigned sz = VT.getSizeInBits();
6629 if (MinSplatBits > sz)
6630 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006631
Bob Wilson24e338e2009-03-02 23:24:16 +00006632 SplatValue = APInt(sz, 0);
6633 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006634
Bob Wilson24e338e2009-03-02 23:24:16 +00006635 // Get the bits. Bits with undefined values (when the corresponding element
6636 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6637 // in SplatValue. If any of the values are not constant, give up and return
6638 // false.
6639 unsigned int nOps = getNumOperands();
6640 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6641 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006642
6643 for (unsigned j = 0; j < nOps; ++j) {
6644 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006645 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006646 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006647
Bob Wilson24e338e2009-03-02 23:24:16 +00006648 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006649 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006650 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006651 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006652 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006653 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006654 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006655 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006656 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006657 }
6658
Bob Wilson24e338e2009-03-02 23:24:16 +00006659 // The build_vector is all constants or undefs. Find the smallest element
6660 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006661
Bob Wilson24e338e2009-03-02 23:24:16 +00006662 HasAnyUndefs = (SplatUndef != 0);
6663 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006664
Bob Wilson24e338e2009-03-02 23:24:16 +00006665 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006666 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6667 APInt LowValue = SplatValue.trunc(HalfSize);
6668 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6669 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006670
Bob Wilson24e338e2009-03-02 23:24:16 +00006671 // If the two halves do not match (ignoring undef bits), stop here.
6672 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6673 MinSplatBits > HalfSize)
6674 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006675
Bob Wilson24e338e2009-03-02 23:24:16 +00006676 SplatValue = HighValue | LowValue;
6677 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006678
Bob Wilson24e338e2009-03-02 23:24:16 +00006679 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006680 }
6681
Bob Wilson24e338e2009-03-02 23:24:16 +00006682 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006683 return true;
6684}
Nate Begeman9008ca62009-04-27 18:41:29 +00006685
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006686SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6687 if (UndefElements) {
6688 UndefElements->clear();
6689 UndefElements->resize(getNumOperands());
6690 }
6691 SDValue Splatted;
6692 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6693 SDValue Op = getOperand(i);
6694 if (Op.getOpcode() == ISD::UNDEF) {
6695 if (UndefElements)
6696 (*UndefElements)[i] = true;
6697 } else if (!Splatted) {
6698 Splatted = Op;
6699 } else if (Splatted != Op) {
6700 return SDValue();
6701 }
6702 }
Stephen Hines36b56882014-04-23 16:57:46 -07006703
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006704 if (!Splatted) {
6705 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6706 "Can only have a splat without a constant for all undefs.");
6707 return getOperand(0);
6708 }
Stephen Hines36b56882014-04-23 16:57:46 -07006709
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006710 return Splatted;
6711}
6712
6713ConstantSDNode *
6714BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
6715 return dyn_cast_or_null<ConstantSDNode>(
6716 getSplatValue(UndefElements).getNode());
6717}
6718
6719ConstantFPSDNode *
6720BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
6721 return dyn_cast_or_null<ConstantFPSDNode>(
6722 getSplatValue(UndefElements).getNode());
Stephen Hines36b56882014-04-23 16:57:46 -07006723}
6724
6725bool BuildVectorSDNode::isConstant() const {
6726 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6727 unsigned Opc = getOperand(i).getOpcode();
6728 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6729 return false;
6730 }
6731 return true;
6732}
6733
Owen Andersone50ed302009-08-10 22:56:29 +00006734bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006735 // Find the first non-undef value in the shuffle mask.
6736 unsigned i, e;
6737 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6738 /* search */;
6739
Nate Begemana6415752009-04-29 18:13:31 +00006740 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006741
Nate Begeman5a5ca152009-04-29 05:20:52 +00006742 // Make sure all remaining elements are either undef or the same as the first
6743 // non-undef value.
6744 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006745 if (Mask[i] >= 0 && Mask[i] != Idx)
6746 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006747 return true;
6748}
David Greenecf495bc2010-01-20 20:13:31 +00006749
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006750#ifndef NDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006751static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006752 SmallPtrSet<const SDNode*, 32> &Visited,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006753 SmallPtrSet<const SDNode*, 32> &Checked,
6754 const llvm::SelectionDAG *DAG) {
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006755 // If this node has already been checked, don't check it again.
6756 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006757 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006758
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006759 // If a node has already been visited on this depth-first walk, reject it as
6760 // a cycle.
6761 if (!Visited.insert(N)) {
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006762 errs() << "Detected cycle in SelectionDAG\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006763 dbgs() << "Offending node:\n";
6764 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006765 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006766 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006767
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006768 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006769 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006770
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006771 Checked.insert(N);
6772 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006773}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006774#endif
David Greenecf495bc2010-01-20 20:13:31 +00006775
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006776void llvm::checkForCycles(const llvm::SDNode *N,
6777 const llvm::SelectionDAG *DAG,
6778 bool force) {
6779#ifndef NDEBUG
6780 bool check = force;
David Greenecf495bc2010-01-20 20:13:31 +00006781#ifdef XDEBUG
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006782 check = true;
6783#endif // XDEBUG
6784 if (check) {
6785 assert(N && "Checking nonexistent SDNode");
6786 SmallPtrSet<const SDNode*, 32> visited;
6787 SmallPtrSet<const SDNode*, 32> checked;
6788 checkForCyclesHelper(N, visited, checked, DAG);
6789 }
6790#endif // !NDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006791}
6792
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006793void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6794 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greenecf495bc2010-01-20 20:13:31 +00006795}