blob: 2c584350a798bc02d47c5d447f446097212f32e9 [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>
Chris Lattner560b5e42004-06-02 04:28:06 +000051using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000052
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000053/// makeVTList - Return an instance of the SDVTList struct initialized with the
54/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000055static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000056 SDVTList Res = {VTs, NumVTs};
57 return Res;
58}
59
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000060// Default null implementations of the callbacks.
61void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
62void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000063
Jim Laskeyd66e6162005-08-17 20:08:02 +000064//===----------------------------------------------------------------------===//
65// ConstantFPSDNode Class
66//===----------------------------------------------------------------------===//
67
68/// isExactlyValue - We don't rely on operator== working on double values, as
69/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
70/// As such, this method can be used to do an exact bit-for-bit comparison of
71/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000072bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000073 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000074}
75
Owen Anderson53aa7a92009-08-10 22:56:29 +000076bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000077 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000078 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000079
Dale Johannesend246b2c2007-08-30 00:23:21 +000080 // convert modifies in place, so make a copy.
81 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000082 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000083 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
84 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000085 &losesInfo);
86 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000087}
88
Jim Laskeyd66e6162005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000090// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000092
Evan Chengc70e33c2006-03-27 06:58:47 +000093/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000094/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000095bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000096 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +000097 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +000098 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +000099
Evan Chengc70e33c2006-03-27 06:58:47 +0000100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Chris Lattnerc2d28112006-03-25 22:57:01 +0000102 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000107
Chris Lattnerc2d28112006-03-25 22:57:01 +0000108 // Do not accept an all-undef vector.
109 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000110
Chris Lattnerc2d28112006-03-25 22:57:01 +0000111 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000112 // elements. We have to be a bit careful here, as the type of the constant
113 // may not be the same as the type of the vector elements due to type
114 // legalization (the elements are promoted to a legal type for the target and
115 // a vector of a type may be legal when the base element type is not).
116 // We only want to check enough bits to cover the vector elements, because
117 // we care if the resultant vector is all ones, not whether the individual
118 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000119 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000120 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000121 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
122 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000123 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000124 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
125 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000126 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000127 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000128 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000129
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000131 // undefs. Even with the above element type twiddling, this should be OK, as
132 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000133 for (++i; i != e; ++i)
134 if (N->getOperand(i) != NotZero &&
135 N->getOperand(i).getOpcode() != ISD::UNDEF)
136 return false;
137 return true;
138}
139
140
Evan Chenga6789912006-03-26 09:50:58 +0000141/// isBuildVectorAllZeros - Return true if the specified node is a
142/// BUILD_VECTOR where all of the elements are 0 or undef.
143bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000144 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +0000145 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000146 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000147
Evan Chenga6789912006-03-26 09:50:58 +0000148 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chengc70e33c2006-03-27 06:58:47 +0000150 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Evan Chengc70e33c2006-03-27 06:58:47 +0000152 // Skip over all of the undef values.
153 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
154 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000155
Evan Chengc70e33c2006-03-27 06:58:47 +0000156 // Do not accept an all-undef vector.
157 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000158
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000159 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chengc70e33c2006-03-27 06:58:47 +0000160 // elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000161 SDValue Zero = N->getOperand(i);
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000162 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
163 if (!CN->isNullValue())
Evan Chengc70e33c2006-03-27 06:58:47 +0000164 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000165 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
166 if (!CFPN->getValueAPF().isPosZero())
Evan Chengc70e33c2006-03-27 06:58:47 +0000167 return false;
168 } else
169 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000170
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000171 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chengc70e33c2006-03-27 06:58:47 +0000172 // undefs.
173 for (++i; i != e; ++i)
174 if (N->getOperand(i) != Zero &&
175 N->getOperand(i).getOpcode() != ISD::UNDEF)
176 return false;
177 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000178}
179
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000180/// \brief Return true if the specified node is a BUILD_VECTOR node of
181/// all ConstantSDNode or undef.
182bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
183 if (N->getOpcode() != ISD::BUILD_VECTOR)
184 return false;
185
186 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
187 SDValue Op = N->getOperand(i);
188 if (Op.getOpcode() == ISD::UNDEF)
189 continue;
190 if (!isa<ConstantSDNode>(Op))
191 return false;
192 }
193 return true;
194}
195
Evan Cheng6200c222008-02-18 23:04:32 +0000196/// isScalarToVector - Return true if the specified node is a
197/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
198/// element is not an undef.
199bool ISD::isScalarToVector(const SDNode *N) {
200 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
201 return true;
202
203 if (N->getOpcode() != ISD::BUILD_VECTOR)
204 return false;
205 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
206 return false;
207 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000208 if (NumElems == 1)
209 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000210 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000211 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000212 if (V.getOpcode() != ISD::UNDEF)
213 return false;
214 }
215 return true;
216}
217
Nadav Rotema62368c2012-07-15 08:38:23 +0000218/// allOperandsUndef - Return true if the node has at least one operand
219/// and all operands of the specified node are ISD::UNDEF.
220bool ISD::allOperandsUndef(const SDNode *N) {
221 // Return false if the node has no operands.
222 // This is "logically inconsistent" with the definition of "all" but
223 // is probably the desired behavior.
224 if (N->getNumOperands() == 0)
225 return false;
226
227 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
228 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
229 return false;
230
231 return true;
232}
233
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000234ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) {
235 switch (ExtType) {
236 case ISD::EXTLOAD:
237 return ISD::ANY_EXTEND;
238 case ISD::SEXTLOAD:
239 return ISD::SIGN_EXTEND;
240 case ISD::ZEXTLOAD:
241 return ISD::ZERO_EXTEND;
242 default:
243 break;
244 }
245
246 llvm_unreachable("Invalid LoadExtType");
247}
248
Chris Lattner061a1ea2005-01-07 07:46:32 +0000249/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
250/// when given the operation for (X op Y).
251ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
252 // To perform this operation, we just need to swap the L and G bits of the
253 // operation.
254 unsigned OldL = (Operation >> 2) & 1;
255 unsigned OldG = (Operation >> 1) & 1;
256 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
257 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000258 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000259}
260
261/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
262/// 'op' is a valid SetCC operation.
263ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
264 unsigned Operation = Op;
265 if (isInteger)
266 Operation ^= 7; // Flip L, G, E bits, but not U.
267 else
268 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000269
Chris Lattner061a1ea2005-01-07 07:46:32 +0000270 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000271 Operation &= ~8; // Don't let N and U bits get set.
272
Chris Lattner061a1ea2005-01-07 07:46:32 +0000273 return ISD::CondCode(Operation);
274}
275
276
277/// isSignedOp - For an integer comparison, return 1 if the comparison is a
278/// signed operation and 2 if the result is an unsigned comparison. Return zero
279/// if the operation does not depend on the sign of the input (setne and seteq).
280static int isSignedOp(ISD::CondCode Opcode) {
281 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000282 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000283 case ISD::SETEQ:
284 case ISD::SETNE: return 0;
285 case ISD::SETLT:
286 case ISD::SETLE:
287 case ISD::SETGT:
288 case ISD::SETGE: return 1;
289 case ISD::SETULT:
290 case ISD::SETULE:
291 case ISD::SETUGT:
292 case ISD::SETUGE: return 2;
293 }
294}
295
296/// getSetCCOrOperation - Return the result of a logical OR between different
297/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
298/// returns SETCC_INVALID if it is not possible to represent the resultant
299/// comparison.
300ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
301 bool isInteger) {
302 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
303 // Cannot fold a signed integer setcc with an unsigned integer setcc.
304 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000305
Chris Lattner061a1ea2005-01-07 07:46:32 +0000306 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000307
Chris Lattner061a1ea2005-01-07 07:46:32 +0000308 // If the N and U bits get set then the resultant comparison DOES suddenly
309 // care about orderedness, and is true when ordered.
310 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000311 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000312
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000313 // Canonicalize illegal integer setcc's.
314 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
315 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000316
Chris Lattner061a1ea2005-01-07 07:46:32 +0000317 return ISD::CondCode(Op);
318}
319
320/// getSetCCAndOperation - Return the result of a logical AND between different
321/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
322/// function returns zero if it is not possible to represent the resultant
323/// comparison.
324ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
325 bool isInteger) {
326 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
327 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000328 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000329
330 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000331 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000332
Chris Lattner393d96a2006-04-27 05:01:07 +0000333 // Canonicalize illegal integer setcc's.
334 if (isInteger) {
335 switch (Result) {
336 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000337 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000338 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000339 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
340 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
341 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000342 }
343 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000344
Chris Lattner393d96a2006-04-27 05:01:07 +0000345 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000346}
347
Jim Laskeyd66e6162005-08-17 20:08:02 +0000348//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000349// SDNode Profile Support
350//===----------------------------------------------------------------------===//
351
Jim Laskeybd0f0882006-10-27 23:52:51 +0000352/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
353///
Jim Laskeyf576b422006-10-27 23:46:08 +0000354static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
355 ID.AddInteger(OpC);
356}
357
358/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
359/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000360static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000361 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000362}
363
Jim Laskeybd0f0882006-10-27 23:52:51 +0000364/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
365///
Jim Laskeyf576b422006-10-27 23:46:08 +0000366static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000367 ArrayRef<SDValue> Ops) {
368 for (auto& Op : Ops) {
369 ID.AddPointer(Op.getNode());
370 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000371 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000372}
373
Dan Gohman768f2c92008-07-07 18:26:29 +0000374/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
375///
376static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000377 ArrayRef<SDUse> Ops) {
378 for (auto& Op : Ops) {
379 ID.AddPointer(Op.getNode());
380 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000381 }
382}
383
Craig Topper633d99b2014-04-27 23:22:43 +0000384static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
385 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000386 AddNodeIDOpcode(ID, OpC);
387 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000388 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000389}
390
Duncan Sands835bdca2008-10-27 15:30:53 +0000391/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
392/// the NodeID data.
393static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000394 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000395 case ISD::TargetExternalSymbol:
396 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000397 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000398 default: break; // Normal nodes don't need extra info.
399 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000400 case ISD::Constant: {
401 const ConstantSDNode *C = cast<ConstantSDNode>(N);
402 ID.AddPointer(C->getConstantIntValue());
403 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000404 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000405 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000406 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000407 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000408 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000409 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000410 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000411 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000412 case ISD::GlobalAddress:
413 case ISD::TargetGlobalTLSAddress:
414 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000415 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000416 ID.AddPointer(GA->getGlobal());
417 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000418 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000419 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000420 break;
421 }
422 case ISD::BasicBlock:
423 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
424 break;
425 case ISD::Register:
426 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
427 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000428 case ISD::RegisterMask:
429 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
430 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000431 case ISD::SRCVALUE:
432 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
433 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000434 case ISD::FrameIndex:
435 case ISD::TargetFrameIndex:
436 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
437 break;
438 case ISD::JumpTable:
439 case ISD::TargetJumpTable:
440 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000441 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000442 break;
443 case ISD::ConstantPool:
444 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000445 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000446 ID.AddInteger(CP->getAlignment());
447 ID.AddInteger(CP->getOffset());
448 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000449 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000450 else
451 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000452 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000453 break;
454 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000455 case ISD::TargetIndex: {
456 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
457 ID.AddInteger(TI->getIndex());
458 ID.AddInteger(TI->getOffset());
459 ID.AddInteger(TI->getTargetFlags());
460 break;
461 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000462 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000463 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000464 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000465 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000466 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000467 break;
468 }
469 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000470 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000471 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000472 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000473 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000474 break;
475 }
Dan Gohman12f24902008-12-23 21:37:04 +0000476 case ISD::ATOMIC_CMP_SWAP:
477 case ISD::ATOMIC_SWAP:
478 case ISD::ATOMIC_LOAD_ADD:
479 case ISD::ATOMIC_LOAD_SUB:
480 case ISD::ATOMIC_LOAD_AND:
481 case ISD::ATOMIC_LOAD_OR:
482 case ISD::ATOMIC_LOAD_XOR:
483 case ISD::ATOMIC_LOAD_NAND:
484 case ISD::ATOMIC_LOAD_MIN:
485 case ISD::ATOMIC_LOAD_MAX:
486 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000487 case ISD::ATOMIC_LOAD_UMAX:
488 case ISD::ATOMIC_LOAD:
489 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000490 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000491 ID.AddInteger(AT->getMemoryVT().getRawBits());
492 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000493 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
494 break;
495 }
496 case ISD::PREFETCH: {
497 const MemSDNode *PF = cast<MemSDNode>(N);
498 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000499 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000500 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000501 case ISD::VECTOR_SHUFFLE: {
502 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000503 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000504 i != e; ++i)
505 ID.AddInteger(SVN->getMaskElt(i));
506 break;
507 }
Dan Gohman6c938802009-10-30 01:27:03 +0000508 case ISD::TargetBlockAddress:
509 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000510 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
511 ID.AddPointer(BA->getBlockAddress());
512 ID.AddInteger(BA->getOffset());
513 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000514 break;
515 }
Mon P Wang6a490372008-06-25 08:15:39 +0000516 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000517
518 // Target specific memory nodes could also have address spaces to check.
519 if (N->isTargetMemoryOpcode())
520 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000521}
522
Duncan Sands835bdca2008-10-27 15:30:53 +0000523/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
524/// data.
525static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
526 AddNodeIDOpcode(ID, N->getOpcode());
527 // Add the return value info.
528 AddNodeIDValueTypes(ID, N->getVTList());
529 // Add the operand info.
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000530 AddNodeIDOperands(ID, makeArrayRef(N->op_begin(), N->op_end()));
Duncan Sands835bdca2008-10-27 15:30:53 +0000531
532 // Handle SDNode leafs with special info.
533 AddNodeIDCustom(ID, N);
534}
535
Dan Gohman2da2bed2008-08-20 15:58:01 +0000536/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000537/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000538/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000539///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000540static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000541encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000542 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000543 assert((ConvType & 3) == ConvType &&
544 "ConvType may not require more than 2 bits!");
545 assert((AM & 7) == AM &&
546 "AM may not require more than 3 bits!");
547 return ConvType |
548 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000549 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000550 (isNonTemporal << 6) |
551 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000552}
553
Jim Laskeyf576b422006-10-27 23:46:08 +0000554//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000555// SelectionDAG Class
556//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000557
Duncan Sands835bdca2008-10-27 15:30:53 +0000558/// doNotCSE - Return true if CSE should not be performed for this node.
559static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000560 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000561 return true; // Never CSE anything that produces a flag.
562
563 switch (N->getOpcode()) {
564 default: break;
565 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000566 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000567 return true; // Never CSE these nodes.
568 }
569
570 // Check that remaining values produced are not flags.
571 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000572 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000573 return true; // Never CSE anything that produces a flag.
574
575 return false;
576}
577
Chris Lattner9c667932005-01-07 21:09:16 +0000578/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000579/// SelectionDAG.
580void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000581 // Create a dummy node (which is not added to allnodes), that adds a reference
582 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000583 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000584
Chris Lattner8927c872006-08-04 17:45:20 +0000585 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000586
Chris Lattner8927c872006-08-04 17:45:20 +0000587 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000588 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000589 if (I->use_empty())
590 DeadNodes.push_back(I);
591
Dan Gohman91697632008-07-07 20:57:48 +0000592 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000593
Dan Gohman91697632008-07-07 20:57:48 +0000594 // If the root changed (e.g. it was a dead load, update the root).
595 setRoot(Dummy.getValue());
596}
597
598/// RemoveDeadNodes - This method deletes the unreachable nodes in the
599/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000600void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000601
Chris Lattner8927c872006-08-04 17:45:20 +0000602 // Process the worklist, deleting the nodes and adding their uses to the
603 // worklist.
604 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000605 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000606
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000607 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000608 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000609
Chris Lattner8927c872006-08-04 17:45:20 +0000610 // Take the node out of the appropriate CSE map.
611 RemoveNodeFromCSEMaps(N);
612
613 // Next, brutally remove the operand list. This is safe to do, as there are
614 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000615 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
616 SDUse &Use = *I++;
617 SDNode *Operand = Use.getNode();
618 Use.set(SDValue());
619
Chris Lattner8927c872006-08-04 17:45:20 +0000620 // Now that we removed this operand, see if there are no uses of it left.
621 if (Operand->use_empty())
622 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000623 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000624
Dan Gohman534c8a22009-01-19 22:39:36 +0000625 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000626 }
Chris Lattner9c667932005-01-07 21:09:16 +0000627}
628
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000629void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000630 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000631
632 // Create a dummy node that adds a reference to the root node, preventing
633 // it from being deleted. (This matters if the root is an operand of the
634 // dead node.)
635 HandleSDNode Dummy(getRoot());
636
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000637 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000638}
639
Chris Lattnerc738d002005-08-29 21:59:31 +0000640void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000641 // First take this out of the appropriate CSE map.
642 RemoveNodeFromCSEMaps(N);
643
Scott Michelcf0da6c2009-02-17 22:15:04 +0000644 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000645 // AllNodes list, and delete the node.
646 DeleteNodeNotInCSEMaps(N);
647}
648
649void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000650 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
651 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000652
Dan Gohman86aa16a2008-09-30 18:30:35 +0000653 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000654 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000655
Dan Gohman534c8a22009-01-19 22:39:36 +0000656 DeallocateNode(N);
657}
658
659void SelectionDAG::DeallocateNode(SDNode *N) {
660 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000661 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000662
Dan Gohman534c8a22009-01-19 22:39:36 +0000663 // Set the opcode to DELETED_NODE to help catch bugs when node
664 // memory is reallocated.
665 N->NodeType = ISD::DELETED_NODE;
666
Dan Gohman2e834902008-08-26 01:44:34 +0000667 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000668
Evan Cheng563fe3c2010-03-25 01:38:16 +0000669 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000670 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000671 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
672 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000673}
674
Chris Lattner19732782005-08-16 18:17:10 +0000675/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
676/// correspond to it. This is useful when we're about to delete or repurpose
677/// the node. We don't want future request for structurally identical nodes
678/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000679bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000680 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000681 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000682 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000683 case ISD::CONDCODE:
684 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
685 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000686 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
687 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000688 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000689 case ISD::ExternalSymbol:
690 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000691 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000692 case ISD::TargetExternalSymbol: {
693 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
694 Erased = TargetExternalSymbols.erase(
695 std::pair<std::string,unsigned char>(ESN->getSymbol(),
696 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000697 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000698 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000699 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000700 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000701 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000702 Erased = ExtendedValueTypeNodes.erase(VT);
703 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000704 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
705 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000706 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000707 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000708 }
Chris Lattner9c667932005-01-07 21:09:16 +0000709 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000710 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000711 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
712 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000713 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000714 break;
715 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000716#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000717 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000718 // flag result (which cannot be CSE'd) or is one of the special cases that are
719 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000720 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000721 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000722 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000723 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000724 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000725 }
726#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000727 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000728}
729
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000730/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
731/// maps and modified in place. Add it back to the CSE maps, unless an identical
732/// node already exists, in which case transfer all its users to the existing
733/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000734///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000735void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000736SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000737 // For node types that aren't CSE'd, just act as if no identical node
738 // already exists.
739 if (!doNotCSE(N)) {
740 SDNode *Existing = CSEMap.GetOrInsertNode(N);
741 if (Existing != N) {
742 // If there was already an existing matching node, use ReplaceAllUsesWith
743 // to replace the dead one with the existing one. This can cause
744 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000745 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000746
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000747 // N is now dead. Inform the listeners and delete it.
748 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
749 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000750 DeleteNodeNotInCSEMaps(N);
751 return;
752 }
753 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000754
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000755 // If the node doesn't already exist, we updated it. Inform listeners.
756 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
757 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000758}
759
Chris Lattnerf34156e2006-01-28 09:32:45 +0000760/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000761/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000762/// return null, otherwise return a pointer to the slot it would take. If a
763/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000764SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000765 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000766 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000767 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000768
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000769 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000770 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000771 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000772 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000773 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000774 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000775}
776
777/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000778/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000779/// return null, otherwise return a pointer to the slot it would take. If a
780/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000781SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000782 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000783 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000784 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000785 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000786
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000787 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000788 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000789 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000790 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000791 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000792 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000793}
794
795
796/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000797/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000798/// return null, otherwise return a pointer to the slot it would take. If a
799/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000800SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000801 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000802 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000803 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000804
Jim Laskeyf576b422006-10-27 23:46:08 +0000805 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000806 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000807 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000808 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000809 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000810}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000811
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000812#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000813/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
814static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsb0e39382008-07-21 10:20:31 +0000815 switch (N->getOpcode()) {
816 default:
817 break;
Duncan Sands17e678b2008-10-29 14:22:20 +0000818 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000819 EVT VT = N->getValueType(0);
Duncan Sands17e678b2008-10-29 14:22:20 +0000820 assert(N->getNumValues() == 1 && "Too many results!");
821 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
822 "Wrong return type!");
823 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
824 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
825 "Mismatched operand types!");
826 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
827 "Wrong operand type!");
828 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
829 "Wrong return type size");
830 break;
831 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000832 case ISD::BUILD_VECTOR: {
Duncan Sands17e678b2008-10-29 14:22:20 +0000833 assert(N->getNumValues() == 1 && "Too many results!");
834 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsb0e39382008-07-21 10:20:31 +0000835 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands17e678b2008-10-29 14:22:20 +0000836 "Wrong number of operands!");
Owen Anderson53aa7a92009-08-10 22:56:29 +0000837 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedmanb7910b72011-09-09 21:04:06 +0000838 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindolab93db662009-04-24 12:40:33 +0000839 assert((I->getValueType() == EltVT ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000840 (EltVT.isInteger() && I->getValueType().isInteger() &&
841 EltVT.bitsLE(I->getValueType()))) &&
842 "Wrong operand type!");
Eli Friedmanb7910b72011-09-09 21:04:06 +0000843 assert(I->getValueType() == N->getOperand(0).getValueType() &&
844 "Operands must all have the same type");
845 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000846 break;
847 }
848 }
849}
850
Duncan Sands7c601de2010-11-20 11:25:00 +0000851/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
852static void VerifySDNode(SDNode *N) {
853 // The SDNode allocators cannot be used to allocate nodes with fields that are
854 // not present in an SDNode!
855 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
856 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
857 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
858 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
859 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
860 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
861 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
862 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
863 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
864 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
865 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
866 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
867 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
868 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
869 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
870 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
871 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
872 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
873 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
874
875 VerifyNodeCommon(N);
876}
877
878/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
879/// invalid.
880static void VerifyMachineNode(SDNode *N) {
881 // The MachineNode allocators cannot be used to allocate nodes with fields
882 // that are not present in a MachineNode!
883 // Currently there are no such nodes.
884
885 VerifyNodeCommon(N);
886}
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000887#endif // NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000888
Owen Anderson53aa7a92009-08-10 22:56:29 +0000889/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000890/// given type.
891///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000892unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000893 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000894 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000895 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000896
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000897 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000898}
Chris Lattner9c667932005-01-07 21:09:16 +0000899
Dale Johannesen8ba713212009-02-07 02:15:05 +0000900// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000901SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Craig Topperc0196b12014-04-14 00:51:57 +0000902 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TLI(nullptr), OptLevel(OL),
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000903 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sanders50b80412013-11-15 12:56:49 +0000904 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
Craig Topperc0196b12014-04-14 00:51:57 +0000905 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000906 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000907 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000908}
909
Juergen Ributzka659ce002014-01-28 01:20:14 +0000910void SelectionDAG::init(MachineFunction &mf, const TargetLowering *tli) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000911 MF = &mf;
Juergen Ributzkab3487102013-11-19 21:20:17 +0000912 TLI = tli;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000913 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000914}
915
Chris Lattner600d3082003-08-11 14:57:33 +0000916SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000917 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000918 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000919 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000920}
921
922void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000923 assert(&*AllNodes.begin() == &EntryNode);
924 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000925 while (!AllNodes.empty())
926 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000927}
928
Dan Gohmane1a9a782008-08-27 23:52:12 +0000929void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000930 allnodes_clear();
931 OperandAllocator.Reset();
932 CSEMap.clear();
933
934 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000935 ExternalSymbols.clear();
936 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000937 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000938 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000939 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000940 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000941
Craig Topperc0196b12014-04-14 00:51:57 +0000942 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000943 AllNodes.push_back(&EntryNode);
944 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000945 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000946}
947
Andrew Trickef9de2a2013-05-25 02:42:55 +0000948SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000949 return VT.bitsGT(Op.getValueType()) ?
950 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
951 getNode(ISD::TRUNCATE, DL, VT, Op);
952}
953
Andrew Trickef9de2a2013-05-25 02:42:55 +0000954SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000955 return VT.bitsGT(Op.getValueType()) ?
956 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
957 getNode(ISD::TRUNCATE, DL, VT, Op);
958}
959
Andrew Trickef9de2a2013-05-25 02:42:55 +0000960SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000961 return VT.bitsGT(Op.getValueType()) ?
962 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
963 getNode(ISD::TRUNCATE, DL, VT, Op);
964}
965
Andrew Trickef9de2a2013-05-25 02:42:55 +0000966SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000967 assert(!VT.isVector() &&
968 "getZeroExtendInReg should use the vector element type instead of "
969 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000970 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000971 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
972 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000973 VT.getSizeInBits());
974 return getNode(ISD::AND, DL, Op.getValueType(), Op,
975 getConstant(Imm, Op.getValueType()));
976}
977
Bob Wilsonc5890052009-01-22 17:39:32 +0000978/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
979///
Andrew Trickef9de2a2013-05-25 02:42:55 +0000980SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000981 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +0000982 SDValue NegOne =
983 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +0000984 return getNode(ISD::XOR, DL, VT, Val, NegOne);
985}
986
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000987SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000988 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +0000989 assert((EltVT.getSizeInBits() >= 64 ||
990 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
991 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000992 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +0000993}
994
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000995SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
996{
997 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000998}
999
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001000SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1001 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001002 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001003
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001004 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001005 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001006
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001007 const TargetLowering *TLI = TM.getTargetLowering();
1008
Duncan Sandsf2641e12011-09-06 19:07:46 +00001009 // In some cases the vector type is legal but the element type is illegal and
1010 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1011 // inserted value (the type does not need to match the vector element type).
1012 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001013 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001014 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001015 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001016 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1017 Elt = ConstantInt::get(*getContext(), NewVal);
1018 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001019 // In other cases the element type is illegal and needs to be expanded, for
1020 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1021 // the value into n parts and use a vector type with n-times the elements.
1022 // Then bitcast to the type requested.
1023 // Legalizing constants too early makes the DAGCombiner's job harder so we
1024 // only legalize if the DAG tells us we must produce legal types.
1025 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1026 TLI->getTypeAction(*getContext(), EltVT) ==
1027 TargetLowering::TypeExpandInteger) {
1028 APInt NewVal = Elt->getValue();
1029 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1030 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1031 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1032 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1033
1034 // Check the temporary vector is the correct size. If this fails then
1035 // getTypeToTransformTo() probably returned a type whose size (in bits)
1036 // isn't a power-of-2 factor of the requested type size.
1037 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1038
1039 SmallVector<SDValue, 2> EltParts;
1040 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1041 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1042 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001043 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001044 }
1045
1046 // EltParts is currently in little endian order. If we actually want
1047 // big-endian order then reverse it now.
1048 if (TLI->isBigEndian())
1049 std::reverse(EltParts.begin(), EltParts.end());
1050
1051 // The elements must be reversed when the element order is different
1052 // to the endianness of the elements (because the BITCAST is itself a
1053 // vector shuffle in this situation). However, we do not need any code to
1054 // perform this reversal because getConstant() is producing a vector
1055 // splat.
1056 // This situation occurs in MIPS MSA.
1057
1058 SmallVector<SDValue, 8> Ops;
1059 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1060 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1061
1062 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1063 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001064 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001065 return Result;
1066 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001067
1068 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1069 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001070 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001071 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001072 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001073 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001074 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001075 void *IP = nullptr;
1076 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001077 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001078 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001079 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001080
Dan Gohman7a7742c2007-12-12 22:21:26 +00001081 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001082 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001083 CSEMap.InsertNode(N, IP);
1084 AllNodes.push_back(N);
1085 }
1086
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001087 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001088 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001089 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001090 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001091 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001092 }
1093 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001094}
1095
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001096SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001097 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001098}
1099
1100
Owen Anderson53aa7a92009-08-10 22:56:29 +00001101SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001102 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001103}
1104
Owen Anderson53aa7a92009-08-10 22:56:29 +00001105SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001106 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001107
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001108 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001109
Chris Lattner381dddc2005-02-17 20:17:32 +00001110 // Do the map lookup using the actual bit pattern for the floating point
1111 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1112 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001113 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001114 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001115 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001116 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001117 void *IP = nullptr;
1118 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001119 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001120 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001121 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001122
Evan Cheng9458e6a2007-06-29 21:36:04 +00001123 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001124 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001125 CSEMap.InsertNode(N, IP);
1126 AllNodes.push_back(N);
1127 }
1128
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001129 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001130 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001131 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001132 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001133 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001134 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001135 }
1136 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001137}
1138
Owen Anderson53aa7a92009-08-10 22:56:29 +00001139SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001140 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001141 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001142 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001143 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001144 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001145 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1146 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001147 bool ignored;
1148 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001149 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001150 &ignored);
1151 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001152 } else
1153 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001154}
1155
Andrew Trickef9de2a2013-05-25 02:42:55 +00001156SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001157 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001158 bool isTargetGA,
1159 unsigned char TargetFlags) {
1160 assert((TargetFlags == 0 || isTargetGA) &&
1161 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001162 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001163
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001164 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001165 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001166 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001167 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001168
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001169 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1170 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001171 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001172 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Rafael Espindola24a669d2014-03-27 15:26:56 +00001173 GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasedGlobal());
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001174 }
1175
Chris Lattner8e34f982009-06-25 21:21:14 +00001176 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001177 if (GVar && GVar->isThreadLocal())
1178 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1179 else
1180 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001181
Jim Laskeyf576b422006-10-27 23:46:08 +00001182 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001183 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001184 ID.AddPointer(GV);
1185 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001186 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001187 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001188 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001189 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001190 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001191
Andrew Trickef9de2a2013-05-25 02:42:55 +00001192 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1193 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001194 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001195 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001196 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001197 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001198}
1199
Owen Anderson53aa7a92009-08-10 22:56:29 +00001200SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001201 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001202 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001203 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001204 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001205 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001206 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001207 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001208
Dan Gohman01c65a22010-03-18 18:49:47 +00001209 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001210 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001211 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001212 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001213}
1214
Owen Anderson53aa7a92009-08-10 22:56:29 +00001215SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001216 unsigned char TargetFlags) {
1217 assert((TargetFlags == 0 || isTarget) &&
1218 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001219 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001220 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001221 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001222 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001223 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001224 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001225 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001226 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001227
Dan Gohman01c65a22010-03-18 18:49:47 +00001228 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1229 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001230 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001231 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001232 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001233}
1234
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001235SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001236 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001237 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001238 unsigned char TargetFlags) {
1239 assert((TargetFlags == 0 || isTarget) &&
1240 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001241 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001242 Alignment =
1243 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001244 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001245 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001246 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001247 ID.AddInteger(Alignment);
1248 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001249 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001250 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001251 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001252 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001253 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001254
Dan Gohman01c65a22010-03-18 18:49:47 +00001255 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1256 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001257 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001258 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001259 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001260}
1261
Chris Lattner061a1ea2005-01-07 07:46:32 +00001262
Owen Anderson53aa7a92009-08-10 22:56:29 +00001263SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001264 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001265 bool isTarget,
1266 unsigned char TargetFlags) {
1267 assert((TargetFlags == 0 || isTarget) &&
1268 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001269 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001270 Alignment =
1271 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001272 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001273 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001274 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001275 ID.AddInteger(Alignment);
1276 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001277 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001278 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001279 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001280 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001281 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001282
Dan Gohman01c65a22010-03-18 18:49:47 +00001283 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1284 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001285 CSEMap.InsertNode(N, IP);
1286 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001287 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001288}
1289
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001290SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1291 unsigned char TargetFlags) {
1292 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001293 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001294 ID.AddInteger(Index);
1295 ID.AddInteger(Offset);
1296 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001297 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001298 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1299 return SDValue(E, 0);
1300
1301 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1302 TargetFlags);
1303 CSEMap.InsertNode(N, IP);
1304 AllNodes.push_back(N);
1305 return SDValue(N, 0);
1306}
1307
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001308SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001309 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001310 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001311 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001312 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001313 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001314 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001315
Dan Gohman01c65a22010-03-18 18:49:47 +00001316 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001317 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001318 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001319 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001320}
1321
Owen Anderson53aa7a92009-08-10 22:56:29 +00001322SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001323 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1324 ValueTypeNodes.size())
1325 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001326
Duncan Sands13237ac2008-06-06 12:08:01 +00001327 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001328 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001329
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001330 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001331 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001332 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001333 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001334}
1335
Owen Anderson53aa7a92009-08-10 22:56:29 +00001336SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001337 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001338 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001339 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001340 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001341 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001342}
1343
Owen Anderson53aa7a92009-08-10 22:56:29 +00001344SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001345 unsigned char TargetFlags) {
1346 SDNode *&N =
1347 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1348 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001349 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001350 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001351 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001352 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001353}
1354
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001355SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001356 if ((unsigned)Cond >= CondCodeNodes.size())
1357 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001358
Craig Topperc0196b12014-04-14 00:51:57 +00001359 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001360 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001361 CondCodeNodes[Cond] = N;
1362 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001363 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001364
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001365 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001366}
1367
Nate Begeman5f829d82009-04-29 05:20:52 +00001368// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1369// the shuffle mask M that point at N1 to point at N2, and indices that point
1370// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001371static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1372 std::swap(N1, N2);
1373 int NElts = M.size();
1374 for (int i = 0; i != NElts; ++i) {
1375 if (M[i] >= NElts)
1376 M[i] -= NElts;
1377 else if (M[i] >= 0)
1378 M[i] += NElts;
1379 }
1380}
1381
Andrew Trickef9de2a2013-05-25 02:42:55 +00001382SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001383 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001384 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1385 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001386
1387 // Canonicalize shuffle undef, undef -> undef
1388 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001389 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001390
Eric Christopherdfda92b2009-08-22 00:40:45 +00001391 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001392 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001393 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001394 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001395 for (unsigned i = 0; i != NElts; ++i) {
1396 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001397 MaskVec.push_back(Mask[i]);
1398 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001399
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001400 // Canonicalize shuffle v, v -> v, undef
1401 if (N1 == N2) {
1402 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001403 for (unsigned i = 0; i != NElts; ++i)
1404 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001405 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001406
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001407 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1408 if (N1.getOpcode() == ISD::UNDEF)
1409 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001410
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001411 // Canonicalize all index into lhs, -> shuffle lhs, undef
1412 // Canonicalize all index into rhs, -> shuffle rhs, undef
1413 bool AllLHS = true, AllRHS = true;
1414 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001415 for (unsigned i = 0; i != NElts; ++i) {
1416 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001417 if (N2Undef)
1418 MaskVec[i] = -1;
1419 else
1420 AllLHS = false;
1421 } else if (MaskVec[i] >= 0) {
1422 AllRHS = false;
1423 }
1424 }
1425 if (AllLHS && AllRHS)
1426 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001427 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001428 N2 = getUNDEF(VT);
1429 if (AllRHS) {
1430 N1 = getUNDEF(VT);
1431 commuteShuffle(N1, N2, MaskVec);
1432 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001433
Craig Topper9a39b072013-08-08 08:03:12 +00001434 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001435 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001436 for (unsigned i = 0; i != NElts; ++i) {
1437 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001438 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001439 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001440 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001441
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001442 // Shuffling a constant splat doesn't change the result.
1443 if (N2Undef && N1.getOpcode() == ISD::BUILD_VECTOR)
1444 if (cast<BuildVectorSDNode>(N1)->getConstantSplatValue())
1445 return N1;
1446
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001447 FoldingSetNodeID ID;
1448 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001449 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001450 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001451 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001452
Craig Topperc0196b12014-04-14 00:51:57 +00001453 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001454 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001455 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001456
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001457 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1458 // SDNode doesn't have access to it. This memory will be "leaked" when
1459 // the node is deallocated, but recovered when the NodeAllocator is released.
1460 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1461 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001462
Dan Gohman01c65a22010-03-18 18:49:47 +00001463 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001464 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1465 dl.getDebugLoc(), N1, N2,
1466 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001467 CSEMap.InsertNode(N, IP);
1468 AllNodes.push_back(N);
1469 return SDValue(N, 0);
1470}
1471
Andrew Trickef9de2a2013-05-25 02:42:55 +00001472SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001473 SDValue Val, SDValue DTy,
1474 SDValue STy, SDValue Rnd, SDValue Sat,
1475 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001476 // If the src and dest types are the same and the conversion is between
1477 // integer types of the same sign or two floats, no conversion is necessary.
1478 if (DTy == STy &&
1479 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001480 return Val;
1481
1482 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001483 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001484 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001485 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001486 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001487 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001488
Jack Carter170a5f22013-09-09 22:02:08 +00001489 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1490 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001491 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001492 CSEMap.InsertNode(N, IP);
1493 AllNodes.push_back(N);
1494 return SDValue(N, 0);
1495}
1496
Owen Anderson53aa7a92009-08-10 22:56:29 +00001497SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001498 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001499 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001500 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001501 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001502 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001503 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001504
Dan Gohman01c65a22010-03-18 18:49:47 +00001505 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001506 CSEMap.InsertNode(N, IP);
1507 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001508 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001509}
1510
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001511SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1512 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001513 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001514 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001515 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001516 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1517 return SDValue(E, 0);
1518
1519 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1520 CSEMap.InsertNode(N, IP);
1521 AllNodes.push_back(N);
1522 return SDValue(N, 0);
1523}
1524
Andrew Trickef9de2a2013-05-25 02:42:55 +00001525SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001526 FoldingSetNodeID ID;
1527 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001528 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001529 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001530 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001531 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001532 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001533
Jack Carter170a5f22013-09-09 22:02:08 +00001534 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1535 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001536 CSEMap.InsertNode(N, IP);
1537 AllNodes.push_back(N);
1538 return SDValue(N, 0);
1539}
1540
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001541
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001542SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001543 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001544 bool isTarget,
1545 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001546 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1547
1548 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001549 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001550 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001551 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001552 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001553 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001554 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001555 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001556
Michael Liaoabb87d42012-09-12 21:43:09 +00001557 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1558 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001559 CSEMap.InsertNode(N, IP);
1560 AllNodes.push_back(N);
1561 return SDValue(N, 0);
1562}
1563
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001564SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001565 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001566 "SrcValue is not a pointer?");
1567
Jim Laskeyf576b422006-10-27 23:46:08 +00001568 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001569 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001570 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001571
Craig Topperc0196b12014-04-14 00:51:57 +00001572 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001573 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001574 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001575
Dan Gohman01c65a22010-03-18 18:49:47 +00001576 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001577 CSEMap.InsertNode(N, IP);
1578 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001579 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001580}
1581
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001582/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1583SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1584 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001585 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001586 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001587
Craig Topperc0196b12014-04-14 00:51:57 +00001588 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1590 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001591
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001592 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1593 CSEMap.InsertNode(N, IP);
1594 AllNodes.push_back(N);
1595 return SDValue(N, 0);
1596}
1597
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001598/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1599SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1600 unsigned SrcAS, unsigned DestAS) {
1601 SDValue Ops[] = {Ptr};
1602 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001603 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001604 ID.AddInteger(SrcAS);
1605 ID.AddInteger(DestAS);
1606
Craig Topperc0196b12014-04-14 00:51:57 +00001607 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001608 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1609 return SDValue(E, 0);
1610
1611 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1612 dl.getDebugLoc(),
1613 VT, Ptr, SrcAS, DestAS);
1614 CSEMap.InsertNode(N, IP);
1615 AllNodes.push_back(N);
1616 return SDValue(N, 0);
1617}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001618
Duncan Sands41826032009-01-31 15:50:11 +00001619/// getShiftAmountOperand - Return the specified value casted to
1620/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001621SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001622 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001623 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001624 if (OpTy == ShTy || OpTy.isVector()) return Op;
1625
1626 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001627 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001628}
1629
Chris Lattner9eb7a822007-10-15 17:47:20 +00001630/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1631/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001632SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001633 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001634 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001635 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001636 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001637 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001638 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001639
David Greene1fbe0542009-11-12 20:49:22 +00001640 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001641 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001642}
1643
Duncan Sands445071c2008-12-09 21:33:20 +00001644/// CreateStackTemporary - Create a stack temporary suitable for holding
1645/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001646SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001647 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1648 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001649 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1650 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001651 const TargetLowering *TLI = TM.getTargetLowering();
1652 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001653 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1654 TD->getPrefTypeAlignment(Ty2));
1655
1656 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001657 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001658 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001659}
1660
Owen Anderson53aa7a92009-08-10 22:56:29 +00001661SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001662 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001663 // These setcc operations always fold.
1664 switch (Cond) {
1665 default: break;
1666 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001667 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001668 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001669 case ISD::SETTRUE2: {
1670 const TargetLowering *TLI = TM.getTargetLowering();
1671 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1672 return getConstant(
1673 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1674 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001675
Chris Lattner393d96a2006-04-27 05:01:07 +00001676 case ISD::SETOEQ:
1677 case ISD::SETOGT:
1678 case ISD::SETOGE:
1679 case ISD::SETOLT:
1680 case ISD::SETOLE:
1681 case ISD::SETONE:
1682 case ISD::SETO:
1683 case ISD::SETUO:
1684 case ISD::SETUEQ:
1685 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001686 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001687 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001688 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001689
Gabor Greiff304a7a2008-08-28 21:40:38 +00001690 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001691 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001692 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001693 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001694
Chris Lattner061a1ea2005-01-07 07:46:32 +00001695 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001696 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001697 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1698 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001699 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1700 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1701 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1702 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1703 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1704 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1705 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1706 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001707 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001708 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001709 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001710 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1711 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001712 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001713 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001714 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001715 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001716 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001717 // fall through
1718 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001719 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001720 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001721 // fall through
1722 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001723 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001724 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001725 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001726 // fall through
1727 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001728 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001729 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001730 // fall through
1731 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001732 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001733 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001734 // fall through
1735 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001736 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001737 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001738 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001739 // fall through
1740 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001741 R==APFloat::cmpEqual, VT);
1742 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1743 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1744 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1745 R==APFloat::cmpEqual, VT);
1746 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1747 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1748 R==APFloat::cmpLessThan, VT);
1749 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1750 R==APFloat::cmpUnordered, VT);
1751 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1752 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001753 }
1754 } else {
1755 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001756 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1757 MVT CompVT = N1.getValueType().getSimpleVT();
1758 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1759 return SDValue();
1760
1761 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001762 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001763 }
1764
Chris Lattnerd47675e2005-08-09 20:20:18 +00001765 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001766 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001767}
1768
Dan Gohman1f372ed2008-02-25 21:11:39 +00001769/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1770/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001771bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001772 // This predicate is not safe for vector operations.
1773 if (Op.getValueType().isVector())
1774 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001775
Dan Gohman1d459e42009-12-11 21:31:27 +00001776 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001777 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1778}
1779
Dan Gohman309d3d52007-06-22 14:59:07 +00001780/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1781/// this predicate to simplify operations downstream. Mask is known to be zero
1782/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001783bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001784 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001785 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001786 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001787 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001788 return (KnownZero & Mask) == Mask;
1789}
1790
1791/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1792/// known to be either zero or one and return them in the KnownZero/KnownOne
1793/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1794/// processing.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001795void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1796 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001797 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001798 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001799
Dan Gohmanf990faf2008-02-13 00:35:47 +00001800 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001801 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001802 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001803
Dan Gohmanf990faf2008-02-13 00:35:47 +00001804 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001805
1806 switch (Op.getOpcode()) {
1807 case ISD::Constant:
1808 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001809 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1810 KnownZero = ~KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00001811 return;
1812 case ISD::AND:
1813 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001814 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1815 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001816 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1817 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001818
1819 // Output known-1 bits are only known if set in both the LHS & RHS.
1820 KnownOne &= KnownOne2;
1821 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1822 KnownZero |= KnownZero2;
1823 return;
1824 case ISD::OR:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001825 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1826 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001827 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1828 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1829
Dan Gohman309d3d52007-06-22 14:59:07 +00001830 // Output known-0 bits are only known if clear in both the LHS & RHS.
1831 KnownZero &= KnownZero2;
1832 // Output known-1 are known to be set if set in either the LHS | RHS.
1833 KnownOne |= KnownOne2;
1834 return;
1835 case ISD::XOR: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001836 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1837 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001838 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1839 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1840
Dan Gohman309d3d52007-06-22 14:59:07 +00001841 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001842 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001843 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1844 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1845 KnownZero = KnownZeroOut;
1846 return;
1847 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001848 case ISD::MUL: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001849 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1850 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001851 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1852 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1853
1854 // If low bits are zero in either operand, output low known-0 bits.
1855 // Also compute a conserative estimate for high known-0 bits.
1856 // More trickiness is possible, but this is sufficient for the
1857 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001858 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001859 unsigned TrailZ = KnownZero.countTrailingOnes() +
1860 KnownZero2.countTrailingOnes();
1861 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001862 KnownZero2.countLeadingOnes(),
1863 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001864
1865 TrailZ = std::min(TrailZ, BitWidth);
1866 LeadZ = std::min(LeadZ, BitWidth);
1867 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1868 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001869 return;
1870 }
1871 case ISD::UDIV: {
1872 // For the purposes of computing leading zeros we can conservatively
1873 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001874 // be less than the denominator.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001875 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001876 unsigned LeadZ = KnownZero2.countLeadingOnes();
1877
Jay Foad25a5e4c2010-12-01 08:53:58 +00001878 KnownOne2.clearAllBits();
1879 KnownZero2.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001880 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001881 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1882 if (RHSUnknownLeadingOnes != BitWidth)
1883 LeadZ = std::min(BitWidth,
1884 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001885
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001886 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001887 return;
1888 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001889 case ISD::SELECT:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001890 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1891 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001892 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1893 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1894
Dan Gohman309d3d52007-06-22 14:59:07 +00001895 // Only known if known in both the LHS and RHS.
1896 KnownOne &= KnownOne2;
1897 KnownZero &= KnownZero2;
1898 return;
1899 case ISD::SELECT_CC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001900 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1901 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001902 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1903 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1904
Dan Gohman309d3d52007-06-22 14:59:07 +00001905 // Only known if known in both the LHS and RHS.
1906 KnownOne &= KnownOne2;
1907 KnownZero &= KnownZero2;
1908 return;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001909 case ISD::SADDO:
1910 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001911 case ISD::SSUBO:
1912 case ISD::USUBO:
1913 case ISD::SMULO:
1914 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001915 if (Op.getResNo() != 1)
1916 return;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001917 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001918 case ISD::SETCC:
1919 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001920 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001921 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001922 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001923 return;
1924 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001925 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001926 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001927 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001928
1929 // If the shift count is an invalid immediate, don't do anything.
1930 if (ShAmt >= BitWidth)
1931 return;
1932
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001933 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001934 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman9db0aa82008-02-26 18:50:50 +00001935 KnownZero <<= ShAmt;
1936 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00001937 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00001938 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001939 }
1940 return;
1941 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001942 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001943 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001944 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001945
Dan Gohman9db0aa82008-02-26 18:50:50 +00001946 // If the shift count is an invalid immediate, don't do anything.
1947 if (ShAmt >= BitWidth)
1948 return;
1949
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001950 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001951 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001952 KnownZero = KnownZero.lshr(ShAmt);
1953 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001954
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001955 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001956 KnownZero |= HighBits; // High bits known zero.
1957 }
1958 return;
1959 case ISD::SRA:
1960 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001961 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001962
Dan Gohman9db0aa82008-02-26 18:50:50 +00001963 // If the shift count is an invalid immediate, don't do anything.
1964 if (ShAmt >= BitWidth)
1965 return;
1966
Dan Gohman309d3d52007-06-22 14:59:07 +00001967 // If any of the demanded bits are produced by the sign extension, we also
1968 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001969 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001970
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001971 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001972 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001973 KnownZero = KnownZero.lshr(ShAmt);
1974 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001975
Dan Gohman309d3d52007-06-22 14:59:07 +00001976 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001977 APInt SignBit = APInt::getSignBit(BitWidth);
1978 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001979
Dan Gohmanb717fda2008-02-20 16:30:17 +00001980 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001981 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001982 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001983 KnownOne |= HighBits; // New bits are known one.
1984 }
1985 }
1986 return;
1987 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001988 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001989 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001990
1991 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00001992 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001993 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00001994
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001995 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001996 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001997
Dan Gohman309d3d52007-06-22 14:59:07 +00001998 // If the sign extended bits are demanded, we know that the sign
1999 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002000 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002001 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002002 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002003
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002004 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2005 KnownOne &= InputDemandedBits;
2006 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002007 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
2008
Dan Gohman309d3d52007-06-22 14:59:07 +00002009 // If the sign bit of the input is known set or clear, then we know the
2010 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002011 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002012 KnownZero |= NewBits;
2013 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002014 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002015 KnownOne |= NewBits;
2016 KnownZero &= ~NewBits;
2017 } else { // Input sign bit unknown
2018 KnownZero &= ~NewBits;
2019 KnownOne &= ~NewBits;
2020 }
2021 return;
2022 }
2023 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002024 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002025 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002026 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002027 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002028 unsigned LowBits = Log2_32(BitWidth)+1;
2029 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002030 KnownOne.clearAllBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002031 return;
2032 }
2033 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002034 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002035 // If this is a ZEXTLoad and we are looking at the loaded value.
2036 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002037 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002038 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002039 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002040 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002041 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002042 }
2043 return;
2044 }
2045 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002046 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002047 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002048 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002049 KnownZero = KnownZero.trunc(InBits);
2050 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002051 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002052 KnownZero = KnownZero.zext(BitWidth);
2053 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002054 KnownZero |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002055 return;
2056 }
2057 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002058 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002059 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002060 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002061
Jay Foad583abbc2010-12-07 08:25:19 +00002062 KnownZero = KnownZero.trunc(InBits);
2063 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002064 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002065
2066 // Note if the sign bit is known to be zero or one.
2067 bool SignBitKnownZero = KnownZero.isNegative();
2068 bool SignBitKnownOne = KnownOne.isNegative();
2069 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2070 "Sign bit can't be known to be both zero and one!");
2071
Jay Foad583abbc2010-12-07 08:25:19 +00002072 KnownZero = KnownZero.zext(BitWidth);
2073 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002074
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002075 // If the sign bit is known zero or one, the top bits match.
2076 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002077 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002078 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002079 KnownOne |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002080 return;
2081 }
2082 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002083 EVT InVT = Op.getOperand(0).getValueType();
2084 unsigned InBits = InVT.getScalarType().getSizeInBits();
2085 KnownZero = KnownZero.trunc(InBits);
2086 KnownOne = KnownOne.trunc(InBits);
2087 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2088 KnownZero = KnownZero.zext(BitWidth);
2089 KnownOne = KnownOne.zext(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002090 return;
2091 }
2092 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002093 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002094 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002095 KnownZero = KnownZero.zext(InBits);
2096 KnownOne = KnownOne.zext(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002097 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002098 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +00002099 KnownZero = KnownZero.trunc(BitWidth);
2100 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002101 break;
2102 }
2103 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002104 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002105 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002106 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2107 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002108 KnownOne &= (~KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002109 return;
2110 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002111 case ISD::FGETSIGN:
2112 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002113 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerafc8f132007-12-22 21:26:52 +00002114 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002115
Dan Gohman72ec3f42008-04-28 17:02:21 +00002116 case ISD::SUB: {
2117 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2118 // We know that the top bits of C-X are clear if X contains less bits
2119 // than C (i.e. no wrap-around can happen). For example, 20-X is
2120 // positive if we can prove that X is >= 0 and < 16.
2121 if (CLHS->getAPIntValue().isNonNegative()) {
2122 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2123 // NLZ can't be BitWidth with no sign bit
2124 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002125 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002126
2127 // If all of the MaskV bits are known to be zero, then we know the
2128 // output top bits are zero, because we now know that the output is
2129 // from [0-C].
2130 if ((KnownZero2 & MaskV) == MaskV) {
2131 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2132 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002133 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002134 }
2135 }
2136 }
2137 }
2138 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002139 case ISD::ADD:
2140 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002141 // Output known-0 bits are known if clear or set in both the low clear bits
2142 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2143 // low 3 bits clear.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002144 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002145 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002146 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2147
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002148 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002149 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002150 KnownZeroOut = std::min(KnownZeroOut,
2151 KnownZero2.countTrailingOnes());
2152
Chris Lattner440b2802010-12-19 20:38:28 +00002153 if (Op.getOpcode() == ISD::ADD) {
2154 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2155 return;
2156 }
2157
2158 // With ADDE, a carry bit may be added in, so we can only use this
2159 // information if we know (at least) that the low two bits are clear. We
2160 // then return to the caller that the low bit is unknown but that other bits
2161 // are known zero.
2162 if (KnownZeroOut >= 2) // ADDE
2163 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohman309d3d52007-06-22 14:59:07 +00002164 return;
2165 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002166 case ISD::SREM:
2167 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002168 const APInt &RA = Rem->getAPIntValue().abs();
2169 if (RA.isPowerOf2()) {
2170 APInt LowBits = RA - 1;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002171 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002172
Duncan Sands33274982010-01-29 09:45:26 +00002173 // The low bits of the first operand are unchanged by the srem.
2174 KnownZero = KnownZero2 & LowBits;
2175 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002176
Duncan Sands33274982010-01-29 09:45:26 +00002177 // If the first operand is non-negative or has all low bits zero, then
2178 // the upper bits are all zero.
2179 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2180 KnownZero |= ~LowBits;
2181
2182 // If the first operand is negative and not all low bits are zero, then
2183 // the upper bits are all one.
2184 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2185 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002186 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002187 }
2188 }
2189 return;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002190 case ISD::UREM: {
2191 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002192 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002193 if (RA.isPowerOf2()) {
2194 APInt LowBits = (RA - 1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002195 KnownZero |= ~LowBits;
2196 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002197 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2198 break;
2199 }
2200 }
2201
2202 // Since the result is less than or equal to either operand, any leading
2203 // zero bits in either operand must also exist in the result.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002204 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2205 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002206
2207 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2208 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002209 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002210 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002211 return;
Dan Gohman309d3d52007-06-22 14:59:07 +00002212 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002213 case ISD::FrameIndex:
2214 case ISD::TargetFrameIndex:
2215 if (unsigned Align = InferPtrAlignment(Op)) {
2216 // The low bits are known zero if the pointer is aligned.
2217 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2218 return;
2219 }
2220 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002221
Dan Gohman309d3d52007-06-22 14:59:07 +00002222 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002223 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2224 break;
2225 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002226 case ISD::INTRINSIC_WO_CHAIN:
2227 case ISD::INTRINSIC_W_CHAIN:
2228 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002229 // Allow the target to implement this method for its nodes.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002230 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002231 return;
2232 }
2233}
2234
2235/// ComputeNumSignBits - Return the number of times the sign bit of the
2236/// register is replicated into the other bits. We know that at least 1 bit
2237/// is always equal to the sign bit (itself), but other cases can give us
2238/// information. For example, immediately after an "SRA X, 2", we know that
2239/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002240unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002241 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002242 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002243 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002244 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002245 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002246 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002247
Dan Gohman309d3d52007-06-22 14:59:07 +00002248 if (Depth == 6)
2249 return 1; // Limit search depth.
2250
2251 switch (Op.getOpcode()) {
2252 default: break;
2253 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002254 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002255 return VTBits-Tmp+1;
2256 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002257 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002258 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002259
Dan Gohman309d3d52007-06-22 14:59:07 +00002260 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002261 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002262 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002263 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002264
Dan Gohman309d3d52007-06-22 14:59:07 +00002265 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002266 Tmp =
2267 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002268 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002269
Dan Gohman309d3d52007-06-22 14:59:07 +00002270 case ISD::SIGN_EXTEND_INREG:
2271 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002272 Tmp =
2273 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002274 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002275
Dan Gohman309d3d52007-06-22 14:59:07 +00002276 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2277 return std::max(Tmp, Tmp2);
2278
2279 case ISD::SRA:
2280 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2281 // SRA X, C -> adds C sign bits.
2282 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002283 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002284 if (Tmp > VTBits) Tmp = VTBits;
2285 }
2286 return Tmp;
2287 case ISD::SHL:
2288 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2289 // shl destroys sign bits.
2290 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002291 if (C->getZExtValue() >= VTBits || // Bad shift.
2292 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2293 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002294 }
2295 break;
2296 case ISD::AND:
2297 case ISD::OR:
2298 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002299 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002300 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002301 if (Tmp != 1) {
2302 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2303 FirstAnswer = std::min(Tmp, Tmp2);
2304 // We computed what we know about the sign bits as our first
2305 // answer. Now proceed to the generic code that uses
2306 // ComputeMaskedBits, and pick whichever answer is better.
2307 }
2308 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002309
2310 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002311 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002312 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002313 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002314 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002315
2316 case ISD::SADDO:
2317 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002318 case ISD::SSUBO:
2319 case ISD::USUBO:
2320 case ISD::SMULO:
2321 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002322 if (Op.getResNo() != 1)
2323 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002324 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002325 case ISD::SETCC:
2326 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002327 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002328 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002329 return VTBits;
2330 break;
2331 case ISD::ROTL:
2332 case ISD::ROTR:
2333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002334 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002335
Dan Gohman309d3d52007-06-22 14:59:07 +00002336 // Handle rotate right by N like a rotate left by 32-N.
2337 if (Op.getOpcode() == ISD::ROTR)
2338 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2339
2340 // If we aren't rotating out all of the known-in sign bits, return the
2341 // number that are left. This handles rotl(sext(x), 1) for example.
2342 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2343 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2344 }
2345 break;
2346 case ISD::ADD:
2347 // Add can have at most one carry bit. Thus we know that the output
2348 // is, at worst, one more bit than the inputs.
2349 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2350 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002351
Dan Gohman309d3d52007-06-22 14:59:07 +00002352 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002353 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002354 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002355 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002356 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002357
Dan Gohman309d3d52007-06-22 14:59:07 +00002358 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2359 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002360 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002361 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002362
Dan Gohman309d3d52007-06-22 14:59:07 +00002363 // If we are subtracting one from a positive number, there is no carry
2364 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002365 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002366 return Tmp;
2367 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002368
Dan Gohman309d3d52007-06-22 14:59:07 +00002369 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2370 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002371 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002372
Dan Gohman309d3d52007-06-22 14:59:07 +00002373 case ISD::SUB:
2374 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2375 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002376
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 // Handle NEG.
2378 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002379 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002380 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002381 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002382 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2383 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002384 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002385 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002386
Dan Gohman309d3d52007-06-22 14:59:07 +00002387 // If the input is known to be positive (the sign bit is known clear),
2388 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002389 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002390 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002391
Dan Gohman309d3d52007-06-22 14:59:07 +00002392 // Otherwise, we treat this like a SUB.
2393 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002394
Dan Gohman309d3d52007-06-22 14:59:07 +00002395 // Sub can have at most one carry bit. Thus we know that the output
2396 // is, at worst, one more bit than the inputs.
2397 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2398 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002399 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002400 case ISD::TRUNCATE:
2401 // FIXME: it's tricky to do anything useful for this, but it is an important
2402 // case for targets like X86.
2403 break;
2404 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002405
Nadav Rotem4536d582013-03-20 22:53:44 +00002406 // If we are looking at the loaded value of the SDNode.
2407 if (Op.getResNo() == 0) {
2408 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2409 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2410 unsigned ExtType = LD->getExtensionType();
2411 switch (ExtType) {
2412 default: break;
2413 case ISD::SEXTLOAD: // '17' bits known
2414 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2415 return VTBits-Tmp+1;
2416 case ISD::ZEXTLOAD: // '16' bits known
2417 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2418 return VTBits-Tmp;
2419 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002420 }
2421 }
2422
2423 // Allow the target to implement this method for its nodes.
2424 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002425 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2427 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002428 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002429 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002430 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002431
Dan Gohman309d3d52007-06-22 14:59:07 +00002432 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2433 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002434 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002435 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002436
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002437 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002438 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002439 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002440 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002441 Mask = KnownOne;
2442 } else {
2443 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002444 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002445 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002446
Dan Gohman309d3d52007-06-22 14:59:07 +00002447 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2448 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002449 Mask = ~Mask;
2450 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002451 // Return # leading zeros. We use 'min' here in case Val was zero before
2452 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002453 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002454}
2455
Chris Lattner46c01a32011-02-13 22:25:43 +00002456/// isBaseWithConstantOffset - Return true if the specified operand is an
2457/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2458/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2459/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002460/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002461bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2462 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2463 !isa<ConstantSDNode>(Op.getOperand(1)))
2464 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002465
2466 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002467 !MaskedValueIsZero(Op.getOperand(0),
2468 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2469 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002470
Chris Lattner46c01a32011-02-13 22:25:43 +00002471 return true;
2472}
2473
2474
Dan Gohmand0d5e682009-09-03 20:34:31 +00002475bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2476 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002477 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002478 return true;
2479
2480 // If the value is a constant, we can obviously see if it is a NaN or not.
2481 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2482 return !C->getValueAPF().isNaN();
2483
2484 // TODO: Recognize more cases here.
2485
2486 return false;
2487}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002488
Dan Gohman38605212010-02-24 06:52:40 +00002489bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2490 // If the value is a constant, we can obviously see if it is a zero or not.
2491 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2492 return !C->isZero();
2493
2494 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002495 switch (Op.getOpcode()) {
2496 default: break;
2497 case ISD::OR:
2498 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2499 return !C->isNullValue();
2500 break;
2501 }
Dan Gohman38605212010-02-24 06:52:40 +00002502
2503 return false;
2504}
2505
2506bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2507 // Check the obvious case.
2508 if (A == B) return true;
2509
2510 // For for negative and positive zero.
2511 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2512 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2513 if (CA->isZero() && CB->isZero()) return true;
2514
2515 // Otherwise they may not be equal.
2516 return false;
2517}
2518
Chris Lattner061a1ea2005-01-07 07:46:32 +00002519/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002520///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002521SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002522 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002523 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002524 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002525 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002526 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002527
Jack Carter170a5f22013-09-09 22:02:08 +00002528 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2529 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002530 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002531
Chris Lattnerfcb16472006-08-11 18:38:11 +00002532 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002533#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002534 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002535#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002536 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002537}
2538
Andrew Trickef9de2a2013-05-25 02:42:55 +00002539SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002540 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002541 // Constant fold unary operations with an integer constant operand. Even
2542 // opaque constant will be folded, because the folding of unary operations
2543 // doesn't create new constants with different values. Nevertheless, the
2544 // opaque flag is preserved during folding to prevent future folding with
2545 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002546 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002547 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002548 switch (Opcode) {
2549 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002550 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002551 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2552 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002553 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002554 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002555 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002556 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2557 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002558 case ISD::UINT_TO_FP:
2559 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002560 APFloat apf(EVTToAPFloatSemantics(VT),
2561 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002562 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002563 Opcode==ISD::SINT_TO_FP,
2564 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002565 return getConstantFP(apf, VT);
2566 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002567 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002568 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002569 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002570 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002571 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002572 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002573 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002574 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2575 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002576 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002577 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2578 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002579 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002580 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002581 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2582 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002583 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002584 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002585 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2586 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002587 }
2588 }
2589
Dale Johannesen446b9002007-08-31 23:34:27 +00002590 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002591 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002592 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002593 switch (Opcode) {
2594 case ISD::FNEG:
2595 V.changeSign();
2596 return getConstantFP(V, VT);
2597 case ISD::FABS:
2598 V.clearSign();
2599 return getConstantFP(V, VT);
2600 case ISD::FCEIL: {
2601 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2602 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002603 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002604 break;
2605 }
2606 case ISD::FTRUNC: {
2607 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2608 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002609 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002610 break;
2611 }
2612 case ISD::FFLOOR: {
2613 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2614 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002615 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002616 break;
2617 }
2618 case ISD::FP_EXTEND: {
2619 bool ignored;
2620 // This can return overflow, underflow, or inexact; we don't care.
2621 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002622 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002623 APFloat::rmNearestTiesToEven, &ignored);
2624 return getConstantFP(V, VT);
2625 }
2626 case ISD::FP_TO_SINT:
2627 case ISD::FP_TO_UINT: {
2628 integerPart x[2];
2629 bool ignored;
2630 assert(integerPartWidth >= 64);
2631 // FIXME need to be more flexible about rounding mode.
2632 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2633 Opcode==ISD::FP_TO_SINT,
2634 APFloat::rmTowardZero, &ignored);
2635 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002636 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002637 APInt api(VT.getSizeInBits(), x);
2638 return getConstant(api, VT);
2639 }
2640 case ISD::BITCAST:
2641 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2642 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2643 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2644 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2645 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002646 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002647 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002648
Gabor Greiff304a7a2008-08-28 21:40:38 +00002649 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002650 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002651 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002652 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002653 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002654 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002655 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002656 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002657 assert(VT.isFloatingPoint() &&
2658 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002659 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002660 assert((!VT.isVector() ||
2661 VT.getVectorNumElements() ==
2662 Operand.getValueType().getVectorNumElements()) &&
2663 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002664 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002665 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002666 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002667 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002668 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002669 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002670 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002671 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2672 "Invalid sext node, dst < src!");
2673 assert((!VT.isVector() ||
2674 VT.getVectorNumElements() ==
2675 Operand.getValueType().getVectorNumElements()) &&
2676 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002677 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2678 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2679 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002680 // sext(undef) = 0, because the top bits will all be the same.
2681 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002682 break;
2683 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002684 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002685 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002686 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002687 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2688 "Invalid zext node, dst < src!");
2689 assert((!VT.isVector() ||
2690 VT.getVectorNumElements() ==
2691 Operand.getValueType().getVectorNumElements()) &&
2692 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002693 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002694 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002695 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002696 else if (OpOpcode == ISD::UNDEF)
2697 // zext(undef) = 0, because the top bits will be zero.
2698 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002699 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002700 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002701 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002702 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002703 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002704 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2705 "Invalid anyext node, dst < src!");
2706 assert((!VT.isVector() ||
2707 VT.getVectorNumElements() ==
2708 Operand.getValueType().getVectorNumElements()) &&
2709 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002710
Dan Gohman08837892010-06-18 00:08:30 +00002711 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2712 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002713 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002714 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002715 else if (OpOpcode == ISD::UNDEF)
2716 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002717
2718 // (ext (trunx x)) -> x
2719 if (OpOpcode == ISD::TRUNCATE) {
2720 SDValue OpOp = Operand.getNode()->getOperand(0);
2721 if (OpOp.getValueType() == VT)
2722 return OpOp;
2723 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002724 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002725 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002726 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002727 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002728 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002729 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2730 "Invalid truncate node, src < dst!");
2731 assert((!VT.isVector() ||
2732 VT.getVectorNumElements() ==
2733 Operand.getValueType().getVectorNumElements()) &&
2734 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002735 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002736 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002737 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2738 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002739 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002740 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2741 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002742 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002743 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002744 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002745 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002746 }
Craig Topper201c1a32012-01-15 01:05:11 +00002747 if (OpOpcode == ISD::UNDEF)
2748 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002749 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002750 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002751 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002752 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002753 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002754 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002755 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2756 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002757 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002758 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002759 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002760 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002761 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002762 (VT.getVectorElementType() == Operand.getValueType() ||
2763 (VT.getVectorElementType().isInteger() &&
2764 Operand.getValueType().isInteger() &&
2765 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002766 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002767 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002768 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002769 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2770 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2771 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2772 Operand.getConstantOperandVal(1) == 0 &&
2773 Operand.getOperand(0).getValueType() == VT)
2774 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002775 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002776 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002777 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002778 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002779 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002780 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002781 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002782 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002783 break;
2784 case ISD::FABS:
2785 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002786 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002787 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002788 }
2789
Chris Lattnerf9c19152005-08-25 19:12:10 +00002790 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002791 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002792 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002793 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002794 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002795 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002796 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002797 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002798 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002799
Jack Carter170a5f22013-09-09 22:02:08 +00002800 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2801 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002802 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002803 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002804 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2805 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002806 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002807
Chris Lattner061a1ea2005-01-07 07:46:32 +00002808 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002809#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002810 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002811#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002812 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002813}
2814
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002815SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2816 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002817 // If the opcode is a target-specific ISD node, there's nothing we can
2818 // do here and the operand rules may not line up with the below, so
2819 // bail early.
2820 if (Opcode >= ISD::BUILTIN_OP_END)
2821 return SDValue();
2822
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002823 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2824 SmallVector<SDValue, 4> Outputs;
2825 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002826
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002827 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2828 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002829 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2830 return SDValue();
2831
2832 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002833 // Scalar instruction.
2834 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002835 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002836 // For vectors extract each constant element into Inputs so we can constant
2837 // fold them individually.
2838 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2839 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2840 if (!BV1 || !BV2)
2841 return SDValue();
2842
2843 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2844
2845 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2846 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2847 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2848 if (!V1 || !V2) // Not a constant, bail.
2849 return SDValue();
2850
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002851 if (V1->isOpaque() || V2->isOpaque())
2852 return SDValue();
2853
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002854 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2855 // FIXME: This is valid and could be handled by truncating the APInts.
2856 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2857 return SDValue();
2858
2859 Inputs.push_back(std::make_pair(V1, V2));
2860 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002861 }
2862
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002863 // We have a number of constant values, constant fold them element by element.
2864 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2865 const APInt &C1 = Inputs[I].first->getAPIntValue();
2866 const APInt &C2 = Inputs[I].second->getAPIntValue();
2867
2868 switch (Opcode) {
2869 case ISD::ADD:
2870 Outputs.push_back(getConstant(C1 + C2, SVT));
2871 break;
2872 case ISD::SUB:
2873 Outputs.push_back(getConstant(C1 - C2, SVT));
2874 break;
2875 case ISD::MUL:
2876 Outputs.push_back(getConstant(C1 * C2, SVT));
2877 break;
2878 case ISD::UDIV:
2879 if (!C2.getBoolValue())
2880 return SDValue();
2881 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2882 break;
2883 case ISD::UREM:
2884 if (!C2.getBoolValue())
2885 return SDValue();
2886 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2887 break;
2888 case ISD::SDIV:
2889 if (!C2.getBoolValue())
2890 return SDValue();
2891 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2892 break;
2893 case ISD::SREM:
2894 if (!C2.getBoolValue())
2895 return SDValue();
2896 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2897 break;
2898 case ISD::AND:
2899 Outputs.push_back(getConstant(C1 & C2, SVT));
2900 break;
2901 case ISD::OR:
2902 Outputs.push_back(getConstant(C1 | C2, SVT));
2903 break;
2904 case ISD::XOR:
2905 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2906 break;
2907 case ISD::SHL:
2908 Outputs.push_back(getConstant(C1 << C2, SVT));
2909 break;
2910 case ISD::SRL:
2911 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2912 break;
2913 case ISD::SRA:
2914 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2915 break;
2916 case ISD::ROTL:
2917 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2918 break;
2919 case ISD::ROTR:
2920 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2921 break;
2922 default:
2923 return SDValue();
2924 }
2925 }
2926
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00002927 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
2928 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002929
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002930 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002931 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002932 return Outputs.back();
2933
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002934 // We may have a vector type but a scalar result. Create a splat.
2935 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
2936
2937 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00002938 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00002939}
2940
Andrew Trickef9de2a2013-05-25 02:42:55 +00002941SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002942 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002943 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2944 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002945 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002946 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002947 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002948 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2949 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002950 // Fold trivial token factors.
2951 if (N1.getOpcode() == ISD::EntryToken) return N2;
2952 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002953 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002954 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002955 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002956 // Concat of UNDEFs is UNDEF.
2957 if (N1.getOpcode() == ISD::UNDEF &&
2958 N2.getOpcode() == ISD::UNDEF)
2959 return getUNDEF(VT);
2960
Dan Gohman550c9af2008-08-14 20:04:46 +00002961 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2962 // one big BUILD_VECTOR.
2963 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2964 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002965 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2966 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002967 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00002968 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00002969 }
2970 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002971 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002972 assert(VT.isInteger() && "This operator does not apply to FP types!");
2973 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002974 N1.getValueType() == VT && "Binary operator types must match!");
2975 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2976 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002977 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002978 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002979 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2980 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002981 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002982 case ISD::OR:
2983 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002984 case ISD::ADD:
2985 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002986 assert(VT.isInteger() && "This operator does not apply to FP types!");
2987 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002988 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002989 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2990 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002991 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002992 return N1;
2993 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002994 case ISD::UDIV:
2995 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00002996 case ISD::MULHU:
2997 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002998 case ISD::MUL:
2999 case ISD::SDIV:
3000 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003001 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003002 assert(N1.getValueType() == N2.getValueType() &&
3003 N1.getValueType() == VT && "Binary operator types must match!");
3004 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003005 case ISD::FADD:
3006 case ISD::FSUB:
3007 case ISD::FMUL:
3008 case ISD::FDIV:
3009 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003010 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003011 if (Opcode == ISD::FADD) {
3012 // 0+x --> x
3013 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3014 if (CFP->getValueAPF().isZero())
3015 return N2;
3016 // x+0 --> x
3017 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3018 if (CFP->getValueAPF().isZero())
3019 return N1;
3020 } else if (Opcode == ISD::FSUB) {
3021 // x-0 --> x
3022 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3023 if (CFP->getValueAPF().isZero())
3024 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003025 } else if (Opcode == ISD::FMUL) {
3026 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3027 SDValue V = N2;
3028
3029 // If the first operand isn't the constant, try the second
3030 if (!CFP) {
3031 CFP = dyn_cast<ConstantFPSDNode>(N2);
3032 V = N1;
3033 }
3034
3035 if (CFP) {
3036 // 0*x --> 0
3037 if (CFP->isZero())
3038 return SDValue(CFP,0);
3039 // 1*x --> x
3040 if (CFP->isExactlyValue(1.0))
3041 return V;
3042 }
Dan Gohman1275e282009-01-23 19:10:37 +00003043 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003044 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003045 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003046 assert(N1.getValueType() == N2.getValueType() &&
3047 N1.getValueType() == VT && "Binary operator types must match!");
3048 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003049 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3050 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003051 N1.getValueType().isFloatingPoint() &&
3052 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003053 "Invalid FCOPYSIGN!");
3054 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003055 case ISD::SHL:
3056 case ISD::SRA:
3057 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003058 case ISD::ROTL:
3059 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003060 assert(VT == N1.getValueType() &&
3061 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003062 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003063 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003064 assert((!VT.isVector() || VT == N2.getValueType()) &&
3065 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003066 // Verify that the shift amount VT is bit enough to hold valid shift
3067 // amounts. This catches things like trying to shift an i1024 value by an
3068 // i8, which is easy to fall into in generic code that uses
3069 // TLI.getShiftAmount().
3070 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003071 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003072 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003073
3074 // Always fold shifts of i1 values so the code generator doesn't need to
3075 // handle them. Since we know the size of the shift has to be less than the
3076 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003077 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003078 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003079 if (N2C && N2C->isNullValue())
3080 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003081 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003082 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003083 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003084 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003085 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003086 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003087 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003088 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003089 "type is vector!");
3090 assert((!EVT.isVector() ||
3091 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3092 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003093 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003094 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003095 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003096 break;
3097 }
Chris Lattner72733e52008-01-17 07:00:52 +00003098 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003099 assert(VT.isFloatingPoint() &&
3100 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003101 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003102 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003103 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003104 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003105 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003106 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003107 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003108 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003109 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003110 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003111 assert(!EVT.isVector() &&
3112 "AssertSExt/AssertZExt type should be the vector element type "
3113 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003114 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003115 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003116 break;
3117 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003118 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003119 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003120 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003121 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003122 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003123 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003124 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003125 "type is vector!");
3126 assert((!EVT.isVector() ||
3127 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3128 "Vector element counts must match in SIGN_EXTEND_INREG");
3129 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003130 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003131
Chris Lattner16713612008-01-22 19:09:33 +00003132 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003133 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003134 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003135 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003136 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003137 return getConstant(Val, VT);
3138 }
Chris Lattner16713612008-01-22 19:09:33 +00003139 break;
3140 }
3141 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003142 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3143 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003144 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003145
Chris Lattner16713612008-01-22 19:09:33 +00003146 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3147 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003148 if (N2C &&
3149 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003150 N1.getNumOperands() > 0) {
3151 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003152 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003153 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003154 N1.getOperand(N2C->getZExtValue() / Factor),
3155 getConstant(N2C->getZExtValue() % Factor,
3156 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003157 }
3158
3159 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3160 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003161 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3162 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003163
3164 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003165 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003166 // are promoted and implicitly truncated, and the result implicitly
3167 // extended. Make that explicit here.
3168 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003169
Bob Wilson59dbbb22009-04-13 22:05:19 +00003170 return Elt;
3171 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003172
Chris Lattner16713612008-01-22 19:09:33 +00003173 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3174 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003175 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003176 // If the indices are the same, return the inserted element else
3177 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003178 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003179 SDValue N1Op2 = N1.getOperand(2);
3180 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3181
3182 if (N1Op2C && N2C) {
3183 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3184 if (VT == N1.getOperand(1).getValueType())
3185 return N1.getOperand(1);
3186 else
3187 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3188 }
3189
Dale Johannesendb393622009-02-03 01:55:44 +00003190 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003191 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003192 }
Chris Lattner16713612008-01-22 19:09:33 +00003193 break;
3194 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003195 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003196 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3197 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003198 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003199 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003200
Chris Lattner16713612008-01-22 19:09:33 +00003201 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3202 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003203 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003204 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003205 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003206
Chris Lattner16713612008-01-22 19:09:33 +00003207 // EXTRACT_ELEMENT of a constant int is also very common.
3208 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003209 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003210 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003211 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3212 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003213 }
3214 break;
David Greeneb6f16112011-01-26 15:38:49 +00003215 case ISD::EXTRACT_SUBVECTOR: {
3216 SDValue Index = N2;
3217 if (VT.isSimple() && N1.getValueType().isSimple()) {
3218 assert(VT.isVector() && N1.getValueType().isVector() &&
3219 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003220 assert(VT.getVectorElementType() ==
3221 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003222 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003223 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003224 "Extract subvector must be from larger vector to smaller vector!");
3225
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003226 if (isa<ConstantSDNode>(Index.getNode())) {
3227 assert((VT.getVectorNumElements() +
3228 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003229 <= N1.getValueType().getVectorNumElements())
3230 && "Extract subvector overflow!");
3231 }
3232
3233 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003234 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003235 return N1;
3236 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003237 break;
Chris Lattner16713612008-01-22 19:09:33 +00003238 }
David Greeneb6f16112011-01-26 15:38:49 +00003239 }
Chris Lattner16713612008-01-22 19:09:33 +00003240
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003241 // Perform trivial constant folding.
3242 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3243 if (SV.getNode()) return SV;
3244
3245 // Canonicalize constant to RHS if commutative.
3246 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3247 std::swap(N1C, N2C);
3248 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003249 }
3250
Chris Lattner16713612008-01-22 19:09:33 +00003251 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003252 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3253 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003254 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003255 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003256 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003257 std::swap(N1CFP, N2CFP);
3258 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003259 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003260 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3261 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003262 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003263 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003264 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003265 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003266 return getConstantFP(V1, VT);
3267 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003268 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003269 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3270 if (s!=APFloat::opInvalidOp)
3271 return getConstantFP(V1, VT);
3272 break;
3273 case ISD::FMUL:
3274 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3275 if (s!=APFloat::opInvalidOp)
3276 return getConstantFP(V1, VT);
3277 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003278 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003279 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3280 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3281 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003282 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003283 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003284 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3285 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3286 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003287 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003288 case ISD::FCOPYSIGN:
3289 V1.copySign(V2);
3290 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003291 default: break;
3292 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003293 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003294
3295 if (Opcode == ISD::FP_ROUND) {
3296 APFloat V = N1CFP->getValueAPF(); // make copy
3297 bool ignored;
3298 // This can return overflow, underflow, or inexact; we don't care.
3299 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003300 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003301 APFloat::rmNearestTiesToEven, &ignored);
3302 return getConstantFP(V, VT);
3303 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003304 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003305
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003306 // Canonicalize an UNDEF to the RHS, even over a constant.
3307 if (N1.getOpcode() == ISD::UNDEF) {
3308 if (isCommutativeBinOp(Opcode)) {
3309 std::swap(N1, N2);
3310 } else {
3311 switch (Opcode) {
3312 case ISD::FP_ROUND_INREG:
3313 case ISD::SIGN_EXTEND_INREG:
3314 case ISD::SUB:
3315 case ISD::FSUB:
3316 case ISD::FDIV:
3317 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003318 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003319 return N1; // fold op(undef, arg2) -> undef
3320 case ISD::UDIV:
3321 case ISD::SDIV:
3322 case ISD::UREM:
3323 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003324 case ISD::SRL:
3325 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003326 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003327 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3328 // For vectors, we can't easily build an all zero vector, just return
3329 // the LHS.
3330 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003331 }
3332 }
3333 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003334
3335 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003336 if (N2.getOpcode() == ISD::UNDEF) {
3337 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003338 case ISD::XOR:
3339 if (N1.getOpcode() == ISD::UNDEF)
3340 // Handle undef ^ undef -> 0 special case. This is a common
3341 // idiom (misuse).
3342 return getConstant(0, VT);
3343 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003344 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003345 case ISD::ADDC:
3346 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003347 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003348 case ISD::UDIV:
3349 case ISD::SDIV:
3350 case ISD::UREM:
3351 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003352 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003353 case ISD::FADD:
3354 case ISD::FSUB:
3355 case ISD::FMUL:
3356 case ISD::FDIV:
3357 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003358 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003359 return N2;
3360 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003361 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003362 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003363 case ISD::SRL:
3364 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003365 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003366 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3367 // For vectors, we can't easily build an all zero vector, just return
3368 // the LHS.
3369 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003370 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003371 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003372 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003373 // For vectors, we can't easily build an all one vector, just return
3374 // the LHS.
3375 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003376 case ISD::SRA:
3377 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003378 }
3379 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003380
Chris Lattner724f7ee2005-05-11 18:57:39 +00003381 // Memoize this node if possible.
3382 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003383 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003384 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003385 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003386 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003387 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003388 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003390 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003391
Jack Carter170a5f22013-09-09 22:02:08 +00003392 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3393 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003394 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003395 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003396 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3397 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003398 }
3399
Chris Lattner061a1ea2005-01-07 07:46:32 +00003400 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003401#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003402 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003403#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003404 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003405}
3406
Andrew Trickef9de2a2013-05-25 02:42:55 +00003407SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003408 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003409 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003411 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003412 case ISD::FMA: {
3413 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3414 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3415 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3416 if (N1CFP && N2CFP && N3CFP) {
3417 APFloat V1 = N1CFP->getValueAPF();
3418 const APFloat &V2 = N2CFP->getValueAPF();
3419 const APFloat &V3 = N3CFP->getValueAPF();
3420 APFloat::opStatus s =
3421 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3422 if (s != APFloat::opInvalidOp)
3423 return getConstantFP(V1, VT);
3424 }
3425 break;
3426 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003427 case ISD::CONCAT_VECTORS:
3428 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3429 // one big BUILD_VECTOR.
3430 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3431 N2.getOpcode() == ISD::BUILD_VECTOR &&
3432 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003433 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3434 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003435 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3436 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003437 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003438 }
3439 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003440 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003441 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003442 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003443 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003444 break;
3445 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003446 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003447 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003448 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003449 return N2; // select true, X, Y -> X
3450 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003451 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003452
3453 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003454 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003455 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003456 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003457 case ISD::INSERT_SUBVECTOR: {
3458 SDValue Index = N3;
3459 if (VT.isSimple() && N1.getValueType().isSimple()
3460 && N2.getValueType().isSimple()) {
3461 assert(VT.isVector() && N1.getValueType().isVector() &&
3462 N2.getValueType().isVector() &&
3463 "Insert subvector VTs must be a vectors");
3464 assert(VT == N1.getValueType() &&
3465 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003466 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003467 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003468 if (isa<ConstantSDNode>(Index.getNode())) {
3469 assert((N2.getValueType().getVectorNumElements() +
3470 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003471 <= VT.getVectorNumElements())
3472 && "Insert subvector overflow!");
3473 }
3474
3475 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003476 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003477 return N2;
3478 }
3479 break;
3480 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003481 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003482 // Fold bit_convert nodes from a type to themselves.
3483 if (N1.getValueType() == VT)
3484 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003485 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003486 }
3487
Chris Lattnerf9c19152005-08-25 19:12:10 +00003488 // Memoize node if it doesn't produce a flag.
3489 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003490 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003491 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003492 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003493 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003494 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003495 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003496 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003497 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003498
Jack Carter170a5f22013-09-09 22:02:08 +00003499 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3500 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003501 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003502 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003503 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3504 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003505 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003506
Chris Lattner061a1ea2005-01-07 07:46:32 +00003507 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003508#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003509 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003510#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003511 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003512}
3513
Andrew Trickef9de2a2013-05-25 02:42:55 +00003514SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003515 SDValue N1, SDValue N2, SDValue N3,
3516 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003517 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003518 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003519}
3520
Andrew Trickef9de2a2013-05-25 02:42:55 +00003521SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003522 SDValue N1, SDValue N2, SDValue N3,
3523 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003524 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003525 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003526}
3527
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003528/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3529/// the incoming stack arguments to be loaded from the stack.
3530SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3531 SmallVector<SDValue, 8> ArgChains;
3532
3533 // Include the original chain at the beginning of the list. When this is
3534 // used by target LowerCall hooks, this helps legalize find the
3535 // CALLSEQ_BEGIN node.
3536 ArgChains.push_back(Chain);
3537
3538 // Add a chain value for each stack argument.
3539 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3540 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3541 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3542 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3543 if (FI->getIndex() < 0)
3544 ArgChains.push_back(SDValue(L, 1));
3545
3546 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003547 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003548}
3549
Dan Gohman544ab2c2008-04-12 04:36:06 +00003550/// getMemsetValue - Vectorized representation of the memset value
3551/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003552static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003553 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003554 assert(Value.getOpcode() != ISD::UNDEF);
3555
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003556 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003558 assert(C->getAPIntValue().getBitWidth() == 8);
3559 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003560 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003561 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003562 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003563 }
Evan Chengef377ad2008-05-15 08:39:06 +00003564
Dale Johannesenabf66b82009-02-03 22:26:09 +00003565 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003566 if (NumBits > 8) {
3567 // Use a multiplication with 0x010101... to extend the input to the
3568 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003569 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003570 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003571 }
3572
3573 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003574}
3575
Dan Gohman544ab2c2008-04-12 04:36:06 +00003576/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3577/// used when a memcpy is turned into a memset when the source is a constant
3578/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003579static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003580 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003581 // Handle vector with all elements zero.
3582 if (Str.empty()) {
3583 if (VT.isInteger())
3584 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003585 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003586 return DAG.getConstantFP(0.0, VT);
3587 else if (VT.isVector()) {
3588 unsigned NumElts = VT.getVectorNumElements();
3589 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003590 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003591 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3592 EltVT, NumElts)));
3593 } else
3594 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003595 }
3596
Duncan Sands13237ac2008-06-06 12:08:01 +00003597 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003598 unsigned NumVTBits = VT.getSizeInBits();
3599 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003600 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3601
Evan Chengc8444b12013-01-10 22:13:27 +00003602 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003603 if (TLI.isLittleEndian()) {
3604 for (unsigned i = 0; i != NumBytes; ++i)
3605 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3606 } else {
3607 for (unsigned i = 0; i != NumBytes; ++i)
3608 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003609 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003610
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003611 // If the "cost" of materializing the integer immediate is less than the cost
3612 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003613 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3614 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003615 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003616 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003617}
3618
Scott Michelcf0da6c2009-02-17 22:15:04 +00003619/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003620///
Andrew Tricke2431c62013-05-25 03:08:10 +00003621static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003622 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003623 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003624 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003625 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003626}
3627
Evan Chengef377ad2008-05-15 08:39:06 +00003628/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3629///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003630static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003631 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003632 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003633 if (Src.getOpcode() == ISD::GlobalAddress)
3634 G = cast<GlobalAddressSDNode>(Src);
3635 else if (Src.getOpcode() == ISD::ADD &&
3636 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3637 Src.getOperand(1).getOpcode() == ISD::Constant) {
3638 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003639 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003640 }
3641 if (!G)
3642 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003643
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003644 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003645}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003646
Evan Cheng43cd9e32010-04-01 06:04:33 +00003647/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3648/// to replace the memset / memcpy. Return true if the number of memory ops
3649/// is below the threshold. It returns the types of the sequence of
3650/// memory ops to perform memset / memcpy by reference.
3651static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003652 unsigned Limit, uint64_t Size,
3653 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003654 bool IsMemset,
3655 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003656 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003657 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003658 SelectionDAG &DAG,
3659 const TargetLowering &TLI) {
3660 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3661 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003662 // If 'SrcAlign' is zero, that means the memory operation does not need to
3663 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3664 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3665 // is the specified alignment of the memory operation. If it is zero, that
3666 // means it's possible to change the alignment of the destination.
3667 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3668 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003669 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003670 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003671 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003672
Chris Lattner28dc6c12010-03-07 07:45:08 +00003673 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003674 unsigned AS = 0;
3675 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
3676 TLI.allowsUnalignedMemoryAccesses(VT, AS)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003677 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003678 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003679 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003680 case 0: VT = MVT::i64; break;
3681 case 4: VT = MVT::i32; break;
3682 case 2: VT = MVT::i16; break;
3683 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003684 }
3685 }
3686
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003687 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003688 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003689 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003690 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003691
Duncan Sands11dd4242008-06-08 20:54:56 +00003692 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003693 VT = LVT;
3694 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003695
Dan Gohman544ab2c2008-04-12 04:36:06 +00003696 unsigned NumMemOps = 0;
3697 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003698 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003699 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003700 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003701 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003702 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003703
3704 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003705 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003706 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003707 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003708 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003709 Found = true;
3710 else if (NewVT == MVT::i64 &&
3711 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003712 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003713 // i64 is usually not legal on 32-bit targets, but f64 may be.
3714 NewVT = MVT::f64;
3715 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003716 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003717 }
3718
Evan Cheng04e55182012-12-12 00:42:09 +00003719 if (!Found) {
3720 do {
3721 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3722 if (NewVT == MVT::i8)
3723 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003724 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003725 }
3726 NewVTSize = NewVT.getSizeInBits() / 8;
3727
Evan Cheng79e2ca92012-12-10 23:21:26 +00003728 // If the new VT cannot cover all of the remaining bits, then consider
3729 // issuing a (or a pair of) unaligned and overlapping load / store.
3730 // FIXME: Only does this for 64-bit or more since we don't have proper
3731 // cost model for unaligned load / store.
3732 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003733 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003734 if (NumMemOps && AllowOverlap &&
3735 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003736 TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003737 VTSize = Size;
3738 else {
3739 VT = NewVT;
3740 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003741 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003742 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003743
Evan Chengb7d3d032012-12-12 20:43:23 +00003744 if (++NumMemOps > Limit)
3745 return false;
3746
Dan Gohman544ab2c2008-04-12 04:36:06 +00003747 MemOps.push_back(VT);
3748 Size -= VTSize;
3749 }
3750
3751 return true;
3752}
3753
Andrew Trickef9de2a2013-05-25 02:42:55 +00003754static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003755 SDValue Chain, SDValue Dst,
3756 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003757 unsigned Align, bool isVol,
3758 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003759 MachinePointerInfo DstPtrInfo,
3760 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003761 // Turn a memcpy of undef to nop.
3762 if (Src.getOpcode() == ISD::UNDEF)
3763 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003764
Dan Gohman714663a2008-05-29 19:42:22 +00003765 // Expand memcpy to a series of load and store ops if the size operand falls
3766 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003767 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3768 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003769 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003770 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003771 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003772 MachineFunction &MF = DAG.getMachineFunction();
3773 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003774 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003775 MF.getFunction()->getAttributes().
3776 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003777 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3778 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3779 DstAlignCanChange = true;
3780 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3781 if (Align > SrcAlign)
3782 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003783 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003784 bool CopyFromStr = isMemSrcFromString(Src, Str);
3785 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003786 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003787
Evan Cheng4c014c82010-04-01 18:19:11 +00003788 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003789 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003790 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003791 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003792 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003793
Evan Cheng43cd9e32010-04-01 06:04:33 +00003794 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003795 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003796 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003797
3798 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003799 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003800 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3801 if (!TRI->needsStackRealignment(MF))
3802 while (NewAlign > Align &&
3803 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3804 NewAlign /= 2;
3805
Evan Cheng43cd9e32010-04-01 06:04:33 +00003806 if (NewAlign > Align) {
3807 // Give the stack frame object a larger alignment if needed.
3808 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3809 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3810 Align = NewAlign;
3811 }
3812 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003813
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003814 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003815 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003816 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003817 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003818 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003819 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003820 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003821
Evan Cheng79e2ca92012-12-10 23:21:26 +00003822 if (VTSize > Size) {
3823 // Issuing an unaligned load / store pair that overlaps with the previous
3824 // pair. Adjust the offset accordingly.
3825 assert(i == NumMemOps-1 && i != 0);
3826 SrcOff -= VTSize - Size;
3827 DstOff -= VTSize - Size;
3828 }
3829
Evan Cheng43cd9e32010-04-01 06:04:33 +00003830 if (CopyFromStr &&
3831 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003832 // It's unlikely a store of a vector immediate can be done in a single
3833 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003834 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003835 // FIXME: Handle other cases where store of vector immediate is done in
3836 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003837 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003838 if (Value.getNode())
3839 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003840 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003841 DstPtrInfo.getWithOffset(DstOff), isVol,
3842 false, Align);
3843 }
3844
3845 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003846 // The type might not be legal for the target. This should only happen
3847 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003848 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3849 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003850 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003851 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003852 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003853 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003854 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003855 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003856 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003857 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003858 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003859 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3860 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003861 }
3862 OutChains.push_back(Store);
3863 SrcOff += VTSize;
3864 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003865 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003866 }
3867
Craig Topper48d114b2014-04-26 18:35:24 +00003868 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003869}
3870
Andrew Trickef9de2a2013-05-25 02:42:55 +00003871static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003872 SDValue Chain, SDValue Dst,
3873 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003874 unsigned Align, bool isVol,
3875 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003876 MachinePointerInfo DstPtrInfo,
3877 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003878 // Turn a memmove of undef to nop.
3879 if (Src.getOpcode() == ISD::UNDEF)
3880 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003881
3882 // Expand memmove to a series of load and store ops if the size operand falls
3883 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003884 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003885 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003886 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003887 MachineFunction &MF = DAG.getMachineFunction();
3888 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003889 bool OptSize = MF.getFunction()->getAttributes().
3890 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003891 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3892 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3893 DstAlignCanChange = true;
3894 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3895 if (Align > SrcAlign)
3896 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003897 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003898
Evan Cheng4c014c82010-04-01 18:19:11 +00003899 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003900 (DstAlignCanChange ? 0 : Align), SrcAlign,
3901 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003902 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003903
Evan Cheng43cd9e32010-04-01 06:04:33 +00003904 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003905 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003906 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003907 if (NewAlign > Align) {
3908 // Give the stack frame object a larger alignment if needed.
3909 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3910 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3911 Align = NewAlign;
3912 }
3913 }
Dan Gohman714663a2008-05-29 19:42:22 +00003914
Evan Cheng43cd9e32010-04-01 06:04:33 +00003915 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003916 SmallVector<SDValue, 8> LoadValues;
3917 SmallVector<SDValue, 8> LoadChains;
3918 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003919 unsigned NumMemOps = MemOps.size();
3920 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003921 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003922 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003923 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00003924
Dale Johannesenabf66b82009-02-03 22:26:09 +00003925 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003926 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003927 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003928 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003929 LoadValues.push_back(Value);
3930 LoadChains.push_back(Value.getValue(1));
3931 SrcOff += VTSize;
3932 }
Craig Topper48d114b2014-04-26 18:35:24 +00003933 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00003934 OutChains.clear();
3935 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003936 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003937 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003938 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003939
Dale Johannesenabf66b82009-02-03 22:26:09 +00003940 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003941 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003942 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003943 OutChains.push_back(Store);
3944 DstOff += VTSize;
3945 }
3946
Craig Topper48d114b2014-04-26 18:35:24 +00003947 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00003948}
3949
Serge Pavlov8ec39992013-09-17 16:24:42 +00003950/// \brief Lower the call to 'memset' intrinsic function into a series of store
3951/// operations.
3952///
3953/// \param DAG Selection DAG where lowered code is placed.
3954/// \param dl Link to corresponding IR location.
3955/// \param Chain Control flow dependency.
3956/// \param Dst Pointer to destination memory location.
3957/// \param Src Value of byte to write into the memory.
3958/// \param Size Number of bytes to write.
3959/// \param Align Alignment of the destination in bytes.
3960/// \param isVol True if destination is volatile.
3961/// \param DstPtrInfo IR information on the memory pointer.
3962/// \returns New head in the control flow, if lowering was successful, empty
3963/// SDValue otherwise.
3964///
3965/// The function tries to replace 'llvm.memset' intrinsic with several store
3966/// operations and value calculation code. This is usually profitable for small
3967/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003968static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003969 SDValue Chain, SDValue Dst,
3970 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003971 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003972 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003973 // Turn a memset of undef to nop.
3974 if (Src.getOpcode() == ISD::UNDEF)
3975 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003976
3977 // Expand memset to a series of load/store ops if the size operand
3978 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003979 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003980 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003981 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003982 MachineFunction &MF = DAG.getMachineFunction();
3983 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003984 bool OptSize = MF.getFunction()->getAttributes().
3985 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003986 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3987 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3988 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003989 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003990 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003991 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00003992 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00003993 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003994 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003995
Evan Cheng43cd9e32010-04-01 06:04:33 +00003996 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003997 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003998 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003999 if (NewAlign > Align) {
4000 // Give the stack frame object a larger alignment if needed.
4001 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4002 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4003 Align = NewAlign;
4004 }
4005 }
4006
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004007 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004008 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004009 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004010
4011 // Find the largest store and generate the bit pattern for it.
4012 EVT LargestVT = MemOps[0];
4013 for (unsigned i = 1; i < NumMemOps; i++)
4014 if (MemOps[i].bitsGT(LargestVT))
4015 LargestVT = MemOps[i];
4016 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4017
Dan Gohman544ab2c2008-04-12 04:36:06 +00004018 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004019 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004020 unsigned VTSize = VT.getSizeInBits() / 8;
4021 if (VTSize > Size) {
4022 // Issuing an unaligned load / store pair that overlaps with the previous
4023 // pair. Adjust the offset accordingly.
4024 assert(i == NumMemOps-1 && i != 0);
4025 DstOff -= VTSize - Size;
4026 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004027
4028 // If this store is smaller than the largest store see whether we can get
4029 // the smaller value for free with a truncate.
4030 SDValue Value = MemSetValue;
4031 if (VT.bitsLT(LargestVT)) {
4032 if (!LargestVT.isVector() && !VT.isVector() &&
4033 TLI.isTruncateFree(LargestVT, VT))
4034 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4035 else
4036 Value = getMemsetValue(Src, VT, DAG, dl);
4037 }
4038 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004039 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004040 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004041 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004042 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004043 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004044 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004045 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004046 }
4047
Craig Topper48d114b2014-04-26 18:35:24 +00004048 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004049}
4050
Andrew Trickef9de2a2013-05-25 02:42:55 +00004051SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004052 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004053 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004054 MachinePointerInfo DstPtrInfo,
4055 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004056 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004057
4058 // Check to see if we should lower the memcpy to loads and stores first.
4059 // For cases within the target-specified limits, this is the best choice.
4060 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4061 if (ConstantSize) {
4062 // Memcpy with size zero? Just return the original chain.
4063 if (ConstantSize->isNullValue())
4064 return Chain;
4065
Evan Cheng43cd9e32010-04-01 06:04:33 +00004066 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4067 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004068 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004069 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004070 return Result;
4071 }
4072
4073 // Then check to see if we should lower the memcpy with target-specific
4074 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004075 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004076 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004077 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004078 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004079 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004080 return Result;
4081
4082 // If we really need inline code and the target declined to provide it,
4083 // use a (potentially long) sequence of loads and stores.
4084 if (AlwaysInline) {
4085 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004086 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004087 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004088 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004089 }
4090
Dan Gohmanf38547c2010-04-05 20:24:08 +00004091 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4092 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4093 // respect volatile, so they may do things like read or write memory
4094 // beyond the given memory regions. But fixing this isn't easy, and most
4095 // people don't care.
4096
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004097 const TargetLowering *TLI = TM.getTargetLowering();
4098
Dan Gohman544ab2c2008-04-12 04:36:06 +00004099 // Emit a library call.
4100 TargetLowering::ArgListTy Args;
4101 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004102 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004103 Entry.Node = Dst; Args.push_back(Entry);
4104 Entry.Node = Src; Args.push_back(Entry);
4105 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004106 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004107 TargetLowering::
4108 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004109 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004110 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00004111 /*isTailCall=*/false,
4112 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004113 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4114 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004115 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004116 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004117
Dan Gohman544ab2c2008-04-12 04:36:06 +00004118 return CallResult.second;
4119}
4120
Andrew Trickef9de2a2013-05-25 02:42:55 +00004121SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004122 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004123 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004124 MachinePointerInfo DstPtrInfo,
4125 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004126 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004127
Dan Gohman714663a2008-05-29 19:42:22 +00004128 // Check to see if we should lower the memmove to loads and stores first.
4129 // For cases within the target-specified limits, this is the best choice.
4130 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4131 if (ConstantSize) {
4132 // Memmove with size zero? Just return the original chain.
4133 if (ConstantSize->isNullValue())
4134 return Chain;
4135
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004136 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004137 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004138 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004139 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004140 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004141 return Result;
4142 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004143
4144 // Then check to see if we should lower the memmove with target-specific
4145 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004146 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004147 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004148 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004149 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004150 return Result;
4151
Mon P Wangbf862242010-04-06 08:27:51 +00004152 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4153 // not be safe. See memcpy above for more details.
4154
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004155 const TargetLowering *TLI = TM.getTargetLowering();
4156
Dan Gohman544ab2c2008-04-12 04:36:06 +00004157 // Emit a library call.
4158 TargetLowering::ArgListTy Args;
4159 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004160 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004161 Entry.Node = Dst; Args.push_back(Entry);
4162 Entry.Node = Src; Args.push_back(Entry);
4163 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004164 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004165 TargetLowering::
4166 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004167 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004168 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004169 /*isTailCall=*/false,
4170 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004171 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4172 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004173 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004174 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004175
Dan Gohman544ab2c2008-04-12 04:36:06 +00004176 return CallResult.second;
4177}
4178
Andrew Trickef9de2a2013-05-25 02:42:55 +00004179SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004180 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004181 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004182 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004183 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004184
4185 // Check to see if we should lower the memset to stores first.
4186 // For cases within the target-specified limits, this is the best choice.
4187 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4188 if (ConstantSize) {
4189 // Memset with size zero? Just return the original chain.
4190 if (ConstantSize->isNullValue())
4191 return Chain;
4192
Mon P Wangc576ee92010-04-04 03:10:48 +00004193 SDValue Result =
4194 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004195 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004196
Gabor Greiff304a7a2008-08-28 21:40:38 +00004197 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004198 return Result;
4199 }
4200
4201 // Then check to see if we should lower the memset with target-specific
4202 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004203 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004204 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004205 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004206 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004207 return Result;
4208
Wesley Peck527da1b2010-11-23 03:31:01 +00004209 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004210 const TargetLowering *TLI = TM.getTargetLowering();
4211 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004212 TargetLowering::ArgListTy Args;
4213 TargetLowering::ArgListEntry Entry;
4214 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4215 Args.push_back(Entry);
4216 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004217 if (Src.getValueType().bitsGT(MVT::i32))
4218 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004219 else
Owen Anderson9f944592009-08-11 20:47:22 +00004220 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004221 Entry.Node = Src;
4222 Entry.Ty = Type::getInt32Ty(*getContext());
4223 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004224 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004225 Entry.Node = Size;
4226 Entry.Ty = IntPtrTy;
4227 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004228 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004229 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004230 TargetLowering::
4231 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004232 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004233 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004234 /*isTailCall=*/false,
4235 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004236 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4237 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004238 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004239 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004240
Dan Gohman544ab2c2008-04-12 04:36:06 +00004241 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004242}
4243
Andrew Trickef9de2a2013-05-25 02:42:55 +00004244SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004245 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004246 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004247 AtomicOrdering SuccessOrdering,
4248 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004249 SynchronizationScope SynchScope) {
4250 FoldingSetNodeID ID;
4251 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004252 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004253 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004254 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004255 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4256 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4257 return SDValue(E, 0);
4258 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004259
4260 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4261 // SDNode doesn't have access to it. This memory will be "leaked" when
4262 // the node is deallocated, but recovered when the allocator is released.
4263 // If the number of operands is less than 5 we use AtomicSDNode's internal
4264 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004265 unsigned NumOps = Ops.size();
4266 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4267 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004268
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004269 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4270 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004271 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004272 SuccessOrdering, FailureOrdering,
4273 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004274 CSEMap.InsertNode(N, IP);
4275 AllNodes.push_back(N);
4276 return SDValue(N, 0);
4277}
4278
4279SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004280 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004281 MachineMemOperand *MMO,
4282 AtomicOrdering Ordering,
4283 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004284 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004285 Ordering, SynchScope);
4286}
4287
4288SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004289 SDValue Chain, SDValue Ptr, SDValue Cmp,
4290 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004291 unsigned Alignment,
Tim Northovere94a5182014-03-11 10:48:52 +00004292 AtomicOrdering SuccessOrdering,
4293 AtomicOrdering FailureOrdering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004294 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004295 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4296 Alignment = getEVTAlignment(MemVT);
4297
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004298 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004299
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004300 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004301 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004302 // FIXME: Volatile isn't really correct; we should keep track of atomic
4303 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004304 unsigned Flags = MachineMemOperand::MOVolatile;
4305 if (Opcode != ISD::ATOMIC_STORE)
4306 Flags |= MachineMemOperand::MOLoad;
4307 if (Opcode != ISD::ATOMIC_LOAD)
4308 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004309
4310 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004311 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004312
Eli Friedmanadec5872011-07-29 03:05:32 +00004313 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004314 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004315}
4316
Andrew Trickef9de2a2013-05-25 02:42:55 +00004317SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004318 SDValue Chain,
4319 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004320 SDValue Swp, MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004321 AtomicOrdering SuccessOrdering,
4322 AtomicOrdering FailureOrdering,
Eli Friedmanadec5872011-07-29 03:05:32 +00004323 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004324 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4325 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4326
Owen Anderson53aa7a92009-08-10 22:56:29 +00004327 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004328
Owen Anderson9f944592009-08-11 20:47:22 +00004329 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004330 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004331 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, SuccessOrdering,
Tim Northovere94a5182014-03-11 10:48:52 +00004332 FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004333}
4334
Andrew Trickef9de2a2013-05-25 02:42:55 +00004335SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004336 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004337 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004338 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004339 unsigned Alignment,
4340 AtomicOrdering Ordering,
4341 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004342 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4343 Alignment = getEVTAlignment(MemVT);
4344
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004345 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004346 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004347 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004348 // For now, atomics are considered to be volatile always, and they are
4349 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004350 // FIXME: Volatile isn't really correct; we should keep track of atomic
4351 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004352 unsigned Flags = MachineMemOperand::MOVolatile;
4353 if (Opcode != ISD::ATOMIC_STORE)
4354 Flags |= MachineMemOperand::MOLoad;
4355 if (Opcode != ISD::ATOMIC_LOAD)
4356 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004357
4358 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004359 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004360 MemVT.getStoreSize(), Alignment);
4361
Eli Friedmanadec5872011-07-29 03:05:32 +00004362 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4363 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004364}
4365
Andrew Trickef9de2a2013-05-25 02:42:55 +00004366SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004367 SDValue Chain,
4368 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004369 MachineMemOperand *MMO,
4370 AtomicOrdering Ordering,
4371 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004372 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4373 Opcode == ISD::ATOMIC_LOAD_SUB ||
4374 Opcode == ISD::ATOMIC_LOAD_AND ||
4375 Opcode == ISD::ATOMIC_LOAD_OR ||
4376 Opcode == ISD::ATOMIC_LOAD_XOR ||
4377 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004378 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004379 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004380 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004381 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004382 Opcode == ISD::ATOMIC_SWAP ||
4383 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004384 "Invalid Atomic Op");
4385
Owen Anderson53aa7a92009-08-10 22:56:29 +00004386 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004387
Eli Friedman342e8df2011-08-24 20:50:09 +00004388 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4389 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004390 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004391 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004392}
4393
Andrew Trickef9de2a2013-05-25 02:42:55 +00004394SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004395 EVT VT, SDValue Chain,
4396 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004397 MachineMemOperand *MMO,
4398 AtomicOrdering Ordering,
4399 SynchronizationScope SynchScope) {
4400 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4401
4402 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004403 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004404 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004405}
4406
Duncan Sands739a0542008-07-02 17:40:58 +00004407/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004408SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4409 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004410 return Ops[0];
4411
Owen Anderson53aa7a92009-08-10 22:56:29 +00004412 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004413 VTs.reserve(Ops.size());
4414 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004415 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004416 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004417}
4418
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004419SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004420SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004421 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004422 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004423 unsigned Align, bool Vol,
4424 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004425 if (Align == 0) // Ensure that codegen never sees alignment 0
4426 Align = getEVTAlignment(MemVT);
4427
4428 MachineFunction &MF = getMachineFunction();
4429 unsigned Flags = 0;
4430 if (WriteMem)
4431 Flags |= MachineMemOperand::MOStore;
4432 if (ReadMem)
4433 Flags |= MachineMemOperand::MOLoad;
4434 if (Vol)
4435 Flags |= MachineMemOperand::MOVolatile;
4436 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004437 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004438
Craig Topper206fcd42014-04-26 19:29:41 +00004439 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004440}
4441
4442SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004443SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004444 ArrayRef<SDValue> Ops, EVT MemVT,
4445 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004446 assert((Opcode == ISD::INTRINSIC_VOID ||
4447 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004448 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004449 Opcode == ISD::LIFETIME_START ||
4450 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004451 (Opcode <= INT_MAX &&
4452 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4453 "Opcode is not a memory-accessing opcode!");
4454
Dale Johannesen839acbb2009-01-29 00:47:48 +00004455 // Memoize the node unless it returns a flag.
4456 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004457 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004458 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004459 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004460 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004461 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004462 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4463 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004464 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004465 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004466
Jack Carter170a5f22013-09-09 22:02:08 +00004467 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4468 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004469 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004470 CSEMap.InsertNode(N, IP);
4471 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004472 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4473 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004474 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004475 }
4476 AllNodes.push_back(N);
4477 return SDValue(N, 0);
4478}
4479
Chris Lattnerea952f02010-09-21 17:24:05 +00004480/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4481/// MachinePointerInfo record from it. This is particularly useful because the
4482/// code generator has many cases where it doesn't bother passing in a
4483/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4484static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4485 // If this is FI+Offset, we can model it.
4486 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4487 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4488
4489 // If this is (FI+Offset1)+Offset2, we can model it.
4490 if (Ptr.getOpcode() != ISD::ADD ||
4491 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4492 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4493 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004494
Chris Lattnerea952f02010-09-21 17:24:05 +00004495 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4496 return MachinePointerInfo::getFixedStack(FI, Offset+
4497 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4498}
4499
4500/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4501/// MachinePointerInfo record from it. This is particularly useful because the
4502/// code generator has many cases where it doesn't bother passing in a
4503/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4504static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4505 // If the 'Offset' value isn't a constant, we can't handle this.
4506 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4507 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4508 if (OffsetOp.getOpcode() == ISD::UNDEF)
4509 return InferPointerInfo(Ptr);
4510 return MachinePointerInfo();
4511}
Wesley Peck527da1b2010-11-23 03:31:01 +00004512
Chris Lattnerea952f02010-09-21 17:24:05 +00004513
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004514SDValue
4515SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004516 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004517 SDValue Ptr, SDValue Offset,
4518 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004519 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004520 unsigned Alignment, const MDNode *TBAAInfo,
4521 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004522 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004523 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004524 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4525 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004526
Dan Gohman48b185d2009-09-25 20:36:54 +00004527 unsigned Flags = MachineMemOperand::MOLoad;
4528 if (isVolatile)
4529 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004530 if (isNonTemporal)
4531 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004532 if (isInvariant)
4533 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004534
Chris Lattnerea952f02010-09-21 17:24:05 +00004535 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4536 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004537 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004538 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004539
Chris Lattnerea952f02010-09-21 17:24:05 +00004540 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004541 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004542 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004543 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004544 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004545}
4546
4547SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004548SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004549 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004550 SDValue Ptr, SDValue Offset, EVT MemVT,
4551 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004552 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004553 ExtType = ISD::NON_EXTLOAD;
4554 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004555 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004556 } else {
4557 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004558 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4559 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004560 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004561 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004562 assert(VT.isVector() == MemVT.isVector() &&
4563 "Cannot use trunc store to convert to or from a vector!");
4564 assert((!VT.isVector() ||
4565 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4566 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004567 }
4568
4569 bool Indexed = AM != ISD::UNINDEXED;
4570 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4571 "Unindexed load with an offset!");
4572
4573 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004574 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004575 SDValue Ops[] = { Chain, Ptr, Offset };
4576 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004577 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004578 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004579 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004580 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004581 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004582 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004583 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004584 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4585 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004586 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004587 }
Jack Carter170a5f22013-09-09 22:02:08 +00004588 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4589 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004590 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004591 CSEMap.InsertNode(N, IP);
4592 AllNodes.push_back(N);
4593 return SDValue(N, 0);
4594}
4595
Andrew Trickef9de2a2013-05-25 02:42:55 +00004596SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004597 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004598 MachinePointerInfo PtrInfo,
4599 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004600 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004601 const MDNode *TBAAInfo,
4602 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004603 SDValue Undef = getUNDEF(Ptr.getValueType());
4604 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004605 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4606 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004607}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004608
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004609SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4610 SDValue Chain, SDValue Ptr,
4611 MachineMemOperand *MMO) {
4612 SDValue Undef = getUNDEF(Ptr.getValueType());
4613 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4614 VT, MMO);
4615}
4616
Andrew Trickef9de2a2013-05-25 02:42:55 +00004617SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004618 SDValue Chain, SDValue Ptr,
4619 MachinePointerInfo PtrInfo, EVT MemVT,
4620 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004621 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004622 SDValue Undef = getUNDEF(Ptr.getValueType());
4623 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004624 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004625 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004626}
4627
4628
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004629SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4630 SDValue Chain, SDValue Ptr, EVT MemVT,
4631 MachineMemOperand *MMO) {
4632 SDValue Undef = getUNDEF(Ptr.getValueType());
4633 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4634 MemVT, MMO);
4635}
4636
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004637SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004638SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004639 SDValue Offset, ISD::MemIndexedMode AM) {
4640 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4641 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4642 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004643 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004644 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004645 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004646 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004647}
4648
Andrew Trickef9de2a2013-05-25 02:42:55 +00004649SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004650 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004651 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004652 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004653 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004654 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004655 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004656 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004657
Dan Gohman48b185d2009-09-25 20:36:54 +00004658 unsigned Flags = MachineMemOperand::MOStore;
4659 if (isVolatile)
4660 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004661 if (isNonTemporal)
4662 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004663
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004664 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004665 PtrInfo = InferPointerInfo(Ptr);
4666
4667 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004668 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004669 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004670 Val.getValueType().getStoreSize(), Alignment,
4671 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004672
4673 return getStore(Chain, dl, Val, Ptr, MMO);
4674}
4675
Andrew Trickef9de2a2013-05-25 02:42:55 +00004676SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004677 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004678 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004679 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004680 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004681 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004682 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004683 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4684 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004685 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004686 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004687 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004688 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004689 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004690 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004691 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4692 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004693 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004694 }
Jack Carter170a5f22013-09-09 22:02:08 +00004695 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4696 dl.getDebugLoc(), VTs,
4697 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004698 CSEMap.InsertNode(N, IP);
4699 AllNodes.push_back(N);
4700 return SDValue(N, 0);
4701}
4702
Andrew Trickef9de2a2013-05-25 02:42:55 +00004703SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004704 SDValue Ptr, MachinePointerInfo PtrInfo,
4705 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004706 unsigned Alignment,
4707 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004708 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004709 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004710 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4711 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004712
Dan Gohman48b185d2009-09-25 20:36:54 +00004713 unsigned Flags = MachineMemOperand::MOStore;
4714 if (isVolatile)
4715 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004716 if (isNonTemporal)
4717 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004718
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004719 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004720 PtrInfo = InferPointerInfo(Ptr);
4721
4722 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004723 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004724 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4725 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004726
4727 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4728}
4729
Andrew Trickef9de2a2013-05-25 02:42:55 +00004730SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004731 SDValue Ptr, EVT SVT,
4732 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004733 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004734
Michael Ilsemand5f91512012-09-10 16:56:31 +00004735 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004736 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004737 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004738 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004739
Dan Gohmancecad352009-12-14 23:40:38 +00004740 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4741 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004742 assert(VT.isInteger() == SVT.isInteger() &&
4743 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004744 assert(VT.isVector() == SVT.isVector() &&
4745 "Cannot use trunc store to convert to or from a vector!");
4746 assert((!VT.isVector() ||
4747 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4748 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004749
Owen Anderson9f944592009-08-11 20:47:22 +00004750 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004751 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004752 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4753 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004754 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004755 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004756 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004757 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004758 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004759 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004760 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4761 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004762 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004763 }
Jack Carter170a5f22013-09-09 22:02:08 +00004764 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4765 dl.getDebugLoc(), VTs,
4766 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004767 CSEMap.InsertNode(N, IP);
4768 AllNodes.push_back(N);
4769 return SDValue(N, 0);
4770}
4771
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004772SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004773SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004774 SDValue Offset, ISD::MemIndexedMode AM) {
4775 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4776 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4777 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004778 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004779 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4780 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004781 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004782 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004783 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004784 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004785 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004786 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004787 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004788
Jack Carter170a5f22013-09-09 22:02:08 +00004789 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4790 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004791 ST->isTruncatingStore(),
4792 ST->getMemoryVT(),
4793 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004794 CSEMap.InsertNode(N, IP);
4795 AllNodes.push_back(N);
4796 return SDValue(N, 0);
4797}
4798
Andrew Trickef9de2a2013-05-25 02:42:55 +00004799SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004800 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004801 SDValue SV,
4802 unsigned Align) {
4803 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00004804 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004805}
4806
Andrew Trickef9de2a2013-05-25 02:42:55 +00004807SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00004808 ArrayRef<SDUse> Ops) {
4809 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004810 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00004811 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004812 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4813 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004814 default: break;
4815 }
4816
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004817 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004818 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00004819 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00004820 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004821}
4822
Andrew Trickef9de2a2013-05-25 02:42:55 +00004823SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00004824 ArrayRef<SDValue> Ops) {
4825 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00004826 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004827 case 0: return getNode(Opcode, DL, VT);
4828 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4829 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4830 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004831 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004832 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004833
Chris Lattnerb0713c72005-04-09 03:27:28 +00004834 switch (Opcode) {
4835 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004836 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004837 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004838 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4839 "LHS and RHS of condition must have same type!");
4840 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4841 "True and False arms of SelectCC must have same type!");
4842 assert(Ops[2].getValueType() == VT &&
4843 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004844 break;
4845 }
4846 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004847 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004848 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4849 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004850 break;
4851 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004852 }
4853
Chris Lattner566307f2005-05-14 07:42:29 +00004854 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004855 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004856 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004857
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004858 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004859 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004860 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004861 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004862
Bill Wendling022d18f2009-12-18 23:32:53 +00004863 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004864 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004865
Jack Carter170a5f22013-09-09 22:02:08 +00004866 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004867 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004868 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004869 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004870 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004871 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004872 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004873
Chris Lattnerb0713c72005-04-09 03:27:28 +00004874 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004875#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004876 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004877#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004878 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004879}
4880
Andrew Trickef9de2a2013-05-25 02:42:55 +00004881SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00004882 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
4883 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00004884}
4885
Andrew Trickef9de2a2013-05-25 02:42:55 +00004886SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00004887 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004888 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00004889 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00004890
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004891#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004892 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004893 // FIXME: figure out how to safely handle things like
4894 // int foo(int x) { return 1 << (x & 255); }
4895 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004896 case ISD::SRA_PARTS:
4897 case ISD::SRL_PARTS:
4898 case ISD::SHL_PARTS:
4899 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004900 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004901 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004902 else if (N3.getOpcode() == ISD::AND)
4903 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4904 // If the and is only masking out bits that cannot effect the shift,
4905 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004906 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004907 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004908 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004909 }
4910 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004911 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004912#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004913
Chris Lattnerf9c19152005-08-25 19:12:10 +00004914 // Memoize the node unless it returns a flag.
4915 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00004916 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004917 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004918 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004919 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004920 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004921 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004922 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004923
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004924 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004925 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4926 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004927 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004928 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4929 DL.getDebugLoc(), VTList, Ops[0],
4930 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004931 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004932 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4933 DL.getDebugLoc(), VTList, Ops[0],
4934 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004935 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004936 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004937 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004938 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004939 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004940 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004941 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004942 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4943 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004944 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004945 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4946 DL.getDebugLoc(), VTList, Ops[0],
4947 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004948 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004949 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4950 DL.getDebugLoc(), VTList, Ops[0],
4951 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004952 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004953 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004954 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004955 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004956 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004957 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004958#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004959 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004960#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004961 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004962}
4963
Andrew Trickef9de2a2013-05-25 02:42:55 +00004964SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Topper48d114b2014-04-26 18:35:24 +00004965 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004966}
4967
Andrew Trickef9de2a2013-05-25 02:42:55 +00004968SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004969 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004970 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00004971 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004972}
4973
Andrew Trickef9de2a2013-05-25 02:42:55 +00004974SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004975 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004976 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00004977 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004978}
4979
Andrew Trickef9de2a2013-05-25 02:42:55 +00004980SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004981 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004982 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00004983 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004984}
4985
Andrew Trickef9de2a2013-05-25 02:42:55 +00004986SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004987 SDValue N1, SDValue N2, SDValue N3,
4988 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004989 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00004990 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004991}
4992
Andrew Trickef9de2a2013-05-25 02:42:55 +00004993SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004994 SDValue N1, SDValue N2, SDValue N3,
4995 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004996 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00004997 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004998}
4999
Owen Anderson53aa7a92009-08-10 22:56:29 +00005000SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005001 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005002}
5003
Owen Anderson53aa7a92009-08-10 22:56:29 +00005004SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005005 FoldingSetNodeID ID;
5006 ID.AddInteger(2U);
5007 ID.AddInteger(VT1.getRawBits());
5008 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005009
Craig Topperc0196b12014-04-14 00:51:57 +00005010 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005011 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005012 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005013 EVT *Array = Allocator.Allocate<EVT>(2);
5014 Array[0] = VT1;
5015 Array[1] = VT2;
5016 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5017 VTListMap.InsertNode(Result, IP);
5018 }
5019 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005020}
Dan Gohman17059682008-07-17 19:10:17 +00005021
Owen Anderson53aa7a92009-08-10 22:56:29 +00005022SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005023 FoldingSetNodeID ID;
5024 ID.AddInteger(3U);
5025 ID.AddInteger(VT1.getRawBits());
5026 ID.AddInteger(VT2.getRawBits());
5027 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005028
Craig Topperc0196b12014-04-14 00:51:57 +00005029 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005030 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005031 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005032 EVT *Array = Allocator.Allocate<EVT>(3);
5033 Array[0] = VT1;
5034 Array[1] = VT2;
5035 Array[2] = VT3;
5036 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5037 VTListMap.InsertNode(Result, IP);
5038 }
5039 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005040}
5041
Owen Anderson53aa7a92009-08-10 22:56:29 +00005042SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005043 FoldingSetNodeID ID;
5044 ID.AddInteger(4U);
5045 ID.AddInteger(VT1.getRawBits());
5046 ID.AddInteger(VT2.getRawBits());
5047 ID.AddInteger(VT3.getRawBits());
5048 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005049
Craig Topperc0196b12014-04-14 00:51:57 +00005050 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005051 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005052 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005053 EVT *Array = Allocator.Allocate<EVT>(4);
5054 Array[0] = VT1;
5055 Array[1] = VT2;
5056 Array[2] = VT3;
5057 Array[3] = VT4;
5058 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5059 VTListMap.InsertNode(Result, IP);
5060 }
5061 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005062}
5063
Craig Topperabb4ac72014-04-16 06:10:51 +00005064SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5065 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005066 FoldingSetNodeID ID;
5067 ID.AddInteger(NumVTs);
5068 for (unsigned index = 0; index < NumVTs; index++) {
5069 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005070 }
5071
Craig Topperc0196b12014-04-14 00:51:57 +00005072 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005073 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005074 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005075 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005076 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005077 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5078 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005079 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005080 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005081}
5082
5083
Chris Lattnerf34156e2006-01-28 09:32:45 +00005084/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5085/// specified operands. If the resultant node already exists in the DAG,
5086/// this does not modify the specified node, instead it returns the node that
5087/// already exists. If the resultant node does not exist in the DAG, the
5088/// input node is returned. As a degenerate case, if you specify the same
5089/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005090SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005091 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005092
Chris Lattnerf34156e2006-01-28 09:32:45 +00005093 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005094 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005095
Chris Lattnerf34156e2006-01-28 09:32:45 +00005096 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005097 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005098 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005099 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005100
Dan Gohmanebeccb42008-07-21 22:38:59 +00005101 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005102 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005103 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005104 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005105
Chris Lattnerf34156e2006-01-28 09:32:45 +00005106 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005107 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005108
Chris Lattnerf34156e2006-01-28 09:32:45 +00005109 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005110 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005111 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005112}
5113
Dan Gohman92c11ac2010-06-18 15:30:29 +00005114SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005115 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005116
Chris Lattnerf34156e2006-01-28 09:32:45 +00005117 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005118 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005119 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005120
Chris Lattnerf34156e2006-01-28 09:32:45 +00005121 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005122 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005123 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005124 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005125
Dan Gohmanebeccb42008-07-21 22:38:59 +00005126 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005127 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005128 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005129 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005130
Chris Lattnerf34156e2006-01-28 09:32:45 +00005131 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005132 if (N->OperandList[0] != Op1)
5133 N->OperandList[0].set(Op1);
5134 if (N->OperandList[1] != Op2)
5135 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005136
Chris Lattnerf34156e2006-01-28 09:32:45 +00005137 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005138 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005139 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005140}
5141
Dan Gohman92c11ac2010-06-18 15:30:29 +00005142SDNode *SelectionDAG::
5143UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005144 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005145 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005146}
5147
Dan Gohman92c11ac2010-06-18 15:30:29 +00005148SDNode *SelectionDAG::
5149UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005150 SDValue Op3, SDValue Op4) {
5151 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005152 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005153}
5154
Dan Gohman92c11ac2010-06-18 15:30:29 +00005155SDNode *SelectionDAG::
5156UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005157 SDValue Op3, SDValue Op4, SDValue Op5) {
5158 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005159 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005160}
5161
Dan Gohman92c11ac2010-06-18 15:30:29 +00005162SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005163UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5164 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005165 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005166 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005167
Chris Lattnerf34156e2006-01-28 09:32:45 +00005168 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005169 bool AnyChange = false;
5170 for (unsigned i = 0; i != NumOps; ++i) {
5171 if (Ops[i] != N->getOperand(i)) {
5172 AnyChange = true;
5173 break;
5174 }
5175 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005176
Chris Lattnerf34156e2006-01-28 09:32:45 +00005177 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005178 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005179
Chris Lattnerf34156e2006-01-28 09:32:45 +00005180 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005181 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005182 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005183 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005184
Dan Gohman2f83b472008-05-02 00:05:03 +00005185 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005186 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005187 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005188 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005189
Chris Lattnerf34156e2006-01-28 09:32:45 +00005190 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005191 for (unsigned i = 0; i != NumOps; ++i)
5192 if (N->OperandList[i] != Ops[i])
5193 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005194
5195 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005196 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005197 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005198}
5199
Dan Gohman91697632008-07-07 20:57:48 +00005200/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005201/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005202void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005203 // Unlike the code in MorphNodeTo that does this, we don't need to
5204 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005205 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5206 SDUse &Use = *I++;
5207 Use.set(SDValue());
5208 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005209}
Chris Lattner19732782005-08-16 18:17:10 +00005210
Dan Gohman17059682008-07-17 19:10:17 +00005211/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5212/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005213///
Dan Gohman17059682008-07-17 19:10:17 +00005214SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005215 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005216 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005217 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005218}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005219
Dan Gohman17059682008-07-17 19:10:17 +00005220SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005221 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005222 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005223 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005224 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005225}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005226
Dan Gohman17059682008-07-17 19:10:17 +00005227SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005228 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005229 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005230 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005231 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005232 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005233}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005234
Dan Gohman17059682008-07-17 19:10:17 +00005235SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005236 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005237 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005238 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005239 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005240 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005241}
Chris Lattner466fece2005-08-21 22:30:30 +00005242
Dan Gohman17059682008-07-17 19:10:17 +00005243SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005244 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005245 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005246 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005247}
5248
Dan Gohman17059682008-07-17 19:10:17 +00005249SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005250 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005251 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005252 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005253}
5254
Dan Gohman17059682008-07-17 19:10:17 +00005255SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005256 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005257 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005258 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005259}
5260
Dan Gohman17059682008-07-17 19:10:17 +00005261SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005262 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005263 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005264 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005265 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005266}
5267
Bill Wendling2d598632008-12-01 23:28:22 +00005268SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005269 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005270 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005271 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005272 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005273}
5274
Scott Michelcf0da6c2009-02-17 22:15:04 +00005275SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005276 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005277 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005278 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005279 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005280 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005281}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005282
Scott Michelcf0da6c2009-02-17 22:15:04 +00005283SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005284 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005285 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005286 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005287 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005288 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005289}
5290
Dan Gohman17059682008-07-17 19:10:17 +00005291SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005292 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005293 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005294 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005295 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005296 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005297 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005298}
5299
Dan Gohman17059682008-07-17 19:10:17 +00005300SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005301 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005302 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005303 SDValue Op3) {
5304 SDVTList VTs = getVTList(VT1, VT2, VT3);
5305 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005306 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005307}
5308
5309SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005310 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005311 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005312 // Reset the NodeID to -1.
5313 N->setNodeId(-1);
5314 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005315}
5316
Andrew Trickef9de2a2013-05-25 02:42:55 +00005317/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005318/// the line number information on the merged node since it is not possible to
5319/// preserve the information that operation is associated with multiple lines.
5320/// This will make the debugger working better at -O0, were there is a higher
5321/// probability having other instructions associated with that line.
5322///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005323/// For IROrder, we keep the smaller of the two
5324SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005325 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005326 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5327 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005328 N->setDebugLoc(DebugLoc());
5329 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005330 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5331 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005332 return N;
5333}
5334
Chris Lattneraf197502010-02-28 21:36:14 +00005335/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005336/// return type, opcode, and operands.
5337///
5338/// Note that MorphNodeTo returns the resultant node. If there is already a
5339/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005340/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005341///
5342/// Using MorphNodeTo is faster than creating a new node and swapping it in
5343/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005344/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005345/// the node's users.
5346///
5347SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005348 SDVTList VTs, ArrayRef<SDValue> Ops) {
5349 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005350 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005351 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005352 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005353 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005354 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005355 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005356 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005357 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005358
Dan Gohmand3fe1742008-09-13 01:54:27 +00005359 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005360 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005361
Dan Gohman17059682008-07-17 19:10:17 +00005362 // Start the morphing.
5363 N->NodeType = Opc;
5364 N->ValueList = VTs.VTs;
5365 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005366
Dan Gohman17059682008-07-17 19:10:17 +00005367 // Clear the operands list, updating used nodes to remove this from their
5368 // use list. Keep track of any operands that become dead as a result.
5369 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005370 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5371 SDUse &Use = *I++;
5372 SDNode *Used = Use.getNode();
5373 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005374 if (Used->use_empty())
5375 DeadNodeSet.insert(Used);
5376 }
5377
Dan Gohman48b185d2009-09-25 20:36:54 +00005378 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5379 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005380 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005381 // If NumOps is larger than the # of operands we can have in a
5382 // MachineSDNode, reallocate the operand list.
5383 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5384 if (MN->OperandsNeedDelete)
5385 delete[] MN->OperandList;
5386 if (NumOps > array_lengthof(MN->LocalOperands))
5387 // We're creating a final node that will live unmorphed for the
5388 // remainder of the current SelectionDAG iteration, so we can allocate
5389 // the operands directly out of a pool with no recycling metadata.
5390 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005391 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005392 else
Craig Topper131de822014-04-27 19:21:16 +00005393 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005394 MN->OperandsNeedDelete = false;
5395 } else
Craig Topper131de822014-04-27 19:21:16 +00005396 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005397 } else {
5398 // If NumOps is larger than the # of operands we currently have, reallocate
5399 // the operand list.
5400 if (NumOps > N->NumOperands) {
5401 if (N->OperandsNeedDelete)
5402 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005403 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005404 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005405 } else
Craig Topper131de822014-04-27 19:21:16 +00005406 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005407 }
5408
5409 // Delete any nodes that are still dead after adding the uses for the
5410 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005411 if (!DeadNodeSet.empty()) {
5412 SmallVector<SDNode *, 16> DeadNodes;
5413 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5414 E = DeadNodeSet.end(); I != E; ++I)
5415 if ((*I)->use_empty())
5416 DeadNodes.push_back(*I);
5417 RemoveDeadNodes(DeadNodes);
5418 }
Dan Gohman91697632008-07-07 20:57:48 +00005419
Dan Gohman17059682008-07-17 19:10:17 +00005420 if (IP)
5421 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005422 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005423}
5424
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005425
Dan Gohman32f71d72009-09-25 18:54:59 +00005426/// getMachineNode - These are used for target selectors to create a new node
5427/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005428///
Dan Gohman32f71d72009-09-25 18:54:59 +00005429/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005430/// node of the specified opcode and operands, it returns that node instead of
5431/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005432MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005433SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005434 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005435 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005436}
Bill Wendlinga434d932009-01-29 09:01:55 +00005437
Dan Gohmana22f2d82009-10-10 01:29:16 +00005438MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005439SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005440 SDVTList VTs = getVTList(VT);
5441 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005442 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005443}
Bill Wendlinga434d932009-01-29 09:01:55 +00005444
Dan Gohmana22f2d82009-10-10 01:29:16 +00005445MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005446SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005447 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005448 SDVTList VTs = getVTList(VT);
5449 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005450 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005451}
Bill Wendlinga434d932009-01-29 09:01:55 +00005452
Dan Gohmana22f2d82009-10-10 01:29:16 +00005453MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005454SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005455 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005456 SDVTList VTs = getVTList(VT);
5457 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005458 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005459}
Bill Wendlinga434d932009-01-29 09:01:55 +00005460
Dan Gohmana22f2d82009-10-10 01:29:16 +00005461MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005462SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005463 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005464 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005465 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005466}
Bill Wendlinga434d932009-01-29 09:01:55 +00005467
Dan Gohmana22f2d82009-10-10 01:29:16 +00005468MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005469SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005470 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005471 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005472}
Bill Wendlinga434d932009-01-29 09:01:55 +00005473
Dan Gohmana22f2d82009-10-10 01:29:16 +00005474MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005475SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005476 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005477 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005478 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005479 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005480}
Bill Wendlinga434d932009-01-29 09:01:55 +00005481
Dan Gohmana22f2d82009-10-10 01:29:16 +00005482MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005483SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005484 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005485 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005486 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005487 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005488}
5489
Dan Gohmana22f2d82009-10-10 01:29:16 +00005490MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005491SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005492 EVT VT1, EVT VT2, SDValue Op1,
5493 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005494 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005495 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005496 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005497}
5498
Dan Gohmana22f2d82009-10-10 01:29:16 +00005499MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005500SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005501 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005502 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005503 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005504 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005505}
Bill Wendlinga434d932009-01-29 09:01:55 +00005506
Dan Gohmana22f2d82009-10-10 01:29:16 +00005507MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005508SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005509 EVT VT1, EVT VT2, EVT VT3,
5510 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005511 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005512 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005513 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005514}
Bill Wendlinga434d932009-01-29 09:01:55 +00005515
Dan Gohmana22f2d82009-10-10 01:29:16 +00005516MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005517SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005518 EVT VT1, EVT VT2, EVT VT3,
5519 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005520 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005521 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005522 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005523}
Bill Wendlinga434d932009-01-29 09:01:55 +00005524
Dan Gohmana22f2d82009-10-10 01:29:16 +00005525MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005526SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005527 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005528 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005529 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005530 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005531}
Bill Wendlinga434d932009-01-29 09:01:55 +00005532
Dan Gohmana22f2d82009-10-10 01:29:16 +00005533MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005534SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005535 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005536 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005537 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005538 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005539}
5540
Dan Gohmana22f2d82009-10-10 01:29:16 +00005541MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005542SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005543 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005544 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005545 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005546 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005547}
5548
Dan Gohmana22f2d82009-10-10 01:29:16 +00005549MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005550SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005551 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005552 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005553 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005554 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005555 const SDValue *Ops = OpsArray.data();
5556 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005557
5558 if (DoCSE) {
5559 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005560 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005561 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005562 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005563 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005564 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005565 }
5566
5567 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005568 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5569 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005570
5571 // Initialize the operands list.
5572 if (NumOps > array_lengthof(N->LocalOperands))
5573 // We're creating a final node that will live unmorphed for the
5574 // remainder of the current SelectionDAG iteration, so we can allocate
5575 // the operands directly out of a pool with no recycling metadata.
5576 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5577 Ops, NumOps);
5578 else
5579 N->InitOperands(N->LocalOperands, Ops, NumOps);
5580 N->OperandsNeedDelete = false;
5581
5582 if (DoCSE)
5583 CSEMap.InsertNode(N, IP);
5584
5585 AllNodes.push_back(N);
5586#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005587 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005588#endif
5589 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005590}
Evan Chengd3f1db92006-02-09 07:15:23 +00005591
Dan Gohmanac33a902009-08-19 18:16:17 +00005592/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005593/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005594SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005595SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005596 SDValue Operand) {
5597 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005598 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005599 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005600 return SDValue(Subreg, 0);
5601}
5602
Bob Wilson2a45a652009-10-08 18:49:46 +00005603/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005604/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005605SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005606SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005607 SDValue Operand, SDValue Subreg) {
5608 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005609 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005610 VT, Operand, Subreg, SRIdxVal);
5611 return SDValue(Result, 0);
5612}
5613
Evan Cheng31604a62008-03-22 01:55:50 +00005614/// getNodeIfExists - Get the specified node if it's already available, or
5615/// else return NULL.
5616SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Craig Topper633d99b2014-04-27 23:22:43 +00005617 ArrayRef<SDValue> Ops) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005618 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005619 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005620 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005621 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005622 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005623 return E;
5624 }
Craig Topperc0196b12014-04-14 00:51:57 +00005625 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005626}
5627
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005628/// getDbgValue - Creates a SDDbgValue node.
5629///
Adrian Prantl32da8892014-04-25 20:49:25 +00005630/// SDNode
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005631SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005632SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
5633 bool IsIndirect, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005634 DebugLoc DL, unsigned O) {
Adrian Prantl32da8892014-04-25 20:49:25 +00005635 return new (Allocator) SDDbgValue(MDPtr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005636}
5637
Adrian Prantl32da8892014-04-25 20:49:25 +00005638/// Constant
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005639SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005640SelectionDAG::getConstantDbgValue(MDNode *MDPtr, const Value *C,
5641 uint64_t Off,
5642 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005643 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5644}
5645
Adrian Prantl32da8892014-04-25 20:49:25 +00005646/// FrameIndex
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005647SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005648SelectionDAG::getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5649 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005650 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5651}
5652
Dan Gohman7d099f72010-03-03 21:33:37 +00005653namespace {
5654
Dan Gohman9cc886b2010-03-04 19:11:28 +00005655/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005656/// pointed to by a use iterator is deleted, increment the use iterator
5657/// so that it doesn't dangle.
5658///
Dan Gohman7d099f72010-03-03 21:33:37 +00005659class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005660 SDNode::use_iterator &UI;
5661 SDNode::use_iterator &UE;
5662
Craig Topper7b883b32014-03-08 06:31:39 +00005663 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005664 // Increment the iterator as needed.
5665 while (UI != UE && N == *UI)
5666 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005667 }
5668
5669public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005670 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005671 SDNode::use_iterator &ui,
5672 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005673 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005674};
5675
5676}
5677
Evan Cheng445b91a2006-08-07 22:13:29 +00005678/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005679/// This can cause recursive merging of nodes in the DAG.
5680///
Chris Lattner76858912008-02-03 03:35:22 +00005681/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005682///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005683void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005684 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005685 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005686 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005687 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005688
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005689 // Iterate over all the existing uses of From. New uses will be added
5690 // to the beginning of the use list, which we avoid visiting.
5691 // This specifically avoids visiting uses of From that arise while the
5692 // replacement is happening, because any such uses would be the result
5693 // of CSE: If an existing node looks like From after one of its operands
5694 // is replaced by To, we don't want to replace of all its users with To
5695 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005696 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005697 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005698 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005699 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005700
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005701 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005702 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005703
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005704 // A user can appear in a use list multiple times, and when this
5705 // happens the uses are usually next to each other in the list.
5706 // To help reduce the number of CSE recomputations, process all
5707 // the uses of this user that we can find this way.
5708 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005709 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005710 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005711 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005712 } while (UI != UE && *UI == User);
5713
5714 // Now that we have modified User, add it back to the CSE maps. If it
5715 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005716 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005717 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005718
5719 // If we just RAUW'd the root, take note.
5720 if (FromN == getRoot())
5721 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005722}
5723
5724/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5725/// This can cause recursive merging of nodes in the DAG.
5726///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005727/// This version assumes that for each value of From, there is a
5728/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005729///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005730void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005731#ifndef NDEBUG
5732 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5733 assert((!From->hasAnyUseOfValue(i) ||
5734 From->getValueType(i) == To->getValueType(i)) &&
5735 "Cannot use this version of ReplaceAllUsesWith!");
5736#endif
Dan Gohman17059682008-07-17 19:10:17 +00005737
5738 // Handle the trivial case.
5739 if (From == To)
5740 return;
5741
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005742 // Iterate over just the existing users of From. See the comments in
5743 // the ReplaceAllUsesWith above.
5744 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005745 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005746 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005747 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005748
Chris Lattner373f0482005-08-26 18:36:28 +00005749 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005750 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005751
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005752 // A user can appear in a use list multiple times, and when this
5753 // happens the uses are usually next to each other in the list.
5754 // To help reduce the number of CSE recomputations, process all
5755 // the uses of this user that we can find this way.
5756 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005757 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005758 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005759 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005760 } while (UI != UE && *UI == User);
5761
5762 // Now that we have modified User, add it back to the CSE maps. If it
5763 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005764 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005765 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005766
5767 // If we just RAUW'd the root, take note.
5768 if (From == getRoot().getNode())
5769 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005770}
5771
Chris Lattner373f0482005-08-26 18:36:28 +00005772/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5773/// This can cause recursive merging of nodes in the DAG.
5774///
5775/// This version can replace From with any result values. To must match the
5776/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005777void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005778 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005779 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005780
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005781 // Iterate over just the existing users of From. See the comments in
5782 // the ReplaceAllUsesWith above.
5783 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005784 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005785 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005786 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005787
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005788 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005789 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005790
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005791 // A user can appear in a use list multiple times, and when this
5792 // happens the uses are usually next to each other in the list.
5793 // To help reduce the number of CSE recomputations, process all
5794 // the uses of this user that we can find this way.
5795 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005796 SDUse &Use = UI.getUse();
5797 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005798 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005799 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005800 } while (UI != UE && *UI == User);
5801
5802 // Now that we have modified User, add it back to the CSE maps. If it
5803 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005804 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005805 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005806
5807 // If we just RAUW'd the root, take note.
5808 if (From == getRoot().getNode())
5809 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005810}
5811
Chris Lattner375e1a72006-02-17 21:58:01 +00005812/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005813/// uses of other values produced by From.getNode() alone. The Deleted
5814/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005815void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005816 // Handle the really simple, really trivial case efficiently.
5817 if (From == To) return;
5818
Chris Lattner375e1a72006-02-17 21:58:01 +00005819 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005820 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005821 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005822 return;
5823 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005824
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005825 // Iterate over just the existing users of From. See the comments in
5826 // the ReplaceAllUsesWith above.
5827 SDNode::use_iterator UI = From.getNode()->use_begin(),
5828 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005829 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005830 while (UI != UE) {
5831 SDNode *User = *UI;
5832 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005833
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005834 // A user can appear in a use list multiple times, and when this
5835 // happens the uses are usually next to each other in the list.
5836 // To help reduce the number of CSE recomputations, process all
5837 // the uses of this user that we can find this way.
5838 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005839 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005840
5841 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005842 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005843 ++UI;
5844 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005845 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005846
5847 // If this node hasn't been modified yet, it's still in the CSE maps,
5848 // so remove its old self from the CSE maps.
5849 if (!UserRemovedFromCSEMaps) {
5850 RemoveNodeFromCSEMaps(User);
5851 UserRemovedFromCSEMaps = true;
5852 }
5853
5854 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005855 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005856 } while (UI != UE && *UI == User);
5857
5858 // We are iterating over all uses of the From node, so if a use
5859 // doesn't use the specific value, no changes are made.
5860 if (!UserRemovedFromCSEMaps)
5861 continue;
5862
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005863 // Now that we have modified User, add it back to the CSE maps. If it
5864 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005865 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005866 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005867
5868 // If we just RAUW'd the root, take note.
5869 if (From == getRoot())
5870 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005871}
5872
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005873namespace {
5874 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5875 /// to record information about a use.
5876 struct UseMemo {
5877 SDNode *User;
5878 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005879 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005880 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005881
5882 /// operator< - Sort Memos by User.
5883 bool operator<(const UseMemo &L, const UseMemo &R) {
5884 return (intptr_t)L.User < (intptr_t)R.User;
5885 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005886}
5887
Dan Gohman17059682008-07-17 19:10:17 +00005888/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005889/// uses of other values produced by From.getNode() alone. The same value
5890/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005891/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005892void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5893 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005894 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005895 // Handle the simple, trivial case efficiently.
5896 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005897 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005898
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005899 // Read up all the uses and make records of them. This helps
5900 // processing new uses that are introduced during the
5901 // replacement process.
5902 SmallVector<UseMemo, 4> Uses;
5903 for (unsigned i = 0; i != Num; ++i) {
5904 unsigned FromResNo = From[i].getResNo();
5905 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005906 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005907 E = FromNode->use_end(); UI != E; ++UI) {
5908 SDUse &Use = UI.getUse();
5909 if (Use.getResNo() == FromResNo) {
5910 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005911 Uses.push_back(Memo);
5912 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005913 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005914 }
Dan Gohman17059682008-07-17 19:10:17 +00005915
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005916 // Sort the uses, so that all the uses from a given User are together.
5917 std::sort(Uses.begin(), Uses.end());
5918
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005919 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5920 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005921 // We know that this user uses some value of From. If it is the right
5922 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005923 SDNode *User = Uses[UseIndex].User;
5924
5925 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005926 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005927
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005928 // The Uses array is sorted, so all the uses for a given User
5929 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005930 // To help reduce the number of CSE recomputations, process all
5931 // the uses of this user that we can find this way.
5932 do {
5933 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005934 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005935 ++UseIndex;
5936
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005937 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005938 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5939
Dan Gohman17059682008-07-17 19:10:17 +00005940 // Now that we have modified User, add it back to the CSE maps. If it
5941 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005942 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005943 }
5944}
5945
Evan Cheng9631a602006-08-01 08:20:41 +00005946/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005947/// based on their topological order. It returns the maximum id and a vector
5948/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005949unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005950
Dan Gohman86aa16a2008-09-30 18:30:35 +00005951 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005952
Dan Gohman86aa16a2008-09-30 18:30:35 +00005953 // SortedPos tracks the progress of the algorithm. Nodes before it are
5954 // sorted, nodes after it are unsorted. When the algorithm completes
5955 // it is at the end of the list.
5956 allnodes_iterator SortedPos = allnodes_begin();
5957
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005958 // Visit all the nodes. Move nodes with no operands to the front of
5959 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005960 // operand count. Before we do this, the Node Id fields of the nodes
5961 // may contain arbitrary values. After, the Node Id fields for nodes
5962 // before SortedPos will contain the topological sort index, and the
5963 // Node Id fields for nodes At SortedPos and after will contain the
5964 // count of outstanding operands.
5965 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5966 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005967 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005968 unsigned Degree = N->getNumOperands();
5969 if (Degree == 0) {
5970 // A node with no uses, add it to the result array immediately.
5971 N->setNodeId(DAGSize++);
5972 allnodes_iterator Q = N;
5973 if (Q != SortedPos)
5974 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005975 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005976 ++SortedPos;
5977 } else {
5978 // Temporarily use the Node Id as scratch space for the degree count.
5979 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005980 }
5981 }
5982
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005983 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005984 // such that by the time the end is reached all nodes will be sorted.
5985 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5986 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00005987 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00005988 // N is in sorted position, so all its uses have one less operand
5989 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005990 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5991 UI != UE; ++UI) {
5992 SDNode *P = *UI;
5993 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00005994 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005995 --Degree;
5996 if (Degree == 0) {
5997 // All of P's operands are sorted, so P may sorted now.
5998 P->setNodeId(DAGSize++);
5999 if (P != SortedPos)
6000 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006001 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006002 ++SortedPos;
6003 } else {
6004 // Update P's outstanding operand count.
6005 P->setNodeId(Degree);
6006 }
6007 }
David Greene3b2a68c2010-01-20 00:59:23 +00006008 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006009#ifndef NDEBUG
6010 SDNode *S = ++I;
6011 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00006012 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00006013#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006014 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006015 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006016 }
6017
6018 assert(SortedPos == AllNodes.end() &&
6019 "Topological sort incomplete!");
6020 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6021 "First node in topological sort is not the entry token!");
6022 assert(AllNodes.front().getNodeId() == 0 &&
6023 "First node in topological sort has non-zero id!");
6024 assert(AllNodes.front().getNumOperands() == 0 &&
6025 "First node in topological sort has operands!");
6026 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6027 "Last node in topologic sort has unexpected id!");
6028 assert(AllNodes.back().use_empty() &&
6029 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006030 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006031 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006032}
6033
Evan Cheng563fe3c2010-03-25 01:38:16 +00006034/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6035/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006036void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6037 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006038 if (SD)
6039 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006040}
Evan Cheng29eefc12006-07-27 06:39:06 +00006041
Devang Patelefc6b162011-01-25 23:27:42 +00006042/// TransferDbgValues - Transfer SDDbgValues.
6043void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6044 if (From == To || !From.getNode()->getHasDebugValue())
6045 return;
6046 SDNode *FromNode = From.getNode();
6047 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006048 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006049 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006050 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006051 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006052 SDDbgValue *Dbg = *I;
6053 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6054 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
Adrian Prantl32da8892014-04-25 20:49:25 +00006055 Dbg->isIndirect(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006056 Dbg->getOffset(), Dbg->getDebugLoc(),
6057 Dbg->getOrder());
6058 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006059 }
6060 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006061 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006062 E = ClonedDVs.end(); I != E; ++I)
6063 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006064}
6065
Jim Laskeyd66e6162005-08-17 20:08:02 +00006066//===----------------------------------------------------------------------===//
6067// SDNode Class
6068//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006069
Chris Lattner3bf17b62007-02-04 02:41:42 +00006070HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006071 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006072}
6073
Andrew Trickef9de2a2013-05-25 02:42:55 +00006074GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6075 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006076 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006077 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006078 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006079}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006080
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006081AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6082 SDValue X, unsigned SrcAS,
6083 unsigned DestAS)
6084 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6085 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6086
Andrew Trickef9de2a2013-05-25 02:42:55 +00006087MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6088 EVT memvt, MachineMemOperand *mmo)
6089 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006090 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006091 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006092 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006093 assert(isNonTemporal() == MMO->isNonTemporal() &&
6094 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006095 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006096}
6097
Andrew Trickef9de2a2013-05-25 02:42:55 +00006098MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006099 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6100 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006101 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006102 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006103 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006104 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6105 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006106}
6107
Jim Laskeyf576b422006-10-27 23:46:08 +00006108/// Profile - Gather unique data for the node.
6109///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006110void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006111 AddNodeIDNode(ID, this);
6112}
6113
Owen Anderson3b1665e2009-08-25 22:27:22 +00006114namespace {
6115 struct EVTArray {
6116 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006117
Owen Anderson3b1665e2009-08-25 22:27:22 +00006118 EVTArray() {
6119 VTs.reserve(MVT::LAST_VALUETYPE);
6120 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6121 VTs.push_back(MVT((MVT::SimpleValueType)i));
6122 }
6123 };
6124}
6125
Owen Anderson53aa7a92009-08-10 22:56:29 +00006126static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006127static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006128static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006129
Chris Lattner88fa11c2005-11-08 23:30:28 +00006130/// getValueTypeList - Return a pointer to the specified value type.
6131///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006132const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006133 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006134 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006135 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006136 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006137 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006138 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006139 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006140 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006141}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006142
Chris Lattner40e79822005-01-12 18:37:47 +00006143/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6144/// indicated value. This method ignores uses of other values defined by this
6145/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006146bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006147 assert(Value < getNumValues() && "Bad value!");
6148
Roman Levenstein51f532f2008-04-07 10:06:32 +00006149 // TODO: Only iterate over uses of a given value of the node
6150 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006151 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006152 if (NUses == 0)
6153 return false;
6154 --NUses;
6155 }
Chris Lattner40e79822005-01-12 18:37:47 +00006156 }
6157
6158 // Found exactly the right number of uses?
6159 return NUses == 0;
6160}
6161
6162
Evan Cheng358c3d12007-08-02 05:29:38 +00006163/// hasAnyUseOfValue - Return true if there are any use of the indicated
6164/// value. This method ignores uses of other values defined by this operation.
6165bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6166 assert(Value < getNumValues() && "Bad value!");
6167
Dan Gohman7a510c22008-07-09 22:39:01 +00006168 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006169 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006170 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006171
6172 return false;
6173}
6174
6175
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006176/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006177///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006178bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006179 bool Seen = false;
6180 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006181 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006182 if (User == this)
6183 Seen = true;
6184 else
6185 return false;
6186 }
6187
6188 return Seen;
6189}
6190
Evan Cheng9456dd82006-11-03 07:31:32 +00006191/// isOperand - Return true if this node is an operand of N.
6192///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006193bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006194 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6195 if (*this == N->getOperand(i))
6196 return true;
6197 return false;
6198}
6199
Evan Cheng567d2e52008-03-04 00:41:45 +00006200bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006201 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006202 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006203 return true;
6204 return false;
6205}
Evan Chengd37645c2006-02-05 06:29:23 +00006206
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006207/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006208/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006209/// side-effecting instructions on any chain path. In practice, this looks
6210/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006211/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006212bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006213 unsigned Depth) const {
6214 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006215
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006216 // Don't search too deeply, we just want to be able to see through
6217 // TokenFactor's etc.
6218 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006219
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006220 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006221 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006222 if (getOpcode() == ISD::TokenFactor) {
6223 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006224 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6225 return false;
6226 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006227 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006228
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006229 // Loads don't have side effects, look through them.
6230 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6231 if (!Ld->isVolatile())
6232 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6233 }
6234 return false;
6235}
6236
Lang Hames5a004992011-07-07 04:31:51 +00006237/// hasPredecessor - Return true if N is a predecessor of this node.
6238/// N is either an operand of this node, or can be reached by recursively
6239/// traversing up the operands.
6240/// NOTE: This is an expensive method. Use it carefully.
6241bool SDNode::hasPredecessor(const SDNode *N) const {
6242 SmallPtrSet<const SDNode *, 32> Visited;
6243 SmallVector<const SDNode *, 16> Worklist;
6244 return hasPredecessorHelper(N, Visited, Worklist);
6245}
Dan Gohmancd139c02009-10-28 03:44:30 +00006246
Craig Topperb94011f2013-07-14 04:42:23 +00006247bool
6248SDNode::hasPredecessorHelper(const SDNode *N,
6249 SmallPtrSet<const SDNode *, 32> &Visited,
6250 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006251 if (Visited.empty()) {
6252 Worklist.push_back(this);
6253 } else {
6254 // Take a look in the visited set. If we've already encountered this node
6255 // we needn't search further.
6256 if (Visited.count(N))
6257 return true;
6258 }
6259
6260 // Haven't visited N yet. Continue the search.
6261 while (!Worklist.empty()) {
6262 const SDNode *M = Worklist.pop_back_val();
6263 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6264 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006265 if (Visited.insert(Op))
6266 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006267 if (Op == N)
6268 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006269 }
Lang Hames5a004992011-07-07 04:31:51 +00006270 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006271
6272 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006273}
6274
Evan Cheng5d9fd972006-10-04 00:56:09 +00006275uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6276 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006277 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006278}
6279
Mon P Wang32f8bb92009-11-30 02:42:02 +00006280SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6281 assert(N->getNumValues() == 1 &&
6282 "Can't unroll a vector with multiple results!");
6283
6284 EVT VT = N->getValueType(0);
6285 unsigned NE = VT.getVectorNumElements();
6286 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006287 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006288
6289 SmallVector<SDValue, 8> Scalars;
6290 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6291
6292 // If ResNE is 0, fully unroll the vector op.
6293 if (ResNE == 0)
6294 ResNE = NE;
6295 else if (NE > ResNE)
6296 NE = ResNE;
6297
6298 unsigned i;
6299 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006300 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006301 SDValue Operand = N->getOperand(j);
6302 EVT OperandVT = Operand.getValueType();
6303 if (OperandVT.isVector()) {
6304 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006305 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006306 EVT OperandEltVT = OperandVT.getVectorElementType();
6307 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6308 OperandEltVT,
6309 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006310 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006311 } else {
6312 // A scalar operand; just use it as is.
6313 Operands[j] = Operand;
6314 }
6315 }
6316
6317 switch (N->getOpcode()) {
6318 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006319 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006320 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006321 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006322 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006323 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006324 case ISD::SHL:
6325 case ISD::SRA:
6326 case ISD::SRL:
6327 case ISD::ROTL:
6328 case ISD::ROTR:
6329 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006330 getShiftAmountOperand(Operands[0].getValueType(),
6331 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006332 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006333 case ISD::SIGN_EXTEND_INREG:
6334 case ISD::FP_ROUND_INREG: {
6335 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6336 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6337 Operands[0],
6338 getValueType(ExtVT)));
6339 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006340 }
6341 }
6342
6343 for (; i < ResNE; ++i)
6344 Scalars.push_back(getUNDEF(EltVT));
6345
6346 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006347 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006348}
6349
Evan Chengf5938d52009-12-09 01:36:00 +00006350
Wesley Peck527da1b2010-11-23 03:31:01 +00006351/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6352/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006353/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006354bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006355 unsigned Bytes, int Dist) const {
6356 if (LD->getChain() != Base->getChain())
6357 return false;
6358 EVT VT = LD->getValueType(0);
6359 if (VT.getSizeInBits() / 8 != Bytes)
6360 return false;
6361
6362 SDValue Loc = LD->getOperand(1);
6363 SDValue BaseLoc = Base->getOperand(1);
6364 if (Loc.getOpcode() == ISD::FrameIndex) {
6365 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6366 return false;
6367 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6368 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6369 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6370 int FS = MFI->getObjectSize(FI);
6371 int BFS = MFI->getObjectSize(BFI);
6372 if (FS != BFS || FS != (int)Bytes) return false;
6373 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6374 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006375
6376 // Handle X+C
6377 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6378 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6379 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006380
Craig Topperc0196b12014-04-14 00:51:57 +00006381 const GlobalValue *GV1 = nullptr;
6382 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006383 int64_t Offset1 = 0;
6384 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006385 const TargetLowering *TLI = TM.getTargetLowering();
6386 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6387 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006388 if (isGA1 && isGA2 && GV1 == GV2)
6389 return Offset1 == (Offset2 + Dist*Bytes);
6390 return false;
6391}
6392
6393
Evan Cheng34a23ea2009-12-09 01:04:59 +00006394/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6395/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006396unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006397 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006398 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006399 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006400 const TargetLowering *TLI = TM.getTargetLowering();
6401 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006402 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006403 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00006404 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006405 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006406 unsigned AlignBits = KnownZero.countTrailingOnes();
6407 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6408 if (Align)
6409 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006410 }
Evan Chengd938faf2009-12-09 01:53:58 +00006411
Evan Cheng34a23ea2009-12-09 01:04:59 +00006412 // If this is a direct reference to a stack slot, use information about the
6413 // stack slot's alignment.
6414 int FrameIdx = 1 << 31;
6415 int64_t FrameOffset = 0;
6416 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6417 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006418 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006419 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006420 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006421 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6422 FrameOffset = Ptr.getConstantOperandVal(1);
6423 }
6424
6425 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006426 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006427 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6428 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006429 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006430 }
6431
6432 return 0;
6433}
6434
Juergen Ributzkab3487102013-11-19 21:20:17 +00006435/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6436/// which is split (or expanded) into two not necessarily identical pieces.
6437std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6438 // Currently all types are split in half.
6439 EVT LoVT, HiVT;
6440 if (!VT.isVector()) {
6441 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6442 } else {
6443 unsigned NumElements = VT.getVectorNumElements();
6444 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6445 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6446 NumElements/2);
6447 }
6448 return std::make_pair(LoVT, HiVT);
6449}
6450
6451/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6452/// low/high part.
6453std::pair<SDValue, SDValue>
6454SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6455 const EVT &HiVT) {
6456 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6457 N.getValueType().getVectorNumElements() &&
6458 "More vector elements requested than available!");
6459 SDValue Lo, Hi;
6460 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6461 getConstant(0, TLI->getVectorIdxTy()));
6462 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6463 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6464 return std::make_pair(Lo, Hi);
6465}
6466
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006467void SelectionDAG::ExtractVectorElements(SDValue Op,
6468 SmallVectorImpl<SDValue> &Args,
6469 unsigned Start, unsigned Count) {
6470 EVT VT = Op.getValueType();
6471 if (Count == 0)
6472 Count = VT.getVectorNumElements();
6473
6474 EVT EltVT = VT.getVectorElementType();
6475 EVT IdxTy = TLI->getVectorIdxTy();
6476 SDLoc SL(Op);
6477 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6478 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6479 Op, getConstant(i, IdxTy)));
6480 }
6481}
6482
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006483// getAddressSpace - Return the address space this GlobalAddress belongs to.
6484unsigned GlobalAddressSDNode::getAddressSpace() const {
6485 return getGlobal()->getType()->getAddressSpace();
6486}
6487
6488
Chris Lattner229907c2011-07-18 04:54:35 +00006489Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006490 if (isMachineConstantPoolEntry())
6491 return Val.MachineCPVal->getType();
6492 return Val.ConstVal->getType();
6493}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006494
Bob Wilson85cefe82009-03-02 23:24:16 +00006495bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6496 APInt &SplatUndef,
6497 unsigned &SplatBitSize,
6498 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006499 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006500 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006501 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006502 assert(VT.isVector() && "Expected a vector type");
6503 unsigned sz = VT.getSizeInBits();
6504 if (MinSplatBits > sz)
6505 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006506
Bob Wilson85cefe82009-03-02 23:24:16 +00006507 SplatValue = APInt(sz, 0);
6508 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006509
Bob Wilson85cefe82009-03-02 23:24:16 +00006510 // Get the bits. Bits with undefined values (when the corresponding element
6511 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6512 // in SplatValue. If any of the values are not constant, give up and return
6513 // false.
6514 unsigned int nOps = getNumOperands();
6515 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6516 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006517
6518 for (unsigned j = 0; j < nOps; ++j) {
6519 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006520 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006521 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006522
Bob Wilson85cefe82009-03-02 23:24:16 +00006523 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006524 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006525 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006526 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006527 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006528 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006529 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006530 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006531 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006532 }
6533
Bob Wilson85cefe82009-03-02 23:24:16 +00006534 // The build_vector is all constants or undefs. Find the smallest element
6535 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006536
Bob Wilson85cefe82009-03-02 23:24:16 +00006537 HasAnyUndefs = (SplatUndef != 0);
6538 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006539
Bob Wilson85cefe82009-03-02 23:24:16 +00006540 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006541 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6542 APInt LowValue = SplatValue.trunc(HalfSize);
6543 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6544 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006545
Bob Wilson85cefe82009-03-02 23:24:16 +00006546 // If the two halves do not match (ignoring undef bits), stop here.
6547 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6548 MinSplatBits > HalfSize)
6549 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006550
Bob Wilson85cefe82009-03-02 23:24:16 +00006551 SplatValue = HighValue | LowValue;
6552 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006553
Bob Wilson85cefe82009-03-02 23:24:16 +00006554 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006555 }
6556
Bob Wilson85cefe82009-03-02 23:24:16 +00006557 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006558 return true;
6559}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006560
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006561ConstantSDNode *BuildVectorSDNode::getConstantSplatValue() const {
Matt Arsenault985b9de2014-03-17 18:58:01 +00006562 SDValue Op0 = getOperand(0);
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006563 if (Op0.getOpcode() != ISD::Constant)
6564 return nullptr;
6565
6566 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
6567 if (getOperand(i) != Op0)
Matt Arsenault985b9de2014-03-17 18:58:01 +00006568 return nullptr;
Matt Arsenault985b9de2014-03-17 18:58:01 +00006569
6570 return cast<ConstantSDNode>(Op0);
6571}
6572
Juergen Ributzka73844052014-01-13 20:51:35 +00006573bool BuildVectorSDNode::isConstant() const {
6574 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6575 unsigned Opc = getOperand(i).getOpcode();
6576 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6577 return false;
6578 }
6579 return true;
6580}
6581
Owen Anderson53aa7a92009-08-10 22:56:29 +00006582bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006583 // Find the first non-undef value in the shuffle mask.
6584 unsigned i, e;
6585 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6586 /* search */;
6587
Nate Begeman39b59db2009-04-29 18:13:31 +00006588 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006589
Nate Begeman5f829d82009-04-29 05:20:52 +00006590 // Make sure all remaining elements are either undef or the same as the first
6591 // non-undef value.
6592 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006593 if (Mask[i] >= 0 && Mask[i] != Idx)
6594 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006595 return true;
6596}
David Greene09851602010-01-20 20:13:31 +00006597
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006598#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006599static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006600 SmallPtrSet<const SDNode*, 32> &Visited,
6601 SmallPtrSet<const SDNode*, 32> &Checked) {
6602 // If this node has already been checked, don't check it again.
6603 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006604 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006605
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006606 // If a node has already been visited on this depth-first walk, reject it as
6607 // a cycle.
6608 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006609 dbgs() << "Offending node:\n";
6610 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006611 errs() << "Detected cycle in SelectionDAG\n";
6612 abort();
David Greene09851602010-01-20 20:13:31 +00006613 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006614
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006615 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6616 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006617
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006618 Checked.insert(N);
6619 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006620}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006621#endif
David Greene09851602010-01-20 20:13:31 +00006622
6623void llvm::checkForCycles(const llvm::SDNode *N) {
6624#ifdef XDEBUG
Alp Toker6a033742013-10-29 02:35:28 +00006625 assert(N && "Checking nonexistent SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006626 SmallPtrSet<const SDNode*, 32> visited;
6627 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006628 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006629#endif
6630}
6631
6632void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6633 checkForCycles(DAG->getRoot().getNode());
6634}