blob: ef8f13701df527e75b1e0563ca6eca6c5598400a [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
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +0000966SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT) {
967 if (VT.bitsLE(Op.getValueType()))
968 return getNode(ISD::TRUNCATE, SL, VT, Op);
969
970 TargetLowering::BooleanContent BType = TLI->getBooleanContents(VT.isVector());
971 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
972}
973
Andrew Trickef9de2a2013-05-25 02:42:55 +0000974SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000975 assert(!VT.isVector() &&
976 "getZeroExtendInReg should use the vector element type instead of "
977 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000978 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000979 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
980 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000981 VT.getSizeInBits());
982 return getNode(ISD::AND, DL, Op.getValueType(), Op,
983 getConstant(Imm, Op.getValueType()));
984}
985
Bob Wilsonc5890052009-01-22 17:39:32 +0000986/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
987///
Andrew Trickef9de2a2013-05-25 02:42:55 +0000988SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000989 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +0000990 SDValue NegOne =
991 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +0000992 return getNode(ISD::XOR, DL, VT, Val, NegOne);
993}
994
Pete Cooper7fd1d722014-05-12 23:26:58 +0000995SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
996 EVT EltVT = VT.getScalarType();
997 SDValue TrueValue;
998 switch (TLI->getBooleanContents(VT.isVector())) {
999 case TargetLowering::ZeroOrOneBooleanContent:
1000 case TargetLowering::UndefinedBooleanContent:
1001 TrueValue = getConstant(1, VT);
1002 break;
1003 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1004 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1005 VT);
1006 break;
1007 }
1008 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1009}
1010
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001011SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001012 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001013 assert((EltVT.getSizeInBits() >= 64 ||
1014 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1015 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001016 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001017}
1018
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001019SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1020{
1021 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001022}
1023
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001024SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1025 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001026 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001027
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001028 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001029 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001030
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001031 const TargetLowering *TLI = TM.getTargetLowering();
1032
Duncan Sandsf2641e12011-09-06 19:07:46 +00001033 // In some cases the vector type is legal but the element type is illegal and
1034 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1035 // inserted value (the type does not need to match the vector element type).
1036 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001037 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001038 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001039 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001040 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1041 Elt = ConstantInt::get(*getContext(), NewVal);
1042 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001043 // In other cases the element type is illegal and needs to be expanded, for
1044 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1045 // the value into n parts and use a vector type with n-times the elements.
1046 // Then bitcast to the type requested.
1047 // Legalizing constants too early makes the DAGCombiner's job harder so we
1048 // only legalize if the DAG tells us we must produce legal types.
1049 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1050 TLI->getTypeAction(*getContext(), EltVT) ==
1051 TargetLowering::TypeExpandInteger) {
1052 APInt NewVal = Elt->getValue();
1053 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1054 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1055 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1056 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1057
1058 // Check the temporary vector is the correct size. If this fails then
1059 // getTypeToTransformTo() probably returned a type whose size (in bits)
1060 // isn't a power-of-2 factor of the requested type size.
1061 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1062
1063 SmallVector<SDValue, 2> EltParts;
1064 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1065 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1066 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001067 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001068 }
1069
1070 // EltParts is currently in little endian order. If we actually want
1071 // big-endian order then reverse it now.
1072 if (TLI->isBigEndian())
1073 std::reverse(EltParts.begin(), EltParts.end());
1074
1075 // The elements must be reversed when the element order is different
1076 // to the endianness of the elements (because the BITCAST is itself a
1077 // vector shuffle in this situation). However, we do not need any code to
1078 // perform this reversal because getConstant() is producing a vector
1079 // splat.
1080 // This situation occurs in MIPS MSA.
1081
1082 SmallVector<SDValue, 8> Ops;
1083 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1084 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1085
1086 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1087 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001088 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001089 return Result;
1090 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001091
1092 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1093 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001094 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001095 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001096 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001097 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001098 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001099 void *IP = nullptr;
1100 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001101 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001102 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001103 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001104
Dan Gohman7a7742c2007-12-12 22:21:26 +00001105 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001106 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001107 CSEMap.InsertNode(N, IP);
1108 AllNodes.push_back(N);
1109 }
1110
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001111 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001112 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001113 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001114 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001115 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001116 }
1117 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001118}
1119
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001120SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001121 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001122}
1123
1124
Owen Anderson53aa7a92009-08-10 22:56:29 +00001125SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001126 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001127}
1128
Owen Anderson53aa7a92009-08-10 22:56:29 +00001129SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001130 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001131
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001132 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001133
Chris Lattner381dddc2005-02-17 20:17:32 +00001134 // Do the map lookup using the actual bit pattern for the floating point
1135 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1136 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001137 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001138 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001139 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001140 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001141 void *IP = nullptr;
1142 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001143 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001144 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001145 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001146
Evan Cheng9458e6a2007-06-29 21:36:04 +00001147 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001148 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001149 CSEMap.InsertNode(N, IP);
1150 AllNodes.push_back(N);
1151 }
1152
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001153 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001154 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001155 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001156 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001157 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001158 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001159 }
1160 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001161}
1162
Owen Anderson53aa7a92009-08-10 22:56:29 +00001163SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001164 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001165 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001166 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001167 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001168 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001169 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1170 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001171 bool ignored;
1172 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001173 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001174 &ignored);
1175 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001176 } else
1177 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001178}
1179
Andrew Trickef9de2a2013-05-25 02:42:55 +00001180SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001181 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001182 bool isTargetGA,
1183 unsigned char TargetFlags) {
1184 assert((TargetFlags == 0 || isTargetGA) &&
1185 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001186 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001187
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001188 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001189 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001190 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001191 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001192
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001193 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1194 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001195 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001196 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Rafael Espindolae0098922014-05-16 22:37:03 +00001197 GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasee());
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001198 }
1199
Chris Lattner8e34f982009-06-25 21:21:14 +00001200 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001201 if (GVar && GVar->isThreadLocal())
1202 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1203 else
1204 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001205
Jim Laskeyf576b422006-10-27 23:46:08 +00001206 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001207 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001208 ID.AddPointer(GV);
1209 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001210 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001211 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001212 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001213 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001214 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001215
Andrew Trickef9de2a2013-05-25 02:42:55 +00001216 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1217 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001218 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001219 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001220 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001221 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001222}
1223
Owen Anderson53aa7a92009-08-10 22:56:29 +00001224SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001225 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001226 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001227 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001228 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001229 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001230 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001231 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001232
Dan Gohman01c65a22010-03-18 18:49:47 +00001233 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001234 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001235 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001236 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001237}
1238
Owen Anderson53aa7a92009-08-10 22:56:29 +00001239SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001240 unsigned char TargetFlags) {
1241 assert((TargetFlags == 0 || isTarget) &&
1242 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001243 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001244 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001245 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001246 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001247 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001248 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001249 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001250 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001251
Dan Gohman01c65a22010-03-18 18:49:47 +00001252 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1253 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001254 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001255 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001256 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001257}
1258
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001259SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001260 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001261 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001262 unsigned char TargetFlags) {
1263 assert((TargetFlags == 0 || isTarget) &&
1264 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001265 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001266 Alignment =
1267 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001268 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001269 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001270 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001271 ID.AddInteger(Alignment);
1272 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001273 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001274 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001275 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001276 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001277 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001278
Dan Gohman01c65a22010-03-18 18:49:47 +00001279 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1280 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001281 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001282 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001283 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001284}
1285
Chris Lattner061a1ea2005-01-07 07:46:32 +00001286
Owen Anderson53aa7a92009-08-10 22:56:29 +00001287SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001288 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001289 bool isTarget,
1290 unsigned char TargetFlags) {
1291 assert((TargetFlags == 0 || isTarget) &&
1292 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001293 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001294 Alignment =
1295 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001296 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001297 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001298 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001299 ID.AddInteger(Alignment);
1300 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001301 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001302 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001303 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001304 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001305 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001306
Dan Gohman01c65a22010-03-18 18:49:47 +00001307 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1308 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001309 CSEMap.InsertNode(N, IP);
1310 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001311 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001312}
1313
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001314SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1315 unsigned char TargetFlags) {
1316 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001317 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001318 ID.AddInteger(Index);
1319 ID.AddInteger(Offset);
1320 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001321 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001322 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1323 return SDValue(E, 0);
1324
1325 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1326 TargetFlags);
1327 CSEMap.InsertNode(N, IP);
1328 AllNodes.push_back(N);
1329 return SDValue(N, 0);
1330}
1331
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001332SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001333 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001334 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001335 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001336 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001337 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001338 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001339
Dan Gohman01c65a22010-03-18 18:49:47 +00001340 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001341 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001342 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001343 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001344}
1345
Owen Anderson53aa7a92009-08-10 22:56:29 +00001346SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001347 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1348 ValueTypeNodes.size())
1349 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001350
Duncan Sands13237ac2008-06-06 12:08:01 +00001351 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001352 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001353
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001354 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001355 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001356 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001358}
1359
Owen Anderson53aa7a92009-08-10 22:56:29 +00001360SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001361 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001362 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001363 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001364 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001365 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001366}
1367
Owen Anderson53aa7a92009-08-10 22:56:29 +00001368SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001369 unsigned char TargetFlags) {
1370 SDNode *&N =
1371 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1372 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001373 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001374 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001375 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001376 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001377}
1378
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001379SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001380 if ((unsigned)Cond >= CondCodeNodes.size())
1381 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001382
Craig Topperc0196b12014-04-14 00:51:57 +00001383 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001384 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001385 CondCodeNodes[Cond] = N;
1386 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001387 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001388
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001389 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001390}
1391
Nate Begeman5f829d82009-04-29 05:20:52 +00001392// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1393// the shuffle mask M that point at N1 to point at N2, and indices that point
1394// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001395static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1396 std::swap(N1, N2);
1397 int NElts = M.size();
1398 for (int i = 0; i != NElts; ++i) {
1399 if (M[i] >= NElts)
1400 M[i] -= NElts;
1401 else if (M[i] >= 0)
1402 M[i] += NElts;
1403 }
1404}
1405
Andrew Trickef9de2a2013-05-25 02:42:55 +00001406SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001407 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001408 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1409 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001410
1411 // Canonicalize shuffle undef, undef -> undef
1412 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001413 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001414
Eric Christopherdfda92b2009-08-22 00:40:45 +00001415 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001416 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001417 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001418 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001419 for (unsigned i = 0; i != NElts; ++i) {
1420 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001421 MaskVec.push_back(Mask[i]);
1422 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001423
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001424 // Canonicalize shuffle v, v -> v, undef
1425 if (N1 == N2) {
1426 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001427 for (unsigned i = 0; i != NElts; ++i)
1428 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001429 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001430
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001431 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1432 if (N1.getOpcode() == ISD::UNDEF)
1433 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001434
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001435 // Canonicalize all index into lhs, -> shuffle lhs, undef
1436 // Canonicalize all index into rhs, -> shuffle rhs, undef
1437 bool AllLHS = true, AllRHS = true;
1438 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001439 for (unsigned i = 0; i != NElts; ++i) {
1440 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001441 if (N2Undef)
1442 MaskVec[i] = -1;
1443 else
1444 AllLHS = false;
1445 } else if (MaskVec[i] >= 0) {
1446 AllRHS = false;
1447 }
1448 }
1449 if (AllLHS && AllRHS)
1450 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001451 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001452 N2 = getUNDEF(VT);
1453 if (AllRHS) {
1454 N1 = getUNDEF(VT);
1455 commuteShuffle(N1, N2, MaskVec);
1456 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001457
Craig Topper9a39b072013-08-08 08:03:12 +00001458 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001459 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001460 for (unsigned i = 0; i != NElts; ++i) {
1461 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001462 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001463 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001464 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001465
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001466 // Shuffling a constant splat doesn't change the result.
1467 if (N2Undef && N1.getOpcode() == ISD::BUILD_VECTOR)
1468 if (cast<BuildVectorSDNode>(N1)->getConstantSplatValue())
1469 return N1;
1470
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001471 FoldingSetNodeID ID;
1472 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001473 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001474 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001475 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001476
Craig Topperc0196b12014-04-14 00:51:57 +00001477 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001478 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001479 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001480
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001481 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1482 // SDNode doesn't have access to it. This memory will be "leaked" when
1483 // the node is deallocated, but recovered when the NodeAllocator is released.
1484 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1485 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001486
Dan Gohman01c65a22010-03-18 18:49:47 +00001487 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001488 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1489 dl.getDebugLoc(), N1, N2,
1490 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001491 CSEMap.InsertNode(N, IP);
1492 AllNodes.push_back(N);
1493 return SDValue(N, 0);
1494}
1495
Andrew Trickef9de2a2013-05-25 02:42:55 +00001496SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001497 SDValue Val, SDValue DTy,
1498 SDValue STy, SDValue Rnd, SDValue Sat,
1499 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001500 // If the src and dest types are the same and the conversion is between
1501 // integer types of the same sign or two floats, no conversion is necessary.
1502 if (DTy == STy &&
1503 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001504 return Val;
1505
1506 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001507 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001508 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001509 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001511 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001512
Jack Carter170a5f22013-09-09 22:02:08 +00001513 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1514 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001515 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001516 CSEMap.InsertNode(N, IP);
1517 AllNodes.push_back(N);
1518 return SDValue(N, 0);
1519}
1520
Owen Anderson53aa7a92009-08-10 22:56:29 +00001521SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001522 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001523 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001524 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001525 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001526 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001527 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001528
Dan Gohman01c65a22010-03-18 18:49:47 +00001529 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001530 CSEMap.InsertNode(N, IP);
1531 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001532 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001533}
1534
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001535SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1536 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001537 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001538 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001539 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001540 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1541 return SDValue(E, 0);
1542
1543 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1544 CSEMap.InsertNode(N, IP);
1545 AllNodes.push_back(N);
1546 return SDValue(N, 0);
1547}
1548
Andrew Trickef9de2a2013-05-25 02:42:55 +00001549SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001550 FoldingSetNodeID ID;
1551 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001552 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001553 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001554 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001555 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001556 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001557
Jack Carter170a5f22013-09-09 22:02:08 +00001558 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1559 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001560 CSEMap.InsertNode(N, IP);
1561 AllNodes.push_back(N);
1562 return SDValue(N, 0);
1563}
1564
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001565
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001566SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001567 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001568 bool isTarget,
1569 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001570 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1571
1572 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001573 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001574 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001575 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001576 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001577 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001578 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001579 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001580
Michael Liaoabb87d42012-09-12 21:43:09 +00001581 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1582 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001583 CSEMap.InsertNode(N, IP);
1584 AllNodes.push_back(N);
1585 return SDValue(N, 0);
1586}
1587
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001588SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001589 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001590 "SrcValue is not a pointer?");
1591
Jim Laskeyf576b422006-10-27 23:46:08 +00001592 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001593 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001594 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001595
Craig Topperc0196b12014-04-14 00:51:57 +00001596 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001597 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001598 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001599
Dan Gohman01c65a22010-03-18 18:49:47 +00001600 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001601 CSEMap.InsertNode(N, IP);
1602 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001603 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001604}
1605
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001606/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1607SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1608 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001609 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001610 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001611
Craig Topperc0196b12014-04-14 00:51:57 +00001612 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001613 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1614 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001615
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001616 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1617 CSEMap.InsertNode(N, IP);
1618 AllNodes.push_back(N);
1619 return SDValue(N, 0);
1620}
1621
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001622/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1623SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1624 unsigned SrcAS, unsigned DestAS) {
1625 SDValue Ops[] = {Ptr};
1626 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001627 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001628 ID.AddInteger(SrcAS);
1629 ID.AddInteger(DestAS);
1630
Craig Topperc0196b12014-04-14 00:51:57 +00001631 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001632 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1633 return SDValue(E, 0);
1634
1635 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1636 dl.getDebugLoc(),
1637 VT, Ptr, SrcAS, DestAS);
1638 CSEMap.InsertNode(N, IP);
1639 AllNodes.push_back(N);
1640 return SDValue(N, 0);
1641}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001642
Duncan Sands41826032009-01-31 15:50:11 +00001643/// getShiftAmountOperand - Return the specified value casted to
1644/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001645SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001646 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001647 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001648 if (OpTy == ShTy || OpTy.isVector()) return Op;
1649
1650 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001651 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001652}
1653
Chris Lattner9eb7a822007-10-15 17:47:20 +00001654/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1655/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001656SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001657 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001658 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001659 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001660 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001661 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001662 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001663
David Greene1fbe0542009-11-12 20:49:22 +00001664 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001665 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001666}
1667
Duncan Sands445071c2008-12-09 21:33:20 +00001668/// CreateStackTemporary - Create a stack temporary suitable for holding
1669/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001670SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001671 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1672 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001673 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1674 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001675 const TargetLowering *TLI = TM.getTargetLowering();
1676 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001677 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1678 TD->getPrefTypeAlignment(Ty2));
1679
1680 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001681 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001682 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001683}
1684
Owen Anderson53aa7a92009-08-10 22:56:29 +00001685SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001686 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001687 // These setcc operations always fold.
1688 switch (Cond) {
1689 default: break;
1690 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001691 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001692 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001693 case ISD::SETTRUE2: {
1694 const TargetLowering *TLI = TM.getTargetLowering();
1695 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1696 return getConstant(
1697 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1698 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001699
Chris Lattner393d96a2006-04-27 05:01:07 +00001700 case ISD::SETOEQ:
1701 case ISD::SETOGT:
1702 case ISD::SETOGE:
1703 case ISD::SETOLT:
1704 case ISD::SETOLE:
1705 case ISD::SETONE:
1706 case ISD::SETO:
1707 case ISD::SETUO:
1708 case ISD::SETUEQ:
1709 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001710 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001711 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001712 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001713
Gabor Greiff304a7a2008-08-28 21:40:38 +00001714 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001715 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001716 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001717 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001718
Chris Lattner061a1ea2005-01-07 07:46:32 +00001719 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001720 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001721 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1722 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001723 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1724 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1725 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1726 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1727 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1728 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1729 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1730 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001731 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001732 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001733 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001734 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1735 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001736 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001737 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001738 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001739 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001740 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001741 // fall through
1742 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001743 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001744 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001745 // fall through
1746 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001747 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001748 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001749 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001750 // fall through
1751 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001752 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001753 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001754 // fall through
1755 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001756 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001757 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001758 // fall through
1759 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001760 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001761 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001762 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001763 // fall through
1764 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001765 R==APFloat::cmpEqual, VT);
1766 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1767 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1768 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1769 R==APFloat::cmpEqual, VT);
1770 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1771 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1772 R==APFloat::cmpLessThan, VT);
1773 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1774 R==APFloat::cmpUnordered, VT);
1775 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1776 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001777 }
1778 } else {
1779 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001780 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1781 MVT CompVT = N1.getValueType().getSimpleVT();
1782 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1783 return SDValue();
1784
1785 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001786 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001787 }
1788
Chris Lattnerd47675e2005-08-09 20:20:18 +00001789 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001790 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001791}
1792
Dan Gohman1f372ed2008-02-25 21:11:39 +00001793/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1794/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001795bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001796 // This predicate is not safe for vector operations.
1797 if (Op.getValueType().isVector())
1798 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001799
Dan Gohman1d459e42009-12-11 21:31:27 +00001800 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001801 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1802}
1803
Dan Gohman309d3d52007-06-22 14:59:07 +00001804/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1805/// this predicate to simplify operations downstream. Mask is known to be zero
1806/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001807bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001808 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001809 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001810 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001811 return (KnownZero & Mask) == Mask;
1812}
1813
Jay Foada0653a32014-05-14 21:14:37 +00001814/// Determine which bits of Op are known to be either zero or one and return
1815/// them in the KnownZero/KnownOne bitsets.
1816void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1817 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001818 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001819 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001820
Dan Gohmanf990faf2008-02-13 00:35:47 +00001821 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001822 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001823 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001824
Dan Gohmanf990faf2008-02-13 00:35:47 +00001825 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001826
1827 switch (Op.getOpcode()) {
1828 case ISD::Constant:
1829 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001830 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1831 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001832 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001833 case ISD::AND:
1834 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001835 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1836 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001837
1838 // Output known-1 bits are only known if set in both the LHS & RHS.
1839 KnownOne &= KnownOne2;
1840 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1841 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001842 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001843 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001844 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1845 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001846
Dan Gohman309d3d52007-06-22 14:59:07 +00001847 // Output known-0 bits are only known if clear in both the LHS & RHS.
1848 KnownZero &= KnownZero2;
1849 // Output known-1 are known to be set if set in either the LHS | RHS.
1850 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001851 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001852 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001853 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1854 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001855
Dan Gohman309d3d52007-06-22 14:59:07 +00001856 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001857 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001858 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1859 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1860 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001861 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001862 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001863 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00001864 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1865 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001866
1867 // If low bits are zero in either operand, output low known-0 bits.
1868 // Also compute a conserative estimate for high known-0 bits.
1869 // More trickiness is possible, but this is sufficient for the
1870 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001871 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001872 unsigned TrailZ = KnownZero.countTrailingOnes() +
1873 KnownZero2.countTrailingOnes();
1874 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001875 KnownZero2.countLeadingOnes(),
1876 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001877
1878 TrailZ = std::min(TrailZ, BitWidth);
1879 LeadZ = std::min(LeadZ, BitWidth);
1880 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1881 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001882 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001883 }
1884 case ISD::UDIV: {
1885 // For the purposes of computing leading zeros we can conservatively
1886 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001887 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00001888 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001889 unsigned LeadZ = KnownZero2.countLeadingOnes();
1890
Jay Foad25a5e4c2010-12-01 08:53:58 +00001891 KnownOne2.clearAllBits();
1892 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00001893 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001894 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1895 if (RHSUnknownLeadingOnes != BitWidth)
1896 LeadZ = std::min(BitWidth,
1897 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001898
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001899 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001900 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001901 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001902 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00001903 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1904 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001905
Dan Gohman309d3d52007-06-22 14:59:07 +00001906 // Only known if known in both the LHS and RHS.
1907 KnownOne &= KnownOne2;
1908 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001909 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001910 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00001911 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1912 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001913
Dan Gohman309d3d52007-06-22 14:59:07 +00001914 // Only known if known in both the LHS and RHS.
1915 KnownOne &= KnownOne2;
1916 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001917 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001918 case ISD::SADDO:
1919 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001920 case ISD::SSUBO:
1921 case ISD::USUBO:
1922 case ISD::SMULO:
1923 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001924 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00001925 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001926 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001927 case ISD::SETCC:
1928 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001929 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001930 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001931 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00001932 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001933 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001934 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001935 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001936 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001937
1938 // If the shift count is an invalid immediate, don't do anything.
1939 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00001940 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00001941
Jay Foada0653a32014-05-14 21:14:37 +00001942 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00001943 KnownZero <<= ShAmt;
1944 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00001945 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00001946 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001947 }
Jay Foad5a29c362014-05-15 12:12:55 +00001948 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001949 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001950 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001951 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001952 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001953
Dan Gohman9db0aa82008-02-26 18:50:50 +00001954 // If the shift count is an invalid immediate, don't do anything.
1955 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00001956 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00001957
Jay Foada0653a32014-05-14 21:14:37 +00001958 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00001959 KnownZero = KnownZero.lshr(ShAmt);
1960 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001961
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001962 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001963 KnownZero |= HighBits; // High bits known zero.
1964 }
Jay Foad5a29c362014-05-15 12:12:55 +00001965 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001966 case ISD::SRA:
1967 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001968 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001969
Dan Gohman9db0aa82008-02-26 18:50:50 +00001970 // If the shift count is an invalid immediate, don't do anything.
1971 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00001972 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00001973
Dan Gohman309d3d52007-06-22 14:59:07 +00001974 // If any of the demanded bits are produced by the sign extension, we also
1975 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001976 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001977
Jay Foada0653a32014-05-14 21:14:37 +00001978 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00001979 KnownZero = KnownZero.lshr(ShAmt);
1980 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001981
Dan Gohman309d3d52007-06-22 14:59:07 +00001982 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001983 APInt SignBit = APInt::getSignBit(BitWidth);
1984 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001985
Dan Gohmanb717fda2008-02-20 16:30:17 +00001986 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001987 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001988 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001989 KnownOne |= HighBits; // New bits are known one.
1990 }
1991 }
Jay Foad5a29c362014-05-15 12:12:55 +00001992 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001993 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001994 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001995 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001996
1997 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00001998 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001999 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002000
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002001 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002002 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002003
Dan Gohman309d3d52007-06-22 14:59:07 +00002004 // If the sign extended bits are demanded, we know that the sign
2005 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002006 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002007 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002008 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002009
Jay Foada0653a32014-05-14 21:14:37 +00002010 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002011 KnownOne &= InputDemandedBits;
2012 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002013
Dan Gohman309d3d52007-06-22 14:59:07 +00002014 // If the sign bit of the input is known set or clear, then we know the
2015 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002016 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002017 KnownZero |= NewBits;
2018 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002019 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002020 KnownOne |= NewBits;
2021 KnownZero &= ~NewBits;
2022 } else { // Input sign bit unknown
2023 KnownZero &= ~NewBits;
2024 KnownOne &= ~NewBits;
2025 }
Jay Foad5a29c362014-05-15 12:12:55 +00002026 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002027 }
2028 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002029 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002030 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002031 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002032 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002033 unsigned LowBits = Log2_32(BitWidth)+1;
2034 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002035 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002036 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002037 }
2038 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002039 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002040 // If this is a ZEXTLoad and we are looking at the loaded value.
2041 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002042 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002043 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002044 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002045 } else if (const MDNode *Ranges = LD->getRanges()) {
Jay Foada0653a32014-05-14 21:14:37 +00002046 computeKnownBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002047 }
Jay Foad5a29c362014-05-15 12:12:55 +00002048 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002049 }
2050 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002051 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002052 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002053 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002054 KnownZero = KnownZero.trunc(InBits);
2055 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002056 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002057 KnownZero = KnownZero.zext(BitWidth);
2058 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002059 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002060 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002061 }
2062 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002063 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002064 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002065 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002066
Jay Foad583abbc2010-12-07 08:25:19 +00002067 KnownZero = KnownZero.trunc(InBits);
2068 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002069 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002070
2071 // Note if the sign bit is known to be zero or one.
2072 bool SignBitKnownZero = KnownZero.isNegative();
2073 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002074
Jay Foad583abbc2010-12-07 08:25:19 +00002075 KnownZero = KnownZero.zext(BitWidth);
2076 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002077
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002078 // If the sign bit is known zero or one, the top bits match.
2079 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002080 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002081 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002082 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002083 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002084 }
2085 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002086 EVT InVT = Op.getOperand(0).getValueType();
2087 unsigned InBits = InVT.getScalarType().getSizeInBits();
2088 KnownZero = KnownZero.trunc(InBits);
2089 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002090 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002091 KnownZero = KnownZero.zext(BitWidth);
2092 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002093 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002094 }
2095 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002096 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002097 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002098 KnownZero = KnownZero.zext(InBits);
2099 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002100 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002101 KnownZero = KnownZero.trunc(BitWidth);
2102 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002103 break;
2104 }
2105 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002106 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002107 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002108 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002109 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002110 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002111 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002112 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002113 case ISD::FGETSIGN:
2114 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002115 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002116 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002117
Dan Gohman72ec3f42008-04-28 17:02:21 +00002118 case ISD::SUB: {
2119 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2120 // We know that the top bits of C-X are clear if X contains less bits
2121 // than C (i.e. no wrap-around can happen). For example, 20-X is
2122 // positive if we can prove that X is >= 0 and < 16.
2123 if (CLHS->getAPIntValue().isNonNegative()) {
2124 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2125 // NLZ can't be BitWidth with no sign bit
2126 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002127 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002128
2129 // If all of the MaskV bits are known to be zero, then we know the
2130 // output top bits are zero, because we now know that the output is
2131 // from [0-C].
2132 if ((KnownZero2 & MaskV) == MaskV) {
2133 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2134 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002135 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002136 }
2137 }
2138 }
2139 }
2140 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002141 case ISD::ADD:
2142 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002143 // Output known-0 bits are known if clear or set in both the low clear bits
2144 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2145 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002146 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002147 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2148
Jay Foada0653a32014-05-14 21:14:37 +00002149 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
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);
Jay Foad5a29c362014-05-15 12:12:55 +00002155 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002156 }
2157
2158 // With ADDE, a carry bit may be added in, so we can only use this
2159 // information if we know (at least) that the low two bits are clear. We
2160 // then return to the caller that the low bit is unknown but that other bits
2161 // are known zero.
2162 if (KnownZeroOut >= 2) // ADDE
2163 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002164 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002165 }
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;
Jay Foada0653a32014-05-14 21:14:37 +00002171 computeKnownBits(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 }
Jay Foad5a29c362014-05-15 12:12:55 +00002189 break;
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;
Jay Foada0653a32014-05-14 21:14:37 +00002196 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002197 break;
2198 }
2199 }
2200
2201 // Since the result is less than or equal to either operand, any leading
2202 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002203 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2204 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002205
2206 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2207 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002208 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002209 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002210 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002211 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002212 case ISD::FrameIndex:
2213 case ISD::TargetFrameIndex:
2214 if (unsigned Align = InferPtrAlignment(Op)) {
2215 // The low bits are known zero if the pointer is aligned.
2216 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002217 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002218 }
2219 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002220
Dan Gohman309d3d52007-06-22 14:59:07 +00002221 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002222 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2223 break;
2224 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002225 case ISD::INTRINSIC_WO_CHAIN:
2226 case ISD::INTRINSIC_W_CHAIN:
2227 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002228 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002229 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002230 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002231 }
Jay Foad5a29c362014-05-15 12:12:55 +00002232
2233 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002234}
2235
2236/// ComputeNumSignBits - Return the number of times the sign bit of the
2237/// register is replicated into the other bits. We know that at least 1 bit
2238/// is always equal to the sign bit (itself), but other cases can give us
2239/// information. For example, immediately after an "SRA X, 2", we know that
2240/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002241unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002242 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002243 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002244 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002245 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002246 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002247 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002248
Dan Gohman309d3d52007-06-22 14:59:07 +00002249 if (Depth == 6)
2250 return 1; // Limit search depth.
2251
2252 switch (Op.getOpcode()) {
2253 default: break;
2254 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002255 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002256 return VTBits-Tmp+1;
2257 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002258 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002259 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002260
Dan Gohman309d3d52007-06-22 14:59:07 +00002261 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002262 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002263 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002264 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002265
Dan Gohman309d3d52007-06-22 14:59:07 +00002266 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002267 Tmp =
2268 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002269 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002270
Dan Gohman309d3d52007-06-22 14:59:07 +00002271 case ISD::SIGN_EXTEND_INREG:
2272 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002273 Tmp =
2274 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002275 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002276
Dan Gohman309d3d52007-06-22 14:59:07 +00002277 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2278 return std::max(Tmp, Tmp2);
2279
2280 case ISD::SRA:
2281 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2282 // SRA X, C -> adds C sign bits.
2283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002284 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002285 if (Tmp > VTBits) Tmp = VTBits;
2286 }
2287 return Tmp;
2288 case ISD::SHL:
2289 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2290 // shl destroys sign bits.
2291 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002292 if (C->getZExtValue() >= VTBits || // Bad shift.
2293 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2294 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002295 }
2296 break;
2297 case ISD::AND:
2298 case ISD::OR:
2299 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002300 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002301 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002302 if (Tmp != 1) {
2303 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2304 FirstAnswer = std::min(Tmp, Tmp2);
2305 // We computed what we know about the sign bits as our first
2306 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002307 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002308 }
2309 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002310
2311 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002312 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002313 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002314 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002315 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002316
2317 case ISD::SADDO:
2318 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002319 case ISD::SSUBO:
2320 case ISD::USUBO:
2321 case ISD::SMULO:
2322 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002323 if (Op.getResNo() != 1)
2324 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002325 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002326 case ISD::SETCC:
2327 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002328 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002329 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002330 return VTBits;
2331 break;
2332 case ISD::ROTL:
2333 case ISD::ROTR:
2334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002335 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002336
Dan Gohman309d3d52007-06-22 14:59:07 +00002337 // Handle rotate right by N like a rotate left by 32-N.
2338 if (Op.getOpcode() == ISD::ROTR)
2339 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2340
2341 // If we aren't rotating out all of the known-in sign bits, return the
2342 // number that are left. This handles rotl(sext(x), 1) for example.
2343 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2344 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2345 }
2346 break;
2347 case ISD::ADD:
2348 // Add can have at most one carry bit. Thus we know that the output
2349 // is, at worst, one more bit than the inputs.
2350 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2351 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002352
Dan Gohman309d3d52007-06-22 14:59:07 +00002353 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002354 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002355 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002356 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002357 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002358
Dan Gohman309d3d52007-06-22 14:59:07 +00002359 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2360 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002361 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002362 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002363
Dan Gohman309d3d52007-06-22 14:59:07 +00002364 // If we are subtracting one from a positive number, there is no carry
2365 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002366 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002367 return Tmp;
2368 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002369
Dan Gohman309d3d52007-06-22 14:59:07 +00002370 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2371 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002372 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002373
Dan Gohman309d3d52007-06-22 14:59:07 +00002374 case ISD::SUB:
2375 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2376 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002377
Dan Gohman309d3d52007-06-22 14:59:07 +00002378 // Handle NEG.
2379 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002380 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002381 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002382 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002383 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2384 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002385 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002386 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002387
Dan Gohman309d3d52007-06-22 14:59:07 +00002388 // If the input is known to be positive (the sign bit is known clear),
2389 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002390 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002391 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002392
Dan Gohman309d3d52007-06-22 14:59:07 +00002393 // Otherwise, we treat this like a SUB.
2394 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002395
Dan Gohman309d3d52007-06-22 14:59:07 +00002396 // Sub can have at most one carry bit. Thus we know that the output
2397 // is, at worst, one more bit than the inputs.
2398 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2399 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002400 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002401 case ISD::TRUNCATE:
2402 // FIXME: it's tricky to do anything useful for this, but it is an important
2403 // case for targets like X86.
2404 break;
2405 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002406
Nadav Rotem4536d582013-03-20 22:53:44 +00002407 // If we are looking at the loaded value of the SDNode.
2408 if (Op.getResNo() == 0) {
2409 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2410 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2411 unsigned ExtType = LD->getExtensionType();
2412 switch (ExtType) {
2413 default: break;
2414 case ISD::SEXTLOAD: // '17' bits known
2415 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2416 return VTBits-Tmp+1;
2417 case ISD::ZEXTLOAD: // '16' bits known
2418 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2419 return VTBits-Tmp;
2420 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002421 }
2422 }
2423
2424 // Allow the target to implement this method for its nodes.
2425 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002426 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002427 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2428 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002429 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002430 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002431 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002432
Dan Gohman309d3d52007-06-22 14:59:07 +00002433 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2434 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002435 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002436 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002437
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002438 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002439 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002440 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002441 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002442 Mask = KnownOne;
2443 } else {
2444 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002445 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002447
Dan Gohman309d3d52007-06-22 14:59:07 +00002448 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2449 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002450 Mask = ~Mask;
2451 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002452 // Return # leading zeros. We use 'min' here in case Val was zero before
2453 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002454 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002455}
2456
Chris Lattner46c01a32011-02-13 22:25:43 +00002457/// isBaseWithConstantOffset - Return true if the specified operand is an
2458/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2459/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2460/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002461/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002462bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2463 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2464 !isa<ConstantSDNode>(Op.getOperand(1)))
2465 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002466
2467 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002468 !MaskedValueIsZero(Op.getOperand(0),
2469 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2470 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002471
Chris Lattner46c01a32011-02-13 22:25:43 +00002472 return true;
2473}
2474
2475
Dan Gohmand0d5e682009-09-03 20:34:31 +00002476bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2477 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002478 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002479 return true;
2480
2481 // If the value is a constant, we can obviously see if it is a NaN or not.
2482 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2483 return !C->getValueAPF().isNaN();
2484
2485 // TODO: Recognize more cases here.
2486
2487 return false;
2488}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002489
Dan Gohman38605212010-02-24 06:52:40 +00002490bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2491 // If the value is a constant, we can obviously see if it is a zero or not.
2492 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2493 return !C->isZero();
2494
2495 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002496 switch (Op.getOpcode()) {
2497 default: break;
2498 case ISD::OR:
2499 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2500 return !C->isNullValue();
2501 break;
2502 }
Dan Gohman38605212010-02-24 06:52:40 +00002503
2504 return false;
2505}
2506
2507bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2508 // Check the obvious case.
2509 if (A == B) return true;
2510
2511 // For for negative and positive zero.
2512 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2513 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2514 if (CA->isZero() && CB->isZero()) return true;
2515
2516 // Otherwise they may not be equal.
2517 return false;
2518}
2519
Chris Lattner061a1ea2005-01-07 07:46:32 +00002520/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002521///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002522SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002523 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002524 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002525 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002526 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002527 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002528
Jack Carter170a5f22013-09-09 22:02:08 +00002529 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2530 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002531 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002532
Chris Lattnerfcb16472006-08-11 18:38:11 +00002533 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002534#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002535 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002536#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002537 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002538}
2539
Andrew Trickef9de2a2013-05-25 02:42:55 +00002540SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002541 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002542 // Constant fold unary operations with an integer constant operand. Even
2543 // opaque constant will be folded, because the folding of unary operations
2544 // doesn't create new constants with different values. Nevertheless, the
2545 // opaque flag is preserved during folding to prevent future folding with
2546 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002547 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002548 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002549 switch (Opcode) {
2550 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002551 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002552 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2553 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002554 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002555 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002556 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002557 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2558 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002559 case ISD::UINT_TO_FP:
2560 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002561 APFloat apf(EVTToAPFloatSemantics(VT),
2562 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002563 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002564 Opcode==ISD::SINT_TO_FP,
2565 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002566 return getConstantFP(apf, VT);
2567 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002568 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002569 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002570 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002571 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002572 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002573 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002574 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002575 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2576 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002577 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002578 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2579 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002580 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002581 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002582 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2583 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002584 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002585 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002586 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2587 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002588 }
2589 }
2590
Dale Johannesen446b9002007-08-31 23:34:27 +00002591 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002592 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002593 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002594 switch (Opcode) {
2595 case ISD::FNEG:
2596 V.changeSign();
2597 return getConstantFP(V, VT);
2598 case ISD::FABS:
2599 V.clearSign();
2600 return getConstantFP(V, VT);
2601 case ISD::FCEIL: {
2602 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2603 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002604 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002605 break;
2606 }
2607 case ISD::FTRUNC: {
2608 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2609 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002610 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002611 break;
2612 }
2613 case ISD::FFLOOR: {
2614 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2615 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002616 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002617 break;
2618 }
2619 case ISD::FP_EXTEND: {
2620 bool ignored;
2621 // This can return overflow, underflow, or inexact; we don't care.
2622 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002623 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002624 APFloat::rmNearestTiesToEven, &ignored);
2625 return getConstantFP(V, VT);
2626 }
2627 case ISD::FP_TO_SINT:
2628 case ISD::FP_TO_UINT: {
2629 integerPart x[2];
2630 bool ignored;
2631 assert(integerPartWidth >= 64);
2632 // FIXME need to be more flexible about rounding mode.
2633 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2634 Opcode==ISD::FP_TO_SINT,
2635 APFloat::rmTowardZero, &ignored);
2636 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002637 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002638 APInt api(VT.getSizeInBits(), x);
2639 return getConstant(api, VT);
2640 }
2641 case ISD::BITCAST:
2642 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2643 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2644 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2645 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2646 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002647 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002648 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002649
Gabor Greiff304a7a2008-08-28 21:40:38 +00002650 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002651 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002652 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002653 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002654 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002655 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002656 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002657 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002658 assert(VT.isFloatingPoint() &&
2659 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002660 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002661 assert((!VT.isVector() ||
2662 VT.getVectorNumElements() ==
2663 Operand.getValueType().getVectorNumElements()) &&
2664 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002665 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002666 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002667 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002668 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002669 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002670 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002671 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002672 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2673 "Invalid sext node, dst < src!");
2674 assert((!VT.isVector() ||
2675 VT.getVectorNumElements() ==
2676 Operand.getValueType().getVectorNumElements()) &&
2677 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002678 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2679 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2680 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002681 // sext(undef) = 0, because the top bits will all be the same.
2682 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002683 break;
2684 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002685 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002686 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002687 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002688 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2689 "Invalid zext node, dst < src!");
2690 assert((!VT.isVector() ||
2691 VT.getVectorNumElements() ==
2692 Operand.getValueType().getVectorNumElements()) &&
2693 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002694 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002695 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002696 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002697 else if (OpOpcode == ISD::UNDEF)
2698 // zext(undef) = 0, because the top bits will be zero.
2699 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002700 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002701 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002702 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002703 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002704 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002705 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2706 "Invalid anyext node, dst < src!");
2707 assert((!VT.isVector() ||
2708 VT.getVectorNumElements() ==
2709 Operand.getValueType().getVectorNumElements()) &&
2710 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002711
Dan Gohman08837892010-06-18 00:08:30 +00002712 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2713 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002714 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002715 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002716 else if (OpOpcode == ISD::UNDEF)
2717 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002718
2719 // (ext (trunx x)) -> x
2720 if (OpOpcode == ISD::TRUNCATE) {
2721 SDValue OpOp = Operand.getNode()->getOperand(0);
2722 if (OpOp.getValueType() == VT)
2723 return OpOp;
2724 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002725 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002726 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002727 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002728 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002729 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002730 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2731 "Invalid truncate node, src < dst!");
2732 assert((!VT.isVector() ||
2733 VT.getVectorNumElements() ==
2734 Operand.getValueType().getVectorNumElements()) &&
2735 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002736 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002737 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002738 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2739 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002740 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002741 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2742 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002743 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002744 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002745 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002746 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002747 }
Craig Topper201c1a32012-01-15 01:05:11 +00002748 if (OpOpcode == ISD::UNDEF)
2749 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002750 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002751 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002752 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002753 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002754 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002755 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002756 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2757 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002758 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002759 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002760 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002761 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002762 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002763 (VT.getVectorElementType() == Operand.getValueType() ||
2764 (VT.getVectorElementType().isInteger() &&
2765 Operand.getValueType().isInteger() &&
2766 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002767 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002768 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002769 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002770 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2771 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2772 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2773 Operand.getConstantOperandVal(1) == 0 &&
2774 Operand.getOperand(0).getValueType() == VT)
2775 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002776 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002777 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002778 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002779 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002780 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002781 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002782 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002783 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002784 break;
2785 case ISD::FABS:
2786 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002787 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002788 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002789 }
2790
Chris Lattnerf9c19152005-08-25 19:12:10 +00002791 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002792 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002793 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002794 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002795 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002796 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002797 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002798 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002799 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002800
Jack Carter170a5f22013-09-09 22:02:08 +00002801 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2802 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002803 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002804 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002805 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2806 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002807 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002808
Chris Lattner061a1ea2005-01-07 07:46:32 +00002809 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002810#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002811 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002812#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002813 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002814}
2815
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002816SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2817 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002818 // If the opcode is a target-specific ISD node, there's nothing we can
2819 // do here and the operand rules may not line up with the below, so
2820 // bail early.
2821 if (Opcode >= ISD::BUILTIN_OP_END)
2822 return SDValue();
2823
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002824 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2825 SmallVector<SDValue, 4> Outputs;
2826 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002827
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002828 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2829 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002830 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2831 return SDValue();
2832
2833 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002834 // Scalar instruction.
2835 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002836 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002837 // For vectors extract each constant element into Inputs so we can constant
2838 // fold them individually.
2839 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2840 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2841 if (!BV1 || !BV2)
2842 return SDValue();
2843
2844 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2845
2846 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2847 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2848 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2849 if (!V1 || !V2) // Not a constant, bail.
2850 return SDValue();
2851
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002852 if (V1->isOpaque() || V2->isOpaque())
2853 return SDValue();
2854
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002855 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2856 // FIXME: This is valid and could be handled by truncating the APInts.
2857 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2858 return SDValue();
2859
2860 Inputs.push_back(std::make_pair(V1, V2));
2861 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002862 }
2863
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002864 // We have a number of constant values, constant fold them element by element.
2865 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2866 const APInt &C1 = Inputs[I].first->getAPIntValue();
2867 const APInt &C2 = Inputs[I].second->getAPIntValue();
2868
2869 switch (Opcode) {
2870 case ISD::ADD:
2871 Outputs.push_back(getConstant(C1 + C2, SVT));
2872 break;
2873 case ISD::SUB:
2874 Outputs.push_back(getConstant(C1 - C2, SVT));
2875 break;
2876 case ISD::MUL:
2877 Outputs.push_back(getConstant(C1 * C2, SVT));
2878 break;
2879 case ISD::UDIV:
2880 if (!C2.getBoolValue())
2881 return SDValue();
2882 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2883 break;
2884 case ISD::UREM:
2885 if (!C2.getBoolValue())
2886 return SDValue();
2887 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2888 break;
2889 case ISD::SDIV:
2890 if (!C2.getBoolValue())
2891 return SDValue();
2892 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2893 break;
2894 case ISD::SREM:
2895 if (!C2.getBoolValue())
2896 return SDValue();
2897 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2898 break;
2899 case ISD::AND:
2900 Outputs.push_back(getConstant(C1 & C2, SVT));
2901 break;
2902 case ISD::OR:
2903 Outputs.push_back(getConstant(C1 | C2, SVT));
2904 break;
2905 case ISD::XOR:
2906 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2907 break;
2908 case ISD::SHL:
2909 Outputs.push_back(getConstant(C1 << C2, SVT));
2910 break;
2911 case ISD::SRL:
2912 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2913 break;
2914 case ISD::SRA:
2915 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2916 break;
2917 case ISD::ROTL:
2918 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2919 break;
2920 case ISD::ROTR:
2921 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2922 break;
2923 default:
2924 return SDValue();
2925 }
2926 }
2927
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00002928 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
2929 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002930
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002931 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002932 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002933 return Outputs.back();
2934
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002935 // We may have a vector type but a scalar result. Create a splat.
2936 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
2937
2938 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00002939 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00002940}
2941
Andrew Trickef9de2a2013-05-25 02:42:55 +00002942SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002943 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002944 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2945 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002946 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002947 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002948 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002949 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2950 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002951 // Fold trivial token factors.
2952 if (N1.getOpcode() == ISD::EntryToken) return N2;
2953 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002954 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002955 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002956 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002957 // Concat of UNDEFs is UNDEF.
2958 if (N1.getOpcode() == ISD::UNDEF &&
2959 N2.getOpcode() == ISD::UNDEF)
2960 return getUNDEF(VT);
2961
Dan Gohman550c9af2008-08-14 20:04:46 +00002962 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2963 // one big BUILD_VECTOR.
2964 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2965 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002966 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2967 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002968 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00002969 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00002970 }
2971 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002972 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002973 assert(VT.isInteger() && "This operator does not apply to FP types!");
2974 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002975 N1.getValueType() == VT && "Binary operator types must match!");
2976 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2977 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002978 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002979 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002980 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2981 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002982 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002983 case ISD::OR:
2984 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002985 case ISD::ADD:
2986 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002987 assert(VT.isInteger() && "This operator does not apply to FP types!");
2988 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002989 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002990 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2991 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002992 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002993 return N1;
2994 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002995 case ISD::UDIV:
2996 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00002997 case ISD::MULHU:
2998 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002999 case ISD::MUL:
3000 case ISD::SDIV:
3001 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003002 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003003 assert(N1.getValueType() == N2.getValueType() &&
3004 N1.getValueType() == VT && "Binary operator types must match!");
3005 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003006 case ISD::FADD:
3007 case ISD::FSUB:
3008 case ISD::FMUL:
3009 case ISD::FDIV:
3010 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003011 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003012 if (Opcode == ISD::FADD) {
3013 // 0+x --> x
3014 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3015 if (CFP->getValueAPF().isZero())
3016 return N2;
3017 // x+0 --> x
3018 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3019 if (CFP->getValueAPF().isZero())
3020 return N1;
3021 } else if (Opcode == ISD::FSUB) {
3022 // x-0 --> x
3023 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3024 if (CFP->getValueAPF().isZero())
3025 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003026 } else if (Opcode == ISD::FMUL) {
3027 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3028 SDValue V = N2;
3029
3030 // If the first operand isn't the constant, try the second
3031 if (!CFP) {
3032 CFP = dyn_cast<ConstantFPSDNode>(N2);
3033 V = N1;
3034 }
3035
3036 if (CFP) {
3037 // 0*x --> 0
3038 if (CFP->isZero())
3039 return SDValue(CFP,0);
3040 // 1*x --> x
3041 if (CFP->isExactlyValue(1.0))
3042 return V;
3043 }
Dan Gohman1275e282009-01-23 19:10:37 +00003044 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003045 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003046 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003047 assert(N1.getValueType() == N2.getValueType() &&
3048 N1.getValueType() == VT && "Binary operator types must match!");
3049 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003050 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3051 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003052 N1.getValueType().isFloatingPoint() &&
3053 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003054 "Invalid FCOPYSIGN!");
3055 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003056 case ISD::SHL:
3057 case ISD::SRA:
3058 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003059 case ISD::ROTL:
3060 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003061 assert(VT == N1.getValueType() &&
3062 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003063 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003064 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003065 assert((!VT.isVector() || VT == N2.getValueType()) &&
3066 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003067 // Verify that the shift amount VT is bit enough to hold valid shift
3068 // amounts. This catches things like trying to shift an i1024 value by an
3069 // i8, which is easy to fall into in generic code that uses
3070 // TLI.getShiftAmount().
3071 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003072 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003073 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003074
3075 // Always fold shifts of i1 values so the code generator doesn't need to
3076 // handle them. Since we know the size of the shift has to be less than the
3077 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003078 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003079 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003080 if (N2C && N2C->isNullValue())
3081 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003082 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003083 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003084 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003085 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003086 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003087 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003088 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003089 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003090 "type is vector!");
3091 assert((!EVT.isVector() ||
3092 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3093 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003094 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003095 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003096 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003097 break;
3098 }
Chris Lattner72733e52008-01-17 07:00:52 +00003099 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003100 assert(VT.isFloatingPoint() &&
3101 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003102 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003103 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003104 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003105 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003106 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003107 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003108 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003109 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003110 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003111 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003112 assert(!EVT.isVector() &&
3113 "AssertSExt/AssertZExt type should be the vector element type "
3114 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003115 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003116 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003117 break;
3118 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003119 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003120 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003121 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003122 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003123 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003124 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003125 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003126 "type is vector!");
3127 assert((!EVT.isVector() ||
3128 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3129 "Vector element counts must match in SIGN_EXTEND_INREG");
3130 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003131 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003132
Chris Lattner16713612008-01-22 19:09:33 +00003133 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003134 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003135 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003136 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003137 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003138 return getConstant(Val, VT);
3139 }
Chris Lattner16713612008-01-22 19:09:33 +00003140 break;
3141 }
3142 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003143 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3144 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003145 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003146
Chris Lattner16713612008-01-22 19:09:33 +00003147 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3148 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003149 if (N2C &&
3150 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003151 N1.getNumOperands() > 0) {
3152 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003153 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003154 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003155 N1.getOperand(N2C->getZExtValue() / Factor),
3156 getConstant(N2C->getZExtValue() % Factor,
3157 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003158 }
3159
3160 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3161 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003162 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3163 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003164
3165 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003166 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003167 // are promoted and implicitly truncated, and the result implicitly
3168 // extended. Make that explicit here.
3169 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003170
Bob Wilson59dbbb22009-04-13 22:05:19 +00003171 return Elt;
3172 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003173
Chris Lattner16713612008-01-22 19:09:33 +00003174 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3175 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003176 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003177 // If the indices are the same, return the inserted element else
3178 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003179 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003180 SDValue N1Op2 = N1.getOperand(2);
3181 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3182
3183 if (N1Op2C && N2C) {
3184 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3185 if (VT == N1.getOperand(1).getValueType())
3186 return N1.getOperand(1);
3187 else
3188 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3189 }
3190
Dale Johannesendb393622009-02-03 01:55:44 +00003191 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003192 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003193 }
Chris Lattner16713612008-01-22 19:09:33 +00003194 break;
3195 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003196 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003197 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3198 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003199 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003200 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003201
Chris Lattner16713612008-01-22 19:09:33 +00003202 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3203 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003204 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003205 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003206 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003207
Chris Lattner16713612008-01-22 19:09:33 +00003208 // EXTRACT_ELEMENT of a constant int is also very common.
3209 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003210 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003211 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003212 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3213 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003214 }
3215 break;
David Greeneb6f16112011-01-26 15:38:49 +00003216 case ISD::EXTRACT_SUBVECTOR: {
3217 SDValue Index = N2;
3218 if (VT.isSimple() && N1.getValueType().isSimple()) {
3219 assert(VT.isVector() && N1.getValueType().isVector() &&
3220 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003221 assert(VT.getVectorElementType() ==
3222 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003223 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003224 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003225 "Extract subvector must be from larger vector to smaller vector!");
3226
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003227 if (isa<ConstantSDNode>(Index.getNode())) {
3228 assert((VT.getVectorNumElements() +
3229 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003230 <= N1.getValueType().getVectorNumElements())
3231 && "Extract subvector overflow!");
3232 }
3233
3234 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003235 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003236 return N1;
3237 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003238 break;
Chris Lattner16713612008-01-22 19:09:33 +00003239 }
David Greeneb6f16112011-01-26 15:38:49 +00003240 }
Chris Lattner16713612008-01-22 19:09:33 +00003241
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003242 // Perform trivial constant folding.
3243 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3244 if (SV.getNode()) return SV;
3245
3246 // Canonicalize constant to RHS if commutative.
3247 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3248 std::swap(N1C, N2C);
3249 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003250 }
3251
Chris Lattner16713612008-01-22 19:09:33 +00003252 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003253 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3254 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003255 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003256 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003257 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003258 std::swap(N1CFP, N2CFP);
3259 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003260 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003261 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3262 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003263 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003264 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003265 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003266 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003267 return getConstantFP(V1, VT);
3268 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003269 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003270 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3271 if (s!=APFloat::opInvalidOp)
3272 return getConstantFP(V1, VT);
3273 break;
3274 case ISD::FMUL:
3275 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3276 if (s!=APFloat::opInvalidOp)
3277 return getConstantFP(V1, VT);
3278 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003279 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003280 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3281 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3282 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003283 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003284 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003285 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3286 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3287 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003288 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003289 case ISD::FCOPYSIGN:
3290 V1.copySign(V2);
3291 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003292 default: break;
3293 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003294 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003295
3296 if (Opcode == ISD::FP_ROUND) {
3297 APFloat V = N1CFP->getValueAPF(); // make copy
3298 bool ignored;
3299 // This can return overflow, underflow, or inexact; we don't care.
3300 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003301 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003302 APFloat::rmNearestTiesToEven, &ignored);
3303 return getConstantFP(V, VT);
3304 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003305 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003306
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003307 // Canonicalize an UNDEF to the RHS, even over a constant.
3308 if (N1.getOpcode() == ISD::UNDEF) {
3309 if (isCommutativeBinOp(Opcode)) {
3310 std::swap(N1, N2);
3311 } else {
3312 switch (Opcode) {
3313 case ISD::FP_ROUND_INREG:
3314 case ISD::SIGN_EXTEND_INREG:
3315 case ISD::SUB:
3316 case ISD::FSUB:
3317 case ISD::FDIV:
3318 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003319 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003320 return N1; // fold op(undef, arg2) -> undef
3321 case ISD::UDIV:
3322 case ISD::SDIV:
3323 case ISD::UREM:
3324 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003325 case ISD::SRL:
3326 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003327 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003328 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3329 // For vectors, we can't easily build an all zero vector, just return
3330 // the LHS.
3331 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003332 }
3333 }
3334 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003335
3336 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003337 if (N2.getOpcode() == ISD::UNDEF) {
3338 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003339 case ISD::XOR:
3340 if (N1.getOpcode() == ISD::UNDEF)
3341 // Handle undef ^ undef -> 0 special case. This is a common
3342 // idiom (misuse).
3343 return getConstant(0, VT);
3344 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003345 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003346 case ISD::ADDC:
3347 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003348 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003349 case ISD::UDIV:
3350 case ISD::SDIV:
3351 case ISD::UREM:
3352 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003353 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003354 case ISD::FADD:
3355 case ISD::FSUB:
3356 case ISD::FMUL:
3357 case ISD::FDIV:
3358 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003359 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003360 return N2;
3361 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003362 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003363 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003364 case ISD::SRL:
3365 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003366 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003367 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3368 // For vectors, we can't easily build an all zero vector, just return
3369 // the LHS.
3370 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003371 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003372 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003373 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003374 // For vectors, we can't easily build an all one vector, just return
3375 // the LHS.
3376 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003377 case ISD::SRA:
3378 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003379 }
3380 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003381
Chris Lattner724f7ee2005-05-11 18:57:39 +00003382 // Memoize this node if possible.
3383 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003384 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003385 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003386 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003387 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003388 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003389 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003390 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003391 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003392
Jack Carter170a5f22013-09-09 22:02:08 +00003393 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3394 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003395 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003396 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003397 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3398 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003399 }
3400
Chris Lattner061a1ea2005-01-07 07:46:32 +00003401 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003402#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003403 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003404#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003405 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003406}
3407
Andrew Trickef9de2a2013-05-25 02:42:55 +00003408SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003409 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003410 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003411 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003412 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003413 case ISD::FMA: {
3414 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3415 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3416 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3417 if (N1CFP && N2CFP && N3CFP) {
3418 APFloat V1 = N1CFP->getValueAPF();
3419 const APFloat &V2 = N2CFP->getValueAPF();
3420 const APFloat &V3 = N3CFP->getValueAPF();
3421 APFloat::opStatus s =
3422 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3423 if (s != APFloat::opInvalidOp)
3424 return getConstantFP(V1, VT);
3425 }
3426 break;
3427 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003428 case ISD::CONCAT_VECTORS:
3429 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3430 // one big BUILD_VECTOR.
3431 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3432 N2.getOpcode() == ISD::BUILD_VECTOR &&
3433 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003434 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3435 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003436 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3437 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003438 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003439 }
3440 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003441 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003442 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003443 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003444 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003445 break;
3446 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003447 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003448 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003449 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003450 return N2; // select true, X, Y -> X
3451 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003452 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003453
3454 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003455 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003456 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003457 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003458 case ISD::INSERT_SUBVECTOR: {
3459 SDValue Index = N3;
3460 if (VT.isSimple() && N1.getValueType().isSimple()
3461 && N2.getValueType().isSimple()) {
3462 assert(VT.isVector() && N1.getValueType().isVector() &&
3463 N2.getValueType().isVector() &&
3464 "Insert subvector VTs must be a vectors");
3465 assert(VT == N1.getValueType() &&
3466 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003467 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003468 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003469 if (isa<ConstantSDNode>(Index.getNode())) {
3470 assert((N2.getValueType().getVectorNumElements() +
3471 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003472 <= VT.getVectorNumElements())
3473 && "Insert subvector overflow!");
3474 }
3475
3476 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003477 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003478 return N2;
3479 }
3480 break;
3481 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003482 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003483 // Fold bit_convert nodes from a type to themselves.
3484 if (N1.getValueType() == VT)
3485 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003486 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003487 }
3488
Chris Lattnerf9c19152005-08-25 19:12:10 +00003489 // Memoize node if it doesn't produce a flag.
3490 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003491 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003492 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003493 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003494 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003495 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003496 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003497 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003498 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003499
Jack Carter170a5f22013-09-09 22:02:08 +00003500 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3501 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003502 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003503 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003504 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3505 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003506 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003507
Chris Lattner061a1ea2005-01-07 07:46:32 +00003508 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003509#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003510 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003511#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003512 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003513}
3514
Andrew Trickef9de2a2013-05-25 02:42:55 +00003515SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003516 SDValue N1, SDValue N2, SDValue N3,
3517 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003518 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003519 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003520}
3521
Andrew Trickef9de2a2013-05-25 02:42:55 +00003522SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003523 SDValue N1, SDValue N2, SDValue N3,
3524 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003525 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003526 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003527}
3528
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003529/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3530/// the incoming stack arguments to be loaded from the stack.
3531SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3532 SmallVector<SDValue, 8> ArgChains;
3533
3534 // Include the original chain at the beginning of the list. When this is
3535 // used by target LowerCall hooks, this helps legalize find the
3536 // CALLSEQ_BEGIN node.
3537 ArgChains.push_back(Chain);
3538
3539 // Add a chain value for each stack argument.
3540 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3541 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3542 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3543 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3544 if (FI->getIndex() < 0)
3545 ArgChains.push_back(SDValue(L, 1));
3546
3547 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003548 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003549}
3550
Dan Gohman544ab2c2008-04-12 04:36:06 +00003551/// getMemsetValue - Vectorized representation of the memset value
3552/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003553static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003554 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003555 assert(Value.getOpcode() != ISD::UNDEF);
3556
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003557 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003558 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003559 assert(C->getAPIntValue().getBitWidth() == 8);
3560 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003561 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003562 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003563 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003564 }
Evan Chengef377ad2008-05-15 08:39:06 +00003565
Dale Johannesenabf66b82009-02-03 22:26:09 +00003566 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003567 if (NumBits > 8) {
3568 // Use a multiplication with 0x010101... to extend the input to the
3569 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003570 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003571 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003572 }
3573
3574 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003575}
3576
Dan Gohman544ab2c2008-04-12 04:36:06 +00003577/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3578/// used when a memcpy is turned into a memset when the source is a constant
3579/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003580static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003581 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003582 // Handle vector with all elements zero.
3583 if (Str.empty()) {
3584 if (VT.isInteger())
3585 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003586 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003587 return DAG.getConstantFP(0.0, VT);
3588 else if (VT.isVector()) {
3589 unsigned NumElts = VT.getVectorNumElements();
3590 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003591 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003592 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3593 EltVT, NumElts)));
3594 } else
3595 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003596 }
3597
Duncan Sands13237ac2008-06-06 12:08:01 +00003598 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003599 unsigned NumVTBits = VT.getSizeInBits();
3600 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003601 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3602
Evan Chengc8444b12013-01-10 22:13:27 +00003603 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003604 if (TLI.isLittleEndian()) {
3605 for (unsigned i = 0; i != NumBytes; ++i)
3606 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3607 } else {
3608 for (unsigned i = 0; i != NumBytes; ++i)
3609 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003610 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003611
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003612 // If the "cost" of materializing the integer immediate is less than the cost
3613 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003614 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3615 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003616 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003617 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003618}
3619
Scott Michelcf0da6c2009-02-17 22:15:04 +00003620/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003621///
Andrew Tricke2431c62013-05-25 03:08:10 +00003622static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003623 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003624 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003625 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003626 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003627}
3628
Evan Chengef377ad2008-05-15 08:39:06 +00003629/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3630///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003631static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003632 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003633 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003634 if (Src.getOpcode() == ISD::GlobalAddress)
3635 G = cast<GlobalAddressSDNode>(Src);
3636 else if (Src.getOpcode() == ISD::ADD &&
3637 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3638 Src.getOperand(1).getOpcode() == ISD::Constant) {
3639 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003640 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003641 }
3642 if (!G)
3643 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003644
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003645 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003646}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003647
Evan Cheng43cd9e32010-04-01 06:04:33 +00003648/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3649/// to replace the memset / memcpy. Return true if the number of memory ops
3650/// is below the threshold. It returns the types of the sequence of
3651/// memory ops to perform memset / memcpy by reference.
3652static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003653 unsigned Limit, uint64_t Size,
3654 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003655 bool IsMemset,
3656 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003657 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003658 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003659 SelectionDAG &DAG,
3660 const TargetLowering &TLI) {
3661 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3662 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003663 // If 'SrcAlign' is zero, that means the memory operation does not need to
3664 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3665 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3666 // is the specified alignment of the memory operation. If it is zero, that
3667 // means it's possible to change the alignment of the destination.
3668 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3669 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003670 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003671 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003672 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003673
Chris Lattner28dc6c12010-03-07 07:45:08 +00003674 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003675 unsigned AS = 0;
3676 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
3677 TLI.allowsUnalignedMemoryAccesses(VT, AS)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003678 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003679 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003680 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003681 case 0: VT = MVT::i64; break;
3682 case 4: VT = MVT::i32; break;
3683 case 2: VT = MVT::i16; break;
3684 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003685 }
3686 }
3687
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003688 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003689 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003690 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003691 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003692
Duncan Sands11dd4242008-06-08 20:54:56 +00003693 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003694 VT = LVT;
3695 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003696
Dan Gohman544ab2c2008-04-12 04:36:06 +00003697 unsigned NumMemOps = 0;
3698 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003699 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003700 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003701 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003702 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003703 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003704
3705 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003706 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003707 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003708 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003709 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003710 Found = true;
3711 else if (NewVT == MVT::i64 &&
3712 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003713 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003714 // i64 is usually not legal on 32-bit targets, but f64 may be.
3715 NewVT = MVT::f64;
3716 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003717 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003718 }
3719
Evan Cheng04e55182012-12-12 00:42:09 +00003720 if (!Found) {
3721 do {
3722 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3723 if (NewVT == MVT::i8)
3724 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003725 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003726 }
3727 NewVTSize = NewVT.getSizeInBits() / 8;
3728
Evan Cheng79e2ca92012-12-10 23:21:26 +00003729 // If the new VT cannot cover all of the remaining bits, then consider
3730 // issuing a (or a pair of) unaligned and overlapping load / store.
3731 // FIXME: Only does this for 64-bit or more since we don't have proper
3732 // cost model for unaligned load / store.
3733 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003734 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003735 if (NumMemOps && AllowOverlap &&
3736 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003737 TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003738 VTSize = Size;
3739 else {
3740 VT = NewVT;
3741 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003742 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003743 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003744
Evan Chengb7d3d032012-12-12 20:43:23 +00003745 if (++NumMemOps > Limit)
3746 return false;
3747
Dan Gohman544ab2c2008-04-12 04:36:06 +00003748 MemOps.push_back(VT);
3749 Size -= VTSize;
3750 }
3751
3752 return true;
3753}
3754
Andrew Trickef9de2a2013-05-25 02:42:55 +00003755static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003756 SDValue Chain, SDValue Dst,
3757 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003758 unsigned Align, bool isVol,
3759 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003760 MachinePointerInfo DstPtrInfo,
3761 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003762 // Turn a memcpy of undef to nop.
3763 if (Src.getOpcode() == ISD::UNDEF)
3764 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003765
Dan Gohman714663a2008-05-29 19:42:22 +00003766 // Expand memcpy to a series of load and store ops if the size operand falls
3767 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003768 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3769 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003770 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003771 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003772 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003773 MachineFunction &MF = DAG.getMachineFunction();
3774 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003775 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003776 MF.getFunction()->getAttributes().
3777 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003778 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3779 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3780 DstAlignCanChange = true;
3781 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3782 if (Align > SrcAlign)
3783 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003784 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003785 bool CopyFromStr = isMemSrcFromString(Src, Str);
3786 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003787 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003788
Evan Cheng4c014c82010-04-01 18:19:11 +00003789 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003790 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003791 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003792 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003793 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003794
Evan Cheng43cd9e32010-04-01 06:04:33 +00003795 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003796 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003797 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003798
3799 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003800 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003801 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3802 if (!TRI->needsStackRealignment(MF))
3803 while (NewAlign > Align &&
3804 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3805 NewAlign /= 2;
3806
Evan Cheng43cd9e32010-04-01 06:04:33 +00003807 if (NewAlign > Align) {
3808 // Give the stack frame object a larger alignment if needed.
3809 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3810 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3811 Align = NewAlign;
3812 }
3813 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003814
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003815 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003816 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003817 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003818 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003819 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003820 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003821 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003822
Evan Cheng79e2ca92012-12-10 23:21:26 +00003823 if (VTSize > Size) {
3824 // Issuing an unaligned load / store pair that overlaps with the previous
3825 // pair. Adjust the offset accordingly.
3826 assert(i == NumMemOps-1 && i != 0);
3827 SrcOff -= VTSize - Size;
3828 DstOff -= VTSize - Size;
3829 }
3830
Evan Cheng43cd9e32010-04-01 06:04:33 +00003831 if (CopyFromStr &&
3832 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003833 // It's unlikely a store of a vector immediate can be done in a single
3834 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003835 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003836 // FIXME: Handle other cases where store of vector immediate is done in
3837 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003838 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003839 if (Value.getNode())
3840 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003841 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003842 DstPtrInfo.getWithOffset(DstOff), isVol,
3843 false, Align);
3844 }
3845
3846 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003847 // The type might not be legal for the target. This should only happen
3848 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003849 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3850 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003851 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003852 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003853 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003854 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003855 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003856 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003857 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003858 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003859 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003860 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3861 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003862 }
3863 OutChains.push_back(Store);
3864 SrcOff += VTSize;
3865 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003866 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003867 }
3868
Craig Topper48d114b2014-04-26 18:35:24 +00003869 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003870}
3871
Andrew Trickef9de2a2013-05-25 02:42:55 +00003872static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003873 SDValue Chain, SDValue Dst,
3874 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003875 unsigned Align, bool isVol,
3876 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003877 MachinePointerInfo DstPtrInfo,
3878 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003879 // Turn a memmove of undef to nop.
3880 if (Src.getOpcode() == ISD::UNDEF)
3881 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003882
3883 // Expand memmove to a series of load and store ops if the size operand falls
3884 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003885 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003886 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003887 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003888 MachineFunction &MF = DAG.getMachineFunction();
3889 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003890 bool OptSize = MF.getFunction()->getAttributes().
3891 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003892 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3893 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3894 DstAlignCanChange = true;
3895 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3896 if (Align > SrcAlign)
3897 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003898 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003899
Evan Cheng4c014c82010-04-01 18:19:11 +00003900 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003901 (DstAlignCanChange ? 0 : Align), SrcAlign,
3902 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003903 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003904
Evan Cheng43cd9e32010-04-01 06:04:33 +00003905 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003906 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003907 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003908 if (NewAlign > Align) {
3909 // Give the stack frame object a larger alignment if needed.
3910 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3911 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3912 Align = NewAlign;
3913 }
3914 }
Dan Gohman714663a2008-05-29 19:42:22 +00003915
Evan Cheng43cd9e32010-04-01 06:04:33 +00003916 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003917 SmallVector<SDValue, 8> LoadValues;
3918 SmallVector<SDValue, 8> LoadChains;
3919 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003920 unsigned NumMemOps = MemOps.size();
3921 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003922 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003923 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003924 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00003925
Dale Johannesenabf66b82009-02-03 22:26:09 +00003926 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003927 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003928 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003929 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003930 LoadValues.push_back(Value);
3931 LoadChains.push_back(Value.getValue(1));
3932 SrcOff += VTSize;
3933 }
Craig Topper48d114b2014-04-26 18:35:24 +00003934 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00003935 OutChains.clear();
3936 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003937 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003938 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003939 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003940
Dale Johannesenabf66b82009-02-03 22:26:09 +00003941 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003942 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003943 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003944 OutChains.push_back(Store);
3945 DstOff += VTSize;
3946 }
3947
Craig Topper48d114b2014-04-26 18:35:24 +00003948 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00003949}
3950
Serge Pavlov8ec39992013-09-17 16:24:42 +00003951/// \brief Lower the call to 'memset' intrinsic function into a series of store
3952/// operations.
3953///
3954/// \param DAG Selection DAG where lowered code is placed.
3955/// \param dl Link to corresponding IR location.
3956/// \param Chain Control flow dependency.
3957/// \param Dst Pointer to destination memory location.
3958/// \param Src Value of byte to write into the memory.
3959/// \param Size Number of bytes to write.
3960/// \param Align Alignment of the destination in bytes.
3961/// \param isVol True if destination is volatile.
3962/// \param DstPtrInfo IR information on the memory pointer.
3963/// \returns New head in the control flow, if lowering was successful, empty
3964/// SDValue otherwise.
3965///
3966/// The function tries to replace 'llvm.memset' intrinsic with several store
3967/// operations and value calculation code. This is usually profitable for small
3968/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003969static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003970 SDValue Chain, SDValue Dst,
3971 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003972 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003973 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003974 // Turn a memset of undef to nop.
3975 if (Src.getOpcode() == ISD::UNDEF)
3976 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003977
3978 // Expand memset to a series of load/store ops if the size operand
3979 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003980 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003981 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003982 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003983 MachineFunction &MF = DAG.getMachineFunction();
3984 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003985 bool OptSize = MF.getFunction()->getAttributes().
3986 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003987 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3988 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3989 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003990 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003991 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003992 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00003993 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00003994 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003995 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003996
Evan Cheng43cd9e32010-04-01 06:04:33 +00003997 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003998 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003999 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004000 if (NewAlign > Align) {
4001 // Give the stack frame object a larger alignment if needed.
4002 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4003 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4004 Align = NewAlign;
4005 }
4006 }
4007
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004008 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004009 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004010 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004011
4012 // Find the largest store and generate the bit pattern for it.
4013 EVT LargestVT = MemOps[0];
4014 for (unsigned i = 1; i < NumMemOps; i++)
4015 if (MemOps[i].bitsGT(LargestVT))
4016 LargestVT = MemOps[i];
4017 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4018
Dan Gohman544ab2c2008-04-12 04:36:06 +00004019 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004020 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004021 unsigned VTSize = VT.getSizeInBits() / 8;
4022 if (VTSize > Size) {
4023 // Issuing an unaligned load / store pair that overlaps with the previous
4024 // pair. Adjust the offset accordingly.
4025 assert(i == NumMemOps-1 && i != 0);
4026 DstOff -= VTSize - Size;
4027 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004028
4029 // If this store is smaller than the largest store see whether we can get
4030 // the smaller value for free with a truncate.
4031 SDValue Value = MemSetValue;
4032 if (VT.bitsLT(LargestVT)) {
4033 if (!LargestVT.isVector() && !VT.isVector() &&
4034 TLI.isTruncateFree(LargestVT, VT))
4035 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4036 else
4037 Value = getMemsetValue(Src, VT, DAG, dl);
4038 }
4039 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004040 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004041 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004042 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004043 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004044 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004045 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004046 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004047 }
4048
Craig Topper48d114b2014-04-26 18:35:24 +00004049 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004050}
4051
Andrew Trickef9de2a2013-05-25 02:42:55 +00004052SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004053 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004054 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004055 MachinePointerInfo DstPtrInfo,
4056 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004057 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004058
4059 // Check to see if we should lower the memcpy to loads and stores first.
4060 // For cases within the target-specified limits, this is the best choice.
4061 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4062 if (ConstantSize) {
4063 // Memcpy with size zero? Just return the original chain.
4064 if (ConstantSize->isNullValue())
4065 return Chain;
4066
Evan Cheng43cd9e32010-04-01 06:04:33 +00004067 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4068 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004069 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004070 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004071 return Result;
4072 }
4073
4074 // Then check to see if we should lower the memcpy with target-specific
4075 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004076 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004077 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004078 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004079 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004080 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004081 return Result;
4082
4083 // If we really need inline code and the target declined to provide it,
4084 // use a (potentially long) sequence of loads and stores.
4085 if (AlwaysInline) {
4086 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004087 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004088 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004089 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004090 }
4091
Dan Gohmanf38547c2010-04-05 20:24:08 +00004092 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4093 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4094 // respect volatile, so they may do things like read or write memory
4095 // beyond the given memory regions. But fixing this isn't easy, and most
4096 // people don't care.
4097
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004098 const TargetLowering *TLI = TM.getTargetLowering();
4099
Dan Gohman544ab2c2008-04-12 04:36:06 +00004100 // Emit a library call.
4101 TargetLowering::ArgListTy Args;
4102 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004103 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004104 Entry.Node = Dst; Args.push_back(Entry);
4105 Entry.Node = Src; Args.push_back(Entry);
4106 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004107 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004108 TargetLowering::
4109 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004110 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004111 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00004112 /*isTailCall=*/false,
4113 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004114 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4115 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004116 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004117 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004118
Dan Gohman544ab2c2008-04-12 04:36:06 +00004119 return CallResult.second;
4120}
4121
Andrew Trickef9de2a2013-05-25 02:42:55 +00004122SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004123 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004124 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004125 MachinePointerInfo DstPtrInfo,
4126 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004127 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004128
Dan Gohman714663a2008-05-29 19:42:22 +00004129 // Check to see if we should lower the memmove to loads and stores first.
4130 // For cases within the target-specified limits, this is the best choice.
4131 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4132 if (ConstantSize) {
4133 // Memmove with size zero? Just return the original chain.
4134 if (ConstantSize->isNullValue())
4135 return Chain;
4136
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004137 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004138 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004139 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004140 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004141 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004142 return Result;
4143 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004144
4145 // Then check to see if we should lower the memmove with target-specific
4146 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004147 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004148 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004149 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004150 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004151 return Result;
4152
Mon P Wangbf862242010-04-06 08:27:51 +00004153 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4154 // not be safe. See memcpy above for more details.
4155
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004156 const TargetLowering *TLI = TM.getTargetLowering();
4157
Dan Gohman544ab2c2008-04-12 04:36:06 +00004158 // Emit a library call.
4159 TargetLowering::ArgListTy Args;
4160 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004161 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004162 Entry.Node = Dst; Args.push_back(Entry);
4163 Entry.Node = Src; Args.push_back(Entry);
4164 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004165 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004166 TargetLowering::
4167 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004168 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004169 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004170 /*isTailCall=*/false,
4171 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004172 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4173 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004174 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004175 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004176
Dan Gohman544ab2c2008-04-12 04:36:06 +00004177 return CallResult.second;
4178}
4179
Andrew Trickef9de2a2013-05-25 02:42:55 +00004180SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004181 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004182 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004183 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004184 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004185
4186 // Check to see if we should lower the memset to stores first.
4187 // For cases within the target-specified limits, this is the best choice.
4188 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4189 if (ConstantSize) {
4190 // Memset with size zero? Just return the original chain.
4191 if (ConstantSize->isNullValue())
4192 return Chain;
4193
Mon P Wangc576ee92010-04-04 03:10:48 +00004194 SDValue Result =
4195 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004196 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004197
Gabor Greiff304a7a2008-08-28 21:40:38 +00004198 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004199 return Result;
4200 }
4201
4202 // Then check to see if we should lower the memset with target-specific
4203 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004204 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004205 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004206 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004207 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004208 return Result;
4209
Wesley Peck527da1b2010-11-23 03:31:01 +00004210 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004211 const TargetLowering *TLI = TM.getTargetLowering();
4212 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004213 TargetLowering::ArgListTy Args;
4214 TargetLowering::ArgListEntry Entry;
4215 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4216 Args.push_back(Entry);
4217 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004218 if (Src.getValueType().bitsGT(MVT::i32))
4219 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004220 else
Owen Anderson9f944592009-08-11 20:47:22 +00004221 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004222 Entry.Node = Src;
4223 Entry.Ty = Type::getInt32Ty(*getContext());
4224 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004225 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004226 Entry.Node = Size;
4227 Entry.Ty = IntPtrTy;
4228 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004229 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004230 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004231 TargetLowering::
4232 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004233 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004234 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004235 /*isTailCall=*/false,
4236 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004237 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4238 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004239 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004240 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004241
Dan Gohman544ab2c2008-04-12 04:36:06 +00004242 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004243}
4244
Andrew Trickef9de2a2013-05-25 02:42:55 +00004245SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004246 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004247 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004248 AtomicOrdering SuccessOrdering,
4249 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004250 SynchronizationScope SynchScope) {
4251 FoldingSetNodeID ID;
4252 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004253 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004254 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004255 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004256 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4257 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4258 return SDValue(E, 0);
4259 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004260
4261 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4262 // SDNode doesn't have access to it. This memory will be "leaked" when
4263 // the node is deallocated, but recovered when the allocator is released.
4264 // If the number of operands is less than 5 we use AtomicSDNode's internal
4265 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004266 unsigned NumOps = Ops.size();
4267 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4268 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004269
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004270 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4271 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004272 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004273 SuccessOrdering, FailureOrdering,
4274 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004275 CSEMap.InsertNode(N, IP);
4276 AllNodes.push_back(N);
4277 return SDValue(N, 0);
4278}
4279
4280SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004281 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004282 MachineMemOperand *MMO,
4283 AtomicOrdering Ordering,
4284 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004285 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004286 Ordering, SynchScope);
4287}
4288
4289SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004290 SDValue Chain, SDValue Ptr, SDValue Cmp,
4291 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004292 unsigned Alignment,
Tim Northovere94a5182014-03-11 10:48:52 +00004293 AtomicOrdering SuccessOrdering,
4294 AtomicOrdering FailureOrdering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004295 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004296 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4297 Alignment = getEVTAlignment(MemVT);
4298
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004299 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004300
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004301 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004302 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004303 // FIXME: Volatile isn't really correct; we should keep track of atomic
4304 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004305 unsigned Flags = MachineMemOperand::MOVolatile;
4306 if (Opcode != ISD::ATOMIC_STORE)
4307 Flags |= MachineMemOperand::MOLoad;
4308 if (Opcode != ISD::ATOMIC_LOAD)
4309 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004310
4311 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004312 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004313
Eli Friedmanadec5872011-07-29 03:05:32 +00004314 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004315 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004316}
4317
Andrew Trickef9de2a2013-05-25 02:42:55 +00004318SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004319 SDValue Chain,
4320 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004321 SDValue Swp, MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004322 AtomicOrdering SuccessOrdering,
4323 AtomicOrdering FailureOrdering,
Eli Friedmanadec5872011-07-29 03:05:32 +00004324 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004325 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4326 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4327
Owen Anderson53aa7a92009-08-10 22:56:29 +00004328 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004329
Owen Anderson9f944592009-08-11 20:47:22 +00004330 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004331 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004332 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, SuccessOrdering,
Tim Northovere94a5182014-03-11 10:48:52 +00004333 FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004334}
4335
Andrew Trickef9de2a2013-05-25 02:42:55 +00004336SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004337 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004338 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004339 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004340 unsigned Alignment,
4341 AtomicOrdering Ordering,
4342 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004343 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4344 Alignment = getEVTAlignment(MemVT);
4345
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004346 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004347 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004348 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004349 // For now, atomics are considered to be volatile always, and they are
4350 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004351 // FIXME: Volatile isn't really correct; we should keep track of atomic
4352 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004353 unsigned Flags = MachineMemOperand::MOVolatile;
4354 if (Opcode != ISD::ATOMIC_STORE)
4355 Flags |= MachineMemOperand::MOLoad;
4356 if (Opcode != ISD::ATOMIC_LOAD)
4357 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004358
4359 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004360 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004361 MemVT.getStoreSize(), Alignment);
4362
Eli Friedmanadec5872011-07-29 03:05:32 +00004363 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4364 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004365}
4366
Andrew Trickef9de2a2013-05-25 02:42:55 +00004367SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004368 SDValue Chain,
4369 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004370 MachineMemOperand *MMO,
4371 AtomicOrdering Ordering,
4372 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004373 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4374 Opcode == ISD::ATOMIC_LOAD_SUB ||
4375 Opcode == ISD::ATOMIC_LOAD_AND ||
4376 Opcode == ISD::ATOMIC_LOAD_OR ||
4377 Opcode == ISD::ATOMIC_LOAD_XOR ||
4378 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004379 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004380 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004381 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004382 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004383 Opcode == ISD::ATOMIC_SWAP ||
4384 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004385 "Invalid Atomic Op");
4386
Owen Anderson53aa7a92009-08-10 22:56:29 +00004387 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004388
Eli Friedman342e8df2011-08-24 20:50:09 +00004389 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4390 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004391 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004392 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004393}
4394
Andrew Trickef9de2a2013-05-25 02:42:55 +00004395SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004396 EVT VT, SDValue Chain,
4397 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004398 MachineMemOperand *MMO,
4399 AtomicOrdering Ordering,
4400 SynchronizationScope SynchScope) {
4401 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4402
4403 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004404 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004405 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004406}
4407
Duncan Sands739a0542008-07-02 17:40:58 +00004408/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004409SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4410 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004411 return Ops[0];
4412
Owen Anderson53aa7a92009-08-10 22:56:29 +00004413 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004414 VTs.reserve(Ops.size());
4415 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004416 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004417 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004418}
4419
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004420SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004421SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004422 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004423 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004424 unsigned Align, bool Vol,
4425 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004426 if (Align == 0) // Ensure that codegen never sees alignment 0
4427 Align = getEVTAlignment(MemVT);
4428
4429 MachineFunction &MF = getMachineFunction();
4430 unsigned Flags = 0;
4431 if (WriteMem)
4432 Flags |= MachineMemOperand::MOStore;
4433 if (ReadMem)
4434 Flags |= MachineMemOperand::MOLoad;
4435 if (Vol)
4436 Flags |= MachineMemOperand::MOVolatile;
4437 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004438 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004439
Craig Topper206fcd42014-04-26 19:29:41 +00004440 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004441}
4442
4443SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004444SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004445 ArrayRef<SDValue> Ops, EVT MemVT,
4446 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004447 assert((Opcode == ISD::INTRINSIC_VOID ||
4448 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004449 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004450 Opcode == ISD::LIFETIME_START ||
4451 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004452 (Opcode <= INT_MAX &&
4453 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4454 "Opcode is not a memory-accessing opcode!");
4455
Dale Johannesen839acbb2009-01-29 00:47:48 +00004456 // Memoize the node unless it returns a flag.
4457 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004458 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004459 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004460 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004461 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004462 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004463 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4464 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004465 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004466 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004467
Jack Carter170a5f22013-09-09 22:02:08 +00004468 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4469 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004470 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004471 CSEMap.InsertNode(N, IP);
4472 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004473 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4474 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004475 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004476 }
4477 AllNodes.push_back(N);
4478 return SDValue(N, 0);
4479}
4480
Chris Lattnerea952f02010-09-21 17:24:05 +00004481/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4482/// MachinePointerInfo record from it. This is particularly useful because the
4483/// code generator has many cases where it doesn't bother passing in a
4484/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4485static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4486 // If this is FI+Offset, we can model it.
4487 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4488 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4489
4490 // If this is (FI+Offset1)+Offset2, we can model it.
4491 if (Ptr.getOpcode() != ISD::ADD ||
4492 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4493 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4494 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004495
Chris Lattnerea952f02010-09-21 17:24:05 +00004496 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4497 return MachinePointerInfo::getFixedStack(FI, Offset+
4498 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4499}
4500
4501/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4502/// MachinePointerInfo record from it. This is particularly useful because the
4503/// code generator has many cases where it doesn't bother passing in a
4504/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4505static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4506 // If the 'Offset' value isn't a constant, we can't handle this.
4507 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4508 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4509 if (OffsetOp.getOpcode() == ISD::UNDEF)
4510 return InferPointerInfo(Ptr);
4511 return MachinePointerInfo();
4512}
Wesley Peck527da1b2010-11-23 03:31:01 +00004513
Chris Lattnerea952f02010-09-21 17:24:05 +00004514
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004515SDValue
4516SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004517 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004518 SDValue Ptr, SDValue Offset,
4519 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004520 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004521 unsigned Alignment, const MDNode *TBAAInfo,
4522 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004523 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004524 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004525 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4526 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004527
Dan Gohman48b185d2009-09-25 20:36:54 +00004528 unsigned Flags = MachineMemOperand::MOLoad;
4529 if (isVolatile)
4530 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004531 if (isNonTemporal)
4532 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004533 if (isInvariant)
4534 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004535
Chris Lattnerea952f02010-09-21 17:24:05 +00004536 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4537 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004538 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004539 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004540
Chris Lattnerea952f02010-09-21 17:24:05 +00004541 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004542 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004543 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004544 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004545 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004546}
4547
4548SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004549SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004550 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004551 SDValue Ptr, SDValue Offset, EVT MemVT,
4552 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004553 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004554 ExtType = ISD::NON_EXTLOAD;
4555 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004556 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004557 } else {
4558 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004559 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4560 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004561 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004562 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004563 assert(VT.isVector() == MemVT.isVector() &&
4564 "Cannot use trunc store to convert to or from a vector!");
4565 assert((!VT.isVector() ||
4566 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4567 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004568 }
4569
4570 bool Indexed = AM != ISD::UNINDEXED;
4571 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4572 "Unindexed load with an offset!");
4573
4574 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004575 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004576 SDValue Ops[] = { Chain, Ptr, Offset };
4577 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004578 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004579 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004580 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004581 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004582 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004583 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004584 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004585 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4586 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004587 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004588 }
Jack Carter170a5f22013-09-09 22:02:08 +00004589 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4590 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004591 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004592 CSEMap.InsertNode(N, IP);
4593 AllNodes.push_back(N);
4594 return SDValue(N, 0);
4595}
4596
Andrew Trickef9de2a2013-05-25 02:42:55 +00004597SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004598 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004599 MachinePointerInfo PtrInfo,
4600 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004601 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004602 const MDNode *TBAAInfo,
4603 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004604 SDValue Undef = getUNDEF(Ptr.getValueType());
4605 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004606 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4607 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004608}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004609
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004610SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4611 SDValue Chain, SDValue Ptr,
4612 MachineMemOperand *MMO) {
4613 SDValue Undef = getUNDEF(Ptr.getValueType());
4614 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4615 VT, MMO);
4616}
4617
Andrew Trickef9de2a2013-05-25 02:42:55 +00004618SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004619 SDValue Chain, SDValue Ptr,
4620 MachinePointerInfo PtrInfo, EVT MemVT,
4621 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004622 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004623 SDValue Undef = getUNDEF(Ptr.getValueType());
4624 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004625 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004626 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004627}
4628
4629
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004630SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4631 SDValue Chain, SDValue Ptr, EVT MemVT,
4632 MachineMemOperand *MMO) {
4633 SDValue Undef = getUNDEF(Ptr.getValueType());
4634 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4635 MemVT, MMO);
4636}
4637
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004638SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004639SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004640 SDValue Offset, ISD::MemIndexedMode AM) {
4641 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4642 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4643 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004644 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004645 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004646 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004647 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004648}
4649
Andrew Trickef9de2a2013-05-25 02:42:55 +00004650SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004651 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004652 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004653 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004654 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004655 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004656 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004657 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004658
Dan Gohman48b185d2009-09-25 20:36:54 +00004659 unsigned Flags = MachineMemOperand::MOStore;
4660 if (isVolatile)
4661 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004662 if (isNonTemporal)
4663 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004664
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004665 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004666 PtrInfo = InferPointerInfo(Ptr);
4667
4668 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004669 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004670 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004671 Val.getValueType().getStoreSize(), Alignment,
4672 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004673
4674 return getStore(Chain, dl, Val, Ptr, MMO);
4675}
4676
Andrew Trickef9de2a2013-05-25 02:42:55 +00004677SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004678 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004679 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004680 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004681 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004682 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004683 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004684 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4685 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004686 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004687 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004688 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004689 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004690 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004691 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004692 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4693 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004694 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004695 }
Jack Carter170a5f22013-09-09 22:02:08 +00004696 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4697 dl.getDebugLoc(), VTs,
4698 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004699 CSEMap.InsertNode(N, IP);
4700 AllNodes.push_back(N);
4701 return SDValue(N, 0);
4702}
4703
Andrew Trickef9de2a2013-05-25 02:42:55 +00004704SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004705 SDValue Ptr, MachinePointerInfo PtrInfo,
4706 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004707 unsigned Alignment,
4708 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004709 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004710 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004711 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4712 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004713
Dan Gohman48b185d2009-09-25 20:36:54 +00004714 unsigned Flags = MachineMemOperand::MOStore;
4715 if (isVolatile)
4716 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004717 if (isNonTemporal)
4718 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004719
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004720 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004721 PtrInfo = InferPointerInfo(Ptr);
4722
4723 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004724 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004725 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4726 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004727
4728 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4729}
4730
Andrew Trickef9de2a2013-05-25 02:42:55 +00004731SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004732 SDValue Ptr, EVT SVT,
4733 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004734 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004735
Michael Ilsemand5f91512012-09-10 16:56:31 +00004736 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004737 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004738 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004739 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004740
Dan Gohmancecad352009-12-14 23:40:38 +00004741 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4742 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004743 assert(VT.isInteger() == SVT.isInteger() &&
4744 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004745 assert(VT.isVector() == SVT.isVector() &&
4746 "Cannot use trunc store to convert to or from a vector!");
4747 assert((!VT.isVector() ||
4748 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4749 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004750
Owen Anderson9f944592009-08-11 20:47:22 +00004751 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004752 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004753 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4754 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004755 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004756 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004757 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004758 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004759 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004760 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004761 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4762 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004763 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004764 }
Jack Carter170a5f22013-09-09 22:02:08 +00004765 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4766 dl.getDebugLoc(), VTs,
4767 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004768 CSEMap.InsertNode(N, IP);
4769 AllNodes.push_back(N);
4770 return SDValue(N, 0);
4771}
4772
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004773SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004774SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004775 SDValue Offset, ISD::MemIndexedMode AM) {
4776 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4777 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4778 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004779 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004780 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4781 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004782 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004783 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004784 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004785 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004786 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004788 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004789
Jack Carter170a5f22013-09-09 22:02:08 +00004790 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4791 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004792 ST->isTruncatingStore(),
4793 ST->getMemoryVT(),
4794 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004795 CSEMap.InsertNode(N, IP);
4796 AllNodes.push_back(N);
4797 return SDValue(N, 0);
4798}
4799
Andrew Trickef9de2a2013-05-25 02:42:55 +00004800SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004801 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004802 SDValue SV,
4803 unsigned Align) {
4804 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00004805 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004806}
4807
Andrew Trickef9de2a2013-05-25 02:42:55 +00004808SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00004809 ArrayRef<SDUse> Ops) {
4810 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004811 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00004812 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004813 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4814 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004815 default: break;
4816 }
4817
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004818 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004819 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00004820 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00004821 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004822}
4823
Andrew Trickef9de2a2013-05-25 02:42:55 +00004824SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00004825 ArrayRef<SDValue> Ops) {
4826 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00004827 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004828 case 0: return getNode(Opcode, DL, VT);
4829 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4830 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4831 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004832 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004833 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004834
Chris Lattnerb0713c72005-04-09 03:27:28 +00004835 switch (Opcode) {
4836 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004837 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004838 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004839 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4840 "LHS and RHS of condition must have same type!");
4841 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4842 "True and False arms of SelectCC must have same type!");
4843 assert(Ops[2].getValueType() == VT &&
4844 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004845 break;
4846 }
4847 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004848 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004849 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4850 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004851 break;
4852 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004853 }
4854
Chris Lattner566307f2005-05-14 07:42:29 +00004855 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004856 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004857 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004858
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004859 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004860 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004861 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004862 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004863
Bill Wendling022d18f2009-12-18 23:32:53 +00004864 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004865 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004866
Jack Carter170a5f22013-09-09 22:02:08 +00004867 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004868 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004869 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004870 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004871 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004872 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004873 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004874
Chris Lattnerb0713c72005-04-09 03:27:28 +00004875 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004876#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004877 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004878#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004879 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004880}
4881
Andrew Trickef9de2a2013-05-25 02:42:55 +00004882SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00004883 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
4884 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00004885}
4886
Andrew Trickef9de2a2013-05-25 02:42:55 +00004887SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00004888 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004889 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00004890 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00004891
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004892#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004893 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004894 // FIXME: figure out how to safely handle things like
4895 // int foo(int x) { return 1 << (x & 255); }
4896 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004897 case ISD::SRA_PARTS:
4898 case ISD::SRL_PARTS:
4899 case ISD::SHL_PARTS:
4900 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004901 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004902 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004903 else if (N3.getOpcode() == ISD::AND)
4904 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4905 // If the and is only masking out bits that cannot effect the shift,
4906 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004907 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004908 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004909 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004910 }
4911 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004912 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004913#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004914
Chris Lattnerf9c19152005-08-25 19:12:10 +00004915 // Memoize the node unless it returns a flag.
4916 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00004917 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004918 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004919 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004920 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004921 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004922 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004923 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004924
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004925 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004926 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4927 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004928 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004929 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4930 DL.getDebugLoc(), VTList, Ops[0],
4931 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004932 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004933 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4934 DL.getDebugLoc(), VTList, Ops[0],
4935 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004936 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004937 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004938 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004939 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004940 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004941 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004942 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004943 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4944 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004945 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004946 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4947 DL.getDebugLoc(), VTList, Ops[0],
4948 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004949 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004950 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4951 DL.getDebugLoc(), VTList, Ops[0],
4952 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004953 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004954 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004955 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004956 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004957 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004958 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004959#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004960 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004961#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004962 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004963}
4964
Andrew Trickef9de2a2013-05-25 02:42:55 +00004965SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Topper48d114b2014-04-26 18:35:24 +00004966 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004967}
4968
Andrew Trickef9de2a2013-05-25 02:42:55 +00004969SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004970 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004971 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00004972 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004973}
4974
Andrew Trickef9de2a2013-05-25 02:42:55 +00004975SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004976 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004977 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00004978 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004979}
4980
Andrew Trickef9de2a2013-05-25 02:42:55 +00004981SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004982 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004983 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00004984 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004985}
4986
Andrew Trickef9de2a2013-05-25 02:42:55 +00004987SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004988 SDValue N1, SDValue N2, SDValue N3,
4989 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004990 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00004991 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004992}
4993
Andrew Trickef9de2a2013-05-25 02:42:55 +00004994SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004995 SDValue N1, SDValue N2, SDValue N3,
4996 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004997 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00004998 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004999}
5000
Owen Anderson53aa7a92009-08-10 22:56:29 +00005001SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005002 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005003}
5004
Owen Anderson53aa7a92009-08-10 22:56:29 +00005005SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005006 FoldingSetNodeID ID;
5007 ID.AddInteger(2U);
5008 ID.AddInteger(VT1.getRawBits());
5009 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005010
Craig Topperc0196b12014-04-14 00:51:57 +00005011 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005012 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005013 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005014 EVT *Array = Allocator.Allocate<EVT>(2);
5015 Array[0] = VT1;
5016 Array[1] = VT2;
5017 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5018 VTListMap.InsertNode(Result, IP);
5019 }
5020 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005021}
Dan Gohman17059682008-07-17 19:10:17 +00005022
Owen Anderson53aa7a92009-08-10 22:56:29 +00005023SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005024 FoldingSetNodeID ID;
5025 ID.AddInteger(3U);
5026 ID.AddInteger(VT1.getRawBits());
5027 ID.AddInteger(VT2.getRawBits());
5028 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005029
Craig Topperc0196b12014-04-14 00:51:57 +00005030 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005031 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005032 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005033 EVT *Array = Allocator.Allocate<EVT>(3);
5034 Array[0] = VT1;
5035 Array[1] = VT2;
5036 Array[2] = VT3;
5037 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5038 VTListMap.InsertNode(Result, IP);
5039 }
5040 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005041}
5042
Owen Anderson53aa7a92009-08-10 22:56:29 +00005043SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005044 FoldingSetNodeID ID;
5045 ID.AddInteger(4U);
5046 ID.AddInteger(VT1.getRawBits());
5047 ID.AddInteger(VT2.getRawBits());
5048 ID.AddInteger(VT3.getRawBits());
5049 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005050
Craig Topperc0196b12014-04-14 00:51:57 +00005051 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005052 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005053 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005054 EVT *Array = Allocator.Allocate<EVT>(4);
5055 Array[0] = VT1;
5056 Array[1] = VT2;
5057 Array[2] = VT3;
5058 Array[3] = VT4;
5059 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5060 VTListMap.InsertNode(Result, IP);
5061 }
5062 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005063}
5064
Craig Topperabb4ac72014-04-16 06:10:51 +00005065SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5066 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005067 FoldingSetNodeID ID;
5068 ID.AddInteger(NumVTs);
5069 for (unsigned index = 0; index < NumVTs; index++) {
5070 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005071 }
5072
Craig Topperc0196b12014-04-14 00:51:57 +00005073 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005074 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005075 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005076 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005077 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005078 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5079 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005080 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005081 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005082}
5083
5084
Chris Lattnerf34156e2006-01-28 09:32:45 +00005085/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5086/// specified operands. If the resultant node already exists in the DAG,
5087/// this does not modify the specified node, instead it returns the node that
5088/// already exists. If the resultant node does not exist in the DAG, the
5089/// input node is returned. As a degenerate case, if you specify the same
5090/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005091SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005092 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005093
Chris Lattnerf34156e2006-01-28 09:32:45 +00005094 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005095 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005096
Chris Lattnerf34156e2006-01-28 09:32:45 +00005097 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005098 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005099 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005100 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005101
Dan Gohmanebeccb42008-07-21 22:38:59 +00005102 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005103 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005104 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005105 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005106
Chris Lattnerf34156e2006-01-28 09:32:45 +00005107 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005108 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005109
Chris Lattnerf34156e2006-01-28 09:32:45 +00005110 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005111 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005112 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005113}
5114
Dan Gohman92c11ac2010-06-18 15:30:29 +00005115SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005116 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005117
Chris Lattnerf34156e2006-01-28 09:32:45 +00005118 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005119 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005120 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005121
Chris Lattnerf34156e2006-01-28 09:32:45 +00005122 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005123 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005124 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005125 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005126
Dan Gohmanebeccb42008-07-21 22:38:59 +00005127 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005128 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005129 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005130 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005131
Chris Lattnerf34156e2006-01-28 09:32:45 +00005132 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005133 if (N->OperandList[0] != Op1)
5134 N->OperandList[0].set(Op1);
5135 if (N->OperandList[1] != Op2)
5136 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005137
Chris Lattnerf34156e2006-01-28 09:32:45 +00005138 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005139 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005140 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005141}
5142
Dan Gohman92c11ac2010-06-18 15:30:29 +00005143SDNode *SelectionDAG::
5144UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005145 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005146 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005147}
5148
Dan Gohman92c11ac2010-06-18 15:30:29 +00005149SDNode *SelectionDAG::
5150UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005151 SDValue Op3, SDValue Op4) {
5152 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005153 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005154}
5155
Dan Gohman92c11ac2010-06-18 15:30:29 +00005156SDNode *SelectionDAG::
5157UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005158 SDValue Op3, SDValue Op4, SDValue Op5) {
5159 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005160 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005161}
5162
Dan Gohman92c11ac2010-06-18 15:30:29 +00005163SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005164UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5165 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005166 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005167 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005168
Chris Lattnerf34156e2006-01-28 09:32:45 +00005169 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005170 bool AnyChange = false;
5171 for (unsigned i = 0; i != NumOps; ++i) {
5172 if (Ops[i] != N->getOperand(i)) {
5173 AnyChange = true;
5174 break;
5175 }
5176 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005177
Chris Lattnerf34156e2006-01-28 09:32:45 +00005178 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005179 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005180
Chris Lattnerf34156e2006-01-28 09:32:45 +00005181 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005182 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005183 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005184 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005185
Dan Gohman2f83b472008-05-02 00:05:03 +00005186 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005187 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005188 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005189 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005190
Chris Lattnerf34156e2006-01-28 09:32:45 +00005191 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005192 for (unsigned i = 0; i != NumOps; ++i)
5193 if (N->OperandList[i] != Ops[i])
5194 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005195
5196 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005197 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005198 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005199}
5200
Dan Gohman91697632008-07-07 20:57:48 +00005201/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005202/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005203void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005204 // Unlike the code in MorphNodeTo that does this, we don't need to
5205 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005206 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5207 SDUse &Use = *I++;
5208 Use.set(SDValue());
5209 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005210}
Chris Lattner19732782005-08-16 18:17:10 +00005211
Dan Gohman17059682008-07-17 19:10:17 +00005212/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5213/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005214///
Dan Gohman17059682008-07-17 19:10:17 +00005215SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005216 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005217 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005218 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005219}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005220
Dan Gohman17059682008-07-17 19:10:17 +00005221SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005222 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005223 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005224 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005225 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005226}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005227
Dan Gohman17059682008-07-17 19:10:17 +00005228SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005229 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005230 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005231 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005232 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005233 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005234}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005235
Dan Gohman17059682008-07-17 19:10:17 +00005236SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005237 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005238 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005239 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005240 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005241 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005242}
Chris Lattner466fece2005-08-21 22:30:30 +00005243
Dan Gohman17059682008-07-17 19:10:17 +00005244SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005245 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005246 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005247 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005248}
5249
Dan Gohman17059682008-07-17 19:10:17 +00005250SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005251 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005252 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005253 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005254}
5255
Dan Gohman17059682008-07-17 19:10:17 +00005256SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005257 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005258 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005259 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005260}
5261
Dan Gohman17059682008-07-17 19:10:17 +00005262SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005263 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005264 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005265 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005266 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005267}
5268
Bill Wendling2d598632008-12-01 23:28:22 +00005269SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005270 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005271 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005272 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005273 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005274}
5275
Scott Michelcf0da6c2009-02-17 22:15:04 +00005276SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005277 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005278 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005279 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005280 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005281 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005282}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005283
Scott Michelcf0da6c2009-02-17 22:15:04 +00005284SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005285 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005286 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005287 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005288 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005289 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005290}
5291
Dan Gohman17059682008-07-17 19:10:17 +00005292SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005293 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005294 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005295 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005296 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005297 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005298 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005299}
5300
Dan Gohman17059682008-07-17 19:10:17 +00005301SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005302 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005303 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005304 SDValue Op3) {
5305 SDVTList VTs = getVTList(VT1, VT2, VT3);
5306 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005307 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005308}
5309
5310SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005311 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005312 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005313 // Reset the NodeID to -1.
5314 N->setNodeId(-1);
5315 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005316}
5317
Andrew Trickef9de2a2013-05-25 02:42:55 +00005318/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005319/// the line number information on the merged node since it is not possible to
5320/// preserve the information that operation is associated with multiple lines.
5321/// This will make the debugger working better at -O0, were there is a higher
5322/// probability having other instructions associated with that line.
5323///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005324/// For IROrder, we keep the smaller of the two
5325SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005326 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005327 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5328 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005329 N->setDebugLoc(DebugLoc());
5330 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005331 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5332 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005333 return N;
5334}
5335
Chris Lattneraf197502010-02-28 21:36:14 +00005336/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005337/// return type, opcode, and operands.
5338///
5339/// Note that MorphNodeTo returns the resultant node. If there is already a
5340/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005341/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005342///
5343/// Using MorphNodeTo is faster than creating a new node and swapping it in
5344/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005345/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005346/// the node's users.
5347///
5348SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005349 SDVTList VTs, ArrayRef<SDValue> Ops) {
5350 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005351 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005352 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005353 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005354 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005355 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005356 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005357 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005358 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005359
Dan Gohmand3fe1742008-09-13 01:54:27 +00005360 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005361 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005362
Dan Gohman17059682008-07-17 19:10:17 +00005363 // Start the morphing.
5364 N->NodeType = Opc;
5365 N->ValueList = VTs.VTs;
5366 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005367
Dan Gohman17059682008-07-17 19:10:17 +00005368 // Clear the operands list, updating used nodes to remove this from their
5369 // use list. Keep track of any operands that become dead as a result.
5370 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005371 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5372 SDUse &Use = *I++;
5373 SDNode *Used = Use.getNode();
5374 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005375 if (Used->use_empty())
5376 DeadNodeSet.insert(Used);
5377 }
5378
Dan Gohman48b185d2009-09-25 20:36:54 +00005379 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5380 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005381 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005382 // If NumOps is larger than the # of operands we can have in a
5383 // MachineSDNode, reallocate the operand list.
5384 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5385 if (MN->OperandsNeedDelete)
5386 delete[] MN->OperandList;
5387 if (NumOps > array_lengthof(MN->LocalOperands))
5388 // We're creating a final node that will live unmorphed for the
5389 // remainder of the current SelectionDAG iteration, so we can allocate
5390 // the operands directly out of a pool with no recycling metadata.
5391 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005392 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005393 else
Craig Topper131de822014-04-27 19:21:16 +00005394 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005395 MN->OperandsNeedDelete = false;
5396 } else
Craig Topper131de822014-04-27 19:21:16 +00005397 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005398 } else {
5399 // If NumOps is larger than the # of operands we currently have, reallocate
5400 // the operand list.
5401 if (NumOps > N->NumOperands) {
5402 if (N->OperandsNeedDelete)
5403 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005404 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005405 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005406 } else
Craig Topper131de822014-04-27 19:21:16 +00005407 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005408 }
5409
5410 // Delete any nodes that are still dead after adding the uses for the
5411 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005412 if (!DeadNodeSet.empty()) {
5413 SmallVector<SDNode *, 16> DeadNodes;
5414 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5415 E = DeadNodeSet.end(); I != E; ++I)
5416 if ((*I)->use_empty())
5417 DeadNodes.push_back(*I);
5418 RemoveDeadNodes(DeadNodes);
5419 }
Dan Gohman91697632008-07-07 20:57:48 +00005420
Dan Gohman17059682008-07-17 19:10:17 +00005421 if (IP)
5422 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005423 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005424}
5425
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005426
Dan Gohman32f71d72009-09-25 18:54:59 +00005427/// getMachineNode - These are used for target selectors to create a new node
5428/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005429///
Dan Gohman32f71d72009-09-25 18:54:59 +00005430/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005431/// node of the specified opcode and operands, it returns that node instead of
5432/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005433MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005434SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005435 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005436 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005437}
Bill Wendlinga434d932009-01-29 09:01:55 +00005438
Dan Gohmana22f2d82009-10-10 01:29:16 +00005439MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005440SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005441 SDVTList VTs = getVTList(VT);
5442 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005443 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005444}
Bill Wendlinga434d932009-01-29 09:01:55 +00005445
Dan Gohmana22f2d82009-10-10 01:29:16 +00005446MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005447SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005448 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005449 SDVTList VTs = getVTList(VT);
5450 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005451 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005452}
Bill Wendlinga434d932009-01-29 09:01:55 +00005453
Dan Gohmana22f2d82009-10-10 01:29:16 +00005454MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005455SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005456 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005457 SDVTList VTs = getVTList(VT);
5458 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005459 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005460}
Bill Wendlinga434d932009-01-29 09:01:55 +00005461
Dan Gohmana22f2d82009-10-10 01:29:16 +00005462MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005463SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005464 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005465 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005466 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005467}
Bill Wendlinga434d932009-01-29 09:01:55 +00005468
Dan Gohmana22f2d82009-10-10 01:29:16 +00005469MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005470SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005471 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005472 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005473}
Bill Wendlinga434d932009-01-29 09:01:55 +00005474
Dan Gohmana22f2d82009-10-10 01:29:16 +00005475MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005476SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005477 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005478 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005479 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005480 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005481}
Bill Wendlinga434d932009-01-29 09:01:55 +00005482
Dan Gohmana22f2d82009-10-10 01:29:16 +00005483MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005484SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005485 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005486 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005487 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005488 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005489}
5490
Dan Gohmana22f2d82009-10-10 01:29:16 +00005491MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005492SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005493 EVT VT1, EVT VT2, SDValue Op1,
5494 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005495 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005496 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005497 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005498}
5499
Dan Gohmana22f2d82009-10-10 01:29:16 +00005500MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005501SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005502 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005503 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005504 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005505 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005506}
Bill Wendlinga434d932009-01-29 09:01:55 +00005507
Dan Gohmana22f2d82009-10-10 01:29:16 +00005508MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005509SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005510 EVT VT1, EVT VT2, EVT VT3,
5511 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005512 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005513 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005514 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005515}
Bill Wendlinga434d932009-01-29 09:01:55 +00005516
Dan Gohmana22f2d82009-10-10 01:29:16 +00005517MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005518SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005519 EVT VT1, EVT VT2, EVT VT3,
5520 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005521 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005522 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005523 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005524}
Bill Wendlinga434d932009-01-29 09:01:55 +00005525
Dan Gohmana22f2d82009-10-10 01:29:16 +00005526MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005527SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005528 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005529 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005530 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005531 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005532}
Bill Wendlinga434d932009-01-29 09:01:55 +00005533
Dan Gohmana22f2d82009-10-10 01:29:16 +00005534MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005535SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005536 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005537 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005538 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005539 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005540}
5541
Dan Gohmana22f2d82009-10-10 01:29:16 +00005542MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005543SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005544 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005545 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005546 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005547 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005548}
5549
Dan Gohmana22f2d82009-10-10 01:29:16 +00005550MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005551SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005552 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005553 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005554 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005555 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005556 const SDValue *Ops = OpsArray.data();
5557 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005558
5559 if (DoCSE) {
5560 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005561 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005562 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005563 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005564 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005565 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005566 }
5567
5568 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005569 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5570 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005571
5572 // Initialize the operands list.
5573 if (NumOps > array_lengthof(N->LocalOperands))
5574 // We're creating a final node that will live unmorphed for the
5575 // remainder of the current SelectionDAG iteration, so we can allocate
5576 // the operands directly out of a pool with no recycling metadata.
5577 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5578 Ops, NumOps);
5579 else
5580 N->InitOperands(N->LocalOperands, Ops, NumOps);
5581 N->OperandsNeedDelete = false;
5582
5583 if (DoCSE)
5584 CSEMap.InsertNode(N, IP);
5585
5586 AllNodes.push_back(N);
5587#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005588 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005589#endif
5590 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005591}
Evan Chengd3f1db92006-02-09 07:15:23 +00005592
Dan Gohmanac33a902009-08-19 18:16:17 +00005593/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005594/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005595SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005596SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005597 SDValue Operand) {
5598 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005599 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005600 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005601 return SDValue(Subreg, 0);
5602}
5603
Bob Wilson2a45a652009-10-08 18:49:46 +00005604/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005605/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005606SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005607SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005608 SDValue Operand, SDValue Subreg) {
5609 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005610 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005611 VT, Operand, Subreg, SRIdxVal);
5612 return SDValue(Result, 0);
5613}
5614
Evan Cheng31604a62008-03-22 01:55:50 +00005615/// getNodeIfExists - Get the specified node if it's already available, or
5616/// else return NULL.
5617SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Craig Topper633d99b2014-04-27 23:22:43 +00005618 ArrayRef<SDValue> Ops) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005619 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005620 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005621 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005622 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005623 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005624 return E;
5625 }
Craig Topperc0196b12014-04-14 00:51:57 +00005626 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005627}
5628
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005629/// getDbgValue - Creates a SDDbgValue node.
5630///
Adrian Prantl32da8892014-04-25 20:49:25 +00005631/// SDNode
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005632SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005633SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
5634 bool IsIndirect, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005635 DebugLoc DL, unsigned O) {
Adrian Prantl32da8892014-04-25 20:49:25 +00005636 return new (Allocator) SDDbgValue(MDPtr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005637}
5638
Adrian Prantl32da8892014-04-25 20:49:25 +00005639/// Constant
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005640SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005641SelectionDAG::getConstantDbgValue(MDNode *MDPtr, const Value *C,
5642 uint64_t Off,
5643 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005644 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5645}
5646
Adrian Prantl32da8892014-04-25 20:49:25 +00005647/// FrameIndex
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005648SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005649SelectionDAG::getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5650 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005651 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5652}
5653
Dan Gohman7d099f72010-03-03 21:33:37 +00005654namespace {
5655
Dan Gohman9cc886b2010-03-04 19:11:28 +00005656/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005657/// pointed to by a use iterator is deleted, increment the use iterator
5658/// so that it doesn't dangle.
5659///
Dan Gohman7d099f72010-03-03 21:33:37 +00005660class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005661 SDNode::use_iterator &UI;
5662 SDNode::use_iterator &UE;
5663
Craig Topper7b883b32014-03-08 06:31:39 +00005664 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005665 // Increment the iterator as needed.
5666 while (UI != UE && N == *UI)
5667 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005668 }
5669
5670public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005671 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005672 SDNode::use_iterator &ui,
5673 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005674 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005675};
5676
5677}
5678
Evan Cheng445b91a2006-08-07 22:13:29 +00005679/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005680/// This can cause recursive merging of nodes in the DAG.
5681///
Chris Lattner76858912008-02-03 03:35:22 +00005682/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005683///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005684void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005685 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005686 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005687 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005688 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005689
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005690 // Iterate over all the existing uses of From. New uses will be added
5691 // to the beginning of the use list, which we avoid visiting.
5692 // This specifically avoids visiting uses of From that arise while the
5693 // replacement is happening, because any such uses would be the result
5694 // of CSE: If an existing node looks like From after one of its operands
5695 // is replaced by To, we don't want to replace of all its users with To
5696 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005697 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005698 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005699 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005700 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005701
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005702 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005703 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005704
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005705 // A user can appear in a use list multiple times, and when this
5706 // happens the uses are usually next to each other in the list.
5707 // To help reduce the number of CSE recomputations, process all
5708 // the uses of this user that we can find this way.
5709 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005710 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005711 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005712 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005713 } while (UI != UE && *UI == User);
5714
5715 // Now that we have modified User, add it back to the CSE maps. If it
5716 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005717 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005718 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005719
5720 // If we just RAUW'd the root, take note.
5721 if (FromN == getRoot())
5722 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005723}
5724
5725/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5726/// This can cause recursive merging of nodes in the DAG.
5727///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005728/// This version assumes that for each value of From, there is a
5729/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005730///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005731void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005732#ifndef NDEBUG
5733 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5734 assert((!From->hasAnyUseOfValue(i) ||
5735 From->getValueType(i) == To->getValueType(i)) &&
5736 "Cannot use this version of ReplaceAllUsesWith!");
5737#endif
Dan Gohman17059682008-07-17 19:10:17 +00005738
5739 // Handle the trivial case.
5740 if (From == To)
5741 return;
5742
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005743 // Iterate over just the existing users of From. See the comments in
5744 // the ReplaceAllUsesWith above.
5745 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005746 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005747 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005748 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005749
Chris Lattner373f0482005-08-26 18:36:28 +00005750 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005751 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005752
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005753 // A user can appear in a use list multiple times, and when this
5754 // happens the uses are usually next to each other in the list.
5755 // To help reduce the number of CSE recomputations, process all
5756 // the uses of this user that we can find this way.
5757 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005758 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005759 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005760 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005761 } while (UI != UE && *UI == User);
5762
5763 // Now that we have modified User, add it back to the CSE maps. If it
5764 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005765 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005766 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005767
5768 // If we just RAUW'd the root, take note.
5769 if (From == getRoot().getNode())
5770 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005771}
5772
Chris Lattner373f0482005-08-26 18:36:28 +00005773/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5774/// This can cause recursive merging of nodes in the DAG.
5775///
5776/// This version can replace From with any result values. To must match the
5777/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005778void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005779 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005780 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005781
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005782 // Iterate over just the existing users of From. See the comments in
5783 // the ReplaceAllUsesWith above.
5784 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005785 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005786 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005787 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005788
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005789 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005790 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005791
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005792 // A user can appear in a use list multiple times, and when this
5793 // happens the uses are usually next to each other in the list.
5794 // To help reduce the number of CSE recomputations, process all
5795 // the uses of this user that we can find this way.
5796 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005797 SDUse &Use = UI.getUse();
5798 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005799 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005800 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005801 } while (UI != UE && *UI == User);
5802
5803 // Now that we have modified User, add it back to the CSE maps. If it
5804 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005805 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005806 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005807
5808 // If we just RAUW'd the root, take note.
5809 if (From == getRoot().getNode())
5810 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005811}
5812
Chris Lattner375e1a72006-02-17 21:58:01 +00005813/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005814/// uses of other values produced by From.getNode() alone. The Deleted
5815/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005816void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005817 // Handle the really simple, really trivial case efficiently.
5818 if (From == To) return;
5819
Chris Lattner375e1a72006-02-17 21:58:01 +00005820 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005821 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005822 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005823 return;
5824 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005825
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005826 // Iterate over just the existing users of From. See the comments in
5827 // the ReplaceAllUsesWith above.
5828 SDNode::use_iterator UI = From.getNode()->use_begin(),
5829 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005830 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005831 while (UI != UE) {
5832 SDNode *User = *UI;
5833 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005834
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005835 // A user can appear in a use list multiple times, and when this
5836 // happens the uses are usually next to each other in the list.
5837 // To help reduce the number of CSE recomputations, process all
5838 // the uses of this user that we can find this way.
5839 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005840 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005841
5842 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005843 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005844 ++UI;
5845 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005846 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005847
5848 // If this node hasn't been modified yet, it's still in the CSE maps,
5849 // so remove its old self from the CSE maps.
5850 if (!UserRemovedFromCSEMaps) {
5851 RemoveNodeFromCSEMaps(User);
5852 UserRemovedFromCSEMaps = true;
5853 }
5854
5855 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005856 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005857 } while (UI != UE && *UI == User);
5858
5859 // We are iterating over all uses of the From node, so if a use
5860 // doesn't use the specific value, no changes are made.
5861 if (!UserRemovedFromCSEMaps)
5862 continue;
5863
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005864 // Now that we have modified User, add it back to the CSE maps. If it
5865 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005866 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005867 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005868
5869 // If we just RAUW'd the root, take note.
5870 if (From == getRoot())
5871 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005872}
5873
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005874namespace {
5875 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5876 /// to record information about a use.
5877 struct UseMemo {
5878 SDNode *User;
5879 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005880 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005881 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005882
5883 /// operator< - Sort Memos by User.
5884 bool operator<(const UseMemo &L, const UseMemo &R) {
5885 return (intptr_t)L.User < (intptr_t)R.User;
5886 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005887}
5888
Dan Gohman17059682008-07-17 19:10:17 +00005889/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005890/// uses of other values produced by From.getNode() alone. The same value
5891/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005892/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005893void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5894 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005895 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005896 // Handle the simple, trivial case efficiently.
5897 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005898 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005899
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005900 // Read up all the uses and make records of them. This helps
5901 // processing new uses that are introduced during the
5902 // replacement process.
5903 SmallVector<UseMemo, 4> Uses;
5904 for (unsigned i = 0; i != Num; ++i) {
5905 unsigned FromResNo = From[i].getResNo();
5906 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005907 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005908 E = FromNode->use_end(); UI != E; ++UI) {
5909 SDUse &Use = UI.getUse();
5910 if (Use.getResNo() == FromResNo) {
5911 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005912 Uses.push_back(Memo);
5913 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005914 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005915 }
Dan Gohman17059682008-07-17 19:10:17 +00005916
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005917 // Sort the uses, so that all the uses from a given User are together.
5918 std::sort(Uses.begin(), Uses.end());
5919
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005920 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5921 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005922 // We know that this user uses some value of From. If it is the right
5923 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005924 SDNode *User = Uses[UseIndex].User;
5925
5926 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005927 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005928
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005929 // The Uses array is sorted, so all the uses for a given User
5930 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005931 // To help reduce the number of CSE recomputations, process all
5932 // the uses of this user that we can find this way.
5933 do {
5934 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005935 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005936 ++UseIndex;
5937
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005938 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005939 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5940
Dan Gohman17059682008-07-17 19:10:17 +00005941 // Now that we have modified User, add it back to the CSE maps. If it
5942 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005943 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005944 }
5945}
5946
Evan Cheng9631a602006-08-01 08:20:41 +00005947/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005948/// based on their topological order. It returns the maximum id and a vector
5949/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005950unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005951
Dan Gohman86aa16a2008-09-30 18:30:35 +00005952 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005953
Dan Gohman86aa16a2008-09-30 18:30:35 +00005954 // SortedPos tracks the progress of the algorithm. Nodes before it are
5955 // sorted, nodes after it are unsorted. When the algorithm completes
5956 // it is at the end of the list.
5957 allnodes_iterator SortedPos = allnodes_begin();
5958
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005959 // Visit all the nodes. Move nodes with no operands to the front of
5960 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005961 // operand count. Before we do this, the Node Id fields of the nodes
5962 // may contain arbitrary values. After, the Node Id fields for nodes
5963 // before SortedPos will contain the topological sort index, and the
5964 // Node Id fields for nodes At SortedPos and after will contain the
5965 // count of outstanding operands.
5966 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5967 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005968 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005969 unsigned Degree = N->getNumOperands();
5970 if (Degree == 0) {
5971 // A node with no uses, add it to the result array immediately.
5972 N->setNodeId(DAGSize++);
5973 allnodes_iterator Q = N;
5974 if (Q != SortedPos)
5975 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005976 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005977 ++SortedPos;
5978 } else {
5979 // Temporarily use the Node Id as scratch space for the degree count.
5980 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005981 }
5982 }
5983
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005984 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005985 // such that by the time the end is reached all nodes will be sorted.
5986 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5987 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00005988 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00005989 // N is in sorted position, so all its uses have one less operand
5990 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005991 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5992 UI != UE; ++UI) {
5993 SDNode *P = *UI;
5994 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00005995 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005996 --Degree;
5997 if (Degree == 0) {
5998 // All of P's operands are sorted, so P may sorted now.
5999 P->setNodeId(DAGSize++);
6000 if (P != SortedPos)
6001 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006002 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006003 ++SortedPos;
6004 } else {
6005 // Update P's outstanding operand count.
6006 P->setNodeId(Degree);
6007 }
6008 }
David Greene3b2a68c2010-01-20 00:59:23 +00006009 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006010#ifndef NDEBUG
6011 SDNode *S = ++I;
6012 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00006013 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00006014#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006015 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006016 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006017 }
6018
6019 assert(SortedPos == AllNodes.end() &&
6020 "Topological sort incomplete!");
6021 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6022 "First node in topological sort is not the entry token!");
6023 assert(AllNodes.front().getNodeId() == 0 &&
6024 "First node in topological sort has non-zero id!");
6025 assert(AllNodes.front().getNumOperands() == 0 &&
6026 "First node in topological sort has operands!");
6027 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6028 "Last node in topologic sort has unexpected id!");
6029 assert(AllNodes.back().use_empty() &&
6030 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006031 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006032 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006033}
6034
Evan Cheng563fe3c2010-03-25 01:38:16 +00006035/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6036/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006037void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6038 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006039 if (SD)
6040 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006041}
Evan Cheng29eefc12006-07-27 06:39:06 +00006042
Devang Patelefc6b162011-01-25 23:27:42 +00006043/// TransferDbgValues - Transfer SDDbgValues.
6044void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6045 if (From == To || !From.getNode()->getHasDebugValue())
6046 return;
6047 SDNode *FromNode = From.getNode();
6048 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006049 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006050 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006051 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006052 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006053 SDDbgValue *Dbg = *I;
6054 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6055 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
Adrian Prantl32da8892014-04-25 20:49:25 +00006056 Dbg->isIndirect(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006057 Dbg->getOffset(), Dbg->getDebugLoc(),
6058 Dbg->getOrder());
6059 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006060 }
6061 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006062 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006063 E = ClonedDVs.end(); I != E; ++I)
6064 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006065}
6066
Jim Laskeyd66e6162005-08-17 20:08:02 +00006067//===----------------------------------------------------------------------===//
6068// SDNode Class
6069//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006070
Chris Lattner3bf17b62007-02-04 02:41:42 +00006071HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006072 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006073}
6074
Andrew Trickef9de2a2013-05-25 02:42:55 +00006075GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6076 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006077 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006078 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006079 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006080}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006081
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006082AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6083 SDValue X, unsigned SrcAS,
6084 unsigned DestAS)
6085 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6086 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6087
Andrew Trickef9de2a2013-05-25 02:42:55 +00006088MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6089 EVT memvt, MachineMemOperand *mmo)
6090 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006091 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006092 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006093 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006094 assert(isNonTemporal() == MMO->isNonTemporal() &&
6095 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006096 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006097}
6098
Andrew Trickef9de2a2013-05-25 02:42:55 +00006099MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006100 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6101 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006102 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006103 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006104 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006105 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6106 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006107}
6108
Jim Laskeyf576b422006-10-27 23:46:08 +00006109/// Profile - Gather unique data for the node.
6110///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006111void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006112 AddNodeIDNode(ID, this);
6113}
6114
Owen Anderson3b1665e2009-08-25 22:27:22 +00006115namespace {
6116 struct EVTArray {
6117 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006118
Owen Anderson3b1665e2009-08-25 22:27:22 +00006119 EVTArray() {
6120 VTs.reserve(MVT::LAST_VALUETYPE);
6121 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6122 VTs.push_back(MVT((MVT::SimpleValueType)i));
6123 }
6124 };
6125}
6126
Owen Anderson53aa7a92009-08-10 22:56:29 +00006127static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006128static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006129static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006130
Chris Lattner88fa11c2005-11-08 23:30:28 +00006131/// getValueTypeList - Return a pointer to the specified value type.
6132///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006133const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006134 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006135 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006136 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006137 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006138 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006139 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006140 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006141 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006142}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006143
Chris Lattner40e79822005-01-12 18:37:47 +00006144/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6145/// indicated value. This method ignores uses of other values defined by this
6146/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006147bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006148 assert(Value < getNumValues() && "Bad value!");
6149
Roman Levenstein51f532f2008-04-07 10:06:32 +00006150 // TODO: Only iterate over uses of a given value of the node
6151 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006152 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006153 if (NUses == 0)
6154 return false;
6155 --NUses;
6156 }
Chris Lattner40e79822005-01-12 18:37:47 +00006157 }
6158
6159 // Found exactly the right number of uses?
6160 return NUses == 0;
6161}
6162
6163
Evan Cheng358c3d12007-08-02 05:29:38 +00006164/// hasAnyUseOfValue - Return true if there are any use of the indicated
6165/// value. This method ignores uses of other values defined by this operation.
6166bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6167 assert(Value < getNumValues() && "Bad value!");
6168
Dan Gohman7a510c22008-07-09 22:39:01 +00006169 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006170 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006171 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006172
6173 return false;
6174}
6175
6176
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006177/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006178///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006179bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006180 bool Seen = false;
6181 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006182 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006183 if (User == this)
6184 Seen = true;
6185 else
6186 return false;
6187 }
6188
6189 return Seen;
6190}
6191
Evan Cheng9456dd82006-11-03 07:31:32 +00006192/// isOperand - Return true if this node is an operand of N.
6193///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006194bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006195 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6196 if (*this == N->getOperand(i))
6197 return true;
6198 return false;
6199}
6200
Evan Cheng567d2e52008-03-04 00:41:45 +00006201bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006202 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006203 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006204 return true;
6205 return false;
6206}
Evan Chengd37645c2006-02-05 06:29:23 +00006207
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006208/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006209/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006210/// side-effecting instructions on any chain path. In practice, this looks
6211/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006212/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006213bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006214 unsigned Depth) const {
6215 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006216
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006217 // Don't search too deeply, we just want to be able to see through
6218 // TokenFactor's etc.
6219 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006220
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006221 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006222 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006223 if (getOpcode() == ISD::TokenFactor) {
6224 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006225 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6226 return false;
6227 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006228 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006229
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006230 // Loads don't have side effects, look through them.
6231 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6232 if (!Ld->isVolatile())
6233 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6234 }
6235 return false;
6236}
6237
Lang Hames5a004992011-07-07 04:31:51 +00006238/// hasPredecessor - Return true if N is a predecessor of this node.
6239/// N is either an operand of this node, or can be reached by recursively
6240/// traversing up the operands.
6241/// NOTE: This is an expensive method. Use it carefully.
6242bool SDNode::hasPredecessor(const SDNode *N) const {
6243 SmallPtrSet<const SDNode *, 32> Visited;
6244 SmallVector<const SDNode *, 16> Worklist;
6245 return hasPredecessorHelper(N, Visited, Worklist);
6246}
Dan Gohmancd139c02009-10-28 03:44:30 +00006247
Craig Topperb94011f2013-07-14 04:42:23 +00006248bool
6249SDNode::hasPredecessorHelper(const SDNode *N,
6250 SmallPtrSet<const SDNode *, 32> &Visited,
6251 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006252 if (Visited.empty()) {
6253 Worklist.push_back(this);
6254 } else {
6255 // Take a look in the visited set. If we've already encountered this node
6256 // we needn't search further.
6257 if (Visited.count(N))
6258 return true;
6259 }
6260
6261 // Haven't visited N yet. Continue the search.
6262 while (!Worklist.empty()) {
6263 const SDNode *M = Worklist.pop_back_val();
6264 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6265 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006266 if (Visited.insert(Op))
6267 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006268 if (Op == N)
6269 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006270 }
Lang Hames5a004992011-07-07 04:31:51 +00006271 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006272
6273 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006274}
6275
Evan Cheng5d9fd972006-10-04 00:56:09 +00006276uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6277 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006278 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006279}
6280
Mon P Wang32f8bb92009-11-30 02:42:02 +00006281SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6282 assert(N->getNumValues() == 1 &&
6283 "Can't unroll a vector with multiple results!");
6284
6285 EVT VT = N->getValueType(0);
6286 unsigned NE = VT.getVectorNumElements();
6287 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006288 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006289
6290 SmallVector<SDValue, 8> Scalars;
6291 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6292
6293 // If ResNE is 0, fully unroll the vector op.
6294 if (ResNE == 0)
6295 ResNE = NE;
6296 else if (NE > ResNE)
6297 NE = ResNE;
6298
6299 unsigned i;
6300 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006301 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006302 SDValue Operand = N->getOperand(j);
6303 EVT OperandVT = Operand.getValueType();
6304 if (OperandVT.isVector()) {
6305 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006306 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006307 EVT OperandEltVT = OperandVT.getVectorElementType();
6308 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6309 OperandEltVT,
6310 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006311 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006312 } else {
6313 // A scalar operand; just use it as is.
6314 Operands[j] = Operand;
6315 }
6316 }
6317
6318 switch (N->getOpcode()) {
6319 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006320 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006321 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006322 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006323 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006324 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006325 case ISD::SHL:
6326 case ISD::SRA:
6327 case ISD::SRL:
6328 case ISD::ROTL:
6329 case ISD::ROTR:
6330 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006331 getShiftAmountOperand(Operands[0].getValueType(),
6332 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006333 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006334 case ISD::SIGN_EXTEND_INREG:
6335 case ISD::FP_ROUND_INREG: {
6336 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6337 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6338 Operands[0],
6339 getValueType(ExtVT)));
6340 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006341 }
6342 }
6343
6344 for (; i < ResNE; ++i)
6345 Scalars.push_back(getUNDEF(EltVT));
6346
6347 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006348 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006349}
6350
Evan Chengf5938d52009-12-09 01:36:00 +00006351
Wesley Peck527da1b2010-11-23 03:31:01 +00006352/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6353/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006354/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006355bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006356 unsigned Bytes, int Dist) const {
6357 if (LD->getChain() != Base->getChain())
6358 return false;
6359 EVT VT = LD->getValueType(0);
6360 if (VT.getSizeInBits() / 8 != Bytes)
6361 return false;
6362
6363 SDValue Loc = LD->getOperand(1);
6364 SDValue BaseLoc = Base->getOperand(1);
6365 if (Loc.getOpcode() == ISD::FrameIndex) {
6366 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6367 return false;
6368 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6369 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6370 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6371 int FS = MFI->getObjectSize(FI);
6372 int BFS = MFI->getObjectSize(BFI);
6373 if (FS != BFS || FS != (int)Bytes) return false;
6374 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6375 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006376
6377 // Handle X+C
6378 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6379 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6380 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006381
Craig Topperc0196b12014-04-14 00:51:57 +00006382 const GlobalValue *GV1 = nullptr;
6383 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006384 int64_t Offset1 = 0;
6385 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006386 const TargetLowering *TLI = TM.getTargetLowering();
6387 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6388 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006389 if (isGA1 && isGA2 && GV1 == GV2)
6390 return Offset1 == (Offset2 + Dist*Bytes);
6391 return false;
6392}
6393
6394
Evan Cheng34a23ea2009-12-09 01:04:59 +00006395/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6396/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006397unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006398 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006399 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006400 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006401 const TargetLowering *TLI = TM.getTargetLowering();
6402 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006403 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006404 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +00006405 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6406 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006407 unsigned AlignBits = KnownZero.countTrailingOnes();
6408 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6409 if (Align)
6410 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006411 }
Evan Chengd938faf2009-12-09 01:53:58 +00006412
Evan Cheng34a23ea2009-12-09 01:04:59 +00006413 // If this is a direct reference to a stack slot, use information about the
6414 // stack slot's alignment.
6415 int FrameIdx = 1 << 31;
6416 int64_t FrameOffset = 0;
6417 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6418 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006419 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006420 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006421 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006422 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6423 FrameOffset = Ptr.getConstantOperandVal(1);
6424 }
6425
6426 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006427 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006428 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6429 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006430 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006431 }
6432
6433 return 0;
6434}
6435
Juergen Ributzkab3487102013-11-19 21:20:17 +00006436/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6437/// which is split (or expanded) into two not necessarily identical pieces.
6438std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6439 // Currently all types are split in half.
6440 EVT LoVT, HiVT;
6441 if (!VT.isVector()) {
6442 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6443 } else {
6444 unsigned NumElements = VT.getVectorNumElements();
6445 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6446 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6447 NumElements/2);
6448 }
6449 return std::make_pair(LoVT, HiVT);
6450}
6451
6452/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6453/// low/high part.
6454std::pair<SDValue, SDValue>
6455SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6456 const EVT &HiVT) {
6457 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6458 N.getValueType().getVectorNumElements() &&
6459 "More vector elements requested than available!");
6460 SDValue Lo, Hi;
6461 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6462 getConstant(0, TLI->getVectorIdxTy()));
6463 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6464 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6465 return std::make_pair(Lo, Hi);
6466}
6467
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006468void SelectionDAG::ExtractVectorElements(SDValue Op,
6469 SmallVectorImpl<SDValue> &Args,
6470 unsigned Start, unsigned Count) {
6471 EVT VT = Op.getValueType();
6472 if (Count == 0)
6473 Count = VT.getVectorNumElements();
6474
6475 EVT EltVT = VT.getVectorElementType();
6476 EVT IdxTy = TLI->getVectorIdxTy();
6477 SDLoc SL(Op);
6478 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6479 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6480 Op, getConstant(i, IdxTy)));
6481 }
6482}
6483
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006484// getAddressSpace - Return the address space this GlobalAddress belongs to.
6485unsigned GlobalAddressSDNode::getAddressSpace() const {
6486 return getGlobal()->getType()->getAddressSpace();
6487}
6488
6489
Chris Lattner229907c2011-07-18 04:54:35 +00006490Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006491 if (isMachineConstantPoolEntry())
6492 return Val.MachineCPVal->getType();
6493 return Val.ConstVal->getType();
6494}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006495
Bob Wilson85cefe82009-03-02 23:24:16 +00006496bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6497 APInt &SplatUndef,
6498 unsigned &SplatBitSize,
6499 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006500 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006501 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006502 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006503 assert(VT.isVector() && "Expected a vector type");
6504 unsigned sz = VT.getSizeInBits();
6505 if (MinSplatBits > sz)
6506 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006507
Bob Wilson85cefe82009-03-02 23:24:16 +00006508 SplatValue = APInt(sz, 0);
6509 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006510
Bob Wilson85cefe82009-03-02 23:24:16 +00006511 // Get the bits. Bits with undefined values (when the corresponding element
6512 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6513 // in SplatValue. If any of the values are not constant, give up and return
6514 // false.
6515 unsigned int nOps = getNumOperands();
6516 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6517 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006518
6519 for (unsigned j = 0; j < nOps; ++j) {
6520 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006521 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006522 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006523
Bob Wilson85cefe82009-03-02 23:24:16 +00006524 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006525 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006526 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006527 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006528 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006529 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006530 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006531 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006532 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006533 }
6534
Bob Wilson85cefe82009-03-02 23:24:16 +00006535 // The build_vector is all constants or undefs. Find the smallest element
6536 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006537
Bob Wilson85cefe82009-03-02 23:24:16 +00006538 HasAnyUndefs = (SplatUndef != 0);
6539 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006540
Bob Wilson85cefe82009-03-02 23:24:16 +00006541 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006542 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6543 APInt LowValue = SplatValue.trunc(HalfSize);
6544 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6545 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006546
Bob Wilson85cefe82009-03-02 23:24:16 +00006547 // If the two halves do not match (ignoring undef bits), stop here.
6548 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6549 MinSplatBits > HalfSize)
6550 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006551
Bob Wilson85cefe82009-03-02 23:24:16 +00006552 SplatValue = HighValue | LowValue;
6553 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006554
Bob Wilson85cefe82009-03-02 23:24:16 +00006555 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006556 }
6557
Bob Wilson85cefe82009-03-02 23:24:16 +00006558 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006559 return true;
6560}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006561
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006562ConstantSDNode *BuildVectorSDNode::getConstantSplatValue() const {
Matt Arsenault985b9de2014-03-17 18:58:01 +00006563 SDValue Op0 = getOperand(0);
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006564 if (Op0.getOpcode() != ISD::Constant)
6565 return nullptr;
6566
6567 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
6568 if (getOperand(i) != Op0)
Matt Arsenault985b9de2014-03-17 18:58:01 +00006569 return nullptr;
Matt Arsenault985b9de2014-03-17 18:58:01 +00006570
6571 return cast<ConstantSDNode>(Op0);
6572}
6573
Juergen Ributzka73844052014-01-13 20:51:35 +00006574bool BuildVectorSDNode::isConstant() const {
6575 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6576 unsigned Opc = getOperand(i).getOpcode();
6577 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6578 return false;
6579 }
6580 return true;
6581}
6582
Owen Anderson53aa7a92009-08-10 22:56:29 +00006583bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006584 // Find the first non-undef value in the shuffle mask.
6585 unsigned i, e;
6586 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6587 /* search */;
6588
Nate Begeman39b59db2009-04-29 18:13:31 +00006589 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006590
Nate Begeman5f829d82009-04-29 05:20:52 +00006591 // Make sure all remaining elements are either undef or the same as the first
6592 // non-undef value.
6593 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006594 if (Mask[i] >= 0 && Mask[i] != Idx)
6595 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006596 return true;
6597}
David Greene09851602010-01-20 20:13:31 +00006598
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006599#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006600static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006601 SmallPtrSet<const SDNode*, 32> &Visited,
6602 SmallPtrSet<const SDNode*, 32> &Checked) {
6603 // If this node has already been checked, don't check it again.
6604 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006605 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006606
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006607 // If a node has already been visited on this depth-first walk, reject it as
6608 // a cycle.
6609 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006610 dbgs() << "Offending node:\n";
6611 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006612 errs() << "Detected cycle in SelectionDAG\n";
6613 abort();
David Greene09851602010-01-20 20:13:31 +00006614 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006615
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006616 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6617 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006618
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006619 Checked.insert(N);
6620 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006621}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006622#endif
David Greene09851602010-01-20 20:13:31 +00006623
6624void llvm::checkForCycles(const llvm::SDNode *N) {
6625#ifdef XDEBUG
Alp Toker6a033742013-10-29 02:35:28 +00006626 assert(N && "Checking nonexistent SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006627 SmallPtrSet<const SDNode*, 32> visited;
6628 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006629 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006630#endif
6631}
6632
6633void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6634 checkForCycles(DAG->getRoot().getNode());
6635}