blob: b9a0f44990f2734d5e7f3a886c8a58b7c784a5d3 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendling6de55a02009-12-21 19:34:59 +000013
Chris Lattner600d3082003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng00fd0b62010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
Bill Wendlingbd092622008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetIntrinsicInfo.h"
44#include "llvm/Target/TargetLowering.h"
45#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetOptions.h"
47#include "llvm/Target/TargetRegisterInfo.h"
48#include "llvm/Target/TargetSelectionDAGInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000049#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000050#include <cmath>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000051
Chris Lattner560b5e42004-06-02 04:28:06 +000052using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000053
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000054/// makeVTList - Return an instance of the SDVTList struct initialized with the
55/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000056static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000057 SDVTList Res = {VTs, NumVTs};
58 return Res;
59}
60
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000061// Default null implementations of the callbacks.
62void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
63void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000064
Jim Laskeyd66e6162005-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 Johannesenb6d2bec2007-08-26 01:18:27 +000073bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000074 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000075}
76
Owen Anderson53aa7a92009-08-10 22:56:29 +000077bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000078 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000079 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000080
Dale Johannesend246b2c2007-08-30 00:23:21 +000081 // convert modifies in place, so make a copy.
82 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000083 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000084 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
85 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000086 &losesInfo);
87 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000088}
89
Jim Laskeyd66e6162005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000093
Evan Chengc70e33c2006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000097 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +000098 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +000099 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000100
Evan Chengc70e33c2006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000102
Chris Lattnerc2d28112006-03-25 22:57:01 +0000103 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000104
Chris Lattnerc2d28112006-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 Michelcf0da6c2009-02-17 22:15:04 +0000108
Chris Lattnerc2d28112006-03-25 22:57:01 +0000109 // Do not accept an all-undef vector.
110 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000111
Chris Lattnerc2d28112006-03-25 22:57:01 +0000112 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-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 Gohman2ce6f2a2008-07-27 21:46:04 +0000120 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000121 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000122 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
123 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000124 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000125 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
126 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000127 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000128 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000129 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000130
Chris Lattnerc2d28112006-03-25 22:57:01 +0000131 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-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 Lattnerc2d28112006-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 Chenga6789912006-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 Lattner7e7ad592006-04-15 23:38:00 +0000145 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +0000146 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000147 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000148
Evan Chenga6789912006-03-26 09:50:58 +0000149 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000150
Kevin Qin93d45ec2014-06-24 05:37:27 +0000151 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 Michelcf0da6c2009-02-17 22:15:04 +0000175
Evan Chengc70e33c2006-03-27 06:58:47 +0000176 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000177 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000178 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000179 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000180}
181
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000182/// \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 Cheng6200c222008-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 Wang88ff56c2010-11-19 19:08:12 +0000210 if (NumElems == 1)
211 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000212 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000213 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000214 if (V.getOpcode() != ISD::UNDEF)
215 return false;
216 }
217 return true;
218}
219
Nadav Rotema62368c2012-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
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000236ISD::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 Lattner061a1ea2005-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 Wendling8ec2a4a2008-10-19 20:51:12 +0000260 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-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 Wendling8ec2a4a2008-10-19 20:51:12 +0000271
Chris Lattner061a1ea2005-01-07 07:46:32 +0000272 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000273 Operation &= ~8; // Don't let N and U bits get set.
274
Chris Lattner061a1ea2005-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 Edwinfbcc6632009-07-14 16:55:14 +0000284 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-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 Brukman835702a2005-04-21 22:36:52 +0000307
Chris Lattner061a1ea2005-01-07 07:46:32 +0000308 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000309
Chris Lattner061a1ea2005-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 Lattner8c02c3f2006-05-12 17:03:46 +0000313 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000314
Chris Lattner8c02c3f2006-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 Michelcf0da6c2009-02-17 22:15:04 +0000318
Chris Lattner061a1ea2005-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 Brukman835702a2005-04-21 22:36:52 +0000330 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000331
332 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000333 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000334
Chris Lattner393d96a2006-04-27 05:01:07 +0000335 // Canonicalize illegal integer setcc's.
336 if (isInteger) {
337 switch (Result) {
338 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000339 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000340 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-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 Lattner393d96a2006-04-27 05:01:07 +0000344 }
345 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000346
Chris Lattner393d96a2006-04-27 05:01:07 +0000347 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000348}
349
Jim Laskeyd66e6162005-08-17 20:08:02 +0000350//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000351// SDNode Profile Support
352//===----------------------------------------------------------------------===//
353
Jim Laskeybd0f0882006-10-27 23:52:51 +0000354/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
355///
Jim Laskeyf576b422006-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 Gohmand78c4002008-05-13 00:00:25 +0000362static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000363 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000364}
365
Jim Laskeybd0f0882006-10-27 23:52:51 +0000366/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
367///
Jim Laskeyf576b422006-10-27 23:46:08 +0000368static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000369 ArrayRef<SDValue> Ops) {
370 for (auto& Op : Ops) {
371 ID.AddPointer(Op.getNode());
372 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000373 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000374}
375
Dan Gohman768f2c92008-07-07 18:26:29 +0000376/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
377///
378static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000379 ArrayRef<SDUse> Ops) {
380 for (auto& Op : Ops) {
381 ID.AddPointer(Op.getNode());
382 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000383 }
384}
385
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000386static 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
Craig Topper633d99b2014-04-27 23:22:43 +0000400static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
401 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000402 AddNodeIDOpcode(ID, OpC);
403 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000404 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000405}
406
Duncan Sands835bdca2008-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 Johannesen4bbd2ee2007-03-30 21:38:07 +0000410 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000411 case ISD::TargetExternalSymbol:
412 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000413 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000414 default: break; // Normal nodes don't need extra info.
415 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000416 case ISD::Constant: {
417 const ConstantSDNode *C = cast<ConstantSDNode>(N);
418 ID.AddPointer(C->getConstantIntValue());
419 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000420 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000421 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000422 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000423 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000424 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000425 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000426 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000427 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000428 case ISD::GlobalAddress:
429 case ISD::TargetGlobalTLSAddress:
430 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000431 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000432 ID.AddPointer(GA->getGlobal());
433 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000434 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000435 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-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 Olesen9349351d2012-01-18 23:52:12 +0000444 case ISD::RegisterMask:
445 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
446 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000447 case ISD::SRCVALUE:
448 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
449 break;
Dale Johannesen4bbd2ee2007-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 Lattnerb3586b62009-06-25 21:35:31 +0000457 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000458 break;
459 case ISD::ConstantPool:
460 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000461 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000462 ID.AddInteger(CP->getAlignment());
463 ID.AddInteger(CP->getOffset());
464 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000465 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000466 else
467 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000468 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000469 break;
470 }
Jakob Stoklund Olesen505715d2012-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 Johannesen4bbd2ee2007-03-30 21:38:07 +0000478 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000479 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000480 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000481 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000482 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000483 break;
484 }
485 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000486 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000487 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000488 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000489 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000490 break;
491 }
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000492 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 Gohman12f24902008-12-23 21:37:04 +0000505 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000506 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-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 Friedman342e8df2011-08-24 20:50:09 +0000517 case ISD::ATOMIC_LOAD_UMAX:
518 case ISD::ATOMIC_LOAD:
519 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000520 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000521 ID.AddInteger(AT->getMemoryVT().getRawBits());
522 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-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 Wang6a490372008-06-25 08:15:39 +0000529 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000530 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000531 case ISD::VECTOR_SHUFFLE: {
532 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000533 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000534 i != e; ++i)
535 ID.AddInteger(SVN->getMaskElt(i));
536 break;
537 }
Dan Gohman6c938802009-10-30 01:27:03 +0000538 case ISD::TargetBlockAddress:
539 case ISD::BlockAddress: {
Michael Liaoabb87d42012-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 Gohman6c938802009-10-30 01:27:03 +0000544 break;
545 }
Mon P Wang6a490372008-06-25 08:15:39 +0000546 } // end switch (N->getOpcode())
Pete Cooper91244262012-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 Laskeyf576b422006-10-27 23:46:08 +0000551}
552
Duncan Sands835bdca2008-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.
Craig Topper66e588b2014-06-29 00:40:57 +0000560 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000561
562 // Handle SDNode leafs with special info.
563 AddNodeIDCustom(ID, N);
564}
565
Dan Gohman2da2bed2008-08-20 15:58:01 +0000566/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000567/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000568/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000569///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000570static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000571encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000572 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-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 Greeneb7941b02010-02-17 20:21:42 +0000579 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000580 (isNonTemporal << 6) |
581 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000582}
583
Jim Laskeyf576b422006-10-27 23:46:08 +0000584//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000585// SelectionDAG Class
586//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000587
Duncan Sands835bdca2008-10-27 15:30:53 +0000588/// doNotCSE - Return true if CSE should not be performed for this node.
589static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000590 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-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 Sands835bdca2008-10-27 15:30:53 +0000596 case ISD::EH_LABEL:
Duncan Sands835bdca2008-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 Lattner3e5fbd72010-12-21 02:38:05 +0000602 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000603 return true; // Never CSE anything that produces a flag.
604
605 return false;
606}
607
Chris Lattner9c667932005-01-07 21:09:16 +0000608/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000609/// SelectionDAG.
610void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-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 Lattner06f1d0f2005-10-05 06:35:28 +0000613 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000614
Chris Lattner8927c872006-08-04 17:45:20 +0000615 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000616
Chris Lattner8927c872006-08-04 17:45:20 +0000617 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000618 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000619 if (I->use_empty())
620 DeadNodes.push_back(I);
621
Dan Gohman91697632008-07-07 20:57:48 +0000622 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000623
Dan Gohman91697632008-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 Olesenbeb94692012-04-20 22:08:46 +0000630void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000631
Chris Lattner8927c872006-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 Gohman8e4ac9b2009-01-26 04:35:06 +0000635 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000636
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000637 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000638 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000639
Chris Lattner8927c872006-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 Gohman8e4ac9b2009-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 Lattner8927c872006-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 Lattneraba48dd2005-11-08 18:52:27 +0000653 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000654
Dan Gohman534c8a22009-01-19 22:39:36 +0000655 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000656 }
Chris Lattner9c667932005-01-07 21:09:16 +0000657}
658
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000659void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000660 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-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 Olesenbeb94692012-04-20 22:08:46 +0000667 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000668}
669
Chris Lattnerc738d002005-08-29 21:59:31 +0000670void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000671 // First take this out of the appropriate CSE map.
672 RemoveNodeFromCSEMaps(N);
673
Scott Michelcf0da6c2009-02-17 22:15:04 +0000674 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000675 // AllNodes list, and delete the node.
676 DeleteNodeNotInCSEMaps(N);
677}
678
679void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-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 Gohman534c8a22009-01-19 22:39:36 +0000682
Dan Gohman86aa16a2008-09-30 18:30:35 +0000683 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000684 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000685
Dan Gohman534c8a22009-01-19 22:39:36 +0000686 DeallocateNode(N);
687}
688
689void SelectionDAG::DeallocateNode(SDNode *N) {
690 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000691 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000692
Dan Gohman534c8a22009-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 Gohman2e834902008-08-26 01:44:34 +0000697 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000698
Evan Cheng563fe3c2010-03-25 01:38:16 +0000699 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000700 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000701 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
702 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000703}
704
Chandler Carruth41b20e72014-07-22 04:07:55 +0000705#ifndef NDEBUG
706/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
707static void VerifySDNode(SDNode *N) {
708 switch (N->getOpcode()) {
709 default:
710 break;
711 case ISD::BUILD_PAIR: {
712 EVT VT = N->getValueType(0);
713 assert(N->getNumValues() == 1 && "Too many results!");
714 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
715 "Wrong return type!");
716 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
717 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
718 "Mismatched operand types!");
719 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
720 "Wrong operand type!");
721 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
722 "Wrong return type size");
723 break;
724 }
725 case ISD::BUILD_VECTOR: {
726 assert(N->getNumValues() == 1 && "Too many results!");
727 assert(N->getValueType(0).isVector() && "Wrong return type!");
728 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
729 "Wrong number of operands!");
730 EVT EltVT = N->getValueType(0).getVectorElementType();
731 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
732 assert((I->getValueType() == EltVT ||
733 (EltVT.isInteger() && I->getValueType().isInteger() &&
734 EltVT.bitsLE(I->getValueType()))) &&
735 "Wrong operand type!");
736 assert(I->getValueType() == N->getOperand(0).getValueType() &&
737 "Operands must all have the same type");
738 }
739 break;
740 }
741 }
742}
743#endif // NDEBUG
744
745/// \brief Insert a newly allocated node into the DAG.
746///
747/// Handles insertion into the all nodes list and CSE map, as well as
748/// verification and other common operations when a new node is allocated.
749void SelectionDAG::InsertNode(SDNode *N) {
750 AllNodes.push_back(N);
751#ifndef NDEBUG
752 VerifySDNode(N);
753#endif
754}
755
Chris Lattner19732782005-08-16 18:17:10 +0000756/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
757/// correspond to it. This is useful when we're about to delete or repurpose
758/// the node. We don't want future request for structurally identical nodes
759/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000760bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000761 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000762 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000763 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000764 case ISD::CONDCODE:
765 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
766 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000767 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
768 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000769 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000770 case ISD::ExternalSymbol:
771 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000772 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000773 case ISD::TargetExternalSymbol: {
774 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
775 Erased = TargetExternalSymbols.erase(
776 std::pair<std::string,unsigned char>(ESN->getSymbol(),
777 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000778 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000779 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000780 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000781 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000782 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000783 Erased = ExtendedValueTypeNodes.erase(VT);
784 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000785 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
786 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000787 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000788 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000789 }
Chris Lattner9c667932005-01-07 21:09:16 +0000790 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000791 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000792 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
793 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000794 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000795 break;
796 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000797#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000798 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000799 // flag result (which cannot be CSE'd) or is one of the special cases that are
800 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000801 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000802 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000803 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000804 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000805 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000806 }
807#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000808 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000809}
810
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000811/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
812/// maps and modified in place. Add it back to the CSE maps, unless an identical
813/// node already exists, in which case transfer all its users to the existing
814/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000815///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000816void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000817SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000818 // For node types that aren't CSE'd, just act as if no identical node
819 // already exists.
820 if (!doNotCSE(N)) {
821 SDNode *Existing = CSEMap.GetOrInsertNode(N);
822 if (Existing != N) {
823 // If there was already an existing matching node, use ReplaceAllUsesWith
824 // to replace the dead one with the existing one. This can cause
825 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000826 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000827
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000828 // N is now dead. Inform the listeners and delete it.
829 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
830 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000831 DeleteNodeNotInCSEMaps(N);
832 return;
833 }
834 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000835
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000836 // If the node doesn't already exist, we updated it. Inform listeners.
837 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
838 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000839}
840
Chris Lattnerf34156e2006-01-28 09:32:45 +0000841/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000842/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000843/// return null, otherwise return a pointer to the slot it would take. If a
844/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000845SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000846 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000847 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000848 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000849
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000850 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000851 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000852 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000853 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000854 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000855 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000856}
857
858/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000859/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000860/// return null, otherwise return a pointer to the slot it would take. If a
861/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000862SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000863 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000864 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000865 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000866 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000867
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000868 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000869 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000870 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000871 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000872 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000873 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000874}
875
876
877/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000878/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000879/// return null, otherwise return a pointer to the slot it would take. If a
880/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000881SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000882 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000883 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000884 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000885
Jim Laskeyf576b422006-10-27 23:46:08 +0000886 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000887 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000888 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000889 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000890 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000891}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000892
Owen Anderson53aa7a92009-08-10 22:56:29 +0000893/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000894/// given type.
895///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000896unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000897 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000898 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000899 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000900
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000901 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000902}
Chris Lattner9c667932005-01-07 21:09:16 +0000903
Dale Johannesen8ba713212009-02-07 02:15:05 +0000904// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000905SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Craig Topperc0196b12014-04-14 00:51:57 +0000906 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TLI(nullptr), OptLevel(OL),
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000907 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sanders50b80412013-11-15 12:56:49 +0000908 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
Craig Topperc0196b12014-04-14 00:51:57 +0000909 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000910 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000911 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000912}
913
Juergen Ributzka659ce002014-01-28 01:20:14 +0000914void SelectionDAG::init(MachineFunction &mf, const TargetLowering *tli) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000915 MF = &mf;
Juergen Ributzkab3487102013-11-19 21:20:17 +0000916 TLI = tli;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000917 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000918}
919
Chris Lattner600d3082003-08-11 14:57:33 +0000920SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000921 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000922 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000923 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000924}
925
926void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000927 assert(&*AllNodes.begin() == &EntryNode);
928 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000929 while (!AllNodes.empty())
930 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000931}
932
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000933BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
934 SDVTList VTs, SDValue N1,
935 SDValue N2, bool nuw, bool nsw,
936 bool exact) {
937 if (isBinOpWithFlags(Opcode)) {
938 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
939 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
940 FN->setHasNoUnsignedWrap(nuw);
941 FN->setHasNoSignedWrap(nsw);
942 FN->setIsExact(exact);
943
944 return FN;
945 }
946
947 BinarySDNode *N = new (NodeAllocator)
948 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
949 return N;
950}
951
Dan Gohmane1a9a782008-08-27 23:52:12 +0000952void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000953 allnodes_clear();
954 OperandAllocator.Reset();
955 CSEMap.clear();
956
957 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000958 ExternalSymbols.clear();
959 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000960 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000961 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000962 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000963 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000964
Craig Topperc0196b12014-04-14 00:51:57 +0000965 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000966 AllNodes.push_back(&EntryNode);
967 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000968 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000969}
970
Andrew Trickef9de2a2013-05-25 02:42:55 +0000971SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000972 return VT.bitsGT(Op.getValueType()) ?
973 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
974 getNode(ISD::TRUNCATE, DL, VT, Op);
975}
976
Andrew Trickef9de2a2013-05-25 02:42:55 +0000977SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000978 return VT.bitsGT(Op.getValueType()) ?
979 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
980 getNode(ISD::TRUNCATE, DL, VT, Op);
981}
982
Andrew Trickef9de2a2013-05-25 02:42:55 +0000983SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000984 return VT.bitsGT(Op.getValueType()) ?
985 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
986 getNode(ISD::TRUNCATE, DL, VT, Op);
987}
988
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000989SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
990 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +0000991 if (VT.bitsLE(Op.getValueType()))
992 return getNode(ISD::TRUNCATE, SL, VT, Op);
993
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000994 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +0000995 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
996}
997
Andrew Trickef9de2a2013-05-25 02:42:55 +0000998SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000999 assert(!VT.isVector() &&
1000 "getZeroExtendInReg should use the vector element type instead of "
1001 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001002 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001003 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1004 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001005 VT.getSizeInBits());
1006 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1007 getConstant(Imm, Op.getValueType()));
1008}
1009
Chandler Carruth0b666e02014-07-10 12:32:32 +00001010SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1011 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1012 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1013 "The sizes of the input and result must match in order to perform the "
1014 "extend in-register.");
1015 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1016 "The destination vector type must have fewer lanes than the input.");
1017 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1018}
1019
1020SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1021 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1022 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1023 "The sizes of the input and result must match in order to perform the "
1024 "extend in-register.");
1025 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1026 "The destination vector type must have fewer lanes than the input.");
1027 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1028}
1029
Chandler Carruthafe4b252014-07-09 10:58:18 +00001030SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1031 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001032 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1033 "The sizes of the input and result must match in order to perform the "
1034 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001035 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1036 "The destination vector type must have fewer lanes than the input.");
1037 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1038}
1039
Bob Wilsonc5890052009-01-22 17:39:32 +00001040/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1041///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001042SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001043 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001044 SDValue NegOne =
1045 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001046 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1047}
1048
Pete Cooper7fd1d722014-05-12 23:26:58 +00001049SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1050 EVT EltVT = VT.getScalarType();
1051 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001052 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001053 case TargetLowering::ZeroOrOneBooleanContent:
1054 case TargetLowering::UndefinedBooleanContent:
1055 TrueValue = getConstant(1, VT);
1056 break;
1057 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1058 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1059 VT);
1060 break;
1061 }
1062 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1063}
1064
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001065SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001066 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001067 assert((EltVT.getSizeInBits() >= 64 ||
1068 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1069 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001070 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001071}
1072
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001073SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1074{
1075 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001076}
1077
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001078SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1079 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001080 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001081
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001082 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001083 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001084
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001085 const TargetLowering *TLI = TM.getTargetLowering();
1086
Duncan Sandsf2641e12011-09-06 19:07:46 +00001087 // In some cases the vector type is legal but the element type is illegal and
1088 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1089 // inserted value (the type does not need to match the vector element type).
1090 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001091 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001092 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001093 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001094 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1095 Elt = ConstantInt::get(*getContext(), NewVal);
1096 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001097 // In other cases the element type is illegal and needs to be expanded, for
1098 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1099 // the value into n parts and use a vector type with n-times the elements.
1100 // Then bitcast to the type requested.
1101 // Legalizing constants too early makes the DAGCombiner's job harder so we
1102 // only legalize if the DAG tells us we must produce legal types.
1103 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1104 TLI->getTypeAction(*getContext(), EltVT) ==
1105 TargetLowering::TypeExpandInteger) {
1106 APInt NewVal = Elt->getValue();
1107 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1108 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1109 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1110 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1111
1112 // Check the temporary vector is the correct size. If this fails then
1113 // getTypeToTransformTo() probably returned a type whose size (in bits)
1114 // isn't a power-of-2 factor of the requested type size.
1115 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1116
1117 SmallVector<SDValue, 2> EltParts;
1118 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1119 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1120 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001121 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001122 }
1123
1124 // EltParts is currently in little endian order. If we actually want
1125 // big-endian order then reverse it now.
1126 if (TLI->isBigEndian())
1127 std::reverse(EltParts.begin(), EltParts.end());
1128
1129 // The elements must be reversed when the element order is different
1130 // to the endianness of the elements (because the BITCAST is itself a
1131 // vector shuffle in this situation). However, we do not need any code to
1132 // perform this reversal because getConstant() is producing a vector
1133 // splat.
1134 // This situation occurs in MIPS MSA.
1135
1136 SmallVector<SDValue, 8> Ops;
1137 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1138 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1139
1140 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1141 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001142 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001143 return Result;
1144 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001145
1146 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1147 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001148 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001149 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001150 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001151 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001152 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001153 void *IP = nullptr;
1154 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001155 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001156 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001157 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001158
Dan Gohman7a7742c2007-12-12 22:21:26 +00001159 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001160 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001161 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001162 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001163 }
1164
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001165 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001166 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001167 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001168 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001169 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001170 }
1171 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001172}
1173
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001174SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001175 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001176}
1177
1178
Owen Anderson53aa7a92009-08-10 22:56:29 +00001179SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001180 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001181}
1182
Owen Anderson53aa7a92009-08-10 22:56:29 +00001183SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001184 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001185
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001186 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001187
Chris Lattner381dddc2005-02-17 20:17:32 +00001188 // Do the map lookup using the actual bit pattern for the floating point
1189 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1190 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001191 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001192 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001193 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001194 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001195 void *IP = nullptr;
1196 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001197 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001198 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001199 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001200
Evan Cheng9458e6a2007-06-29 21:36:04 +00001201 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001202 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001203 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001204 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001205 }
1206
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001207 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001208 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001209 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001210 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001211 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001212 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001213 }
1214 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001215}
1216
Owen Anderson53aa7a92009-08-10 22:56:29 +00001217SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001218 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001219 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001220 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001221 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001222 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001223 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1224 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001225 bool ignored;
1226 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001227 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001228 &ignored);
1229 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001230 } else
1231 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001232}
1233
Andrew Trickef9de2a2013-05-25 02:42:55 +00001234SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001235 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001236 bool isTargetGA,
1237 unsigned char TargetFlags) {
1238 assert((TargetFlags == 0 || isTargetGA) &&
1239 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001240 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001241
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001242 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001243 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001244 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001245 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001246
Chris Lattner8e34f982009-06-25 21:21:14 +00001247 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001248 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001249 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1250 else
1251 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001252
Jim Laskeyf576b422006-10-27 23:46:08 +00001253 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001254 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001255 ID.AddPointer(GV);
1256 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001257 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001258 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001259 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001260 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001261 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001262
Andrew Trickef9de2a2013-05-25 02:42:55 +00001263 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1264 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001265 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001266 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001267 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001268 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001269}
1270
Owen Anderson53aa7a92009-08-10 22:56:29 +00001271SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001272 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001273 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001274 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001275 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001276 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001277 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001278 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001279
Dan Gohman01c65a22010-03-18 18:49:47 +00001280 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001281 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001282 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001283 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001284}
1285
Owen Anderson53aa7a92009-08-10 22:56:29 +00001286SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001287 unsigned char TargetFlags) {
1288 assert((TargetFlags == 0 || isTarget) &&
1289 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001290 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001291 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001292 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001293 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001294 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001295 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001296 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001297 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001298
Dan Gohman01c65a22010-03-18 18:49:47 +00001299 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1300 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001301 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001302 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001303 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001304}
1305
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001306SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001307 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001308 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001309 unsigned char TargetFlags) {
1310 assert((TargetFlags == 0 || isTarget) &&
1311 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001312 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001313 Alignment =
1314 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001315 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001316 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001317 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001318 ID.AddInteger(Alignment);
1319 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001320 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001321 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001322 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001323 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001324 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001325
Dan Gohman01c65a22010-03-18 18:49:47 +00001326 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1327 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001328 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001329 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001330 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001331}
1332
Chris Lattner061a1ea2005-01-07 07:46:32 +00001333
Owen Anderson53aa7a92009-08-10 22:56:29 +00001334SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001335 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001336 bool isTarget,
1337 unsigned char TargetFlags) {
1338 assert((TargetFlags == 0 || isTarget) &&
1339 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001340 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001341 Alignment =
1342 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001343 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001344 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001345 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001346 ID.AddInteger(Alignment);
1347 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001348 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001349 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001350 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001351 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001352 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001353
Dan Gohman01c65a22010-03-18 18:49:47 +00001354 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1355 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001356 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001357 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001358 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001359}
1360
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001361SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1362 unsigned char TargetFlags) {
1363 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001364 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001365 ID.AddInteger(Index);
1366 ID.AddInteger(Offset);
1367 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001368 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001369 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1370 return SDValue(E, 0);
1371
1372 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1373 TargetFlags);
1374 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001375 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001376 return SDValue(N, 0);
1377}
1378
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001379SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001380 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001381 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001382 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001383 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001384 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001385 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001386
Dan Gohman01c65a22010-03-18 18:49:47 +00001387 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001388 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001389 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001390 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001391}
1392
Owen Anderson53aa7a92009-08-10 22:56:29 +00001393SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001394 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1395 ValueTypeNodes.size())
1396 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001397
Duncan Sands13237ac2008-06-06 12:08:01 +00001398 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001399 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001400
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001401 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001402 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001403 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001404 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001405}
1406
Owen Anderson53aa7a92009-08-10 22:56:29 +00001407SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001408 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001409 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001410 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001411 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001412 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001413}
1414
Owen Anderson53aa7a92009-08-10 22:56:29 +00001415SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001416 unsigned char TargetFlags) {
1417 SDNode *&N =
1418 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1419 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001420 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001421 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001422 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001423 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001424}
1425
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001426SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001427 if ((unsigned)Cond >= CondCodeNodes.size())
1428 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001429
Craig Topperc0196b12014-04-14 00:51:57 +00001430 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001431 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001432 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001433 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001434 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001435
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001436 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001437}
1438
Nate Begeman5f829d82009-04-29 05:20:52 +00001439// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1440// the shuffle mask M that point at N1 to point at N2, and indices that point
1441// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001442static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1443 std::swap(N1, N2);
1444 int NElts = M.size();
1445 for (int i = 0; i != NElts; ++i) {
1446 if (M[i] >= NElts)
1447 M[i] -= NElts;
1448 else if (M[i] >= 0)
1449 M[i] += NElts;
1450 }
1451}
1452
Andrew Trickef9de2a2013-05-25 02:42:55 +00001453SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001454 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001455 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1456 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001457
1458 // Canonicalize shuffle undef, undef -> undef
1459 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001460 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001461
Eric Christopherdfda92b2009-08-22 00:40:45 +00001462 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001463 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001464 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001465 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001466 for (unsigned i = 0; i != NElts; ++i) {
1467 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001468 MaskVec.push_back(Mask[i]);
1469 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001470
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001471 // Canonicalize shuffle v, v -> v, undef
1472 if (N1 == N2) {
1473 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001474 for (unsigned i = 0; i != NElts; ++i)
1475 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001476 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001477
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001478 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1479 if (N1.getOpcode() == ISD::UNDEF)
1480 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001481
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001482 // Canonicalize all index into lhs, -> shuffle lhs, undef
1483 // Canonicalize all index into rhs, -> shuffle rhs, undef
1484 bool AllLHS = true, AllRHS = true;
1485 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001486 for (unsigned i = 0; i != NElts; ++i) {
1487 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001488 if (N2Undef)
1489 MaskVec[i] = -1;
1490 else
1491 AllLHS = false;
1492 } else if (MaskVec[i] >= 0) {
1493 AllRHS = false;
1494 }
1495 }
1496 if (AllLHS && AllRHS)
1497 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001498 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001499 N2 = getUNDEF(VT);
1500 if (AllRHS) {
1501 N1 = getUNDEF(VT);
1502 commuteShuffle(N1, N2, MaskVec);
1503 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001504 // Reset our undef status after accounting for the mask.
1505 N2Undef = N2.getOpcode() == ISD::UNDEF;
1506 // Re-check whether both sides ended up undef.
1507 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1508 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001509
Craig Topper9a39b072013-08-08 08:03:12 +00001510 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001511 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001512 for (unsigned i = 0; i != NElts; ++i) {
1513 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001514 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001515 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001516 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001517
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001518 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001519 if (N2Undef) {
1520 SDValue V = N1;
1521
1522 // Look through any bitcasts. We check that these don't change the number
1523 // (and size) of elements and just changes their types.
1524 while (V.getOpcode() == ISD::BITCAST)
1525 V = V->getOperand(0);
1526
1527 // A splat should always show up as a build vector node.
1528 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001529 BitVector UndefElements;
1530 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001531 // If this is a splat of an undef, shuffling it is also undef.
1532 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1533 return getUNDEF(VT);
1534
1535 // We only have a splat which can skip shuffles if there is a splatted
1536 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001537 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001538 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1539 // number of elements match or the value splatted is a zero constant.
1540 if (V.getValueType().getVectorNumElements() ==
1541 VT.getVectorNumElements())
1542 return N1;
1543 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1544 if (C->isNullValue())
1545 return N1;
1546 }
1547 }
1548 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001549
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001550 FoldingSetNodeID ID;
1551 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001552 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001553 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001554 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001555
Craig Topperc0196b12014-04-14 00:51:57 +00001556 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001557 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001558 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001559
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001560 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1561 // SDNode doesn't have access to it. This memory will be "leaked" when
1562 // the node is deallocated, but recovered when the NodeAllocator is released.
1563 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1564 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001565
Dan Gohman01c65a22010-03-18 18:49:47 +00001566 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001567 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1568 dl.getDebugLoc(), N1, N2,
1569 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001570 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001571 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001572 return SDValue(N, 0);
1573}
1574
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001575SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1576 MVT VT = SV.getSimpleValueType(0);
1577 unsigned NumElems = VT.getVectorNumElements();
1578 SmallVector<int, 8> MaskVec;
1579
1580 for (unsigned i = 0; i != NumElems; ++i) {
1581 int Idx = SV.getMaskElt(i);
1582 if (Idx >= 0) {
1583 if (Idx < (int)NumElems)
1584 Idx += NumElems;
1585 else
1586 Idx -= NumElems;
1587 }
1588 MaskVec.push_back(Idx);
1589 }
1590
1591 SDValue Op0 = SV.getOperand(0);
1592 SDValue Op1 = SV.getOperand(1);
1593 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1594}
1595
Andrew Trickef9de2a2013-05-25 02:42:55 +00001596SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001597 SDValue Val, SDValue DTy,
1598 SDValue STy, SDValue Rnd, SDValue Sat,
1599 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001600 // If the src and dest types are the same and the conversion is between
1601 // integer types of the same sign or two floats, no conversion is necessary.
1602 if (DTy == STy &&
1603 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001604 return Val;
1605
1606 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001607 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001608 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001609 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001610 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001611 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001612
Jack Carter170a5f22013-09-09 22:02:08 +00001613 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1614 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001615 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001616 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001617 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001618 return SDValue(N, 0);
1619}
1620
Owen Anderson53aa7a92009-08-10 22:56:29 +00001621SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001622 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001623 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001624 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001625 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001626 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001627 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001628
Dan Gohman01c65a22010-03-18 18:49:47 +00001629 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001630 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001631 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001632 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001633}
1634
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001635SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1636 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001637 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001638 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001639 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1641 return SDValue(E, 0);
1642
1643 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1644 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001645 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001646 return SDValue(N, 0);
1647}
1648
Andrew Trickef9de2a2013-05-25 02:42:55 +00001649SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001650 FoldingSetNodeID ID;
1651 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001652 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001653 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001654 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001655 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001656 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001657
Jack Carter170a5f22013-09-09 22:02:08 +00001658 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1659 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001660 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001661 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001662 return SDValue(N, 0);
1663}
1664
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001665
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001666SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001667 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001668 bool isTarget,
1669 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001670 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1671
1672 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001673 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001674 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001675 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001676 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001677 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001678 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001679 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001680
Michael Liaoabb87d42012-09-12 21:43:09 +00001681 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1682 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001683 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001684 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001685 return SDValue(N, 0);
1686}
1687
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001688SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001689 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001690 "SrcValue is not a pointer?");
1691
Jim Laskeyf576b422006-10-27 23:46:08 +00001692 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001693 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001694 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001695
Craig Topperc0196b12014-04-14 00:51:57 +00001696 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001697 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001698 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001699
Dan Gohman01c65a22010-03-18 18:49:47 +00001700 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001701 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001702 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001703 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001704}
1705
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001706/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1707SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1708 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001709 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001710 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001711
Craig Topperc0196b12014-04-14 00:51:57 +00001712 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001713 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1714 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001715
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001716 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1717 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001718 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001719 return SDValue(N, 0);
1720}
1721
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001722/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1723SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1724 unsigned SrcAS, unsigned DestAS) {
1725 SDValue Ops[] = {Ptr};
1726 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001727 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001728 ID.AddInteger(SrcAS);
1729 ID.AddInteger(DestAS);
1730
Craig Topperc0196b12014-04-14 00:51:57 +00001731 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001732 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1733 return SDValue(E, 0);
1734
1735 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1736 dl.getDebugLoc(),
1737 VT, Ptr, SrcAS, DestAS);
1738 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001739 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001740 return SDValue(N, 0);
1741}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001742
Duncan Sands41826032009-01-31 15:50:11 +00001743/// getShiftAmountOperand - Return the specified value casted to
1744/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001745SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001746 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001747 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001748 if (OpTy == ShTy || OpTy.isVector()) return Op;
1749
1750 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001751 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001752}
1753
Chris Lattner9eb7a822007-10-15 17:47:20 +00001754/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1755/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001756SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001757 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001758 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001759 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001760 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001761 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001762 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001763
David Greene1fbe0542009-11-12 20:49:22 +00001764 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001765 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001766}
1767
Duncan Sands445071c2008-12-09 21:33:20 +00001768/// CreateStackTemporary - Create a stack temporary suitable for holding
1769/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001770SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001771 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1772 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001773 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1774 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001775 const TargetLowering *TLI = TM.getTargetLowering();
1776 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001777 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1778 TD->getPrefTypeAlignment(Ty2));
1779
1780 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001781 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001782 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001783}
1784
Owen Anderson53aa7a92009-08-10 22:56:29 +00001785SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001786 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001787 // These setcc operations always fold.
1788 switch (Cond) {
1789 default: break;
1790 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001791 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001792 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001793 case ISD::SETTRUE2: {
1794 const TargetLowering *TLI = TM.getTargetLowering();
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001795 TargetLowering::BooleanContent Cnt =
1796 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001797 return getConstant(
1798 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1799 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001800
Chris Lattner393d96a2006-04-27 05:01:07 +00001801 case ISD::SETOEQ:
1802 case ISD::SETOGT:
1803 case ISD::SETOGE:
1804 case ISD::SETOLT:
1805 case ISD::SETOLE:
1806 case ISD::SETONE:
1807 case ISD::SETO:
1808 case ISD::SETUO:
1809 case ISD::SETUEQ:
1810 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001811 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001812 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001813 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001814
Gabor Greiff304a7a2008-08-28 21:40:38 +00001815 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001816 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001817 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001818 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001819
Chris Lattner061a1ea2005-01-07 07:46:32 +00001820 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001821 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001822 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1823 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001824 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1825 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1826 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1827 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1828 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1829 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1830 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1831 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001832 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001833 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001834 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001835 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1836 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001837 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001838 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001839 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001840 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001841 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001842 // fall through
1843 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001844 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001845 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001846 // fall through
1847 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001848 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001849 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001850 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001851 // fall through
1852 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001853 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001854 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001855 // fall through
1856 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001857 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001858 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001859 // fall through
1860 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001861 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001862 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001863 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001864 // fall through
1865 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001866 R==APFloat::cmpEqual, VT);
1867 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1868 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1869 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1870 R==APFloat::cmpEqual, VT);
1871 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1872 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1873 R==APFloat::cmpLessThan, VT);
1874 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1875 R==APFloat::cmpUnordered, VT);
1876 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1877 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001878 }
1879 } else {
1880 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001881 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1882 MVT CompVT = N1.getValueType().getSimpleVT();
1883 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1884 return SDValue();
1885
1886 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001887 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001888 }
1889
Chris Lattnerd47675e2005-08-09 20:20:18 +00001890 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001891 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001892}
1893
Dan Gohman1f372ed2008-02-25 21:11:39 +00001894/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1895/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001896bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001897 // This predicate is not safe for vector operations.
1898 if (Op.getValueType().isVector())
1899 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001900
Dan Gohman1d459e42009-12-11 21:31:27 +00001901 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001902 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1903}
1904
Dan Gohman309d3d52007-06-22 14:59:07 +00001905/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1906/// this predicate to simplify operations downstream. Mask is known to be zero
1907/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001908bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001909 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001910 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001911 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001912 return (KnownZero & Mask) == Mask;
1913}
1914
Jay Foada0653a32014-05-14 21:14:37 +00001915/// Determine which bits of Op are known to be either zero or one and return
1916/// them in the KnownZero/KnownOne bitsets.
1917void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1918 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001919 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001920 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001921
Dan Gohmanf990faf2008-02-13 00:35:47 +00001922 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001923 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001924 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001925
Dan Gohmanf990faf2008-02-13 00:35:47 +00001926 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001927
1928 switch (Op.getOpcode()) {
1929 case ISD::Constant:
1930 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001931 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1932 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001933 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001934 case ISD::AND:
1935 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001936 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1937 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001938
1939 // Output known-1 bits are only known if set in both the LHS & RHS.
1940 KnownOne &= KnownOne2;
1941 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1942 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001943 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001944 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001945 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1946 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001947
Dan Gohman309d3d52007-06-22 14:59:07 +00001948 // Output known-0 bits are only known if clear in both the LHS & RHS.
1949 KnownZero &= KnownZero2;
1950 // Output known-1 are known to be set if set in either the LHS | RHS.
1951 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001952 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001953 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001954 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1955 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001956
Dan Gohman309d3d52007-06-22 14:59:07 +00001957 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001958 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001959 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1960 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1961 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001962 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001963 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001964 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00001965 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1966 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001967
1968 // If low bits are zero in either operand, output low known-0 bits.
1969 // Also compute a conserative estimate for high known-0 bits.
1970 // More trickiness is possible, but this is sufficient for the
1971 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001972 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001973 unsigned TrailZ = KnownZero.countTrailingOnes() +
1974 KnownZero2.countTrailingOnes();
1975 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001976 KnownZero2.countLeadingOnes(),
1977 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001978
1979 TrailZ = std::min(TrailZ, BitWidth);
1980 LeadZ = std::min(LeadZ, BitWidth);
1981 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1982 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001983 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001984 }
1985 case ISD::UDIV: {
1986 // For the purposes of computing leading zeros we can conservatively
1987 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001988 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00001989 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001990 unsigned LeadZ = KnownZero2.countLeadingOnes();
1991
Jay Foad25a5e4c2010-12-01 08:53:58 +00001992 KnownOne2.clearAllBits();
1993 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00001994 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001995 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1996 if (RHSUnknownLeadingOnes != BitWidth)
1997 LeadZ = std::min(BitWidth,
1998 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001999
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002000 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002001 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002002 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002003 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002004 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2005 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002006
Dan Gohman309d3d52007-06-22 14:59:07 +00002007 // Only known if known in both the LHS and RHS.
2008 KnownOne &= KnownOne2;
2009 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002010 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002011 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002012 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2013 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002014
Dan Gohman309d3d52007-06-22 14:59:07 +00002015 // Only known if known in both the LHS and RHS.
2016 KnownOne &= KnownOne2;
2017 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002018 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002019 case ISD::SADDO:
2020 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002021 case ISD::SSUBO:
2022 case ISD::USUBO:
2023 case ISD::SMULO:
2024 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002025 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002026 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002027 // The boolean result conforms to getBooleanContents.
2028 // If we know the result of a setcc has the top bits zero, use this info.
2029 // We know that we have an integer-based boolean since these operations
2030 // are only available for integer.
2031 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2032 TargetLowering::ZeroOrOneBooleanContent &&
2033 BitWidth > 1)
2034 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2035 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002036 case ISD::SETCC:
2037 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002038 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2039 TargetLowering::ZeroOrOneBooleanContent &&
2040 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002041 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002042 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002043 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002044 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002045 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002046 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002047
2048 // If the shift count is an invalid immediate, don't do anything.
2049 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002050 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002051
Jay Foada0653a32014-05-14 21:14:37 +00002052 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002053 KnownZero <<= ShAmt;
2054 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002055 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002056 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002057 }
Jay Foad5a29c362014-05-15 12:12:55 +00002058 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002059 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002060 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002061 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002062 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002063
Dan Gohman9db0aa82008-02-26 18:50:50 +00002064 // If the shift count is an invalid immediate, don't do anything.
2065 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002066 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002067
Jay Foada0653a32014-05-14 21:14:37 +00002068 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002069 KnownZero = KnownZero.lshr(ShAmt);
2070 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002071
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002072 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002073 KnownZero |= HighBits; // High bits known zero.
2074 }
Jay Foad5a29c362014-05-15 12:12:55 +00002075 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002076 case ISD::SRA:
2077 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002078 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002079
Dan Gohman9db0aa82008-02-26 18:50:50 +00002080 // If the shift count is an invalid immediate, don't do anything.
2081 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002082 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002083
Dan Gohman309d3d52007-06-22 14:59:07 +00002084 // If any of the demanded bits are produced by the sign extension, we also
2085 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002086 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002087
Jay Foada0653a32014-05-14 21:14:37 +00002088 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002089 KnownZero = KnownZero.lshr(ShAmt);
2090 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002091
Dan Gohman309d3d52007-06-22 14:59:07 +00002092 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002093 APInt SignBit = APInt::getSignBit(BitWidth);
2094 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002095
Dan Gohmanb717fda2008-02-20 16:30:17 +00002096 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002097 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002098 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002099 KnownOne |= HighBits; // New bits are known one.
2100 }
2101 }
Jay Foad5a29c362014-05-15 12:12:55 +00002102 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002103 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002104 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002105 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002106
2107 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002108 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002109 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002110
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002111 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002112 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002113
Dan Gohman309d3d52007-06-22 14:59:07 +00002114 // If the sign extended bits are demanded, we know that the sign
2115 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002116 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002117 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002118 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002119
Jay Foada0653a32014-05-14 21:14:37 +00002120 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002121 KnownOne &= InputDemandedBits;
2122 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002123
Dan Gohman309d3d52007-06-22 14:59:07 +00002124 // If the sign bit of the input is known set or clear, then we know the
2125 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002126 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002127 KnownZero |= NewBits;
2128 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002129 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002130 KnownOne |= NewBits;
2131 KnownZero &= ~NewBits;
2132 } else { // Input sign bit unknown
2133 KnownZero &= ~NewBits;
2134 KnownOne &= ~NewBits;
2135 }
Jay Foad5a29c362014-05-15 12:12:55 +00002136 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002137 }
2138 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002139 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002140 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002141 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002142 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002143 unsigned LowBits = Log2_32(BitWidth)+1;
2144 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002145 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002146 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002147 }
2148 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002149 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002150 // If this is a ZEXTLoad and we are looking at the loaded value.
2151 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002152 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002153 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002154 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002155 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002156 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002157 }
Jay Foad5a29c362014-05-15 12:12:55 +00002158 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002159 }
2160 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002161 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002162 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002163 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002164 KnownZero = KnownZero.trunc(InBits);
2165 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002166 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002167 KnownZero = KnownZero.zext(BitWidth);
2168 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002169 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002170 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002171 }
2172 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002173 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002174 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002175 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002176
Jay Foad583abbc2010-12-07 08:25:19 +00002177 KnownZero = KnownZero.trunc(InBits);
2178 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002179 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002180
2181 // Note if the sign bit is known to be zero or one.
2182 bool SignBitKnownZero = KnownZero.isNegative();
2183 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002184
Jay Foad583abbc2010-12-07 08:25:19 +00002185 KnownZero = KnownZero.zext(BitWidth);
2186 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002187
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002188 // If the sign bit is known zero or one, the top bits match.
2189 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002190 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002191 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002192 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002193 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002194 }
2195 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002196 EVT InVT = Op.getOperand(0).getValueType();
2197 unsigned InBits = InVT.getScalarType().getSizeInBits();
2198 KnownZero = KnownZero.trunc(InBits);
2199 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002200 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002201 KnownZero = KnownZero.zext(BitWidth);
2202 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002203 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002204 }
2205 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002206 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002207 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002208 KnownZero = KnownZero.zext(InBits);
2209 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002210 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002211 KnownZero = KnownZero.trunc(BitWidth);
2212 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002213 break;
2214 }
2215 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002216 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002217 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002218 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002219 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002220 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002221 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002222 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002223 case ISD::FGETSIGN:
2224 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002225 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002226 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002227
Dan Gohman72ec3f42008-04-28 17:02:21 +00002228 case ISD::SUB: {
2229 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2230 // We know that the top bits of C-X are clear if X contains less bits
2231 // than C (i.e. no wrap-around can happen). For example, 20-X is
2232 // positive if we can prove that X is >= 0 and < 16.
2233 if (CLHS->getAPIntValue().isNonNegative()) {
2234 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2235 // NLZ can't be BitWidth with no sign bit
2236 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002237 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002238
2239 // If all of the MaskV bits are known to be zero, then we know the
2240 // output top bits are zero, because we now know that the output is
2241 // from [0-C].
2242 if ((KnownZero2 & MaskV) == MaskV) {
2243 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2244 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002245 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002246 }
2247 }
2248 }
2249 }
2250 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002251 case ISD::ADD:
2252 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002253 // Output known-0 bits are known if clear or set in both the low clear bits
2254 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2255 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002256 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002257 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2258
Jay Foada0653a32014-05-14 21:14:37 +00002259 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002260 KnownZeroOut = std::min(KnownZeroOut,
2261 KnownZero2.countTrailingOnes());
2262
Chris Lattner440b2802010-12-19 20:38:28 +00002263 if (Op.getOpcode() == ISD::ADD) {
2264 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002265 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002266 }
2267
2268 // With ADDE, a carry bit may be added in, so we can only use this
2269 // information if we know (at least) that the low two bits are clear. We
2270 // then return to the caller that the low bit is unknown but that other bits
2271 // are known zero.
2272 if (KnownZeroOut >= 2) // ADDE
2273 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002274 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002275 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002276 case ISD::SREM:
2277 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002278 const APInt &RA = Rem->getAPIntValue().abs();
2279 if (RA.isPowerOf2()) {
2280 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002281 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002282
Duncan Sands33274982010-01-29 09:45:26 +00002283 // The low bits of the first operand are unchanged by the srem.
2284 KnownZero = KnownZero2 & LowBits;
2285 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002286
Duncan Sands33274982010-01-29 09:45:26 +00002287 // If the first operand is non-negative or has all low bits zero, then
2288 // the upper bits are all zero.
2289 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2290 KnownZero |= ~LowBits;
2291
2292 // If the first operand is negative and not all low bits are zero, then
2293 // the upper bits are all one.
2294 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2295 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002296 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002297 }
2298 }
Jay Foad5a29c362014-05-15 12:12:55 +00002299 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002300 case ISD::UREM: {
2301 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002302 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002303 if (RA.isPowerOf2()) {
2304 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002305 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2306
2307 // The upper bits are all zero, the lower ones are unchanged.
2308 KnownZero = KnownZero2 | ~LowBits;
2309 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002310 break;
2311 }
2312 }
2313
2314 // Since the result is less than or equal to either operand, any leading
2315 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002316 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2317 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002318
2319 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2320 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002321 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002322 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002323 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002324 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002325 case ISD::FrameIndex:
2326 case ISD::TargetFrameIndex:
2327 if (unsigned Align = InferPtrAlignment(Op)) {
2328 // The low bits are known zero if the pointer is aligned.
2329 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002330 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002331 }
2332 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002333
Dan Gohman309d3d52007-06-22 14:59:07 +00002334 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002335 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2336 break;
2337 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002338 case ISD::INTRINSIC_WO_CHAIN:
2339 case ISD::INTRINSIC_W_CHAIN:
2340 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002341 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002342 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002343 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002344 }
Jay Foad5a29c362014-05-15 12:12:55 +00002345
2346 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002347}
2348
2349/// ComputeNumSignBits - Return the number of times the sign bit of the
2350/// register is replicated into the other bits. We know that at least 1 bit
2351/// is always equal to the sign bit (itself), but other cases can give us
2352/// information. For example, immediately after an "SRA X, 2", we know that
2353/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002354unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002355 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002356 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002357 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002358 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002359 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002360 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002361
Dan Gohman309d3d52007-06-22 14:59:07 +00002362 if (Depth == 6)
2363 return 1; // Limit search depth.
2364
2365 switch (Op.getOpcode()) {
2366 default: break;
2367 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002368 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002369 return VTBits-Tmp+1;
2370 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002371 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002372 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002373
Dan Gohman309d3d52007-06-22 14:59:07 +00002374 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002375 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002376 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002378
Dan Gohman309d3d52007-06-22 14:59:07 +00002379 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002380 Tmp =
2381 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002382 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002383
Dan Gohman309d3d52007-06-22 14:59:07 +00002384 case ISD::SIGN_EXTEND_INREG:
2385 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002386 Tmp =
2387 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002388 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002389
Dan Gohman309d3d52007-06-22 14:59:07 +00002390 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2391 return std::max(Tmp, Tmp2);
2392
2393 case ISD::SRA:
2394 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2395 // SRA X, C -> adds C sign bits.
2396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002397 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002398 if (Tmp > VTBits) Tmp = VTBits;
2399 }
2400 return Tmp;
2401 case ISD::SHL:
2402 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2403 // shl destroys sign bits.
2404 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002405 if (C->getZExtValue() >= VTBits || // Bad shift.
2406 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2407 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002408 }
2409 break;
2410 case ISD::AND:
2411 case ISD::OR:
2412 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002413 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002414 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002415 if (Tmp != 1) {
2416 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2417 FirstAnswer = std::min(Tmp, Tmp2);
2418 // We computed what we know about the sign bits as our first
2419 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002420 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002421 }
2422 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002423
2424 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002425 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002427 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002428 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002429
2430 case ISD::SADDO:
2431 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002432 case ISD::SSUBO:
2433 case ISD::USUBO:
2434 case ISD::SMULO:
2435 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002436 if (Op.getResNo() != 1)
2437 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002438 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002439 // If setcc returns 0/-1, all bits are sign bits.
2440 // We know that we have an integer-based boolean since these operations
2441 // are only available for integer.
2442 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2443 TargetLowering::ZeroOrNegativeOneBooleanContent)
2444 return VTBits;
2445 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 case ISD::SETCC:
2447 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002448 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002449 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002450 return VTBits;
2451 break;
2452 case ISD::ROTL:
2453 case ISD::ROTR:
2454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002455 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002456
Dan Gohman309d3d52007-06-22 14:59:07 +00002457 // Handle rotate right by N like a rotate left by 32-N.
2458 if (Op.getOpcode() == ISD::ROTR)
2459 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2460
2461 // If we aren't rotating out all of the known-in sign bits, return the
2462 // number that are left. This handles rotl(sext(x), 1) for example.
2463 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2464 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2465 }
2466 break;
2467 case ISD::ADD:
2468 // Add can have at most one carry bit. Thus we know that the output
2469 // is, at worst, one more bit than the inputs.
2470 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2471 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002472
Dan Gohman309d3d52007-06-22 14:59:07 +00002473 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002474 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002475 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002476 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002477 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002478
Dan Gohman309d3d52007-06-22 14:59:07 +00002479 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2480 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002481 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002482 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002483
Dan Gohman309d3d52007-06-22 14:59:07 +00002484 // If we are subtracting one from a positive number, there is no carry
2485 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002486 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002487 return Tmp;
2488 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002489
Dan Gohman309d3d52007-06-22 14:59:07 +00002490 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2491 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002492 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002493
Dan Gohman309d3d52007-06-22 14:59:07 +00002494 case ISD::SUB:
2495 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2496 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002497
Dan Gohman309d3d52007-06-22 14:59:07 +00002498 // Handle NEG.
2499 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002500 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002501 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002502 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002503 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2504 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002505 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002506 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002507
Dan Gohman309d3d52007-06-22 14:59:07 +00002508 // If the input is known to be positive (the sign bit is known clear),
2509 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002510 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002511 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002512
Dan Gohman309d3d52007-06-22 14:59:07 +00002513 // Otherwise, we treat this like a SUB.
2514 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002515
Dan Gohman309d3d52007-06-22 14:59:07 +00002516 // Sub can have at most one carry bit. Thus we know that the output
2517 // is, at worst, one more bit than the inputs.
2518 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2519 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002520 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002521 case ISD::TRUNCATE:
2522 // FIXME: it's tricky to do anything useful for this, but it is an important
2523 // case for targets like X86.
2524 break;
2525 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002526
Nadav Rotem4536d582013-03-20 22:53:44 +00002527 // If we are looking at the loaded value of the SDNode.
2528 if (Op.getResNo() == 0) {
2529 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2530 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2531 unsigned ExtType = LD->getExtensionType();
2532 switch (ExtType) {
2533 default: break;
2534 case ISD::SEXTLOAD: // '17' bits known
2535 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2536 return VTBits-Tmp+1;
2537 case ISD::ZEXTLOAD: // '16' bits known
2538 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2539 return VTBits-Tmp;
2540 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002541 }
2542 }
2543
2544 // Allow the target to implement this method for its nodes.
2545 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002546 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002547 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2548 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002549 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002550 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002551 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002552
Dan Gohman309d3d52007-06-22 14:59:07 +00002553 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2554 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002555 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002556 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002557
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002558 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002559 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002560 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002561 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002562 Mask = KnownOne;
2563 } else {
2564 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002565 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002566 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002567
Dan Gohman309d3d52007-06-22 14:59:07 +00002568 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2569 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002570 Mask = ~Mask;
2571 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002572 // Return # leading zeros. We use 'min' here in case Val was zero before
2573 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002574 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002575}
2576
Chris Lattner46c01a32011-02-13 22:25:43 +00002577/// isBaseWithConstantOffset - Return true if the specified operand is an
2578/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2579/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2580/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002581/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002582bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2583 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2584 !isa<ConstantSDNode>(Op.getOperand(1)))
2585 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002586
2587 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002588 !MaskedValueIsZero(Op.getOperand(0),
2589 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2590 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002591
Chris Lattner46c01a32011-02-13 22:25:43 +00002592 return true;
2593}
2594
2595
Dan Gohmand0d5e682009-09-03 20:34:31 +00002596bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2597 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002598 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002599 return true;
2600
2601 // If the value is a constant, we can obviously see if it is a NaN or not.
2602 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2603 return !C->getValueAPF().isNaN();
2604
2605 // TODO: Recognize more cases here.
2606
2607 return false;
2608}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002609
Dan Gohman38605212010-02-24 06:52:40 +00002610bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2611 // If the value is a constant, we can obviously see if it is a zero or not.
2612 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2613 return !C->isZero();
2614
2615 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002616 switch (Op.getOpcode()) {
2617 default: break;
2618 case ISD::OR:
2619 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2620 return !C->isNullValue();
2621 break;
2622 }
Dan Gohman38605212010-02-24 06:52:40 +00002623
2624 return false;
2625}
2626
2627bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2628 // Check the obvious case.
2629 if (A == B) return true;
2630
2631 // For for negative and positive zero.
2632 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2633 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2634 if (CA->isZero() && CB->isZero()) return true;
2635
2636 // Otherwise they may not be equal.
2637 return false;
2638}
2639
Chris Lattner061a1ea2005-01-07 07:46:32 +00002640/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002641///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002643 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002644 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002645 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002646 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002647 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002648
Jack Carter170a5f22013-09-09 22:02:08 +00002649 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2650 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002651 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002652
Chandler Carruth41b20e72014-07-22 04:07:55 +00002653 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002654 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002655}
2656
Andrew Trickef9de2a2013-05-25 02:42:55 +00002657SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002658 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002659 // Constant fold unary operations with an integer constant operand. Even
2660 // opaque constant will be folded, because the folding of unary operations
2661 // doesn't create new constants with different values. Nevertheless, the
2662 // opaque flag is preserved during folding to prevent future folding with
2663 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002664 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002665 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002666 switch (Opcode) {
2667 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002668 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002669 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2670 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002671 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002672 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002673 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002674 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2675 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002676 case ISD::UINT_TO_FP:
2677 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002678 APFloat apf(EVTToAPFloatSemantics(VT),
2679 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002680 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002681 Opcode==ISD::SINT_TO_FP,
2682 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002683 return getConstantFP(apf, VT);
2684 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002685 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002686 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002687 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002688 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002689 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002690 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002691 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002692 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2693 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002694 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002695 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2696 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002697 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002698 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002699 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2700 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002701 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002702 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002703 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2704 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002705 }
2706 }
2707
Dale Johannesen446b9002007-08-31 23:34:27 +00002708 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002709 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002710 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002711 switch (Opcode) {
2712 case ISD::FNEG:
2713 V.changeSign();
2714 return getConstantFP(V, VT);
2715 case ISD::FABS:
2716 V.clearSign();
2717 return getConstantFP(V, VT);
2718 case ISD::FCEIL: {
2719 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2720 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002721 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002722 break;
2723 }
2724 case ISD::FTRUNC: {
2725 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2726 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002727 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002728 break;
2729 }
2730 case ISD::FFLOOR: {
2731 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2732 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002733 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002734 break;
2735 }
2736 case ISD::FP_EXTEND: {
2737 bool ignored;
2738 // This can return overflow, underflow, or inexact; we don't care.
2739 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002740 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002741 APFloat::rmNearestTiesToEven, &ignored);
2742 return getConstantFP(V, VT);
2743 }
2744 case ISD::FP_TO_SINT:
2745 case ISD::FP_TO_UINT: {
2746 integerPart x[2];
2747 bool ignored;
2748 assert(integerPartWidth >= 64);
2749 // FIXME need to be more flexible about rounding mode.
2750 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2751 Opcode==ISD::FP_TO_SINT,
2752 APFloat::rmTowardZero, &ignored);
2753 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002754 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002755 APInt api(VT.getSizeInBits(), x);
2756 return getConstant(api, VT);
2757 }
2758 case ISD::BITCAST:
2759 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2760 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2761 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2762 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2763 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002764 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002765 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002766
Jim Grosbachf7502c42014-07-18 00:40:52 +00002767 // Constant fold unary operations with a vector integer operand.
2768 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002769 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002770 switch (Opcode) {
2771 default:
2772 // FIXME: Entirely reasonable to perform folding of other unary
2773 // operations here as the need arises.
2774 break;
2775 case ISD::UINT_TO_FP:
2776 case ISD::SINT_TO_FP: {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002777 SmallVector<SDValue, 8> Ops;
2778 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2779 SDValue OpN = BV->getOperand(i);
2780 // Let the above scalar folding handle the conversion of each
2781 // element.
2782 OpN = getNode(ISD::SINT_TO_FP, DL, VT.getVectorElementType(),
2783 OpN);
2784 Ops.push_back(OpN);
2785 }
2786 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Jim Grosbachf7502c42014-07-18 00:40:52 +00002787 }
2788 }
2789 }
2790 }
2791
Gabor Greiff304a7a2008-08-28 21:40:38 +00002792 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002793 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002794 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002795 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002796 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002797 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002798 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002799 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002800 assert(VT.isFloatingPoint() &&
2801 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002802 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002803 assert((!VT.isVector() ||
2804 VT.getVectorNumElements() ==
2805 Operand.getValueType().getVectorNumElements()) &&
2806 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002807 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002808 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002809 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002810 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002811 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002812 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002813 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002814 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2815 "Invalid sext node, dst < src!");
2816 assert((!VT.isVector() ||
2817 VT.getVectorNumElements() ==
2818 Operand.getValueType().getVectorNumElements()) &&
2819 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002820 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2821 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2822 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002823 // sext(undef) = 0, because the top bits will all be the same.
2824 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002825 break;
2826 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002827 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002828 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002829 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002830 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2831 "Invalid zext node, dst < src!");
2832 assert((!VT.isVector() ||
2833 VT.getVectorNumElements() ==
2834 Operand.getValueType().getVectorNumElements()) &&
2835 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002836 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002837 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002838 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002839 else if (OpOpcode == ISD::UNDEF)
2840 // zext(undef) = 0, because the top bits will be zero.
2841 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002842 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002843 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002844 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002845 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002846 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002847 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2848 "Invalid anyext node, dst < src!");
2849 assert((!VT.isVector() ||
2850 VT.getVectorNumElements() ==
2851 Operand.getValueType().getVectorNumElements()) &&
2852 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002853
Dan Gohman08837892010-06-18 00:08:30 +00002854 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2855 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002856 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002857 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002858 else if (OpOpcode == ISD::UNDEF)
2859 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002860
2861 // (ext (trunx x)) -> x
2862 if (OpOpcode == ISD::TRUNCATE) {
2863 SDValue OpOp = Operand.getNode()->getOperand(0);
2864 if (OpOp.getValueType() == VT)
2865 return OpOp;
2866 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002867 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002868 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002869 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002870 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002871 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002872 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2873 "Invalid truncate node, src < dst!");
2874 assert((!VT.isVector() ||
2875 VT.getVectorNumElements() ==
2876 Operand.getValueType().getVectorNumElements()) &&
2877 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002878 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002879 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002880 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2881 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002882 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002883 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2884 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002885 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002886 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002887 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002888 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002889 }
Craig Topper201c1a32012-01-15 01:05:11 +00002890 if (OpOpcode == ISD::UNDEF)
2891 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002892 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002893 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002894 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002895 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002896 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002897 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002898 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2899 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002900 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002901 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002902 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002903 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002904 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002905 (VT.getVectorElementType() == Operand.getValueType() ||
2906 (VT.getVectorElementType().isInteger() &&
2907 Operand.getValueType().isInteger() &&
2908 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002909 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002910 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002911 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002912 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2913 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2914 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2915 Operand.getConstantOperandVal(1) == 0 &&
2916 Operand.getOperand(0).getValueType() == VT)
2917 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002918 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002919 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002920 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002921 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002922 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002923 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002924 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002925 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002926 break;
2927 case ISD::FABS:
2928 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002929 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002930 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002931 }
2932
Chris Lattnerf9c19152005-08-25 19:12:10 +00002933 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002934 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002935 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002936 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002937 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002938 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002939 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002940 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002941 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002942
Jack Carter170a5f22013-09-09 22:02:08 +00002943 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2944 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002945 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002946 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002947 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2948 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002949 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002950
Chandler Carruth41b20e72014-07-22 04:07:55 +00002951 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002952 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002953}
2954
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002955SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2956 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002957 // If the opcode is a target-specific ISD node, there's nothing we can
2958 // do here and the operand rules may not line up with the below, so
2959 // bail early.
2960 if (Opcode >= ISD::BUILTIN_OP_END)
2961 return SDValue();
2962
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002963 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2964 SmallVector<SDValue, 4> Outputs;
2965 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002966
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002967 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2968 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002969 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2970 return SDValue();
2971
2972 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002973 // Scalar instruction.
2974 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002975 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002976 // For vectors extract each constant element into Inputs so we can constant
2977 // fold them individually.
2978 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2979 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2980 if (!BV1 || !BV2)
2981 return SDValue();
2982
2983 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2984
2985 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2986 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2987 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2988 if (!V1 || !V2) // Not a constant, bail.
2989 return SDValue();
2990
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002991 if (V1->isOpaque() || V2->isOpaque())
2992 return SDValue();
2993
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002994 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2995 // FIXME: This is valid and could be handled by truncating the APInts.
2996 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2997 return SDValue();
2998
2999 Inputs.push_back(std::make_pair(V1, V2));
3000 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003001 }
3002
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003003 // We have a number of constant values, constant fold them element by element.
3004 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3005 const APInt &C1 = Inputs[I].first->getAPIntValue();
3006 const APInt &C2 = Inputs[I].second->getAPIntValue();
3007
3008 switch (Opcode) {
3009 case ISD::ADD:
3010 Outputs.push_back(getConstant(C1 + C2, SVT));
3011 break;
3012 case ISD::SUB:
3013 Outputs.push_back(getConstant(C1 - C2, SVT));
3014 break;
3015 case ISD::MUL:
3016 Outputs.push_back(getConstant(C1 * C2, SVT));
3017 break;
3018 case ISD::UDIV:
3019 if (!C2.getBoolValue())
3020 return SDValue();
3021 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3022 break;
3023 case ISD::UREM:
3024 if (!C2.getBoolValue())
3025 return SDValue();
3026 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3027 break;
3028 case ISD::SDIV:
3029 if (!C2.getBoolValue())
3030 return SDValue();
3031 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3032 break;
3033 case ISD::SREM:
3034 if (!C2.getBoolValue())
3035 return SDValue();
3036 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3037 break;
3038 case ISD::AND:
3039 Outputs.push_back(getConstant(C1 & C2, SVT));
3040 break;
3041 case ISD::OR:
3042 Outputs.push_back(getConstant(C1 | C2, SVT));
3043 break;
3044 case ISD::XOR:
3045 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3046 break;
3047 case ISD::SHL:
3048 Outputs.push_back(getConstant(C1 << C2, SVT));
3049 break;
3050 case ISD::SRL:
3051 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3052 break;
3053 case ISD::SRA:
3054 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3055 break;
3056 case ISD::ROTL:
3057 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3058 break;
3059 case ISD::ROTR:
3060 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3061 break;
3062 default:
3063 return SDValue();
3064 }
3065 }
3066
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003067 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3068 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003069
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003070 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003071 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003072 return Outputs.back();
3073
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003074 // We may have a vector type but a scalar result. Create a splat.
3075 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3076
3077 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003078 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003079}
3080
Andrew Trickef9de2a2013-05-25 02:42:55 +00003081SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003082 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003083 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3084 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003085 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003086 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003087 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003088 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3089 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003090 // Fold trivial token factors.
3091 if (N1.getOpcode() == ISD::EntryToken) return N2;
3092 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003093 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003094 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003095 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003096 // Concat of UNDEFs is UNDEF.
3097 if (N1.getOpcode() == ISD::UNDEF &&
3098 N2.getOpcode() == ISD::UNDEF)
3099 return getUNDEF(VT);
3100
Dan Gohman550c9af2008-08-14 20:04:46 +00003101 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3102 // one big BUILD_VECTOR.
3103 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3104 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003105 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3106 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003107 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003108 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003109 }
3110 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003111 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003112 assert(VT.isInteger() && "This operator does not apply to FP types!");
3113 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003114 N1.getValueType() == VT && "Binary operator types must match!");
3115 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3116 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003117 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003118 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003119 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3120 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003121 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003122 case ISD::OR:
3123 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003124 case ISD::ADD:
3125 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003126 assert(VT.isInteger() && "This operator does not apply to FP types!");
3127 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003128 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003129 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3130 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003131 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003132 return N1;
3133 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003134 case ISD::UDIV:
3135 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003136 case ISD::MULHU:
3137 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003138 case ISD::MUL:
3139 case ISD::SDIV:
3140 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003141 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003142 assert(N1.getValueType() == N2.getValueType() &&
3143 N1.getValueType() == VT && "Binary operator types must match!");
3144 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003145 case ISD::FADD:
3146 case ISD::FSUB:
3147 case ISD::FMUL:
3148 case ISD::FDIV:
3149 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003150 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003151 if (Opcode == ISD::FADD) {
3152 // 0+x --> x
3153 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3154 if (CFP->getValueAPF().isZero())
3155 return N2;
3156 // x+0 --> x
3157 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3158 if (CFP->getValueAPF().isZero())
3159 return N1;
3160 } else if (Opcode == ISD::FSUB) {
3161 // x-0 --> x
3162 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3163 if (CFP->getValueAPF().isZero())
3164 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003165 } else if (Opcode == ISD::FMUL) {
3166 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3167 SDValue V = N2;
3168
3169 // If the first operand isn't the constant, try the second
3170 if (!CFP) {
3171 CFP = dyn_cast<ConstantFPSDNode>(N2);
3172 V = N1;
3173 }
3174
3175 if (CFP) {
3176 // 0*x --> 0
3177 if (CFP->isZero())
3178 return SDValue(CFP,0);
3179 // 1*x --> x
3180 if (CFP->isExactlyValue(1.0))
3181 return V;
3182 }
Dan Gohman1275e282009-01-23 19:10:37 +00003183 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003184 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003185 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003186 assert(N1.getValueType() == N2.getValueType() &&
3187 N1.getValueType() == VT && "Binary operator types must match!");
3188 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003189 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3190 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003191 N1.getValueType().isFloatingPoint() &&
3192 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003193 "Invalid FCOPYSIGN!");
3194 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003195 case ISD::SHL:
3196 case ISD::SRA:
3197 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003198 case ISD::ROTL:
3199 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003200 assert(VT == N1.getValueType() &&
3201 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003202 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003203 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003204 assert((!VT.isVector() || VT == N2.getValueType()) &&
3205 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003206 // Verify that the shift amount VT is bit enough to hold valid shift
3207 // amounts. This catches things like trying to shift an i1024 value by an
3208 // i8, which is easy to fall into in generic code that uses
3209 // TLI.getShiftAmount().
3210 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003211 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003212 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003213
3214 // Always fold shifts of i1 values so the code generator doesn't need to
3215 // handle them. Since we know the size of the shift has to be less than the
3216 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003217 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003218 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003219 if (N2C && N2C->isNullValue())
3220 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003221 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003222 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003223 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003224 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003225 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003226 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003227 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003228 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003229 "type is vector!");
3230 assert((!EVT.isVector() ||
3231 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3232 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003233 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003234 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003235 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003236 break;
3237 }
Chris Lattner72733e52008-01-17 07:00:52 +00003238 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003239 assert(VT.isFloatingPoint() &&
3240 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003241 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003242 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003243 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003244 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003245 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003246 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003247 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003248 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003249 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003250 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003251 assert(!EVT.isVector() &&
3252 "AssertSExt/AssertZExt type should be the vector element type "
3253 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003254 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003255 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003256 break;
3257 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003258 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003259 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003260 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003261 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003262 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003263 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003264 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003265 "type is vector!");
3266 assert((!EVT.isVector() ||
3267 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3268 "Vector element counts must match in SIGN_EXTEND_INREG");
3269 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003270 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003271
Chris Lattner16713612008-01-22 19:09:33 +00003272 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003273 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003274 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003275 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003276 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003277 return getConstant(Val, VT);
3278 }
Chris Lattner16713612008-01-22 19:09:33 +00003279 break;
3280 }
3281 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003282 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3283 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003284 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003285
Chris Lattner16713612008-01-22 19:09:33 +00003286 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3287 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003288 if (N2C &&
3289 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003290 N1.getNumOperands() > 0) {
3291 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003292 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003293 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003294 N1.getOperand(N2C->getZExtValue() / Factor),
3295 getConstant(N2C->getZExtValue() % Factor,
3296 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003297 }
3298
3299 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3300 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003301 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3302 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003303
3304 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003305 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003306 // are promoted and implicitly truncated, and the result implicitly
3307 // extended. Make that explicit here.
3308 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003309
Bob Wilson59dbbb22009-04-13 22:05:19 +00003310 return Elt;
3311 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003312
Chris Lattner16713612008-01-22 19:09:33 +00003313 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3314 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003315 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003316 // If the indices are the same, return the inserted element else
3317 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003318 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003319 SDValue N1Op2 = N1.getOperand(2);
3320 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3321
3322 if (N1Op2C && N2C) {
3323 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3324 if (VT == N1.getOperand(1).getValueType())
3325 return N1.getOperand(1);
3326 else
3327 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3328 }
3329
Dale Johannesendb393622009-02-03 01:55:44 +00003330 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003331 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003332 }
Chris Lattner16713612008-01-22 19:09:33 +00003333 break;
3334 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003335 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003336 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3337 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003338 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003339 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003340
Chris Lattner16713612008-01-22 19:09:33 +00003341 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3342 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003343 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003344 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003345 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003346
Chris Lattner16713612008-01-22 19:09:33 +00003347 // EXTRACT_ELEMENT of a constant int is also very common.
3348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003349 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003350 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003351 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3352 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003353 }
3354 break;
David Greeneb6f16112011-01-26 15:38:49 +00003355 case ISD::EXTRACT_SUBVECTOR: {
3356 SDValue Index = N2;
3357 if (VT.isSimple() && N1.getValueType().isSimple()) {
3358 assert(VT.isVector() && N1.getValueType().isVector() &&
3359 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003360 assert(VT.getVectorElementType() ==
3361 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003362 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003363 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003364 "Extract subvector must be from larger vector to smaller vector!");
3365
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003366 if (isa<ConstantSDNode>(Index.getNode())) {
3367 assert((VT.getVectorNumElements() +
3368 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003369 <= N1.getValueType().getVectorNumElements())
3370 && "Extract subvector overflow!");
3371 }
3372
3373 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003374 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003375 return N1;
3376 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003377 break;
Chris Lattner16713612008-01-22 19:09:33 +00003378 }
David Greeneb6f16112011-01-26 15:38:49 +00003379 }
Chris Lattner16713612008-01-22 19:09:33 +00003380
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003381 // Perform trivial constant folding.
3382 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3383 if (SV.getNode()) return SV;
3384
3385 // Canonicalize constant to RHS if commutative.
3386 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3387 std::swap(N1C, N2C);
3388 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003389 }
3390
Chris Lattner16713612008-01-22 19:09:33 +00003391 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003392 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3393 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003394 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003395 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003396 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003397 std::swap(N1CFP, N2CFP);
3398 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003399 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003400 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3401 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003402 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003403 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003404 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003405 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003406 return getConstantFP(V1, VT);
3407 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003408 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003409 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3410 if (s!=APFloat::opInvalidOp)
3411 return getConstantFP(V1, VT);
3412 break;
3413 case ISD::FMUL:
3414 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3415 if (s!=APFloat::opInvalidOp)
3416 return getConstantFP(V1, VT);
3417 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003418 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003419 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3420 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3421 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003422 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003423 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003424 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3425 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3426 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003427 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003428 case ISD::FCOPYSIGN:
3429 V1.copySign(V2);
3430 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003431 default: break;
3432 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003433 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003434
3435 if (Opcode == ISD::FP_ROUND) {
3436 APFloat V = N1CFP->getValueAPF(); // make copy
3437 bool ignored;
3438 // This can return overflow, underflow, or inexact; we don't care.
3439 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003440 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003441 APFloat::rmNearestTiesToEven, &ignored);
3442 return getConstantFP(V, VT);
3443 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003444 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003445
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003446 // Canonicalize an UNDEF to the RHS, even over a constant.
3447 if (N1.getOpcode() == ISD::UNDEF) {
3448 if (isCommutativeBinOp(Opcode)) {
3449 std::swap(N1, N2);
3450 } else {
3451 switch (Opcode) {
3452 case ISD::FP_ROUND_INREG:
3453 case ISD::SIGN_EXTEND_INREG:
3454 case ISD::SUB:
3455 case ISD::FSUB:
3456 case ISD::FDIV:
3457 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003458 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003459 return N1; // fold op(undef, arg2) -> undef
3460 case ISD::UDIV:
3461 case ISD::SDIV:
3462 case ISD::UREM:
3463 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003464 case ISD::SRL:
3465 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003466 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003467 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3468 // For vectors, we can't easily build an all zero vector, just return
3469 // the LHS.
3470 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003471 }
3472 }
3473 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003474
3475 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003476 if (N2.getOpcode() == ISD::UNDEF) {
3477 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003478 case ISD::XOR:
3479 if (N1.getOpcode() == ISD::UNDEF)
3480 // Handle undef ^ undef -> 0 special case. This is a common
3481 // idiom (misuse).
3482 return getConstant(0, VT);
3483 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003484 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003485 case ISD::ADDC:
3486 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003487 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003488 case ISD::UDIV:
3489 case ISD::SDIV:
3490 case ISD::UREM:
3491 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003492 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003493 case ISD::FADD:
3494 case ISD::FSUB:
3495 case ISD::FMUL:
3496 case ISD::FDIV:
3497 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003498 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003499 return N2;
3500 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003501 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003502 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003503 case ISD::SRL:
3504 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003505 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003506 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3507 // For vectors, we can't easily build an all zero vector, just return
3508 // the LHS.
3509 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003510 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003511 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003512 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003513 // For vectors, we can't easily build an all one vector, just return
3514 // the LHS.
3515 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003516 case ISD::SRA:
3517 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003518 }
3519 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003520
Chris Lattner724f7ee2005-05-11 18:57:39 +00003521 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003522 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003523 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003524 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003525 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003526 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003527 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003528 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003529 if (BinOpHasFlags)
3530 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003531 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003532 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003533 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003534
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003535 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3536
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003537 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003538 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003539
3540 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003541 }
3542
Chandler Carruth41b20e72014-07-22 04:07:55 +00003543 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003544 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003545}
3546
Andrew Trickef9de2a2013-05-25 02:42:55 +00003547SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003548 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003549 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003551 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003552 case ISD::FMA: {
3553 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3554 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3555 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3556 if (N1CFP && N2CFP && N3CFP) {
3557 APFloat V1 = N1CFP->getValueAPF();
3558 const APFloat &V2 = N2CFP->getValueAPF();
3559 const APFloat &V3 = N3CFP->getValueAPF();
3560 APFloat::opStatus s =
3561 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3562 if (s != APFloat::opInvalidOp)
3563 return getConstantFP(V1, VT);
3564 }
3565 break;
3566 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003567 case ISD::CONCAT_VECTORS:
3568 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3569 // one big BUILD_VECTOR.
3570 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3571 N2.getOpcode() == ISD::BUILD_VECTOR &&
3572 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003573 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3574 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003575 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3576 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003577 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003578 }
3579 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003580 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003581 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003582 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003583 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003584 break;
3585 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003586 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003587 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003588 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003589 return N2; // select true, X, Y -> X
3590 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003591 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003592
3593 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003594 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003595 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003596 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003597 case ISD::INSERT_SUBVECTOR: {
3598 SDValue Index = N3;
3599 if (VT.isSimple() && N1.getValueType().isSimple()
3600 && N2.getValueType().isSimple()) {
3601 assert(VT.isVector() && N1.getValueType().isVector() &&
3602 N2.getValueType().isVector() &&
3603 "Insert subvector VTs must be a vectors");
3604 assert(VT == N1.getValueType() &&
3605 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003606 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003607 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003608 if (isa<ConstantSDNode>(Index.getNode())) {
3609 assert((N2.getValueType().getVectorNumElements() +
3610 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003611 <= VT.getVectorNumElements())
3612 && "Insert subvector overflow!");
3613 }
3614
3615 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003616 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003617 return N2;
3618 }
3619 break;
3620 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003621 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003622 // Fold bit_convert nodes from a type to themselves.
3623 if (N1.getValueType() == VT)
3624 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003625 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003626 }
3627
Chris Lattnerf9c19152005-08-25 19:12:10 +00003628 // Memoize node if it doesn't produce a flag.
3629 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003630 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003631 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003632 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003633 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003634 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003635 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003636 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003637 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003638
Jack Carter170a5f22013-09-09 22:02:08 +00003639 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3640 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003641 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003642 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003643 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3644 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003645 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003646
Chandler Carruth41b20e72014-07-22 04:07:55 +00003647 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003648 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003649}
3650
Andrew Trickef9de2a2013-05-25 02:42:55 +00003651SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003652 SDValue N1, SDValue N2, SDValue N3,
3653 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003654 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003655 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003656}
3657
Andrew Trickef9de2a2013-05-25 02:42:55 +00003658SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003659 SDValue N1, SDValue N2, SDValue N3,
3660 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003661 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003662 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003663}
3664
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003665/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3666/// the incoming stack arguments to be loaded from the stack.
3667SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3668 SmallVector<SDValue, 8> ArgChains;
3669
3670 // Include the original chain at the beginning of the list. When this is
3671 // used by target LowerCall hooks, this helps legalize find the
3672 // CALLSEQ_BEGIN node.
3673 ArgChains.push_back(Chain);
3674
3675 // Add a chain value for each stack argument.
3676 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3677 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3678 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3679 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3680 if (FI->getIndex() < 0)
3681 ArgChains.push_back(SDValue(L, 1));
3682
3683 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003684 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003685}
3686
Dan Gohman544ab2c2008-04-12 04:36:06 +00003687/// getMemsetValue - Vectorized representation of the memset value
3688/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003689static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003690 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003691 assert(Value.getOpcode() != ISD::UNDEF);
3692
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003693 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003694 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003695 assert(C->getAPIntValue().getBitWidth() == 8);
3696 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003697 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003698 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003699 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003700 }
Evan Chengef377ad2008-05-15 08:39:06 +00003701
Dale Johannesenabf66b82009-02-03 22:26:09 +00003702 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003703 if (NumBits > 8) {
3704 // Use a multiplication with 0x010101... to extend the input to the
3705 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003706 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003707 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003708 }
3709
3710 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003711}
3712
Dan Gohman544ab2c2008-04-12 04:36:06 +00003713/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3714/// used when a memcpy is turned into a memset when the source is a constant
3715/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003716static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003717 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003718 // Handle vector with all elements zero.
3719 if (Str.empty()) {
3720 if (VT.isInteger())
3721 return DAG.getConstant(0, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003722 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003723 return DAG.getConstantFP(0.0, VT);
3724 else if (VT.isVector()) {
3725 unsigned NumElts = VT.getVectorNumElements();
3726 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003727 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003728 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3729 EltVT, NumElts)));
3730 } else
3731 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003732 }
3733
Duncan Sands13237ac2008-06-06 12:08:01 +00003734 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003735 unsigned NumVTBits = VT.getSizeInBits();
3736 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003737 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3738
Evan Chengc8444b12013-01-10 22:13:27 +00003739 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003740 if (TLI.isLittleEndian()) {
3741 for (unsigned i = 0; i != NumBytes; ++i)
3742 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3743 } else {
3744 for (unsigned i = 0; i != NumBytes; ++i)
3745 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003746 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003747
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003748 // If the "cost" of materializing the integer immediate is less than the cost
3749 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003750 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3751 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003752 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003753 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003754}
3755
Scott Michelcf0da6c2009-02-17 22:15:04 +00003756/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003757///
Andrew Tricke2431c62013-05-25 03:08:10 +00003758static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003759 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003760 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003761 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003762 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003763}
3764
Evan Chengef377ad2008-05-15 08:39:06 +00003765/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3766///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003767static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003768 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003769 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003770 if (Src.getOpcode() == ISD::GlobalAddress)
3771 G = cast<GlobalAddressSDNode>(Src);
3772 else if (Src.getOpcode() == ISD::ADD &&
3773 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3774 Src.getOperand(1).getOpcode() == ISD::Constant) {
3775 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003776 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003777 }
3778 if (!G)
3779 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003780
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003781 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003782}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003783
Evan Cheng43cd9e32010-04-01 06:04:33 +00003784/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3785/// to replace the memset / memcpy. Return true if the number of memory ops
3786/// is below the threshold. It returns the types of the sequence of
3787/// memory ops to perform memset / memcpy by reference.
3788static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003789 unsigned Limit, uint64_t Size,
3790 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003791 bool IsMemset,
3792 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003793 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003794 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003795 SelectionDAG &DAG,
3796 const TargetLowering &TLI) {
3797 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3798 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003799 // If 'SrcAlign' is zero, that means the memory operation does not need to
3800 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3801 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3802 // is the specified alignment of the memory operation. If it is zero, that
3803 // means it's possible to change the alignment of the destination.
3804 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3805 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003806 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003807 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003808 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003809
Chris Lattner28dc6c12010-03-07 07:45:08 +00003810 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003811 unsigned AS = 0;
3812 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003813 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003814 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003815 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003816 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003817 case 0: VT = MVT::i64; break;
3818 case 4: VT = MVT::i32; break;
3819 case 2: VT = MVT::i16; break;
3820 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003821 }
3822 }
3823
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003824 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003825 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003826 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003827 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003828
Duncan Sands11dd4242008-06-08 20:54:56 +00003829 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003830 VT = LVT;
3831 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003832
Dan Gohman544ab2c2008-04-12 04:36:06 +00003833 unsigned NumMemOps = 0;
3834 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003835 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003836 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003837 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003838 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003839 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003840
3841 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003842 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003843 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003844 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003845 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003846 Found = true;
3847 else if (NewVT == MVT::i64 &&
3848 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003849 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003850 // i64 is usually not legal on 32-bit targets, but f64 may be.
3851 NewVT = MVT::f64;
3852 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003853 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003854 }
3855
Evan Cheng04e55182012-12-12 00:42:09 +00003856 if (!Found) {
3857 do {
3858 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3859 if (NewVT == MVT::i8)
3860 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003861 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003862 }
3863 NewVTSize = NewVT.getSizeInBits() / 8;
3864
Evan Cheng79e2ca92012-12-10 23:21:26 +00003865 // If the new VT cannot cover all of the remaining bits, then consider
3866 // issuing a (or a pair of) unaligned and overlapping load / store.
3867 // FIXME: Only does this for 64-bit or more since we don't have proper
3868 // cost model for unaligned load / store.
3869 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003870 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003871 if (NumMemOps && AllowOverlap &&
3872 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003873 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003874 VTSize = Size;
3875 else {
3876 VT = NewVT;
3877 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003878 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003879 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003880
Evan Chengb7d3d032012-12-12 20:43:23 +00003881 if (++NumMemOps > Limit)
3882 return false;
3883
Dan Gohman544ab2c2008-04-12 04:36:06 +00003884 MemOps.push_back(VT);
3885 Size -= VTSize;
3886 }
3887
3888 return true;
3889}
3890
Andrew Trickef9de2a2013-05-25 02:42:55 +00003891static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003892 SDValue Chain, SDValue Dst,
3893 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003894 unsigned Align, bool isVol,
3895 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003896 MachinePointerInfo DstPtrInfo,
3897 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003898 // Turn a memcpy of undef to nop.
3899 if (Src.getOpcode() == ISD::UNDEF)
3900 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003901
Dan Gohman714663a2008-05-29 19:42:22 +00003902 // Expand memcpy to a series of load and store ops if the size operand falls
3903 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003904 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3905 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003906 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003907 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003908 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003909 MachineFunction &MF = DAG.getMachineFunction();
3910 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003911 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003912 MF.getFunction()->getAttributes().
3913 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003914 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3915 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3916 DstAlignCanChange = true;
3917 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3918 if (Align > SrcAlign)
3919 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003920 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003921 bool CopyFromStr = isMemSrcFromString(Src, Str);
3922 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003923 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003924
Evan Cheng4c014c82010-04-01 18:19:11 +00003925 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003926 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003927 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003928 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003929 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003930
Evan Cheng43cd9e32010-04-01 06:04:33 +00003931 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003932 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003933 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003934
3935 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003936 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003937 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3938 if (!TRI->needsStackRealignment(MF))
3939 while (NewAlign > Align &&
3940 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3941 NewAlign /= 2;
3942
Evan Cheng43cd9e32010-04-01 06:04:33 +00003943 if (NewAlign > Align) {
3944 // Give the stack frame object a larger alignment if needed.
3945 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3946 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3947 Align = NewAlign;
3948 }
3949 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003950
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003951 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003952 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003953 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003954 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003955 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003956 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003957 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003958
Evan Cheng79e2ca92012-12-10 23:21:26 +00003959 if (VTSize > Size) {
3960 // Issuing an unaligned load / store pair that overlaps with the previous
3961 // pair. Adjust the offset accordingly.
3962 assert(i == NumMemOps-1 && i != 0);
3963 SrcOff -= VTSize - Size;
3964 DstOff -= VTSize - Size;
3965 }
3966
Evan Cheng43cd9e32010-04-01 06:04:33 +00003967 if (CopyFromStr &&
3968 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003969 // It's unlikely a store of a vector immediate can be done in a single
3970 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003971 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003972 // FIXME: Handle other cases where store of vector immediate is done in
3973 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003974 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003975 if (Value.getNode())
3976 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003977 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003978 DstPtrInfo.getWithOffset(DstOff), isVol,
3979 false, Align);
3980 }
3981
3982 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003983 // The type might not be legal for the target. This should only happen
3984 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003985 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3986 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003987 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003988 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003989 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003990 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003991 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003992 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00003993 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003994 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003995 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003996 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3997 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003998 }
3999 OutChains.push_back(Store);
4000 SrcOff += VTSize;
4001 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004002 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004003 }
4004
Craig Topper48d114b2014-04-26 18:35:24 +00004005 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004006}
4007
Andrew Trickef9de2a2013-05-25 02:42:55 +00004008static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004009 SDValue Chain, SDValue Dst,
4010 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004011 unsigned Align, bool isVol,
4012 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004013 MachinePointerInfo DstPtrInfo,
4014 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004015 // Turn a memmove of undef to nop.
4016 if (Src.getOpcode() == ISD::UNDEF)
4017 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004018
4019 // Expand memmove to a series of load and store ops if the size operand falls
4020 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004021 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004022 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004023 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004024 MachineFunction &MF = DAG.getMachineFunction();
4025 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004026 bool OptSize = MF.getFunction()->getAttributes().
4027 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004028 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4029 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4030 DstAlignCanChange = true;
4031 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4032 if (Align > SrcAlign)
4033 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004034 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004035
Evan Cheng4c014c82010-04-01 18:19:11 +00004036 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004037 (DstAlignCanChange ? 0 : Align), SrcAlign,
4038 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004039 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004040
Evan Cheng43cd9e32010-04-01 06:04:33 +00004041 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004042 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004043 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004044 if (NewAlign > Align) {
4045 // Give the stack frame object a larger alignment if needed.
4046 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4047 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4048 Align = NewAlign;
4049 }
4050 }
Dan Gohman714663a2008-05-29 19:42:22 +00004051
Evan Cheng43cd9e32010-04-01 06:04:33 +00004052 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004053 SmallVector<SDValue, 8> LoadValues;
4054 SmallVector<SDValue, 8> LoadChains;
4055 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004056 unsigned NumMemOps = MemOps.size();
4057 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004058 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004059 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004060 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004061
Dale Johannesenabf66b82009-02-03 22:26:09 +00004062 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004063 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004064 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004065 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004066 LoadValues.push_back(Value);
4067 LoadChains.push_back(Value.getValue(1));
4068 SrcOff += VTSize;
4069 }
Craig Topper48d114b2014-04-26 18:35:24 +00004070 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004071 OutChains.clear();
4072 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004073 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004074 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004075 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004076
Dale Johannesenabf66b82009-02-03 22:26:09 +00004077 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004078 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004079 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004080 OutChains.push_back(Store);
4081 DstOff += VTSize;
4082 }
4083
Craig Topper48d114b2014-04-26 18:35:24 +00004084 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004085}
4086
Serge Pavlov8ec39992013-09-17 16:24:42 +00004087/// \brief Lower the call to 'memset' intrinsic function into a series of store
4088/// operations.
4089///
4090/// \param DAG Selection DAG where lowered code is placed.
4091/// \param dl Link to corresponding IR location.
4092/// \param Chain Control flow dependency.
4093/// \param Dst Pointer to destination memory location.
4094/// \param Src Value of byte to write into the memory.
4095/// \param Size Number of bytes to write.
4096/// \param Align Alignment of the destination in bytes.
4097/// \param isVol True if destination is volatile.
4098/// \param DstPtrInfo IR information on the memory pointer.
4099/// \returns New head in the control flow, if lowering was successful, empty
4100/// SDValue otherwise.
4101///
4102/// The function tries to replace 'llvm.memset' intrinsic with several store
4103/// operations and value calculation code. This is usually profitable for small
4104/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004105static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004106 SDValue Chain, SDValue Dst,
4107 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004108 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004109 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004110 // Turn a memset of undef to nop.
4111 if (Src.getOpcode() == ISD::UNDEF)
4112 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004113
4114 // Expand memset to a series of load/store ops if the size operand
4115 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004116 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004117 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004118 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004119 MachineFunction &MF = DAG.getMachineFunction();
4120 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004121 bool OptSize = MF.getFunction()->getAttributes().
4122 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004123 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4124 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4125 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004126 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004127 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004128 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004129 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004130 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004131 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004132
Evan Cheng43cd9e32010-04-01 06:04:33 +00004133 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004134 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004135 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004136 if (NewAlign > Align) {
4137 // Give the stack frame object a larger alignment if needed.
4138 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4139 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4140 Align = NewAlign;
4141 }
4142 }
4143
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004144 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004145 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004146 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004147
4148 // Find the largest store and generate the bit pattern for it.
4149 EVT LargestVT = MemOps[0];
4150 for (unsigned i = 1; i < NumMemOps; i++)
4151 if (MemOps[i].bitsGT(LargestVT))
4152 LargestVT = MemOps[i];
4153 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4154
Dan Gohman544ab2c2008-04-12 04:36:06 +00004155 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004156 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004157 unsigned VTSize = VT.getSizeInBits() / 8;
4158 if (VTSize > Size) {
4159 // Issuing an unaligned load / store pair that overlaps with the previous
4160 // pair. Adjust the offset accordingly.
4161 assert(i == NumMemOps-1 && i != 0);
4162 DstOff -= VTSize - Size;
4163 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004164
4165 // If this store is smaller than the largest store see whether we can get
4166 // the smaller value for free with a truncate.
4167 SDValue Value = MemSetValue;
4168 if (VT.bitsLT(LargestVT)) {
4169 if (!LargestVT.isVector() && !VT.isVector() &&
4170 TLI.isTruncateFree(LargestVT, VT))
4171 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4172 else
4173 Value = getMemsetValue(Src, VT, DAG, dl);
4174 }
4175 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004176 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004177 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004178 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004179 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004180 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004181 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004182 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004183 }
4184
Craig Topper48d114b2014-04-26 18:35:24 +00004185 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004186}
4187
Andrew Trickef9de2a2013-05-25 02:42:55 +00004188SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004189 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004190 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004191 MachinePointerInfo DstPtrInfo,
4192 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004193 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004194
4195 // Check to see if we should lower the memcpy to loads and stores first.
4196 // For cases within the target-specified limits, this is the best choice.
4197 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4198 if (ConstantSize) {
4199 // Memcpy with size zero? Just return the original chain.
4200 if (ConstantSize->isNullValue())
4201 return Chain;
4202
Evan Cheng43cd9e32010-04-01 06:04:33 +00004203 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4204 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004205 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004206 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004207 return Result;
4208 }
4209
4210 // Then check to see if we should lower the memcpy with target-specific
4211 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004212 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004213 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004214 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004215 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004216 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004217 return Result;
4218
4219 // If we really need inline code and the target declined to provide it,
4220 // use a (potentially long) sequence of loads and stores.
4221 if (AlwaysInline) {
4222 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004223 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004224 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004225 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004226 }
4227
Dan Gohmanf38547c2010-04-05 20:24:08 +00004228 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4229 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4230 // respect volatile, so they may do things like read or write memory
4231 // beyond the given memory regions. But fixing this isn't easy, and most
4232 // people don't care.
4233
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004234 const TargetLowering *TLI = TM.getTargetLowering();
4235
Dan Gohman544ab2c2008-04-12 04:36:06 +00004236 // Emit a library call.
4237 TargetLowering::ArgListTy Args;
4238 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004239 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004240 Entry.Node = Dst; Args.push_back(Entry);
4241 Entry.Node = Src; Args.push_back(Entry);
4242 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004243 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004244 TargetLowering::CallLoweringInfo CLI(*this);
4245 CLI.setDebugLoc(dl).setChain(Chain)
4246 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4247 Type::getVoidTy(*getContext()),
4248 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004249 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004250 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004251 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004252
Dan Gohman544ab2c2008-04-12 04:36:06 +00004253 return CallResult.second;
4254}
4255
Andrew Trickef9de2a2013-05-25 02:42:55 +00004256SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004257 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004258 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004259 MachinePointerInfo DstPtrInfo,
4260 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004261 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004262
Dan Gohman714663a2008-05-29 19:42:22 +00004263 // Check to see if we should lower the memmove to loads and stores first.
4264 // For cases within the target-specified limits, this is the best choice.
4265 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4266 if (ConstantSize) {
4267 // Memmove with size zero? Just return the original chain.
4268 if (ConstantSize->isNullValue())
4269 return Chain;
4270
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004271 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004272 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004273 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004274 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004275 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004276 return Result;
4277 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004278
4279 // Then check to see if we should lower the memmove with target-specific
4280 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004281 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004282 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004283 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004284 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004285 return Result;
4286
Mon P Wangbf862242010-04-06 08:27:51 +00004287 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4288 // not be safe. See memcpy above for more details.
4289
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004290 const TargetLowering *TLI = TM.getTargetLowering();
4291
Dan Gohman544ab2c2008-04-12 04:36:06 +00004292 // Emit a library call.
4293 TargetLowering::ArgListTy Args;
4294 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004295 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004296 Entry.Node = Dst; Args.push_back(Entry);
4297 Entry.Node = Src; Args.push_back(Entry);
4298 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004299 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004300 TargetLowering::CallLoweringInfo CLI(*this);
4301 CLI.setDebugLoc(dl).setChain(Chain)
4302 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4303 Type::getVoidTy(*getContext()),
4304 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004305 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004306 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004307 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004308
Dan Gohman544ab2c2008-04-12 04:36:06 +00004309 return CallResult.second;
4310}
4311
Andrew Trickef9de2a2013-05-25 02:42:55 +00004312SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004313 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004314 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004315 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004316 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004317
4318 // Check to see if we should lower the memset to stores first.
4319 // For cases within the target-specified limits, this is the best choice.
4320 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4321 if (ConstantSize) {
4322 // Memset with size zero? Just return the original chain.
4323 if (ConstantSize->isNullValue())
4324 return Chain;
4325
Mon P Wangc576ee92010-04-04 03:10:48 +00004326 SDValue Result =
4327 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004328 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004329
Gabor Greiff304a7a2008-08-28 21:40:38 +00004330 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004331 return Result;
4332 }
4333
4334 // Then check to see if we should lower the memset with target-specific
4335 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004336 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004337 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004338 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004339 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004340 return Result;
4341
Wesley Peck527da1b2010-11-23 03:31:01 +00004342 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004343 const TargetLowering *TLI = TM.getTargetLowering();
4344 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004345 TargetLowering::ArgListTy Args;
4346 TargetLowering::ArgListEntry Entry;
4347 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4348 Args.push_back(Entry);
4349 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004350 if (Src.getValueType().bitsGT(MVT::i32))
4351 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004352 else
Owen Anderson9f944592009-08-11 20:47:22 +00004353 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004354 Entry.Node = Src;
4355 Entry.Ty = Type::getInt32Ty(*getContext());
4356 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004357 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004358 Entry.Node = Size;
4359 Entry.Ty = IntPtrTy;
4360 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004361 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004362
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004363 // FIXME: pass in SDLoc
4364 TargetLowering::CallLoweringInfo CLI(*this);
4365 CLI.setDebugLoc(dl).setChain(Chain)
4366 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4367 Type::getVoidTy(*getContext()),
4368 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004369 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004370 .setDiscardResult();
4371
4372 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004373 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004374}
4375
Andrew Trickef9de2a2013-05-25 02:42:55 +00004376SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004377 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004378 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004379 AtomicOrdering SuccessOrdering,
4380 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004381 SynchronizationScope SynchScope) {
4382 FoldingSetNodeID ID;
4383 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004384 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004385 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004386 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004387 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4388 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4389 return SDValue(E, 0);
4390 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004391
4392 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4393 // SDNode doesn't have access to it. This memory will be "leaked" when
4394 // the node is deallocated, but recovered when the allocator is released.
4395 // If the number of operands is less than 5 we use AtomicSDNode's internal
4396 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004397 unsigned NumOps = Ops.size();
4398 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4399 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004400
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004401 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4402 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004403 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004404 SuccessOrdering, FailureOrdering,
4405 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004406 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004407 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004408 return SDValue(N, 0);
4409}
4410
4411SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004412 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004413 MachineMemOperand *MMO,
4414 AtomicOrdering Ordering,
4415 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004416 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004417 Ordering, SynchScope);
4418}
4419
Tim Northover420a2162014-06-13 14:24:07 +00004420SDValue SelectionDAG::getAtomicCmpSwap(
4421 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4422 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4423 unsigned Alignment, AtomicOrdering SuccessOrdering,
4424 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4425 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4426 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4427 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4428
Dan Gohman48b185d2009-09-25 20:36:54 +00004429 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4430 Alignment = getEVTAlignment(MemVT);
4431
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004432 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004433
Eli Friedmane978d2f2011-09-07 02:23:42 +00004434 // FIXME: Volatile isn't really correct; we should keep track of atomic
4435 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004436 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004437 Flags |= MachineMemOperand::MOLoad;
4438 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004439
4440 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004441 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004442
Tim Northover420a2162014-06-13 14:24:07 +00004443 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4444 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004445}
4446
Tim Northover420a2162014-06-13 14:24:07 +00004447SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4448 SDVTList VTs, SDValue Chain, SDValue Ptr,
4449 SDValue Cmp, SDValue Swp,
4450 MachineMemOperand *MMO,
4451 AtomicOrdering SuccessOrdering,
4452 AtomicOrdering FailureOrdering,
4453 SynchronizationScope SynchScope) {
4454 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4455 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004456 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4457
Dale Johannesen839acbb2009-01-29 00:47:48 +00004458 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004459 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4460 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004461}
4462
Andrew Trickef9de2a2013-05-25 02:42:55 +00004463SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004464 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004465 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004466 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004467 unsigned Alignment,
4468 AtomicOrdering Ordering,
4469 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004470 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4471 Alignment = getEVTAlignment(MemVT);
4472
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004473 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004474 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004475 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004476 // For now, atomics are considered to be volatile always, and they are
4477 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004478 // FIXME: Volatile isn't really correct; we should keep track of atomic
4479 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004480 unsigned Flags = MachineMemOperand::MOVolatile;
4481 if (Opcode != ISD::ATOMIC_STORE)
4482 Flags |= MachineMemOperand::MOLoad;
4483 if (Opcode != ISD::ATOMIC_LOAD)
4484 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004485
4486 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004487 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004488 MemVT.getStoreSize(), Alignment);
4489
Eli Friedmanadec5872011-07-29 03:05:32 +00004490 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4491 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004492}
4493
Andrew Trickef9de2a2013-05-25 02:42:55 +00004494SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004495 SDValue Chain,
4496 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004497 MachineMemOperand *MMO,
4498 AtomicOrdering Ordering,
4499 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004500 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4501 Opcode == ISD::ATOMIC_LOAD_SUB ||
4502 Opcode == ISD::ATOMIC_LOAD_AND ||
4503 Opcode == ISD::ATOMIC_LOAD_OR ||
4504 Opcode == ISD::ATOMIC_LOAD_XOR ||
4505 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004506 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004507 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004508 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004509 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004510 Opcode == ISD::ATOMIC_SWAP ||
4511 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004512 "Invalid Atomic Op");
4513
Owen Anderson53aa7a92009-08-10 22:56:29 +00004514 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004515
Eli Friedman342e8df2011-08-24 20:50:09 +00004516 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4517 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004518 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004519 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004520}
4521
Andrew Trickef9de2a2013-05-25 02:42:55 +00004522SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004523 EVT VT, SDValue Chain,
4524 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004525 MachineMemOperand *MMO,
4526 AtomicOrdering Ordering,
4527 SynchronizationScope SynchScope) {
4528 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4529
4530 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004531 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004532 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004533}
4534
Duncan Sands739a0542008-07-02 17:40:58 +00004535/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004536SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4537 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004538 return Ops[0];
4539
Owen Anderson53aa7a92009-08-10 22:56:29 +00004540 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004541 VTs.reserve(Ops.size());
4542 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004543 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004544 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004545}
4546
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004547SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004548SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004549 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004550 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004551 unsigned Align, bool Vol,
4552 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004553 if (Align == 0) // Ensure that codegen never sees alignment 0
4554 Align = getEVTAlignment(MemVT);
4555
4556 MachineFunction &MF = getMachineFunction();
4557 unsigned Flags = 0;
4558 if (WriteMem)
4559 Flags |= MachineMemOperand::MOStore;
4560 if (ReadMem)
4561 Flags |= MachineMemOperand::MOLoad;
4562 if (Vol)
4563 Flags |= MachineMemOperand::MOVolatile;
4564 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004565 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004566
Craig Topper206fcd42014-04-26 19:29:41 +00004567 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004568}
4569
4570SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004571SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004572 ArrayRef<SDValue> Ops, EVT MemVT,
4573 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004574 assert((Opcode == ISD::INTRINSIC_VOID ||
4575 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004576 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004577 Opcode == ISD::LIFETIME_START ||
4578 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004579 (Opcode <= INT_MAX &&
4580 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4581 "Opcode is not a memory-accessing opcode!");
4582
Dale Johannesen839acbb2009-01-29 00:47:48 +00004583 // Memoize the node unless it returns a flag.
4584 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004585 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004586 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004587 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004588 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004589 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004590 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4591 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004592 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004593 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004594
Jack Carter170a5f22013-09-09 22:02:08 +00004595 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4596 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004597 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004598 CSEMap.InsertNode(N, IP);
4599 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004600 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4601 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004602 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004603 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004604 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004605 return SDValue(N, 0);
4606}
4607
Chris Lattnerea952f02010-09-21 17:24:05 +00004608/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4609/// MachinePointerInfo record from it. This is particularly useful because the
4610/// code generator has many cases where it doesn't bother passing in a
4611/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4612static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4613 // If this is FI+Offset, we can model it.
4614 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4615 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4616
4617 // If this is (FI+Offset1)+Offset2, we can model it.
4618 if (Ptr.getOpcode() != ISD::ADD ||
4619 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4620 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4621 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004622
Chris Lattnerea952f02010-09-21 17:24:05 +00004623 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4624 return MachinePointerInfo::getFixedStack(FI, Offset+
4625 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4626}
4627
4628/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4629/// MachinePointerInfo record from it. This is particularly useful because the
4630/// code generator has many cases where it doesn't bother passing in a
4631/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4632static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4633 // If the 'Offset' value isn't a constant, we can't handle this.
4634 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4635 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4636 if (OffsetOp.getOpcode() == ISD::UNDEF)
4637 return InferPointerInfo(Ptr);
4638 return MachinePointerInfo();
4639}
Wesley Peck527da1b2010-11-23 03:31:01 +00004640
Chris Lattnerea952f02010-09-21 17:24:05 +00004641
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004642SDValue
4643SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004644 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004645 SDValue Ptr, SDValue Offset,
4646 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004647 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004648 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004649 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004650 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004651 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004652 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4653 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004654
Dan Gohman48b185d2009-09-25 20:36:54 +00004655 unsigned Flags = MachineMemOperand::MOLoad;
4656 if (isVolatile)
4657 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004658 if (isNonTemporal)
4659 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004660 if (isInvariant)
4661 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004662
Chris Lattnerea952f02010-09-21 17:24:05 +00004663 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4664 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004665 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004666 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004667
Chris Lattnerea952f02010-09-21 17:24:05 +00004668 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004669 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004670 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004671 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004672 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004673}
4674
4675SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004676SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004677 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004678 SDValue Ptr, SDValue Offset, EVT MemVT,
4679 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004680 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004681 ExtType = ISD::NON_EXTLOAD;
4682 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004683 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004684 } else {
4685 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004686 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4687 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004688 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004689 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004690 assert(VT.isVector() == MemVT.isVector() &&
4691 "Cannot use trunc store to convert to or from a vector!");
4692 assert((!VT.isVector() ||
4693 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4694 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004695 }
4696
4697 bool Indexed = AM != ISD::UNINDEXED;
4698 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4699 "Unindexed load with an offset!");
4700
4701 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004702 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004703 SDValue Ops[] = { Chain, Ptr, Offset };
4704 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004705 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004706 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004707 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004708 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004709 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004710 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004711 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004712 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4713 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004714 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004715 }
Jack Carter170a5f22013-09-09 22:02:08 +00004716 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4717 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004718 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004719 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004720 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004721 return SDValue(N, 0);
4722}
4723
Andrew Trickef9de2a2013-05-25 02:42:55 +00004724SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004725 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004726 MachinePointerInfo PtrInfo,
4727 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004728 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004729 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004730 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004731 SDValue Undef = getUNDEF(Ptr.getValueType());
4732 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004733 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004734 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004735}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004736
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004737SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4738 SDValue Chain, SDValue Ptr,
4739 MachineMemOperand *MMO) {
4740 SDValue Undef = getUNDEF(Ptr.getValueType());
4741 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4742 VT, MMO);
4743}
4744
Andrew Trickef9de2a2013-05-25 02:42:55 +00004745SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004746 SDValue Chain, SDValue Ptr,
4747 MachinePointerInfo PtrInfo, EVT MemVT,
4748 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004749 bool isInvariant, unsigned Alignment,
4750 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004751 SDValue Undef = getUNDEF(Ptr.getValueType());
4752 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004753 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4754 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004755}
4756
4757
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004758SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4759 SDValue Chain, SDValue Ptr, EVT MemVT,
4760 MachineMemOperand *MMO) {
4761 SDValue Undef = getUNDEF(Ptr.getValueType());
4762 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4763 MemVT, MMO);
4764}
4765
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004766SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004767SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004768 SDValue Offset, ISD::MemIndexedMode AM) {
4769 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4770 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4771 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004772 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004773 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004774 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004775 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004776}
4777
Andrew Trickef9de2a2013-05-25 02:42:55 +00004778SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004779 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004780 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004781 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004782 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004783 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004784 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004785 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004786
Dan Gohman48b185d2009-09-25 20:36:54 +00004787 unsigned Flags = MachineMemOperand::MOStore;
4788 if (isVolatile)
4789 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004790 if (isNonTemporal)
4791 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004792
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004793 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004794 PtrInfo = InferPointerInfo(Ptr);
4795
4796 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004797 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004798 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004799 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004800 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004801
4802 return getStore(Chain, dl, Val, Ptr, MMO);
4803}
4804
Andrew Trickef9de2a2013-05-25 02:42:55 +00004805SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004806 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004807 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004808 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004809 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004810 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004811 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004812 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4813 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004814 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004815 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004816 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004817 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004818 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004819 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004820 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4821 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004822 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004823 }
Jack Carter170a5f22013-09-09 22:02:08 +00004824 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4825 dl.getDebugLoc(), VTs,
4826 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004827 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004828 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004829 return SDValue(N, 0);
4830}
4831
Andrew Trickef9de2a2013-05-25 02:42:55 +00004832SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004833 SDValue Ptr, MachinePointerInfo PtrInfo,
4834 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004835 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004836 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004837 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004838 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004839 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4840 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004841
Dan Gohman48b185d2009-09-25 20:36:54 +00004842 unsigned Flags = MachineMemOperand::MOStore;
4843 if (isVolatile)
4844 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004845 if (isNonTemporal)
4846 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004847
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004848 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004849 PtrInfo = InferPointerInfo(Ptr);
4850
4851 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004852 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004853 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004854 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004855
4856 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4857}
4858
Andrew Trickef9de2a2013-05-25 02:42:55 +00004859SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004860 SDValue Ptr, EVT SVT,
4861 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004862 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004863
Michael Ilsemand5f91512012-09-10 16:56:31 +00004864 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004865 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004866 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004867 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004868
Dan Gohmancecad352009-12-14 23:40:38 +00004869 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4870 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004871 assert(VT.isInteger() == SVT.isInteger() &&
4872 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004873 assert(VT.isVector() == SVT.isVector() &&
4874 "Cannot use trunc store to convert to or from a vector!");
4875 assert((!VT.isVector() ||
4876 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4877 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004878
Owen Anderson9f944592009-08-11 20:47:22 +00004879 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004880 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004881 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4882 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004883 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004884 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004885 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004886 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004887 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004888 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004889 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4890 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004891 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004892 }
Jack Carter170a5f22013-09-09 22:02:08 +00004893 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4894 dl.getDebugLoc(), VTs,
4895 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004896 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004897 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004898 return SDValue(N, 0);
4899}
4900
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004901SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004902SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004903 SDValue Offset, ISD::MemIndexedMode AM) {
4904 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4905 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4906 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004907 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004908 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4909 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004910 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004911 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004912 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004913 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004914 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004915 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004916 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004917
Jack Carter170a5f22013-09-09 22:02:08 +00004918 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4919 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004920 ST->isTruncatingStore(),
4921 ST->getMemoryVT(),
4922 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004923 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004924 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004925 return SDValue(N, 0);
4926}
4927
Andrew Trickef9de2a2013-05-25 02:42:55 +00004928SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004929 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004930 SDValue SV,
4931 unsigned Align) {
4932 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00004933 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004934}
4935
Andrew Trickef9de2a2013-05-25 02:42:55 +00004936SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00004937 ArrayRef<SDUse> Ops) {
4938 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004939 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00004940 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004941 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4942 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004943 default: break;
4944 }
4945
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004946 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004947 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00004948 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00004949 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004950}
4951
Andrew Trickef9de2a2013-05-25 02:42:55 +00004952SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00004953 ArrayRef<SDValue> Ops) {
4954 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00004955 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004956 case 0: return getNode(Opcode, DL, VT);
4957 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4958 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4959 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004960 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004961 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004962
Chris Lattnerb0713c72005-04-09 03:27:28 +00004963 switch (Opcode) {
4964 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004965 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004966 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004967 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4968 "LHS and RHS of condition must have same type!");
4969 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4970 "True and False arms of SelectCC must have same type!");
4971 assert(Ops[2].getValueType() == VT &&
4972 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004973 break;
4974 }
4975 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004976 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004977 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4978 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004979 break;
4980 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004981 }
4982
Chris Lattner566307f2005-05-14 07:42:29 +00004983 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004984 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004985 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004986
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004987 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004988 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004989 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004990 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004991
Bill Wendling022d18f2009-12-18 23:32:53 +00004992 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004993 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004994
Jack Carter170a5f22013-09-09 22:02:08 +00004995 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004996 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004997 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004998 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004999 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005000 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005001 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005002
Chandler Carruth41b20e72014-07-22 04:07:55 +00005003 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005004 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005005}
5006
Andrew Trickef9de2a2013-05-25 02:42:55 +00005007SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005008 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5009 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005010}
5011
Andrew Trickef9de2a2013-05-25 02:42:55 +00005012SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005013 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005014 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005015 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005016
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005017#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005018 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005019 // FIXME: figure out how to safely handle things like
5020 // int foo(int x) { return 1 << (x & 255); }
5021 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005022 case ISD::SRA_PARTS:
5023 case ISD::SRL_PARTS:
5024 case ISD::SHL_PARTS:
5025 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005026 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005027 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005028 else if (N3.getOpcode() == ISD::AND)
5029 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5030 // If the and is only masking out bits that cannot effect the shift,
5031 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005032 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005033 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005034 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005035 }
5036 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005037 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005038#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005039
Chris Lattnerf9c19152005-08-25 19:12:10 +00005040 // Memoize the node unless it returns a flag.
5041 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005042 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005043 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005044 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005045 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005046 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005047 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005048 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005049
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005050 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005051 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5052 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005053 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005054 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5055 DL.getDebugLoc(), VTList, Ops[0],
5056 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005057 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005058 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5059 DL.getDebugLoc(), VTList, Ops[0],
5060 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005061 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005062 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005063 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005064 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005065 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005066 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005067 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005068 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5069 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005070 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005071 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5072 DL.getDebugLoc(), VTList, Ops[0],
5073 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005074 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005075 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5076 DL.getDebugLoc(), VTList, Ops[0],
5077 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005078 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005079 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005080 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005081 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005082 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005083 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005084 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005085}
5086
Andrew Trickef9de2a2013-05-25 02:42:55 +00005087SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Topper48d114b2014-04-26 18:35:24 +00005088 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005089}
5090
Andrew Trickef9de2a2013-05-25 02:42:55 +00005091SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005092 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005093 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005094 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005095}
5096
Andrew Trickef9de2a2013-05-25 02:42:55 +00005097SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005098 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005099 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005100 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005101}
5102
Andrew Trickef9de2a2013-05-25 02:42:55 +00005103SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005104 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005105 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005106 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005107}
5108
Andrew Trickef9de2a2013-05-25 02:42:55 +00005109SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005110 SDValue N1, SDValue N2, SDValue N3,
5111 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005112 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005113 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005114}
5115
Andrew Trickef9de2a2013-05-25 02:42:55 +00005116SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005117 SDValue N1, SDValue N2, SDValue N3,
5118 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005119 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005120 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005121}
5122
Owen Anderson53aa7a92009-08-10 22:56:29 +00005123SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005124 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005125}
5126
Owen Anderson53aa7a92009-08-10 22:56:29 +00005127SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005128 FoldingSetNodeID ID;
5129 ID.AddInteger(2U);
5130 ID.AddInteger(VT1.getRawBits());
5131 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005132
Craig Topperc0196b12014-04-14 00:51:57 +00005133 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005134 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005135 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005136 EVT *Array = Allocator.Allocate<EVT>(2);
5137 Array[0] = VT1;
5138 Array[1] = VT2;
5139 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5140 VTListMap.InsertNode(Result, IP);
5141 }
5142 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005143}
Dan Gohman17059682008-07-17 19:10:17 +00005144
Owen Anderson53aa7a92009-08-10 22:56:29 +00005145SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005146 FoldingSetNodeID ID;
5147 ID.AddInteger(3U);
5148 ID.AddInteger(VT1.getRawBits());
5149 ID.AddInteger(VT2.getRawBits());
5150 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005151
Craig Topperc0196b12014-04-14 00:51:57 +00005152 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005153 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005154 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005155 EVT *Array = Allocator.Allocate<EVT>(3);
5156 Array[0] = VT1;
5157 Array[1] = VT2;
5158 Array[2] = VT3;
5159 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5160 VTListMap.InsertNode(Result, IP);
5161 }
5162 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005163}
5164
Owen Anderson53aa7a92009-08-10 22:56:29 +00005165SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005166 FoldingSetNodeID ID;
5167 ID.AddInteger(4U);
5168 ID.AddInteger(VT1.getRawBits());
5169 ID.AddInteger(VT2.getRawBits());
5170 ID.AddInteger(VT3.getRawBits());
5171 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005172
Craig Topperc0196b12014-04-14 00:51:57 +00005173 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005174 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005175 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005176 EVT *Array = Allocator.Allocate<EVT>(4);
5177 Array[0] = VT1;
5178 Array[1] = VT2;
5179 Array[2] = VT3;
5180 Array[3] = VT4;
5181 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5182 VTListMap.InsertNode(Result, IP);
5183 }
5184 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005185}
5186
Craig Topperabb4ac72014-04-16 06:10:51 +00005187SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5188 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005189 FoldingSetNodeID ID;
5190 ID.AddInteger(NumVTs);
5191 for (unsigned index = 0; index < NumVTs; index++) {
5192 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005193 }
5194
Craig Topperc0196b12014-04-14 00:51:57 +00005195 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005196 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005197 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005198 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005199 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005200 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5201 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005202 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005203 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005204}
5205
5206
Chris Lattnerf34156e2006-01-28 09:32:45 +00005207/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5208/// specified operands. If the resultant node already exists in the DAG,
5209/// this does not modify the specified node, instead it returns the node that
5210/// already exists. If the resultant node does not exist in the DAG, the
5211/// input node is returned. As a degenerate case, if you specify the same
5212/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005213SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005214 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005215
Chris Lattnerf34156e2006-01-28 09:32:45 +00005216 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005217 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005218
Chris Lattnerf34156e2006-01-28 09:32:45 +00005219 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005220 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005221 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005222 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005223
Dan Gohmanebeccb42008-07-21 22:38:59 +00005224 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005225 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005226 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005227 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005228
Chris Lattnerf34156e2006-01-28 09:32:45 +00005229 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005230 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005231
Chris Lattnerf34156e2006-01-28 09:32:45 +00005232 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005233 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005234 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005235}
5236
Dan Gohman92c11ac2010-06-18 15:30:29 +00005237SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005238 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005239
Chris Lattnerf34156e2006-01-28 09:32:45 +00005240 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005241 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005242 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005243
Chris Lattnerf34156e2006-01-28 09:32:45 +00005244 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005245 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005246 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005247 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005248
Dan Gohmanebeccb42008-07-21 22:38:59 +00005249 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005250 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005251 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005252 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005253
Chris Lattnerf34156e2006-01-28 09:32:45 +00005254 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005255 if (N->OperandList[0] != Op1)
5256 N->OperandList[0].set(Op1);
5257 if (N->OperandList[1] != Op2)
5258 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005259
Chris Lattnerf34156e2006-01-28 09:32:45 +00005260 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005261 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005262 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005263}
5264
Dan Gohman92c11ac2010-06-18 15:30:29 +00005265SDNode *SelectionDAG::
5266UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005267 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005268 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005269}
5270
Dan Gohman92c11ac2010-06-18 15:30:29 +00005271SDNode *SelectionDAG::
5272UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005273 SDValue Op3, SDValue Op4) {
5274 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005275 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005276}
5277
Dan Gohman92c11ac2010-06-18 15:30:29 +00005278SDNode *SelectionDAG::
5279UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005280 SDValue Op3, SDValue Op4, SDValue Op5) {
5281 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005282 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005283}
5284
Dan Gohman92c11ac2010-06-18 15:30:29 +00005285SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005286UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5287 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005288 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005289 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005290
Chris Lattnerf34156e2006-01-28 09:32:45 +00005291 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005292 bool AnyChange = false;
5293 for (unsigned i = 0; i != NumOps; ++i) {
5294 if (Ops[i] != N->getOperand(i)) {
5295 AnyChange = true;
5296 break;
5297 }
5298 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005299
Chris Lattnerf34156e2006-01-28 09:32:45 +00005300 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005301 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005302
Chris Lattnerf34156e2006-01-28 09:32:45 +00005303 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005304 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005305 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005306 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005307
Dan Gohman2f83b472008-05-02 00:05:03 +00005308 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005309 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005310 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005311 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005312
Chris Lattnerf34156e2006-01-28 09:32:45 +00005313 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005314 for (unsigned i = 0; i != NumOps; ++i)
5315 if (N->OperandList[i] != Ops[i])
5316 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005317
5318 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005319 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005320 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005321}
5322
Dan Gohman91697632008-07-07 20:57:48 +00005323/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005324/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005325void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005326 // Unlike the code in MorphNodeTo that does this, we don't need to
5327 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005328 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5329 SDUse &Use = *I++;
5330 Use.set(SDValue());
5331 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005332}
Chris Lattner19732782005-08-16 18:17:10 +00005333
Dan Gohman17059682008-07-17 19:10:17 +00005334/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5335/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005336///
Dan Gohman17059682008-07-17 19:10:17 +00005337SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005338 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005339 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005340 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005341}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005342
Dan Gohman17059682008-07-17 19:10:17 +00005343SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005344 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005345 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005346 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005347 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005348}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005349
Dan Gohman17059682008-07-17 19:10:17 +00005350SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005351 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005352 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005353 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005354 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005355 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005356}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005357
Dan Gohman17059682008-07-17 19:10:17 +00005358SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005359 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005360 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005361 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005362 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005363 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005364}
Chris Lattner466fece2005-08-21 22:30:30 +00005365
Dan Gohman17059682008-07-17 19:10:17 +00005366SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005367 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005368 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005369 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005370}
5371
Dan Gohman17059682008-07-17 19:10:17 +00005372SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005373 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005374 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005375 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005376}
5377
Dan Gohman17059682008-07-17 19:10:17 +00005378SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005379 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005380 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005381 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005382}
5383
Dan Gohman17059682008-07-17 19:10:17 +00005384SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005385 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005386 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005387 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005388 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005389}
5390
Bill Wendling2d598632008-12-01 23:28:22 +00005391SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005392 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005393 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005394 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005395 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005396}
5397
Scott Michelcf0da6c2009-02-17 22:15:04 +00005398SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005399 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005400 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005401 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005402 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005403 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005404}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005405
Scott Michelcf0da6c2009-02-17 22:15:04 +00005406SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005407 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005408 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005409 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005410 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005411 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005412}
5413
Dan Gohman17059682008-07-17 19:10:17 +00005414SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005415 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005416 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005417 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005418 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005419 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005420 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005421}
5422
Dan Gohman17059682008-07-17 19:10:17 +00005423SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005424 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005425 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005426 SDValue Op3) {
5427 SDVTList VTs = getVTList(VT1, VT2, VT3);
5428 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005429 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005430}
5431
5432SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005433 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005434 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005435 // Reset the NodeID to -1.
5436 N->setNodeId(-1);
5437 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005438}
5439
Andrew Trickef9de2a2013-05-25 02:42:55 +00005440/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005441/// the line number information on the merged node since it is not possible to
5442/// preserve the information that operation is associated with multiple lines.
5443/// This will make the debugger working better at -O0, were there is a higher
5444/// probability having other instructions associated with that line.
5445///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005446/// For IROrder, we keep the smaller of the two
5447SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005448 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005449 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5450 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005451 N->setDebugLoc(DebugLoc());
5452 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005453 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5454 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005455 return N;
5456}
5457
Chris Lattneraf197502010-02-28 21:36:14 +00005458/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005459/// return type, opcode, and operands.
5460///
5461/// Note that MorphNodeTo returns the resultant node. If there is already a
5462/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005463/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005464///
5465/// Using MorphNodeTo is faster than creating a new node and swapping it in
5466/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005467/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005468/// the node's users.
5469///
5470SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005471 SDVTList VTs, ArrayRef<SDValue> Ops) {
5472 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005473 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005474 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005475 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005476 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005477 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005478 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005479 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005480 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005481
Dan Gohmand3fe1742008-09-13 01:54:27 +00005482 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005483 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005484
Dan Gohman17059682008-07-17 19:10:17 +00005485 // Start the morphing.
5486 N->NodeType = Opc;
5487 N->ValueList = VTs.VTs;
5488 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005489
Dan Gohman17059682008-07-17 19:10:17 +00005490 // Clear the operands list, updating used nodes to remove this from their
5491 // use list. Keep track of any operands that become dead as a result.
5492 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005493 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5494 SDUse &Use = *I++;
5495 SDNode *Used = Use.getNode();
5496 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005497 if (Used->use_empty())
5498 DeadNodeSet.insert(Used);
5499 }
5500
Dan Gohman48b185d2009-09-25 20:36:54 +00005501 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5502 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005503 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005504 // If NumOps is larger than the # of operands we can have in a
5505 // MachineSDNode, reallocate the operand list.
5506 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5507 if (MN->OperandsNeedDelete)
5508 delete[] MN->OperandList;
5509 if (NumOps > array_lengthof(MN->LocalOperands))
5510 // We're creating a final node that will live unmorphed for the
5511 // remainder of the current SelectionDAG iteration, so we can allocate
5512 // the operands directly out of a pool with no recycling metadata.
5513 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005514 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005515 else
Craig Topper131de822014-04-27 19:21:16 +00005516 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005517 MN->OperandsNeedDelete = false;
5518 } else
Craig Topper131de822014-04-27 19:21:16 +00005519 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005520 } else {
5521 // If NumOps is larger than the # of operands we currently have, reallocate
5522 // the operand list.
5523 if (NumOps > N->NumOperands) {
5524 if (N->OperandsNeedDelete)
5525 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005526 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005527 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005528 } else
Craig Topper131de822014-04-27 19:21:16 +00005529 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005530 }
5531
5532 // Delete any nodes that are still dead after adding the uses for the
5533 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005534 if (!DeadNodeSet.empty()) {
5535 SmallVector<SDNode *, 16> DeadNodes;
5536 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5537 E = DeadNodeSet.end(); I != E; ++I)
5538 if ((*I)->use_empty())
5539 DeadNodes.push_back(*I);
5540 RemoveDeadNodes(DeadNodes);
5541 }
Dan Gohman91697632008-07-07 20:57:48 +00005542
Dan Gohman17059682008-07-17 19:10:17 +00005543 if (IP)
5544 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005545 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005546}
5547
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005548
Dan Gohman32f71d72009-09-25 18:54:59 +00005549/// getMachineNode - These are used for target selectors to create a new node
5550/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005551///
Dan Gohman32f71d72009-09-25 18:54:59 +00005552/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005553/// node of the specified opcode and operands, it returns that node instead of
5554/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005555MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005556SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005557 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005558 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005559}
Bill Wendlinga434d932009-01-29 09:01:55 +00005560
Dan Gohmana22f2d82009-10-10 01:29:16 +00005561MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005562SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005563 SDVTList VTs = getVTList(VT);
5564 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005565 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005566}
Bill Wendlinga434d932009-01-29 09:01:55 +00005567
Dan Gohmana22f2d82009-10-10 01:29:16 +00005568MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005569SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005570 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005571 SDVTList VTs = getVTList(VT);
5572 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005573 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005574}
Bill Wendlinga434d932009-01-29 09:01:55 +00005575
Dan Gohmana22f2d82009-10-10 01:29:16 +00005576MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005577SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005578 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005579 SDVTList VTs = getVTList(VT);
5580 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005581 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005582}
Bill Wendlinga434d932009-01-29 09:01:55 +00005583
Dan Gohmana22f2d82009-10-10 01:29:16 +00005584MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005585SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005586 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005587 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005588 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005589}
Bill Wendlinga434d932009-01-29 09:01:55 +00005590
Dan Gohmana22f2d82009-10-10 01:29:16 +00005591MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005592SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005593 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005594 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005595}
Bill Wendlinga434d932009-01-29 09:01:55 +00005596
Dan Gohmana22f2d82009-10-10 01:29:16 +00005597MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005598SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005599 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005600 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005601 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005602 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005603}
Bill Wendlinga434d932009-01-29 09:01:55 +00005604
Dan Gohmana22f2d82009-10-10 01:29:16 +00005605MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005606SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005607 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005608 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005609 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005610 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005611}
5612
Dan Gohmana22f2d82009-10-10 01:29:16 +00005613MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005614SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005615 EVT VT1, EVT VT2, SDValue Op1,
5616 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005617 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005618 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005619 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005620}
5621
Dan Gohmana22f2d82009-10-10 01:29:16 +00005622MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005623SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005624 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005625 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005626 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005627 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005628}
Bill Wendlinga434d932009-01-29 09:01:55 +00005629
Dan Gohmana22f2d82009-10-10 01:29:16 +00005630MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005631SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005632 EVT VT1, EVT VT2, EVT VT3,
5633 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005634 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005635 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005636 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005637}
Bill Wendlinga434d932009-01-29 09:01:55 +00005638
Dan Gohmana22f2d82009-10-10 01:29:16 +00005639MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005640SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005641 EVT VT1, EVT VT2, EVT VT3,
5642 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005643 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005644 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005645 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005646}
Bill Wendlinga434d932009-01-29 09:01:55 +00005647
Dan Gohmana22f2d82009-10-10 01:29:16 +00005648MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005649SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005650 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005651 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005652 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005653 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005654}
Bill Wendlinga434d932009-01-29 09:01:55 +00005655
Dan Gohmana22f2d82009-10-10 01:29:16 +00005656MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005657SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005658 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005659 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005660 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005661 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005662}
5663
Dan Gohmana22f2d82009-10-10 01:29:16 +00005664MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005665SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005666 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005667 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005668 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005669 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005670}
5671
Dan Gohmana22f2d82009-10-10 01:29:16 +00005672MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005673SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005674 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005675 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005676 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005677 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005678 const SDValue *Ops = OpsArray.data();
5679 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005680
5681 if (DoCSE) {
5682 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005683 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005684 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005685 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005686 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005687 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005688 }
5689
5690 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005691 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5692 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005693
5694 // Initialize the operands list.
5695 if (NumOps > array_lengthof(N->LocalOperands))
5696 // We're creating a final node that will live unmorphed for the
5697 // remainder of the current SelectionDAG iteration, so we can allocate
5698 // the operands directly out of a pool with no recycling metadata.
5699 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5700 Ops, NumOps);
5701 else
5702 N->InitOperands(N->LocalOperands, Ops, NumOps);
5703 N->OperandsNeedDelete = false;
5704
5705 if (DoCSE)
5706 CSEMap.InsertNode(N, IP);
5707
Chandler Carruth41b20e72014-07-22 04:07:55 +00005708 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005709 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005710}
Evan Chengd3f1db92006-02-09 07:15:23 +00005711
Dan Gohmanac33a902009-08-19 18:16:17 +00005712/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005713/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005714SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005715SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005716 SDValue Operand) {
5717 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005718 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005719 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005720 return SDValue(Subreg, 0);
5721}
5722
Bob Wilson2a45a652009-10-08 18:49:46 +00005723/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005724/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005725SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005726SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005727 SDValue Operand, SDValue Subreg) {
5728 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005729 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005730 VT, Operand, Subreg, SRIdxVal);
5731 return SDValue(Result, 0);
5732}
5733
Evan Cheng31604a62008-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,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005737 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5738 bool exact) {
5739 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005740 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005741 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005742 if (isBinOpWithFlags(Opcode))
5743 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005744 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005745 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005746 return E;
5747 }
Craig Topperc0196b12014-04-14 00:51:57 +00005748 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005749}
5750
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005751/// getDbgValue - Creates a SDDbgValue node.
5752///
Adrian Prantl32da8892014-04-25 20:49:25 +00005753/// SDNode
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005754SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005755SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
5756 bool IsIndirect, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005757 DebugLoc DL, unsigned O) {
Adrian Prantl32da8892014-04-25 20:49:25 +00005758 return new (Allocator) SDDbgValue(MDPtr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005759}
5760
Adrian Prantl32da8892014-04-25 20:49:25 +00005761/// Constant
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005762SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005763SelectionDAG::getConstantDbgValue(MDNode *MDPtr, const Value *C,
5764 uint64_t Off,
5765 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005766 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5767}
5768
Adrian Prantl32da8892014-04-25 20:49:25 +00005769/// FrameIndex
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005770SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005771SelectionDAG::getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5772 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005773 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5774}
5775
Dan Gohman7d099f72010-03-03 21:33:37 +00005776namespace {
5777
Dan Gohman9cc886b2010-03-04 19:11:28 +00005778/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-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 Gohman7d099f72010-03-03 21:33:37 +00005782class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005783 SDNode::use_iterator &UI;
5784 SDNode::use_iterator &UE;
5785
Craig Topper7b883b32014-03-08 06:31:39 +00005786 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005787 // Increment the iterator as needed.
5788 while (UI != UE && N == *UI)
5789 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005790 }
5791
5792public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005793 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005794 SDNode::use_iterator &ui,
5795 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005796 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005797};
5798
5799}
5800
Evan Cheng445b91a2006-08-07 22:13:29 +00005801/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005802/// This can cause recursive merging of nodes in the DAG.
5803///
Chris Lattner76858912008-02-03 03:35:22 +00005804/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005805///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005806void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005807 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005808 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005809 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005810 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005811
Dan Gohmanf1d38be2009-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 Gohmancd0b1bf2009-01-19 21:44:21 +00005819 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005820 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005821 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005822 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005823
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005824 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005825 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005826
Dan Gohmanf1d38be2009-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 Gohman8e4ac9b2009-01-26 04:35:06 +00005832 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005833 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005834 Use.set(To);
Dan Gohmanf1d38be2009-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 Olesenbeb94692012-04-20 22:08:46 +00005839 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005840 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005841
5842 // If we just RAUW'd the root, take note.
5843 if (FromN == getRoot())
5844 setRoot(To);
Chris Lattner373f0482005-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 Gohman8aa28b92009-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 Lattner373f0482005-08-26 18:36:28 +00005852///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005853void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-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 Gohman17059682008-07-17 19:10:17 +00005860
5861 // Handle the trivial case.
5862 if (From == To)
5863 return;
5864
Dan Gohmancd0b1bf2009-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 Olesenbeb94692012-04-20 22:08:46 +00005868 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005869 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005870 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005871
Chris Lattner373f0482005-08-26 18:36:28 +00005872 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005873 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005874
Dan Gohmanf1d38be2009-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 Gohman8e4ac9b2009-01-26 04:35:06 +00005880 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005881 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005882 Use.setNode(To);
Dan Gohmanf1d38be2009-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 Olesenbeb94692012-04-20 22:08:46 +00005887 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005888 }
Dan Gohman198b7ff2011-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 Lattnerab0de9d2005-08-17 19:00:20 +00005893}
5894
Chris Lattner373f0482005-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 Olesenbeb94692012-04-20 22:08:46 +00005900void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005901 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005902 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005903
Dan Gohmancd0b1bf2009-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 Olesenbeb94692012-04-20 22:08:46 +00005907 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005908 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005909 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005910
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005911 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005912 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005913
Dan Gohmanf1d38be2009-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 Gohman8e4ac9b2009-01-26 04:35:06 +00005919 SDUse &Use = UI.getUse();
5920 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005921 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005922 Use.set(ToOp);
Dan Gohmanf1d38be2009-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 Olesenbeb94692012-04-20 22:08:46 +00005927 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005928 }
Dan Gohman198b7ff2011-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 Lattnerd7ee4d82005-08-24 22:44:39 +00005933}
5934
Chris Lattner375e1a72006-02-17 21:58:01 +00005935/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-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 Olesenbeb94692012-04-20 22:08:46 +00005938void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005939 // Handle the really simple, really trivial case efficiently.
5940 if (From == To) return;
5941
Chris Lattner375e1a72006-02-17 21:58:01 +00005942 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005943 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005944 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005945 return;
5946 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005947
Dan Gohmanf1d38be2009-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 Olesenbeb94692012-04-20 22:08:46 +00005952 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005953 while (UI != UE) {
5954 SDNode *User = *UI;
5955 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005956
Dan Gohmanf1d38be2009-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 Gohman8e4ac9b2009-01-26 04:35:06 +00005962 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005963
5964 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005965 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005966 ++UI;
5967 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005968 }
Dan Gohmanf1d38be2009-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 Gohman8e4ac9b2009-01-26 04:35:06 +00005978 Use.set(To);
Dan Gohmanf1d38be2009-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 Lattner3cfb56d2007-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 Olesenbeb94692012-04-20 22:08:46 +00005988 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005989 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005990
5991 // If we just RAUW'd the root, take note.
5992 if (From == getRoot())
5993 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005994}
5995
Dan Gohmanf1d38be2009-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 Gohman8e4ac9b2009-01-26 04:35:06 +00006002 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006003 };
Dan Gohman8e4ac9b2009-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 Gohmanf1d38be2009-01-25 16:29:12 +00006009}
6010
Dan Gohman17059682008-07-17 19:10:17 +00006011/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-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 Gohman17059682008-07-17 19:10:17 +00006014/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006015void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6016 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006017 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006018 // Handle the simple, trivial case efficiently.
6019 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006020 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006021
Dan Gohmanf1d38be2009-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 Michelcf0da6c2009-02-17 22:15:04 +00006029 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-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 Gohmanf1d38be2009-01-25 16:29:12 +00006034 Uses.push_back(Memo);
6035 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006036 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006037 }
Dan Gohman17059682008-07-17 19:10:17 +00006038
Dan Gohman8e4ac9b2009-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 Gohmanf1d38be2009-01-25 16:29:12 +00006042 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6043 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-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 Gohmanf1d38be2009-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 Gohman17059682008-07-17 19:10:17 +00006049 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006050
Dan Gohman8e4ac9b2009-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 Gohmanf1d38be2009-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 Gohman8e4ac9b2009-01-26 04:35:06 +00006057 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006058 ++UseIndex;
6059
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006060 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006061 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6062
Dan Gohman17059682008-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 Olesenbeb94692012-04-20 22:08:46 +00006065 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006066 }
6067}
6068
Evan Cheng9631a602006-08-01 08:20:41 +00006069/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-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 Gohman86aa16a2008-09-30 18:30:35 +00006072unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006073
Dan Gohman86aa16a2008-09-30 18:30:35 +00006074 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006075
Dan Gohman86aa16a2008-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 Gohman8dfa51c2008-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 Gohman86aa16a2008-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++;
Adam Nemet7d394302014-05-31 16:23:17 +00006090 checkForCycles(N, this);
Dan Gohman86aa16a2008-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 Greene3b2a68c2010-01-20 00:59:23 +00006098 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-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 Cheng9631a602006-08-01 08:20:41 +00006103 }
6104 }
6105
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006106 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-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;
Adam Nemet7d394302014-05-31 16:23:17 +00006110 checkForCycles(N, this);
David Greene3b2a68c2010-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 Gohman86aa16a2008-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 Greene3b2a68c2010-01-20 00:59:23 +00006117 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-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 Greene3b2a68c2010-01-20 00:59:23 +00006124 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006125 ++SortedPos;
6126 } else {
6127 // Update P's outstanding operand count.
6128 P->setNodeId(Degree);
6129 }
6130 }
David Greene3b2a68c2010-01-20 00:59:23 +00006131 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006132#ifndef NDEBUG
6133 SDNode *S = ++I;
6134 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006135 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006136 dbgs() << "Checking if this is due to cycles\n";
6137 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006138#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006139 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006140 }
Dan Gohman86aa16a2008-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 Gohman8dfa51c2008-11-21 19:10:41 +00006155 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006156 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006157}
6158
Evan Cheng563fe3c2010-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 Johannesene0983522010-04-26 20:06:49 +00006161void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6162 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006163 if (SD)
6164 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006165}
Evan Cheng29eefc12006-07-27 06:39:06 +00006166
Devang Patelefc6b162011-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 Kramere1fc29b2011-06-18 13:13:44 +00006173 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006174 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006175 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006176 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006177 SDDbgValue *Dbg = *I;
6178 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6179 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
Adrian Prantl32da8892014-04-25 20:49:25 +00006180 Dbg->isIndirect(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006181 Dbg->getOffset(), Dbg->getDebugLoc(),
6182 Dbg->getOrder());
6183 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006184 }
6185 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006186 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006187 E = ClonedDVs.end(); I != E; ++I)
6188 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006189}
6190
Jim Laskeyd66e6162005-08-17 20:08:02 +00006191//===----------------------------------------------------------------------===//
6192// SDNode Class
6193//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006194
Chris Lattner3bf17b62007-02-04 02:41:42 +00006195HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006196 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006197}
6198
Andrew Trickef9de2a2013-05-25 02:42:55 +00006199GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6200 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006201 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006202 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006203 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006204}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006205
Matt Arsenaultb03bd4d2013-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 Trickef9de2a2013-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 Greeneb7941b02010-02-17 20:21:42 +00006215 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006216 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006217 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006218 assert(isNonTemporal() == MMO->isNonTemporal() &&
6219 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006220 // We check here that the size of the memory operand fits within the size of
6221 // the MMO. This is because the MMO might indicate only a possible address
6222 // range instead of specifying the affected memory addresses precisely.
6223 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006224}
6225
Andrew Trickef9de2a2013-05-25 02:42:55 +00006226MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006227 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6228 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006229 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006230 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006231 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006232 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006233 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006234}
6235
Jim Laskeyf576b422006-10-27 23:46:08 +00006236/// Profile - Gather unique data for the node.
6237///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006238void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006239 AddNodeIDNode(ID, this);
6240}
6241
Owen Anderson3b1665e2009-08-25 22:27:22 +00006242namespace {
6243 struct EVTArray {
6244 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006245
Owen Anderson3b1665e2009-08-25 22:27:22 +00006246 EVTArray() {
6247 VTs.reserve(MVT::LAST_VALUETYPE);
6248 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6249 VTs.push_back(MVT((MVT::SimpleValueType)i));
6250 }
6251 };
6252}
6253
Owen Anderson53aa7a92009-08-10 22:56:29 +00006254static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006255static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006256static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006257
Chris Lattner88fa11c2005-11-08 23:30:28 +00006258/// getValueTypeList - Return a pointer to the specified value type.
6259///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006260const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006261 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006262 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006263 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006264 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006265 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006266 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006267 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006268 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006269}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006270
Chris Lattner40e79822005-01-12 18:37:47 +00006271/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6272/// indicated value. This method ignores uses of other values defined by this
6273/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006274bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006275 assert(Value < getNumValues() && "Bad value!");
6276
Roman Levenstein51f532f2008-04-07 10:06:32 +00006277 // TODO: Only iterate over uses of a given value of the node
6278 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006279 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006280 if (NUses == 0)
6281 return false;
6282 --NUses;
6283 }
Chris Lattner40e79822005-01-12 18:37:47 +00006284 }
6285
6286 // Found exactly the right number of uses?
6287 return NUses == 0;
6288}
6289
6290
Evan Cheng358c3d12007-08-02 05:29:38 +00006291/// hasAnyUseOfValue - Return true if there are any use of the indicated
6292/// value. This method ignores uses of other values defined by this operation.
6293bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6294 assert(Value < getNumValues() && "Bad value!");
6295
Dan Gohman7a510c22008-07-09 22:39:01 +00006296 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006297 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006298 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006299
6300 return false;
6301}
6302
6303
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006304/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006305///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006306bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006307 bool Seen = false;
6308 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006309 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006310 if (User == this)
6311 Seen = true;
6312 else
6313 return false;
6314 }
6315
6316 return Seen;
6317}
6318
Evan Cheng9456dd82006-11-03 07:31:32 +00006319/// isOperand - Return true if this node is an operand of N.
6320///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006321bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006322 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6323 if (*this == N->getOperand(i))
6324 return true;
6325 return false;
6326}
6327
Evan Cheng567d2e52008-03-04 00:41:45 +00006328bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006329 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006330 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006331 return true;
6332 return false;
6333}
Evan Chengd37645c2006-02-05 06:29:23 +00006334
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006335/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006336/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006337/// side-effecting instructions on any chain path. In practice, this looks
6338/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006339/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006340bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006341 unsigned Depth) const {
6342 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006343
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006344 // Don't search too deeply, we just want to be able to see through
6345 // TokenFactor's etc.
6346 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006347
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006348 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006349 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006350 if (getOpcode() == ISD::TokenFactor) {
6351 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006352 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6353 return false;
6354 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006355 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006356
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006357 // Loads don't have side effects, look through them.
6358 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6359 if (!Ld->isVolatile())
6360 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6361 }
6362 return false;
6363}
6364
Lang Hames5a004992011-07-07 04:31:51 +00006365/// hasPredecessor - Return true if N is a predecessor of this node.
6366/// N is either an operand of this node, or can be reached by recursively
6367/// traversing up the operands.
6368/// NOTE: This is an expensive method. Use it carefully.
6369bool SDNode::hasPredecessor(const SDNode *N) const {
6370 SmallPtrSet<const SDNode *, 32> Visited;
6371 SmallVector<const SDNode *, 16> Worklist;
6372 return hasPredecessorHelper(N, Visited, Worklist);
6373}
Dan Gohmancd139c02009-10-28 03:44:30 +00006374
Craig Topperb94011f2013-07-14 04:42:23 +00006375bool
6376SDNode::hasPredecessorHelper(const SDNode *N,
6377 SmallPtrSet<const SDNode *, 32> &Visited,
6378 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006379 if (Visited.empty()) {
6380 Worklist.push_back(this);
6381 } else {
6382 // Take a look in the visited set. If we've already encountered this node
6383 // we needn't search further.
6384 if (Visited.count(N))
6385 return true;
6386 }
6387
6388 // Haven't visited N yet. Continue the search.
6389 while (!Worklist.empty()) {
6390 const SDNode *M = Worklist.pop_back_val();
6391 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6392 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006393 if (Visited.insert(Op))
6394 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006395 if (Op == N)
6396 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006397 }
Lang Hames5a004992011-07-07 04:31:51 +00006398 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006399
6400 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006401}
6402
Evan Cheng5d9fd972006-10-04 00:56:09 +00006403uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6404 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006405 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006406}
6407
Mon P Wang32f8bb92009-11-30 02:42:02 +00006408SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6409 assert(N->getNumValues() == 1 &&
6410 "Can't unroll a vector with multiple results!");
6411
6412 EVT VT = N->getValueType(0);
6413 unsigned NE = VT.getVectorNumElements();
6414 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006415 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006416
6417 SmallVector<SDValue, 8> Scalars;
6418 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6419
6420 // If ResNE is 0, fully unroll the vector op.
6421 if (ResNE == 0)
6422 ResNE = NE;
6423 else if (NE > ResNE)
6424 NE = ResNE;
6425
6426 unsigned i;
6427 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006428 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006429 SDValue Operand = N->getOperand(j);
6430 EVT OperandVT = Operand.getValueType();
6431 if (OperandVT.isVector()) {
6432 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006433 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006434 EVT OperandEltVT = OperandVT.getVectorElementType();
6435 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6436 OperandEltVT,
6437 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006438 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006439 } else {
6440 // A scalar operand; just use it as is.
6441 Operands[j] = Operand;
6442 }
6443 }
6444
6445 switch (N->getOpcode()) {
6446 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006447 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006448 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006449 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006450 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006451 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006452 case ISD::SHL:
6453 case ISD::SRA:
6454 case ISD::SRL:
6455 case ISD::ROTL:
6456 case ISD::ROTR:
6457 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006458 getShiftAmountOperand(Operands[0].getValueType(),
6459 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006460 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006461 case ISD::SIGN_EXTEND_INREG:
6462 case ISD::FP_ROUND_INREG: {
6463 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6464 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6465 Operands[0],
6466 getValueType(ExtVT)));
6467 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006468 }
6469 }
6470
6471 for (; i < ResNE; ++i)
6472 Scalars.push_back(getUNDEF(EltVT));
6473
6474 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006475 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006476}
6477
Evan Chengf5938d52009-12-09 01:36:00 +00006478
Wesley Peck527da1b2010-11-23 03:31:01 +00006479/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6480/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006481/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006482bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006483 unsigned Bytes, int Dist) const {
6484 if (LD->getChain() != Base->getChain())
6485 return false;
6486 EVT VT = LD->getValueType(0);
6487 if (VT.getSizeInBits() / 8 != Bytes)
6488 return false;
6489
6490 SDValue Loc = LD->getOperand(1);
6491 SDValue BaseLoc = Base->getOperand(1);
6492 if (Loc.getOpcode() == ISD::FrameIndex) {
6493 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6494 return false;
6495 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6496 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6497 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6498 int FS = MFI->getObjectSize(FI);
6499 int BFS = MFI->getObjectSize(BFI);
6500 if (FS != BFS || FS != (int)Bytes) return false;
6501 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6502 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006503
6504 // Handle X+C
6505 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6506 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6507 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006508
Craig Topperc0196b12014-04-14 00:51:57 +00006509 const GlobalValue *GV1 = nullptr;
6510 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006511 int64_t Offset1 = 0;
6512 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006513 const TargetLowering *TLI = TM.getTargetLowering();
6514 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6515 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006516 if (isGA1 && isGA2 && GV1 == GV2)
6517 return Offset1 == (Offset2 + Dist*Bytes);
6518 return false;
6519}
6520
6521
Evan Cheng34a23ea2009-12-09 01:04:59 +00006522/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6523/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006524unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006525 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006526 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006527 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006528 const TargetLowering *TLI = TM.getTargetLowering();
6529 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006530 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006531 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +00006532 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6533 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006534 unsigned AlignBits = KnownZero.countTrailingOnes();
6535 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6536 if (Align)
6537 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006538 }
Evan Chengd938faf2009-12-09 01:53:58 +00006539
Evan Cheng34a23ea2009-12-09 01:04:59 +00006540 // If this is a direct reference to a stack slot, use information about the
6541 // stack slot's alignment.
6542 int FrameIdx = 1 << 31;
6543 int64_t FrameOffset = 0;
6544 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6545 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006546 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006547 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006548 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006549 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6550 FrameOffset = Ptr.getConstantOperandVal(1);
6551 }
6552
6553 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006554 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006555 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6556 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006557 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006558 }
6559
6560 return 0;
6561}
6562
Juergen Ributzkab3487102013-11-19 21:20:17 +00006563/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6564/// which is split (or expanded) into two not necessarily identical pieces.
6565std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6566 // Currently all types are split in half.
6567 EVT LoVT, HiVT;
6568 if (!VT.isVector()) {
6569 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6570 } else {
6571 unsigned NumElements = VT.getVectorNumElements();
6572 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6573 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6574 NumElements/2);
6575 }
6576 return std::make_pair(LoVT, HiVT);
6577}
6578
6579/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6580/// low/high part.
6581std::pair<SDValue, SDValue>
6582SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6583 const EVT &HiVT) {
6584 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6585 N.getValueType().getVectorNumElements() &&
6586 "More vector elements requested than available!");
6587 SDValue Lo, Hi;
6588 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6589 getConstant(0, TLI->getVectorIdxTy()));
6590 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6591 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6592 return std::make_pair(Lo, Hi);
6593}
6594
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006595void SelectionDAG::ExtractVectorElements(SDValue Op,
6596 SmallVectorImpl<SDValue> &Args,
6597 unsigned Start, unsigned Count) {
6598 EVT VT = Op.getValueType();
6599 if (Count == 0)
6600 Count = VT.getVectorNumElements();
6601
6602 EVT EltVT = VT.getVectorElementType();
6603 EVT IdxTy = TLI->getVectorIdxTy();
6604 SDLoc SL(Op);
6605 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6606 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6607 Op, getConstant(i, IdxTy)));
6608 }
6609}
6610
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006611// getAddressSpace - Return the address space this GlobalAddress belongs to.
6612unsigned GlobalAddressSDNode::getAddressSpace() const {
6613 return getGlobal()->getType()->getAddressSpace();
6614}
6615
6616
Chris Lattner229907c2011-07-18 04:54:35 +00006617Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006618 if (isMachineConstantPoolEntry())
6619 return Val.MachineCPVal->getType();
6620 return Val.ConstVal->getType();
6621}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006622
Bob Wilson85cefe82009-03-02 23:24:16 +00006623bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6624 APInt &SplatUndef,
6625 unsigned &SplatBitSize,
6626 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006627 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006628 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006629 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006630 assert(VT.isVector() && "Expected a vector type");
6631 unsigned sz = VT.getSizeInBits();
6632 if (MinSplatBits > sz)
6633 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006634
Bob Wilson85cefe82009-03-02 23:24:16 +00006635 SplatValue = APInt(sz, 0);
6636 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006637
Bob Wilson85cefe82009-03-02 23:24:16 +00006638 // Get the bits. Bits with undefined values (when the corresponding element
6639 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6640 // in SplatValue. If any of the values are not constant, give up and return
6641 // false.
6642 unsigned int nOps = getNumOperands();
6643 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6644 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006645
6646 for (unsigned j = 0; j < nOps; ++j) {
6647 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006648 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006649 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006650
Bob Wilson85cefe82009-03-02 23:24:16 +00006651 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006652 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006653 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006654 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006655 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006656 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006657 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006658 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006659 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006660 }
6661
Bob Wilson85cefe82009-03-02 23:24:16 +00006662 // The build_vector is all constants or undefs. Find the smallest element
6663 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006664
Bob Wilson85cefe82009-03-02 23:24:16 +00006665 HasAnyUndefs = (SplatUndef != 0);
6666 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006667
Bob Wilson85cefe82009-03-02 23:24:16 +00006668 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006669 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6670 APInt LowValue = SplatValue.trunc(HalfSize);
6671 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6672 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006673
Bob Wilson85cefe82009-03-02 23:24:16 +00006674 // If the two halves do not match (ignoring undef bits), stop here.
6675 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6676 MinSplatBits > HalfSize)
6677 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006678
Bob Wilson85cefe82009-03-02 23:24:16 +00006679 SplatValue = HighValue | LowValue;
6680 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006681
Bob Wilson85cefe82009-03-02 23:24:16 +00006682 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006683 }
6684
Bob Wilson85cefe82009-03-02 23:24:16 +00006685 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006686 return true;
6687}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006688
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006689SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6690 if (UndefElements) {
6691 UndefElements->clear();
6692 UndefElements->resize(getNumOperands());
6693 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006694 SDValue Splatted;
6695 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6696 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006697 if (Op.getOpcode() == ISD::UNDEF) {
6698 if (UndefElements)
6699 (*UndefElements)[i] = true;
6700 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006701 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006702 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006703 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006704 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006705 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006706
Chandler Carruthb844e722014-07-08 07:19:55 +00006707 if (!Splatted) {
6708 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6709 "Can only have a splat without a constant for all undefs.");
6710 return getOperand(0);
6711 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006712
Chandler Carruthb844e722014-07-08 07:19:55 +00006713 return Splatted;
6714}
6715
6716ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006717BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006718 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006719 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006720}
6721
6722ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006723BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006724 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006725 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006726}
6727
Juergen Ributzka73844052014-01-13 20:51:35 +00006728bool BuildVectorSDNode::isConstant() const {
6729 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6730 unsigned Opc = getOperand(i).getOpcode();
6731 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6732 return false;
6733 }
6734 return true;
6735}
6736
Owen Anderson53aa7a92009-08-10 22:56:29 +00006737bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006738 // Find the first non-undef value in the shuffle mask.
6739 unsigned i, e;
6740 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6741 /* search */;
6742
Nate Begeman39b59db2009-04-29 18:13:31 +00006743 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006744
Nate Begeman5f829d82009-04-29 05:20:52 +00006745 // Make sure all remaining elements are either undef or the same as the first
6746 // non-undef value.
6747 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006748 if (Mask[i] >= 0 && Mask[i] != Idx)
6749 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006750 return true;
6751}
David Greene09851602010-01-20 20:13:31 +00006752
Adam Nemetb4690e32014-05-31 16:23:20 +00006753#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00006754static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006755 SmallPtrSet<const SDNode*, 32> &Visited,
Adam Nemet7d394302014-05-31 16:23:17 +00006756 SmallPtrSet<const SDNode*, 32> &Checked,
6757 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006758 // If this node has already been checked, don't check it again.
6759 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006760 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006761
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006762 // If a node has already been visited on this depth-first walk, reject it as
6763 // a cycle.
6764 if (!Visited.insert(N)) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006765 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006766 dbgs() << "Offending node:\n";
6767 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006768 abort();
David Greene09851602010-01-20 20:13:31 +00006769 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006770
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006771 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00006772 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00006773
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006774 Checked.insert(N);
6775 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006776}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006777#endif
David Greene09851602010-01-20 20:13:31 +00006778
Adam Nemet7d394302014-05-31 16:23:17 +00006779void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00006780 const llvm::SelectionDAG *DAG,
6781 bool force) {
6782#ifndef NDEBUG
6783 bool check = force;
David Greene09851602010-01-20 20:13:31 +00006784#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00006785 check = true;
6786#endif // XDEBUG
6787 if (check) {
6788 assert(N && "Checking nonexistent SDNode");
6789 SmallPtrSet<const SDNode*, 32> visited;
6790 SmallPtrSet<const SDNode*, 32> checked;
6791 checkForCyclesHelper(N, visited, checked, DAG);
6792 }
6793#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00006794}
6795
Adam Nemetb4690e32014-05-31 16:23:20 +00006796void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6797 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00006798}