blob: 46213a106f3adf4a0da58a558296d593e28b01ad [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
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000995SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000996 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +0000997 assert((EltVT.getSizeInBits() >= 64 ||
998 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
999 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001000 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001001}
1002
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001003SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1004{
1005 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001006}
1007
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001008SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1009 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001010 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001011
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001012 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001013 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001014
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001015 const TargetLowering *TLI = TM.getTargetLowering();
1016
Duncan Sandsf2641e12011-09-06 19:07:46 +00001017 // In some cases the vector type is legal but the element type is illegal and
1018 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1019 // inserted value (the type does not need to match the vector element type).
1020 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001021 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001022 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001023 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001024 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1025 Elt = ConstantInt::get(*getContext(), NewVal);
1026 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001027 // In other cases the element type is illegal and needs to be expanded, for
1028 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1029 // the value into n parts and use a vector type with n-times the elements.
1030 // Then bitcast to the type requested.
1031 // Legalizing constants too early makes the DAGCombiner's job harder so we
1032 // only legalize if the DAG tells us we must produce legal types.
1033 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1034 TLI->getTypeAction(*getContext(), EltVT) ==
1035 TargetLowering::TypeExpandInteger) {
1036 APInt NewVal = Elt->getValue();
1037 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1038 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1039 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1040 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1041
1042 // Check the temporary vector is the correct size. If this fails then
1043 // getTypeToTransformTo() probably returned a type whose size (in bits)
1044 // isn't a power-of-2 factor of the requested type size.
1045 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1046
1047 SmallVector<SDValue, 2> EltParts;
1048 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1049 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1050 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001051 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001052 }
1053
1054 // EltParts is currently in little endian order. If we actually want
1055 // big-endian order then reverse it now.
1056 if (TLI->isBigEndian())
1057 std::reverse(EltParts.begin(), EltParts.end());
1058
1059 // The elements must be reversed when the element order is different
1060 // to the endianness of the elements (because the BITCAST is itself a
1061 // vector shuffle in this situation). However, we do not need any code to
1062 // perform this reversal because getConstant() is producing a vector
1063 // splat.
1064 // This situation occurs in MIPS MSA.
1065
1066 SmallVector<SDValue, 8> Ops;
1067 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1068 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1069
1070 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1071 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001072 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001073 return Result;
1074 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001075
1076 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1077 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001078 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001079 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001080 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001081 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001082 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001083 void *IP = nullptr;
1084 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001085 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001086 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001087 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001088
Dan Gohman7a7742c2007-12-12 22:21:26 +00001089 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001090 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001091 CSEMap.InsertNode(N, IP);
1092 AllNodes.push_back(N);
1093 }
1094
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001095 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001096 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001097 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001098 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001099 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001100 }
1101 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001102}
1103
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001104SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001105 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001106}
1107
1108
Owen Anderson53aa7a92009-08-10 22:56:29 +00001109SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001110 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001111}
1112
Owen Anderson53aa7a92009-08-10 22:56:29 +00001113SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001114 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001115
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001116 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001117
Chris Lattner381dddc2005-02-17 20:17:32 +00001118 // Do the map lookup using the actual bit pattern for the floating point
1119 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1120 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001121 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001122 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001123 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001124 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001125 void *IP = nullptr;
1126 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001127 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001128 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001130
Evan Cheng9458e6a2007-06-29 21:36:04 +00001131 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001132 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001133 CSEMap.InsertNode(N, IP);
1134 AllNodes.push_back(N);
1135 }
1136
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001137 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001138 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001139 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001140 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001141 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001142 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001143 }
1144 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001145}
1146
Owen Anderson53aa7a92009-08-10 22:56:29 +00001147SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001148 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001149 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001150 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001151 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001152 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001153 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1154 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001155 bool ignored;
1156 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001157 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001158 &ignored);
1159 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001160 } else
1161 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001162}
1163
Andrew Trickef9de2a2013-05-25 02:42:55 +00001164SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001165 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001166 bool isTargetGA,
1167 unsigned char TargetFlags) {
1168 assert((TargetFlags == 0 || isTargetGA) &&
1169 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001170 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001171
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001172 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001173 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001174 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001175 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001176
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001177 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1178 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001179 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001180 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Rafael Espindola24a669d2014-03-27 15:26:56 +00001181 GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasedGlobal());
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001182 }
1183
Chris Lattner8e34f982009-06-25 21:21:14 +00001184 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001185 if (GVar && GVar->isThreadLocal())
1186 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1187 else
1188 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001189
Jim Laskeyf576b422006-10-27 23:46:08 +00001190 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001191 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001192 ID.AddPointer(GV);
1193 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001194 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001195 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001196 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001197 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001198 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001199
Andrew Trickef9de2a2013-05-25 02:42:55 +00001200 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1201 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001202 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001203 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001204 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001205 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001206}
1207
Owen Anderson53aa7a92009-08-10 22:56:29 +00001208SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001209 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001210 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001211 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001212 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001213 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001214 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001215 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001216
Dan Gohman01c65a22010-03-18 18:49:47 +00001217 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001218 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001219 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001220 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001221}
1222
Owen Anderson53aa7a92009-08-10 22:56:29 +00001223SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001224 unsigned char TargetFlags) {
1225 assert((TargetFlags == 0 || isTarget) &&
1226 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001227 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001228 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001229 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001230 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001231 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001232 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001233 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001234 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001235
Dan Gohman01c65a22010-03-18 18:49:47 +00001236 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1237 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001238 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001239 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001240 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001241}
1242
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001243SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001244 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001245 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001246 unsigned char TargetFlags) {
1247 assert((TargetFlags == 0 || isTarget) &&
1248 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001249 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001250 Alignment =
1251 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001252 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001253 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001254 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001255 ID.AddInteger(Alignment);
1256 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001257 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001258 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001259 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001260 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001261 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001262
Dan Gohman01c65a22010-03-18 18:49:47 +00001263 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1264 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001265 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001266 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001267 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001268}
1269
Chris Lattner061a1ea2005-01-07 07:46:32 +00001270
Owen Anderson53aa7a92009-08-10 22:56:29 +00001271SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001272 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001273 bool isTarget,
1274 unsigned char TargetFlags) {
1275 assert((TargetFlags == 0 || isTarget) &&
1276 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001277 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001278 Alignment =
1279 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001280 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001281 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001282 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001283 ID.AddInteger(Alignment);
1284 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001285 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001286 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001287 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001288 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001289 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001290
Dan Gohman01c65a22010-03-18 18:49:47 +00001291 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1292 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001293 CSEMap.InsertNode(N, IP);
1294 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001295 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001296}
1297
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001298SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1299 unsigned char TargetFlags) {
1300 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001301 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001302 ID.AddInteger(Index);
1303 ID.AddInteger(Offset);
1304 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001305 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001306 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1307 return SDValue(E, 0);
1308
1309 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1310 TargetFlags);
1311 CSEMap.InsertNode(N, IP);
1312 AllNodes.push_back(N);
1313 return SDValue(N, 0);
1314}
1315
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001316SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001317 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001318 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001319 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001320 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001321 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001322 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001323
Dan Gohman01c65a22010-03-18 18:49:47 +00001324 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001325 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001326 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001327 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001328}
1329
Owen Anderson53aa7a92009-08-10 22:56:29 +00001330SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001331 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1332 ValueTypeNodes.size())
1333 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001334
Duncan Sands13237ac2008-06-06 12:08:01 +00001335 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001336 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001337
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001338 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001339 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001340 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001341 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001342}
1343
Owen Anderson53aa7a92009-08-10 22:56:29 +00001344SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001345 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001346 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001347 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001348 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001349 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001350}
1351
Owen Anderson53aa7a92009-08-10 22:56:29 +00001352SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001353 unsigned char TargetFlags) {
1354 SDNode *&N =
1355 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1356 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001358 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001359 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001360 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001361}
1362
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001363SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001364 if ((unsigned)Cond >= CondCodeNodes.size())
1365 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001366
Craig Topperc0196b12014-04-14 00:51:57 +00001367 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001368 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001369 CondCodeNodes[Cond] = N;
1370 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001371 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001372
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001373 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001374}
1375
Nate Begeman5f829d82009-04-29 05:20:52 +00001376// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1377// the shuffle mask M that point at N1 to point at N2, and indices that point
1378// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001379static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1380 std::swap(N1, N2);
1381 int NElts = M.size();
1382 for (int i = 0; i != NElts; ++i) {
1383 if (M[i] >= NElts)
1384 M[i] -= NElts;
1385 else if (M[i] >= 0)
1386 M[i] += NElts;
1387 }
1388}
1389
Andrew Trickef9de2a2013-05-25 02:42:55 +00001390SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001391 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001392 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1393 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001394
1395 // Canonicalize shuffle undef, undef -> undef
1396 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001397 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001398
Eric Christopherdfda92b2009-08-22 00:40:45 +00001399 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001400 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001401 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001402 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001403 for (unsigned i = 0; i != NElts; ++i) {
1404 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001405 MaskVec.push_back(Mask[i]);
1406 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001407
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001408 // Canonicalize shuffle v, v -> v, undef
1409 if (N1 == N2) {
1410 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001411 for (unsigned i = 0; i != NElts; ++i)
1412 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001413 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001414
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001415 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1416 if (N1.getOpcode() == ISD::UNDEF)
1417 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001418
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001419 // Canonicalize all index into lhs, -> shuffle lhs, undef
1420 // Canonicalize all index into rhs, -> shuffle rhs, undef
1421 bool AllLHS = true, AllRHS = true;
1422 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001423 for (unsigned i = 0; i != NElts; ++i) {
1424 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001425 if (N2Undef)
1426 MaskVec[i] = -1;
1427 else
1428 AllLHS = false;
1429 } else if (MaskVec[i] >= 0) {
1430 AllRHS = false;
1431 }
1432 }
1433 if (AllLHS && AllRHS)
1434 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001435 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001436 N2 = getUNDEF(VT);
1437 if (AllRHS) {
1438 N1 = getUNDEF(VT);
1439 commuteShuffle(N1, N2, MaskVec);
1440 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001441
Craig Topper9a39b072013-08-08 08:03:12 +00001442 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001443 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001444 for (unsigned i = 0; i != NElts; ++i) {
1445 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001446 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001447 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001448 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001449
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001450 // Shuffling a constant splat doesn't change the result.
1451 if (N2Undef && N1.getOpcode() == ISD::BUILD_VECTOR)
1452 if (cast<BuildVectorSDNode>(N1)->getConstantSplatValue())
1453 return N1;
1454
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001455 FoldingSetNodeID ID;
1456 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001457 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001458 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001459 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001460
Craig Topperc0196b12014-04-14 00:51:57 +00001461 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001462 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001463 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001464
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001465 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1466 // SDNode doesn't have access to it. This memory will be "leaked" when
1467 // the node is deallocated, but recovered when the NodeAllocator is released.
1468 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1469 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001470
Dan Gohman01c65a22010-03-18 18:49:47 +00001471 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001472 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1473 dl.getDebugLoc(), N1, N2,
1474 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001475 CSEMap.InsertNode(N, IP);
1476 AllNodes.push_back(N);
1477 return SDValue(N, 0);
1478}
1479
Andrew Trickef9de2a2013-05-25 02:42:55 +00001480SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001481 SDValue Val, SDValue DTy,
1482 SDValue STy, SDValue Rnd, SDValue Sat,
1483 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001484 // If the src and dest types are the same and the conversion is between
1485 // integer types of the same sign or two floats, no conversion is necessary.
1486 if (DTy == STy &&
1487 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001488 return Val;
1489
1490 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001491 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001492 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001493 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001494 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001495 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001496
Jack Carter170a5f22013-09-09 22:02:08 +00001497 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1498 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001499 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001500 CSEMap.InsertNode(N, IP);
1501 AllNodes.push_back(N);
1502 return SDValue(N, 0);
1503}
1504
Owen Anderson53aa7a92009-08-10 22:56:29 +00001505SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001506 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001507 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001508 ID.AddInteger(RegNo);
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))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001511 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001512
Dan Gohman01c65a22010-03-18 18:49:47 +00001513 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001514 CSEMap.InsertNode(N, IP);
1515 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001516 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001517}
1518
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001519SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1520 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001521 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001522 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001523 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001524 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1525 return SDValue(E, 0);
1526
1527 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1528 CSEMap.InsertNode(N, IP);
1529 AllNodes.push_back(N);
1530 return SDValue(N, 0);
1531}
1532
Andrew Trickef9de2a2013-05-25 02:42:55 +00001533SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001534 FoldingSetNodeID ID;
1535 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001536 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001537 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001538 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001539 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001540 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001541
Jack Carter170a5f22013-09-09 22:02:08 +00001542 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1543 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001544 CSEMap.InsertNode(N, IP);
1545 AllNodes.push_back(N);
1546 return SDValue(N, 0);
1547}
1548
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001549
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001550SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001551 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001552 bool isTarget,
1553 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001554 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1555
1556 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001557 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001558 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001559 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001560 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001561 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001562 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001563 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001564
Michael Liaoabb87d42012-09-12 21:43:09 +00001565 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1566 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001567 CSEMap.InsertNode(N, IP);
1568 AllNodes.push_back(N);
1569 return SDValue(N, 0);
1570}
1571
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001572SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001573 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001574 "SrcValue is not a pointer?");
1575
Jim Laskeyf576b422006-10-27 23:46:08 +00001576 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001577 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001578 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001579
Craig Topperc0196b12014-04-14 00:51:57 +00001580 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001581 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001582 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001583
Dan Gohman01c65a22010-03-18 18:49:47 +00001584 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001585 CSEMap.InsertNode(N, IP);
1586 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001587 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001588}
1589
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001590/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1591SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1592 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001593 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001594 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001595
Craig Topperc0196b12014-04-14 00:51:57 +00001596 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001597 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1598 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001599
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001600 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1601 CSEMap.InsertNode(N, IP);
1602 AllNodes.push_back(N);
1603 return SDValue(N, 0);
1604}
1605
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001606/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1607SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1608 unsigned SrcAS, unsigned DestAS) {
1609 SDValue Ops[] = {Ptr};
1610 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001611 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001612 ID.AddInteger(SrcAS);
1613 ID.AddInteger(DestAS);
1614
Craig Topperc0196b12014-04-14 00:51:57 +00001615 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001616 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1617 return SDValue(E, 0);
1618
1619 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1620 dl.getDebugLoc(),
1621 VT, Ptr, SrcAS, DestAS);
1622 CSEMap.InsertNode(N, IP);
1623 AllNodes.push_back(N);
1624 return SDValue(N, 0);
1625}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001626
Duncan Sands41826032009-01-31 15:50:11 +00001627/// getShiftAmountOperand - Return the specified value casted to
1628/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001629SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001630 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001631 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001632 if (OpTy == ShTy || OpTy.isVector()) return Op;
1633
1634 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001635 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001636}
1637
Chris Lattner9eb7a822007-10-15 17:47:20 +00001638/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1639/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001640SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001641 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001642 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001643 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001644 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001645 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001646 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001647
David Greene1fbe0542009-11-12 20:49:22 +00001648 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001649 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001650}
1651
Duncan Sands445071c2008-12-09 21:33:20 +00001652/// CreateStackTemporary - Create a stack temporary suitable for holding
1653/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001654SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001655 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1656 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001657 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1658 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001659 const TargetLowering *TLI = TM.getTargetLowering();
1660 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001661 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1662 TD->getPrefTypeAlignment(Ty2));
1663
1664 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001665 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001666 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001667}
1668
Owen Anderson53aa7a92009-08-10 22:56:29 +00001669SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001670 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001671 // These setcc operations always fold.
1672 switch (Cond) {
1673 default: break;
1674 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001675 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001676 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001677 case ISD::SETTRUE2: {
1678 const TargetLowering *TLI = TM.getTargetLowering();
1679 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1680 return getConstant(
1681 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1682 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001683
Chris Lattner393d96a2006-04-27 05:01:07 +00001684 case ISD::SETOEQ:
1685 case ISD::SETOGT:
1686 case ISD::SETOGE:
1687 case ISD::SETOLT:
1688 case ISD::SETOLE:
1689 case ISD::SETONE:
1690 case ISD::SETO:
1691 case ISD::SETUO:
1692 case ISD::SETUEQ:
1693 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001694 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001695 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001696 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001697
Gabor Greiff304a7a2008-08-28 21:40:38 +00001698 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001699 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001700 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001701 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001702
Chris Lattner061a1ea2005-01-07 07:46:32 +00001703 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001704 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001705 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1706 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001707 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1708 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1709 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1710 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1711 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1712 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1713 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1714 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001715 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001716 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001717 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001718 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1719 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001720 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001721 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001722 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001723 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001724 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001725 // fall through
1726 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001727 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001728 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001729 // fall through
1730 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001731 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001732 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001733 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001734 // fall through
1735 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001736 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001737 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001738 // fall through
1739 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001740 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001741 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001742 // fall through
1743 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001744 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001745 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001746 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001747 // fall through
1748 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001749 R==APFloat::cmpEqual, VT);
1750 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1751 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1752 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1753 R==APFloat::cmpEqual, VT);
1754 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1755 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1756 R==APFloat::cmpLessThan, VT);
1757 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1758 R==APFloat::cmpUnordered, VT);
1759 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1760 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001761 }
1762 } else {
1763 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001764 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1765 MVT CompVT = N1.getValueType().getSimpleVT();
1766 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1767 return SDValue();
1768
1769 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001770 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001771 }
1772
Chris Lattnerd47675e2005-08-09 20:20:18 +00001773 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001774 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001775}
1776
Dan Gohman1f372ed2008-02-25 21:11:39 +00001777/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1778/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001779bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001780 // This predicate is not safe for vector operations.
1781 if (Op.getValueType().isVector())
1782 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001783
Dan Gohman1d459e42009-12-11 21:31:27 +00001784 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001785 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1786}
1787
Dan Gohman309d3d52007-06-22 14:59:07 +00001788/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1789/// this predicate to simplify operations downstream. Mask is known to be zero
1790/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001791bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001792 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001793 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001794 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001795 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001796 return (KnownZero & Mask) == Mask;
1797}
1798
1799/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1800/// known to be either zero or one and return them in the KnownZero/KnownOne
1801/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1802/// processing.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001803void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1804 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001805 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001806 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001807
Dan Gohmanf990faf2008-02-13 00:35:47 +00001808 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001809 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001810 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001811
Dan Gohmanf990faf2008-02-13 00:35:47 +00001812 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001813
1814 switch (Op.getOpcode()) {
1815 case ISD::Constant:
1816 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001817 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1818 KnownZero = ~KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00001819 return;
1820 case ISD::AND:
1821 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001822 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1823 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001824 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1825 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001826
1827 // Output known-1 bits are only known if set in both the LHS & RHS.
1828 KnownOne &= KnownOne2;
1829 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1830 KnownZero |= KnownZero2;
1831 return;
1832 case ISD::OR:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001833 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1834 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001835 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1836 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1837
Dan Gohman309d3d52007-06-22 14:59:07 +00001838 // Output known-0 bits are only known if clear in both the LHS & RHS.
1839 KnownZero &= KnownZero2;
1840 // Output known-1 are known to be set if set in either the LHS | RHS.
1841 KnownOne |= KnownOne2;
1842 return;
1843 case ISD::XOR: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001844 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1845 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001846 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1847 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1848
Dan Gohman309d3d52007-06-22 14:59:07 +00001849 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001850 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001851 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1852 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1853 KnownZero = KnownZeroOut;
1854 return;
1855 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001856 case ISD::MUL: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001857 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1858 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001859 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1860 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1861
1862 // If low bits are zero in either operand, output low known-0 bits.
1863 // Also compute a conserative estimate for high known-0 bits.
1864 // More trickiness is possible, but this is sufficient for the
1865 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001866 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001867 unsigned TrailZ = KnownZero.countTrailingOnes() +
1868 KnownZero2.countTrailingOnes();
1869 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001870 KnownZero2.countLeadingOnes(),
1871 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001872
1873 TrailZ = std::min(TrailZ, BitWidth);
1874 LeadZ = std::min(LeadZ, BitWidth);
1875 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1876 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001877 return;
1878 }
1879 case ISD::UDIV: {
1880 // For the purposes of computing leading zeros we can conservatively
1881 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001882 // be less than the denominator.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001883 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001884 unsigned LeadZ = KnownZero2.countLeadingOnes();
1885
Jay Foad25a5e4c2010-12-01 08:53:58 +00001886 KnownOne2.clearAllBits();
1887 KnownZero2.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001888 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001889 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1890 if (RHSUnknownLeadingOnes != BitWidth)
1891 LeadZ = std::min(BitWidth,
1892 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001893
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001894 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001895 return;
1896 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001897 case ISD::SELECT:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001898 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1899 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001900 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1901 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1902
Dan Gohman309d3d52007-06-22 14:59:07 +00001903 // Only known if known in both the LHS and RHS.
1904 KnownOne &= KnownOne2;
1905 KnownZero &= KnownZero2;
1906 return;
1907 case ISD::SELECT_CC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001908 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1909 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001910 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1911 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1912
Dan Gohman309d3d52007-06-22 14:59:07 +00001913 // Only known if known in both the LHS and RHS.
1914 KnownOne &= KnownOne2;
1915 KnownZero &= KnownZero2;
1916 return;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001917 case ISD::SADDO:
1918 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001919 case ISD::SSUBO:
1920 case ISD::USUBO:
1921 case ISD::SMULO:
1922 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001923 if (Op.getResNo() != 1)
1924 return;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001925 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001926 case ISD::SETCC:
1927 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001928 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001929 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001930 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001931 return;
1932 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001933 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001934 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001935 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001936
1937 // If the shift count is an invalid immediate, don't do anything.
1938 if (ShAmt >= BitWidth)
1939 return;
1940
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001941 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001942 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
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 }
1948 return;
1949 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)
1956 return;
1957
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001958 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001959 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001960 KnownZero = KnownZero.lshr(ShAmt);
1961 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001962
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001963 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001964 KnownZero |= HighBits; // High bits known zero.
1965 }
1966 return;
1967 case ISD::SRA:
1968 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001969 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001970
Dan Gohman9db0aa82008-02-26 18:50:50 +00001971 // If the shift count is an invalid immediate, don't do anything.
1972 if (ShAmt >= BitWidth)
1973 return;
1974
Dan Gohman309d3d52007-06-22 14:59:07 +00001975 // If any of the demanded bits are produced by the sign extension, we also
1976 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001977 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001978
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001979 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001980 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001981 KnownZero = KnownZero.lshr(ShAmt);
1982 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001983
Dan Gohman309d3d52007-06-22 14:59:07 +00001984 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001985 APInt SignBit = APInt::getSignBit(BitWidth);
1986 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001987
Dan Gohmanb717fda2008-02-20 16:30:17 +00001988 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001989 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001990 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001991 KnownOne |= HighBits; // New bits are known one.
1992 }
1993 }
1994 return;
1995 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001996 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001997 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001998
1999 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002000 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002001 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002002
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002003 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002004 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002005
Dan Gohman309d3d52007-06-22 14:59:07 +00002006 // If the sign extended bits are demanded, we know that the sign
2007 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002008 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002009 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002010 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002011
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002012 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2013 KnownOne &= InputDemandedBits;
2014 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002015 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
2016
Dan Gohman309d3d52007-06-22 14:59:07 +00002017 // If the sign bit of the input is known set or clear, then we know the
2018 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002019 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002020 KnownZero |= NewBits;
2021 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002022 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002023 KnownOne |= NewBits;
2024 KnownZero &= ~NewBits;
2025 } else { // Input sign bit unknown
2026 KnownZero &= ~NewBits;
2027 KnownOne &= ~NewBits;
2028 }
2029 return;
2030 }
2031 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002032 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002033 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002034 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002035 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002036 unsigned LowBits = Log2_32(BitWidth)+1;
2037 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002038 KnownOne.clearAllBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002039 return;
2040 }
2041 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002042 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002043 // If this is a ZEXTLoad and we are looking at the loaded value.
2044 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002045 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002046 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002047 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002048 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002049 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002050 }
2051 return;
2052 }
2053 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002054 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002055 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002056 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002057 KnownZero = KnownZero.trunc(InBits);
2058 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002059 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002060 KnownZero = KnownZero.zext(BitWidth);
2061 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002062 KnownZero |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002063 return;
2064 }
2065 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002066 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002067 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002068 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002069
Jay Foad583abbc2010-12-07 08:25:19 +00002070 KnownZero = KnownZero.trunc(InBits);
2071 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002072 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002073
2074 // Note if the sign bit is known to be zero or one.
2075 bool SignBitKnownZero = KnownZero.isNegative();
2076 bool SignBitKnownOne = KnownOne.isNegative();
2077 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2078 "Sign bit can't be known to be both zero and one!");
2079
Jay Foad583abbc2010-12-07 08:25:19 +00002080 KnownZero = KnownZero.zext(BitWidth);
2081 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002082
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002083 // If the sign bit is known zero or one, the top bits match.
2084 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002085 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002086 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002087 KnownOne |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002088 return;
2089 }
2090 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002091 EVT InVT = Op.getOperand(0).getValueType();
2092 unsigned InBits = InVT.getScalarType().getSizeInBits();
2093 KnownZero = KnownZero.trunc(InBits);
2094 KnownOne = KnownOne.trunc(InBits);
2095 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2096 KnownZero = KnownZero.zext(BitWidth);
2097 KnownOne = KnownOne.zext(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002098 return;
2099 }
2100 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002101 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002102 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002103 KnownZero = KnownZero.zext(InBits);
2104 KnownOne = KnownOne.zext(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002105 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002106 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +00002107 KnownZero = KnownZero.trunc(BitWidth);
2108 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002109 break;
2110 }
2111 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002112 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002113 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002114 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2115 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002116 KnownOne &= (~KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002117 return;
2118 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002119 case ISD::FGETSIGN:
2120 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002121 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerafc8f132007-12-22 21:26:52 +00002122 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002123
Dan Gohman72ec3f42008-04-28 17:02:21 +00002124 case ISD::SUB: {
2125 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2126 // We know that the top bits of C-X are clear if X contains less bits
2127 // than C (i.e. no wrap-around can happen). For example, 20-X is
2128 // positive if we can prove that X is >= 0 and < 16.
2129 if (CLHS->getAPIntValue().isNonNegative()) {
2130 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2131 // NLZ can't be BitWidth with no sign bit
2132 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002133 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002134
2135 // If all of the MaskV bits are known to be zero, then we know the
2136 // output top bits are zero, because we now know that the output is
2137 // from [0-C].
2138 if ((KnownZero2 & MaskV) == MaskV) {
2139 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2140 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002141 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002142 }
2143 }
2144 }
2145 }
2146 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002147 case ISD::ADD:
2148 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002149 // Output known-0 bits are known if clear or set in both the low clear bits
2150 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2151 // low 3 bits clear.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002152 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002153 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002154 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2155
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002156 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002157 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002158 KnownZeroOut = std::min(KnownZeroOut,
2159 KnownZero2.countTrailingOnes());
2160
Chris Lattner440b2802010-12-19 20:38:28 +00002161 if (Op.getOpcode() == ISD::ADD) {
2162 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2163 return;
2164 }
2165
2166 // With ADDE, a carry bit may be added in, so we can only use this
2167 // information if we know (at least) that the low two bits are clear. We
2168 // then return to the caller that the low bit is unknown but that other bits
2169 // are known zero.
2170 if (KnownZeroOut >= 2) // ADDE
2171 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohman309d3d52007-06-22 14:59:07 +00002172 return;
2173 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002174 case ISD::SREM:
2175 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002176 const APInt &RA = Rem->getAPIntValue().abs();
2177 if (RA.isPowerOf2()) {
2178 APInt LowBits = RA - 1;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002179 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002180
Duncan Sands33274982010-01-29 09:45:26 +00002181 // The low bits of the first operand are unchanged by the srem.
2182 KnownZero = KnownZero2 & LowBits;
2183 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002184
Duncan Sands33274982010-01-29 09:45:26 +00002185 // If the first operand is non-negative or has all low bits zero, then
2186 // the upper bits are all zero.
2187 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2188 KnownZero |= ~LowBits;
2189
2190 // If the first operand is negative and not all low bits are zero, then
2191 // the upper bits are all one.
2192 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2193 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002194 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002195 }
2196 }
2197 return;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002198 case ISD::UREM: {
2199 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002200 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002201 if (RA.isPowerOf2()) {
2202 APInt LowBits = (RA - 1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002203 KnownZero |= ~LowBits;
2204 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002205 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2206 break;
2207 }
2208 }
2209
2210 // Since the result is less than or equal to either operand, any leading
2211 // zero bits in either operand must also exist in the result.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002212 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2213 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002214
2215 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2216 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002217 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002218 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002219 return;
Dan Gohman309d3d52007-06-22 14:59:07 +00002220 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002221 case ISD::FrameIndex:
2222 case ISD::TargetFrameIndex:
2223 if (unsigned Align = InferPtrAlignment(Op)) {
2224 // The low bits are known zero if the pointer is aligned.
2225 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2226 return;
2227 }
2228 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002229
Dan Gohman309d3d52007-06-22 14:59:07 +00002230 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002231 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2232 break;
2233 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002234 case ISD::INTRINSIC_WO_CHAIN:
2235 case ISD::INTRINSIC_W_CHAIN:
2236 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002237 // Allow the target to implement this method for its nodes.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002238 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002239 return;
2240 }
2241}
2242
2243/// ComputeNumSignBits - Return the number of times the sign bit of the
2244/// register is replicated into the other bits. We know that at least 1 bit
2245/// is always equal to the sign bit (itself), but other cases can give us
2246/// information. For example, immediately after an "SRA X, 2", we know that
2247/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002248unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002249 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002250 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002251 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002252 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002253 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002254 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002255
Dan Gohman309d3d52007-06-22 14:59:07 +00002256 if (Depth == 6)
2257 return 1; // Limit search depth.
2258
2259 switch (Op.getOpcode()) {
2260 default: break;
2261 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002262 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002263 return VTBits-Tmp+1;
2264 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002265 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002266 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002267
Dan Gohman309d3d52007-06-22 14:59:07 +00002268 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002269 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002270 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002271 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002272
Dan Gohman309d3d52007-06-22 14:59:07 +00002273 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002274 Tmp =
2275 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002276 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002277
Dan Gohman309d3d52007-06-22 14:59:07 +00002278 case ISD::SIGN_EXTEND_INREG:
2279 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002280 Tmp =
2281 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002282 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002283
Dan Gohman309d3d52007-06-22 14:59:07 +00002284 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2285 return std::max(Tmp, Tmp2);
2286
2287 case ISD::SRA:
2288 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2289 // SRA X, C -> adds C sign bits.
2290 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002291 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002292 if (Tmp > VTBits) Tmp = VTBits;
2293 }
2294 return Tmp;
2295 case ISD::SHL:
2296 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2297 // shl destroys sign bits.
2298 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002299 if (C->getZExtValue() >= VTBits || // Bad shift.
2300 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2301 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002302 }
2303 break;
2304 case ISD::AND:
2305 case ISD::OR:
2306 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002307 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002308 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002309 if (Tmp != 1) {
2310 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2311 FirstAnswer = std::min(Tmp, Tmp2);
2312 // We computed what we know about the sign bits as our first
2313 // answer. Now proceed to the generic code that uses
2314 // ComputeMaskedBits, and pick whichever answer is better.
2315 }
2316 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002317
2318 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002319 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002320 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002321 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002322 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002323
2324 case ISD::SADDO:
2325 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002326 case ISD::SSUBO:
2327 case ISD::USUBO:
2328 case ISD::SMULO:
2329 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002330 if (Op.getResNo() != 1)
2331 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002332 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002333 case ISD::SETCC:
2334 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002335 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002336 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002337 return VTBits;
2338 break;
2339 case ISD::ROTL:
2340 case ISD::ROTR:
2341 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002342 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002343
Dan Gohman309d3d52007-06-22 14:59:07 +00002344 // Handle rotate right by N like a rotate left by 32-N.
2345 if (Op.getOpcode() == ISD::ROTR)
2346 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2347
2348 // If we aren't rotating out all of the known-in sign bits, return the
2349 // number that are left. This handles rotl(sext(x), 1) for example.
2350 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2351 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2352 }
2353 break;
2354 case ISD::ADD:
2355 // Add can have at most one carry bit. Thus we know that the output
2356 // is, at worst, one more bit than the inputs.
2357 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2358 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002359
Dan Gohman309d3d52007-06-22 14:59:07 +00002360 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002361 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002362 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002363 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002364 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002365
Dan Gohman309d3d52007-06-22 14:59:07 +00002366 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2367 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002368 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002369 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002370
Dan Gohman309d3d52007-06-22 14:59:07 +00002371 // If we are subtracting one from a positive number, there is no carry
2372 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002373 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002374 return Tmp;
2375 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002376
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2378 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002379 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002380
Dan Gohman309d3d52007-06-22 14:59:07 +00002381 case ISD::SUB:
2382 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2383 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002384
Dan Gohman309d3d52007-06-22 14:59:07 +00002385 // Handle NEG.
2386 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002387 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002388 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002389 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002390 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2391 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002392 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002393 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002394
Dan Gohman309d3d52007-06-22 14:59:07 +00002395 // If the input is known to be positive (the sign bit is known clear),
2396 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002397 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002398 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002399
Dan Gohman309d3d52007-06-22 14:59:07 +00002400 // Otherwise, we treat this like a SUB.
2401 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002402
Dan Gohman309d3d52007-06-22 14:59:07 +00002403 // Sub can have at most one carry bit. Thus we know that the output
2404 // is, at worst, one more bit than the inputs.
2405 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2406 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002407 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002408 case ISD::TRUNCATE:
2409 // FIXME: it's tricky to do anything useful for this, but it is an important
2410 // case for targets like X86.
2411 break;
2412 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002413
Nadav Rotem4536d582013-03-20 22:53:44 +00002414 // If we are looking at the loaded value of the SDNode.
2415 if (Op.getResNo() == 0) {
2416 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2417 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2418 unsigned ExtType = LD->getExtensionType();
2419 switch (ExtType) {
2420 default: break;
2421 case ISD::SEXTLOAD: // '17' bits known
2422 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2423 return VTBits-Tmp+1;
2424 case ISD::ZEXTLOAD: // '16' bits known
2425 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2426 return VTBits-Tmp;
2427 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002428 }
2429 }
2430
2431 // Allow the target to implement this method for its nodes.
2432 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002433 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002434 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2435 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002436 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002437 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002438 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002439
Dan Gohman309d3d52007-06-22 14:59:07 +00002440 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2441 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002442 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002443 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002444
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002445 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002446 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002447 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002448 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002449 Mask = KnownOne;
2450 } else {
2451 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002452 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002453 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002454
Dan Gohman309d3d52007-06-22 14:59:07 +00002455 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2456 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002457 Mask = ~Mask;
2458 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002459 // Return # leading zeros. We use 'min' here in case Val was zero before
2460 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002461 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002462}
2463
Chris Lattner46c01a32011-02-13 22:25:43 +00002464/// isBaseWithConstantOffset - Return true if the specified operand is an
2465/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2466/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2467/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002468/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002469bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2470 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2471 !isa<ConstantSDNode>(Op.getOperand(1)))
2472 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002473
2474 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002475 !MaskedValueIsZero(Op.getOperand(0),
2476 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2477 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002478
Chris Lattner46c01a32011-02-13 22:25:43 +00002479 return true;
2480}
2481
2482
Dan Gohmand0d5e682009-09-03 20:34:31 +00002483bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2484 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002485 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002486 return true;
2487
2488 // If the value is a constant, we can obviously see if it is a NaN or not.
2489 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2490 return !C->getValueAPF().isNaN();
2491
2492 // TODO: Recognize more cases here.
2493
2494 return false;
2495}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002496
Dan Gohman38605212010-02-24 06:52:40 +00002497bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2498 // If the value is a constant, we can obviously see if it is a zero or not.
2499 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2500 return !C->isZero();
2501
2502 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002503 switch (Op.getOpcode()) {
2504 default: break;
2505 case ISD::OR:
2506 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2507 return !C->isNullValue();
2508 break;
2509 }
Dan Gohman38605212010-02-24 06:52:40 +00002510
2511 return false;
2512}
2513
2514bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2515 // Check the obvious case.
2516 if (A == B) return true;
2517
2518 // For for negative and positive zero.
2519 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2520 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2521 if (CA->isZero() && CB->isZero()) return true;
2522
2523 // Otherwise they may not be equal.
2524 return false;
2525}
2526
Chris Lattner061a1ea2005-01-07 07:46:32 +00002527/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002528///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002529SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002530 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002531 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002532 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002533 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002534 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002535
Jack Carter170a5f22013-09-09 22:02:08 +00002536 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2537 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002538 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002539
Chris Lattnerfcb16472006-08-11 18:38:11 +00002540 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002541#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002542 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002543#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002544 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002545}
2546
Andrew Trickef9de2a2013-05-25 02:42:55 +00002547SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002548 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002549 // Constant fold unary operations with an integer constant operand. Even
2550 // opaque constant will be folded, because the folding of unary operations
2551 // doesn't create new constants with different values. Nevertheless, the
2552 // opaque flag is preserved during folding to prevent future folding with
2553 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002554 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002555 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002556 switch (Opcode) {
2557 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002558 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002559 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2560 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002561 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002562 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002563 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002564 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2565 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002566 case ISD::UINT_TO_FP:
2567 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002568 APFloat apf(EVTToAPFloatSemantics(VT),
2569 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002570 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002571 Opcode==ISD::SINT_TO_FP,
2572 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002573 return getConstantFP(apf, VT);
2574 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002575 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002576 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002577 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002578 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002579 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002580 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002581 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002582 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2583 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002584 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002585 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2586 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002587 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002588 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002589 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2590 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002591 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002592 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002593 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2594 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002595 }
2596 }
2597
Dale Johannesen446b9002007-08-31 23:34:27 +00002598 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002599 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002600 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002601 switch (Opcode) {
2602 case ISD::FNEG:
2603 V.changeSign();
2604 return getConstantFP(V, VT);
2605 case ISD::FABS:
2606 V.clearSign();
2607 return getConstantFP(V, VT);
2608 case ISD::FCEIL: {
2609 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2610 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002611 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002612 break;
2613 }
2614 case ISD::FTRUNC: {
2615 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2616 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002617 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002618 break;
2619 }
2620 case ISD::FFLOOR: {
2621 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2622 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002623 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002624 break;
2625 }
2626 case ISD::FP_EXTEND: {
2627 bool ignored;
2628 // This can return overflow, underflow, or inexact; we don't care.
2629 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002630 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002631 APFloat::rmNearestTiesToEven, &ignored);
2632 return getConstantFP(V, VT);
2633 }
2634 case ISD::FP_TO_SINT:
2635 case ISD::FP_TO_UINT: {
2636 integerPart x[2];
2637 bool ignored;
2638 assert(integerPartWidth >= 64);
2639 // FIXME need to be more flexible about rounding mode.
2640 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2641 Opcode==ISD::FP_TO_SINT,
2642 APFloat::rmTowardZero, &ignored);
2643 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002644 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002645 APInt api(VT.getSizeInBits(), x);
2646 return getConstant(api, VT);
2647 }
2648 case ISD::BITCAST:
2649 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2650 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2651 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2652 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2653 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002654 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002655 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002656
Gabor Greiff304a7a2008-08-28 21:40:38 +00002657 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002658 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002659 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002660 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002661 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002662 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002663 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002664 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002665 assert(VT.isFloatingPoint() &&
2666 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002667 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002668 assert((!VT.isVector() ||
2669 VT.getVectorNumElements() ==
2670 Operand.getValueType().getVectorNumElements()) &&
2671 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002672 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002673 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002674 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002675 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002676 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002677 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002678 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002679 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2680 "Invalid sext node, dst < src!");
2681 assert((!VT.isVector() ||
2682 VT.getVectorNumElements() ==
2683 Operand.getValueType().getVectorNumElements()) &&
2684 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002685 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2686 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2687 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002688 // sext(undef) = 0, because the top bits will all be the same.
2689 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002690 break;
2691 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002692 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002693 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002694 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002695 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2696 "Invalid zext node, dst < src!");
2697 assert((!VT.isVector() ||
2698 VT.getVectorNumElements() ==
2699 Operand.getValueType().getVectorNumElements()) &&
2700 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002701 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002702 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002703 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002704 else if (OpOpcode == ISD::UNDEF)
2705 // zext(undef) = 0, because the top bits will be zero.
2706 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002707 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002708 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002709 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002710 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002711 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002712 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2713 "Invalid anyext node, dst < src!");
2714 assert((!VT.isVector() ||
2715 VT.getVectorNumElements() ==
2716 Operand.getValueType().getVectorNumElements()) &&
2717 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002718
Dan Gohman08837892010-06-18 00:08:30 +00002719 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2720 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002721 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002722 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002723 else if (OpOpcode == ISD::UNDEF)
2724 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002725
2726 // (ext (trunx x)) -> x
2727 if (OpOpcode == ISD::TRUNCATE) {
2728 SDValue OpOp = Operand.getNode()->getOperand(0);
2729 if (OpOp.getValueType() == VT)
2730 return OpOp;
2731 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002732 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002733 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002734 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002735 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002736 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002737 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2738 "Invalid truncate node, src < dst!");
2739 assert((!VT.isVector() ||
2740 VT.getVectorNumElements() ==
2741 Operand.getValueType().getVectorNumElements()) &&
2742 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002743 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002744 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002745 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2746 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002747 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002748 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2749 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002750 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002751 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002752 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002753 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002754 }
Craig Topper201c1a32012-01-15 01:05:11 +00002755 if (OpOpcode == ISD::UNDEF)
2756 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002757 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002758 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002759 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002760 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002761 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002762 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002763 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2764 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002765 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002766 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002767 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002768 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002769 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002770 (VT.getVectorElementType() == Operand.getValueType() ||
2771 (VT.getVectorElementType().isInteger() &&
2772 Operand.getValueType().isInteger() &&
2773 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002774 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002775 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002776 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002777 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2778 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2779 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2780 Operand.getConstantOperandVal(1) == 0 &&
2781 Operand.getOperand(0).getValueType() == VT)
2782 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002783 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002784 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002785 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002786 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002787 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002788 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002789 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002790 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002791 break;
2792 case ISD::FABS:
2793 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002794 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002795 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002796 }
2797
Chris Lattnerf9c19152005-08-25 19:12:10 +00002798 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002799 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002800 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002801 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002802 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002803 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002804 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002805 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002806 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002807
Jack Carter170a5f22013-09-09 22:02:08 +00002808 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2809 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002810 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002811 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002812 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2813 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002814 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002815
Chris Lattner061a1ea2005-01-07 07:46:32 +00002816 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002817#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002818 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002819#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002820 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002821}
2822
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002823SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2824 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002825 // If the opcode is a target-specific ISD node, there's nothing we can
2826 // do here and the operand rules may not line up with the below, so
2827 // bail early.
2828 if (Opcode >= ISD::BUILTIN_OP_END)
2829 return SDValue();
2830
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002831 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2832 SmallVector<SDValue, 4> Outputs;
2833 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002834
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002835 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2836 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002837 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2838 return SDValue();
2839
2840 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002841 // Scalar instruction.
2842 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002843 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002844 // For vectors extract each constant element into Inputs so we can constant
2845 // fold them individually.
2846 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2847 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2848 if (!BV1 || !BV2)
2849 return SDValue();
2850
2851 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2852
2853 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2854 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2855 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2856 if (!V1 || !V2) // Not a constant, bail.
2857 return SDValue();
2858
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002859 if (V1->isOpaque() || V2->isOpaque())
2860 return SDValue();
2861
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002862 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2863 // FIXME: This is valid and could be handled by truncating the APInts.
2864 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2865 return SDValue();
2866
2867 Inputs.push_back(std::make_pair(V1, V2));
2868 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002869 }
2870
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002871 // We have a number of constant values, constant fold them element by element.
2872 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2873 const APInt &C1 = Inputs[I].first->getAPIntValue();
2874 const APInt &C2 = Inputs[I].second->getAPIntValue();
2875
2876 switch (Opcode) {
2877 case ISD::ADD:
2878 Outputs.push_back(getConstant(C1 + C2, SVT));
2879 break;
2880 case ISD::SUB:
2881 Outputs.push_back(getConstant(C1 - C2, SVT));
2882 break;
2883 case ISD::MUL:
2884 Outputs.push_back(getConstant(C1 * C2, SVT));
2885 break;
2886 case ISD::UDIV:
2887 if (!C2.getBoolValue())
2888 return SDValue();
2889 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2890 break;
2891 case ISD::UREM:
2892 if (!C2.getBoolValue())
2893 return SDValue();
2894 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2895 break;
2896 case ISD::SDIV:
2897 if (!C2.getBoolValue())
2898 return SDValue();
2899 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2900 break;
2901 case ISD::SREM:
2902 if (!C2.getBoolValue())
2903 return SDValue();
2904 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2905 break;
2906 case ISD::AND:
2907 Outputs.push_back(getConstant(C1 & C2, SVT));
2908 break;
2909 case ISD::OR:
2910 Outputs.push_back(getConstant(C1 | C2, SVT));
2911 break;
2912 case ISD::XOR:
2913 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2914 break;
2915 case ISD::SHL:
2916 Outputs.push_back(getConstant(C1 << C2, SVT));
2917 break;
2918 case ISD::SRL:
2919 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2920 break;
2921 case ISD::SRA:
2922 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2923 break;
2924 case ISD::ROTL:
2925 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2926 break;
2927 case ISD::ROTR:
2928 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2929 break;
2930 default:
2931 return SDValue();
2932 }
2933 }
2934
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00002935 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
2936 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002937
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002938 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002939 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002940 return Outputs.back();
2941
Benjamin Kramer42d262f2014-05-02 12:35:22 +00002942 // We may have a vector type but a scalar result. Create a splat.
2943 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
2944
2945 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00002946 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00002947}
2948
Andrew Trickef9de2a2013-05-25 02:42:55 +00002949SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002950 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002951 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2952 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002953 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002954 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002955 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002956 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2957 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002958 // Fold trivial token factors.
2959 if (N1.getOpcode() == ISD::EntryToken) return N2;
2960 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002961 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002962 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002963 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002964 // Concat of UNDEFs is UNDEF.
2965 if (N1.getOpcode() == ISD::UNDEF &&
2966 N2.getOpcode() == ISD::UNDEF)
2967 return getUNDEF(VT);
2968
Dan Gohman550c9af2008-08-14 20:04:46 +00002969 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2970 // one big BUILD_VECTOR.
2971 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2972 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002973 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2974 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002975 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00002976 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00002977 }
2978 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002979 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002980 assert(VT.isInteger() && "This operator does not apply to FP types!");
2981 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002982 N1.getValueType() == VT && "Binary operator types must match!");
2983 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2984 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002985 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002986 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002987 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2988 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002989 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002990 case ISD::OR:
2991 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002992 case ISD::ADD:
2993 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002994 assert(VT.isInteger() && "This operator does not apply to FP types!");
2995 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002996 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002997 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2998 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002999 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003000 return N1;
3001 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003002 case ISD::UDIV:
3003 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003004 case ISD::MULHU:
3005 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003006 case ISD::MUL:
3007 case ISD::SDIV:
3008 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003009 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003010 assert(N1.getValueType() == N2.getValueType() &&
3011 N1.getValueType() == VT && "Binary operator types must match!");
3012 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003013 case ISD::FADD:
3014 case ISD::FSUB:
3015 case ISD::FMUL:
3016 case ISD::FDIV:
3017 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003018 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003019 if (Opcode == ISD::FADD) {
3020 // 0+x --> x
3021 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3022 if (CFP->getValueAPF().isZero())
3023 return N2;
3024 // x+0 --> x
3025 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3026 if (CFP->getValueAPF().isZero())
3027 return N1;
3028 } else if (Opcode == ISD::FSUB) {
3029 // x-0 --> x
3030 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3031 if (CFP->getValueAPF().isZero())
3032 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003033 } else if (Opcode == ISD::FMUL) {
3034 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3035 SDValue V = N2;
3036
3037 // If the first operand isn't the constant, try the second
3038 if (!CFP) {
3039 CFP = dyn_cast<ConstantFPSDNode>(N2);
3040 V = N1;
3041 }
3042
3043 if (CFP) {
3044 // 0*x --> 0
3045 if (CFP->isZero())
3046 return SDValue(CFP,0);
3047 // 1*x --> x
3048 if (CFP->isExactlyValue(1.0))
3049 return V;
3050 }
Dan Gohman1275e282009-01-23 19:10:37 +00003051 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003052 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003053 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003054 assert(N1.getValueType() == N2.getValueType() &&
3055 N1.getValueType() == VT && "Binary operator types must match!");
3056 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003057 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3058 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003059 N1.getValueType().isFloatingPoint() &&
3060 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003061 "Invalid FCOPYSIGN!");
3062 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003063 case ISD::SHL:
3064 case ISD::SRA:
3065 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003066 case ISD::ROTL:
3067 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003068 assert(VT == N1.getValueType() &&
3069 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003070 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003071 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003072 assert((!VT.isVector() || VT == N2.getValueType()) &&
3073 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003074 // Verify that the shift amount VT is bit enough to hold valid shift
3075 // amounts. This catches things like trying to shift an i1024 value by an
3076 // i8, which is easy to fall into in generic code that uses
3077 // TLI.getShiftAmount().
3078 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003079 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003080 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003081
3082 // Always fold shifts of i1 values so the code generator doesn't need to
3083 // handle them. Since we know the size of the shift has to be less than the
3084 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003085 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003086 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003087 if (N2C && N2C->isNullValue())
3088 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003089 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003090 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003091 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003092 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003093 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003094 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003095 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003096 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003097 "type is vector!");
3098 assert((!EVT.isVector() ||
3099 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3100 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003101 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003102 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003103 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003104 break;
3105 }
Chris Lattner72733e52008-01-17 07:00:52 +00003106 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003107 assert(VT.isFloatingPoint() &&
3108 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003109 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003110 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003111 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003112 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003113 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003114 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003115 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003116 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003117 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003118 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003119 assert(!EVT.isVector() &&
3120 "AssertSExt/AssertZExt type should be the vector element type "
3121 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003122 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003123 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003124 break;
3125 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003126 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003127 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003128 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003129 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003130 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003131 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003132 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003133 "type is vector!");
3134 assert((!EVT.isVector() ||
3135 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3136 "Vector element counts must match in SIGN_EXTEND_INREG");
3137 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003138 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003139
Chris Lattner16713612008-01-22 19:09:33 +00003140 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003141 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003142 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003143 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003144 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003145 return getConstant(Val, VT);
3146 }
Chris Lattner16713612008-01-22 19:09:33 +00003147 break;
3148 }
3149 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003150 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3151 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003152 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003153
Chris Lattner16713612008-01-22 19:09:33 +00003154 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3155 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003156 if (N2C &&
3157 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003158 N1.getNumOperands() > 0) {
3159 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003160 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003161 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003162 N1.getOperand(N2C->getZExtValue() / Factor),
3163 getConstant(N2C->getZExtValue() % Factor,
3164 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003165 }
3166
3167 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3168 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003169 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3170 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003171
3172 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003173 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003174 // are promoted and implicitly truncated, and the result implicitly
3175 // extended. Make that explicit here.
3176 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003177
Bob Wilson59dbbb22009-04-13 22:05:19 +00003178 return Elt;
3179 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003180
Chris Lattner16713612008-01-22 19:09:33 +00003181 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3182 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003183 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003184 // If the indices are the same, return the inserted element else
3185 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003186 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003187 SDValue N1Op2 = N1.getOperand(2);
3188 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3189
3190 if (N1Op2C && N2C) {
3191 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3192 if (VT == N1.getOperand(1).getValueType())
3193 return N1.getOperand(1);
3194 else
3195 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3196 }
3197
Dale Johannesendb393622009-02-03 01:55:44 +00003198 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003199 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003200 }
Chris Lattner16713612008-01-22 19:09:33 +00003201 break;
3202 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003203 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003204 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3205 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003206 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003207 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003208
Chris Lattner16713612008-01-22 19:09:33 +00003209 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3210 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003211 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003212 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003213 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003214
Chris Lattner16713612008-01-22 19:09:33 +00003215 // EXTRACT_ELEMENT of a constant int is also very common.
3216 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003217 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003218 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003219 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3220 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003221 }
3222 break;
David Greeneb6f16112011-01-26 15:38:49 +00003223 case ISD::EXTRACT_SUBVECTOR: {
3224 SDValue Index = N2;
3225 if (VT.isSimple() && N1.getValueType().isSimple()) {
3226 assert(VT.isVector() && N1.getValueType().isVector() &&
3227 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003228 assert(VT.getVectorElementType() ==
3229 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003230 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003231 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003232 "Extract subvector must be from larger vector to smaller vector!");
3233
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003234 if (isa<ConstantSDNode>(Index.getNode())) {
3235 assert((VT.getVectorNumElements() +
3236 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003237 <= N1.getValueType().getVectorNumElements())
3238 && "Extract subvector overflow!");
3239 }
3240
3241 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003242 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003243 return N1;
3244 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003245 break;
Chris Lattner16713612008-01-22 19:09:33 +00003246 }
David Greeneb6f16112011-01-26 15:38:49 +00003247 }
Chris Lattner16713612008-01-22 19:09:33 +00003248
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003249 // Perform trivial constant folding.
3250 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3251 if (SV.getNode()) return SV;
3252
3253 // Canonicalize constant to RHS if commutative.
3254 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3255 std::swap(N1C, N2C);
3256 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003257 }
3258
Chris Lattner16713612008-01-22 19:09:33 +00003259 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003260 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3261 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003262 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003263 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003264 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003265 std::swap(N1CFP, N2CFP);
3266 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003267 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003268 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3269 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003270 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003271 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003272 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003273 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003274 return getConstantFP(V1, VT);
3275 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003276 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003277 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3278 if (s!=APFloat::opInvalidOp)
3279 return getConstantFP(V1, VT);
3280 break;
3281 case ISD::FMUL:
3282 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3283 if (s!=APFloat::opInvalidOp)
3284 return getConstantFP(V1, VT);
3285 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003286 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003287 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3288 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3289 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003290 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003291 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003292 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3293 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3294 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003295 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003296 case ISD::FCOPYSIGN:
3297 V1.copySign(V2);
3298 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003299 default: break;
3300 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003301 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003302
3303 if (Opcode == ISD::FP_ROUND) {
3304 APFloat V = N1CFP->getValueAPF(); // make copy
3305 bool ignored;
3306 // This can return overflow, underflow, or inexact; we don't care.
3307 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003308 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003309 APFloat::rmNearestTiesToEven, &ignored);
3310 return getConstantFP(V, VT);
3311 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003312 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003313
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003314 // Canonicalize an UNDEF to the RHS, even over a constant.
3315 if (N1.getOpcode() == ISD::UNDEF) {
3316 if (isCommutativeBinOp(Opcode)) {
3317 std::swap(N1, N2);
3318 } else {
3319 switch (Opcode) {
3320 case ISD::FP_ROUND_INREG:
3321 case ISD::SIGN_EXTEND_INREG:
3322 case ISD::SUB:
3323 case ISD::FSUB:
3324 case ISD::FDIV:
3325 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003326 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003327 return N1; // fold op(undef, arg2) -> undef
3328 case ISD::UDIV:
3329 case ISD::SDIV:
3330 case ISD::UREM:
3331 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003332 case ISD::SRL:
3333 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003334 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003335 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3336 // For vectors, we can't easily build an all zero vector, just return
3337 // the LHS.
3338 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003339 }
3340 }
3341 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003342
3343 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003344 if (N2.getOpcode() == ISD::UNDEF) {
3345 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003346 case ISD::XOR:
3347 if (N1.getOpcode() == ISD::UNDEF)
3348 // Handle undef ^ undef -> 0 special case. This is a common
3349 // idiom (misuse).
3350 return getConstant(0, VT);
3351 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003352 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003353 case ISD::ADDC:
3354 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003355 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003356 case ISD::UDIV:
3357 case ISD::SDIV:
3358 case ISD::UREM:
3359 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003360 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003361 case ISD::FADD:
3362 case ISD::FSUB:
3363 case ISD::FMUL:
3364 case ISD::FDIV:
3365 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003366 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003367 return N2;
3368 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003369 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003370 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003371 case ISD::SRL:
3372 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003373 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003374 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3375 // For vectors, we can't easily build an all zero vector, just return
3376 // the LHS.
3377 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003378 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003379 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003380 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003381 // For vectors, we can't easily build an all one vector, just return
3382 // the LHS.
3383 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003384 case ISD::SRA:
3385 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003386 }
3387 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003388
Chris Lattner724f7ee2005-05-11 18:57:39 +00003389 // Memoize this node if possible.
3390 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003391 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003392 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003393 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003394 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003395 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003396 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003397 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003398 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003399
Jack Carter170a5f22013-09-09 22:02:08 +00003400 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3401 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003402 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003403 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003404 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3405 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003406 }
3407
Chris Lattner061a1ea2005-01-07 07:46:32 +00003408 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003409#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003410 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003411#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003412 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003413}
3414
Andrew Trickef9de2a2013-05-25 02:42:55 +00003415SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003416 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003417 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003418 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003419 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003420 case ISD::FMA: {
3421 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3422 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3423 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3424 if (N1CFP && N2CFP && N3CFP) {
3425 APFloat V1 = N1CFP->getValueAPF();
3426 const APFloat &V2 = N2CFP->getValueAPF();
3427 const APFloat &V3 = N3CFP->getValueAPF();
3428 APFloat::opStatus s =
3429 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3430 if (s != APFloat::opInvalidOp)
3431 return getConstantFP(V1, VT);
3432 }
3433 break;
3434 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003435 case ISD::CONCAT_VECTORS:
3436 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3437 // one big BUILD_VECTOR.
3438 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3439 N2.getOpcode() == ISD::BUILD_VECTOR &&
3440 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003441 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3442 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003443 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3444 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003445 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003446 }
3447 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003448 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003449 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003450 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003451 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003452 break;
3453 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003454 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003455 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003456 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003457 return N2; // select true, X, Y -> X
3458 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003459 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003460
3461 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003462 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003463 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003464 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003465 case ISD::INSERT_SUBVECTOR: {
3466 SDValue Index = N3;
3467 if (VT.isSimple() && N1.getValueType().isSimple()
3468 && N2.getValueType().isSimple()) {
3469 assert(VT.isVector() && N1.getValueType().isVector() &&
3470 N2.getValueType().isVector() &&
3471 "Insert subvector VTs must be a vectors");
3472 assert(VT == N1.getValueType() &&
3473 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003474 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003475 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003476 if (isa<ConstantSDNode>(Index.getNode())) {
3477 assert((N2.getValueType().getVectorNumElements() +
3478 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003479 <= VT.getVectorNumElements())
3480 && "Insert subvector overflow!");
3481 }
3482
3483 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003484 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003485 return N2;
3486 }
3487 break;
3488 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003489 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003490 // Fold bit_convert nodes from a type to themselves.
3491 if (N1.getValueType() == VT)
3492 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003493 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003494 }
3495
Chris Lattnerf9c19152005-08-25 19:12:10 +00003496 // Memoize node if it doesn't produce a flag.
3497 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003498 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003499 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003500 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003501 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003502 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003503 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003504 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003505 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003506
Jack Carter170a5f22013-09-09 22:02:08 +00003507 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3508 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003509 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003510 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003511 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3512 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003513 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003514
Chris Lattner061a1ea2005-01-07 07:46:32 +00003515 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003516#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003517 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003518#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003519 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +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) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003525 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003526 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003527}
3528
Andrew Trickef9de2a2013-05-25 02:42:55 +00003529SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003530 SDValue N1, SDValue N2, SDValue N3,
3531 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003532 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003533 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003534}
3535
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003536/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3537/// the incoming stack arguments to be loaded from the stack.
3538SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3539 SmallVector<SDValue, 8> ArgChains;
3540
3541 // Include the original chain at the beginning of the list. When this is
3542 // used by target LowerCall hooks, this helps legalize find the
3543 // CALLSEQ_BEGIN node.
3544 ArgChains.push_back(Chain);
3545
3546 // Add a chain value for each stack argument.
3547 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3548 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3549 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3550 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3551 if (FI->getIndex() < 0)
3552 ArgChains.push_back(SDValue(L, 1));
3553
3554 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003555 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003556}
3557
Dan Gohman544ab2c2008-04-12 04:36:06 +00003558/// getMemsetValue - Vectorized representation of the memset value
3559/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003560static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003561 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003562 assert(Value.getOpcode() != ISD::UNDEF);
3563
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003564 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003565 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003566 assert(C->getAPIntValue().getBitWidth() == 8);
3567 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003568 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003569 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003570 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003571 }
Evan Chengef377ad2008-05-15 08:39:06 +00003572
Dale Johannesenabf66b82009-02-03 22:26:09 +00003573 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003574 if (NumBits > 8) {
3575 // Use a multiplication with 0x010101... to extend the input to the
3576 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003577 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003578 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003579 }
3580
3581 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003582}
3583
Dan Gohman544ab2c2008-04-12 04:36:06 +00003584/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3585/// used when a memcpy is turned into a memset when the source is a constant
3586/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003587static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003588 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003589 // Handle vector with all elements zero.
3590 if (Str.empty()) {
3591 if (VT.isInteger())
3592 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003593 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003594 return DAG.getConstantFP(0.0, VT);
3595 else if (VT.isVector()) {
3596 unsigned NumElts = VT.getVectorNumElements();
3597 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003598 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003599 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3600 EltVT, NumElts)));
3601 } else
3602 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003603 }
3604
Duncan Sands13237ac2008-06-06 12:08:01 +00003605 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003606 unsigned NumVTBits = VT.getSizeInBits();
3607 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003608 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3609
Evan Chengc8444b12013-01-10 22:13:27 +00003610 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003611 if (TLI.isLittleEndian()) {
3612 for (unsigned i = 0; i != NumBytes; ++i)
3613 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3614 } else {
3615 for (unsigned i = 0; i != NumBytes; ++i)
3616 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003617 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003618
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003619 // If the "cost" of materializing the integer immediate is less than the cost
3620 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003621 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3622 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003623 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003624 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003625}
3626
Scott Michelcf0da6c2009-02-17 22:15:04 +00003627/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003628///
Andrew Tricke2431c62013-05-25 03:08:10 +00003629static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003630 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003631 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003632 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003633 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003634}
3635
Evan Chengef377ad2008-05-15 08:39:06 +00003636/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3637///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003638static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003639 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003640 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003641 if (Src.getOpcode() == ISD::GlobalAddress)
3642 G = cast<GlobalAddressSDNode>(Src);
3643 else if (Src.getOpcode() == ISD::ADD &&
3644 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3645 Src.getOperand(1).getOpcode() == ISD::Constant) {
3646 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003647 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003648 }
3649 if (!G)
3650 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003651
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003652 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003653}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003654
Evan Cheng43cd9e32010-04-01 06:04:33 +00003655/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3656/// to replace the memset / memcpy. Return true if the number of memory ops
3657/// is below the threshold. It returns the types of the sequence of
3658/// memory ops to perform memset / memcpy by reference.
3659static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003660 unsigned Limit, uint64_t Size,
3661 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003662 bool IsMemset,
3663 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003664 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003665 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003666 SelectionDAG &DAG,
3667 const TargetLowering &TLI) {
3668 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3669 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003670 // If 'SrcAlign' is zero, that means the memory operation does not need to
3671 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3672 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3673 // is the specified alignment of the memory operation. If it is zero, that
3674 // means it's possible to change the alignment of the destination.
3675 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3676 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003677 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003678 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003679 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003680
Chris Lattner28dc6c12010-03-07 07:45:08 +00003681 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003682 unsigned AS = 0;
3683 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
3684 TLI.allowsUnalignedMemoryAccesses(VT, AS)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003685 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003686 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003687 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003688 case 0: VT = MVT::i64; break;
3689 case 4: VT = MVT::i32; break;
3690 case 2: VT = MVT::i16; break;
3691 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003692 }
3693 }
3694
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003695 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003696 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003697 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003698 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003699
Duncan Sands11dd4242008-06-08 20:54:56 +00003700 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003701 VT = LVT;
3702 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003703
Dan Gohman544ab2c2008-04-12 04:36:06 +00003704 unsigned NumMemOps = 0;
3705 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003706 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003707 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003708 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003709 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003710 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003711
3712 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003713 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003714 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003715 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003716 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003717 Found = true;
3718 else if (NewVT == MVT::i64 &&
3719 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003720 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003721 // i64 is usually not legal on 32-bit targets, but f64 may be.
3722 NewVT = MVT::f64;
3723 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003724 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003725 }
3726
Evan Cheng04e55182012-12-12 00:42:09 +00003727 if (!Found) {
3728 do {
3729 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3730 if (NewVT == MVT::i8)
3731 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003732 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003733 }
3734 NewVTSize = NewVT.getSizeInBits() / 8;
3735
Evan Cheng79e2ca92012-12-10 23:21:26 +00003736 // If the new VT cannot cover all of the remaining bits, then consider
3737 // issuing a (or a pair of) unaligned and overlapping load / store.
3738 // FIXME: Only does this for 64-bit or more since we don't have proper
3739 // cost model for unaligned load / store.
3740 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003741 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003742 if (NumMemOps && AllowOverlap &&
3743 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003744 TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003745 VTSize = Size;
3746 else {
3747 VT = NewVT;
3748 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003749 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003750 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003751
Evan Chengb7d3d032012-12-12 20:43:23 +00003752 if (++NumMemOps > Limit)
3753 return false;
3754
Dan Gohman544ab2c2008-04-12 04:36:06 +00003755 MemOps.push_back(VT);
3756 Size -= VTSize;
3757 }
3758
3759 return true;
3760}
3761
Andrew Trickef9de2a2013-05-25 02:42:55 +00003762static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003763 SDValue Chain, SDValue Dst,
3764 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003765 unsigned Align, bool isVol,
3766 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003767 MachinePointerInfo DstPtrInfo,
3768 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003769 // Turn a memcpy of undef to nop.
3770 if (Src.getOpcode() == ISD::UNDEF)
3771 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003772
Dan Gohman714663a2008-05-29 19:42:22 +00003773 // Expand memcpy to a series of load and store ops if the size operand falls
3774 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003775 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3776 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003777 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003778 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003779 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003780 MachineFunction &MF = DAG.getMachineFunction();
3781 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003782 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003783 MF.getFunction()->getAttributes().
3784 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003785 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3786 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3787 DstAlignCanChange = true;
3788 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3789 if (Align > SrcAlign)
3790 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003791 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003792 bool CopyFromStr = isMemSrcFromString(Src, Str);
3793 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003794 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003795
Evan Cheng4c014c82010-04-01 18:19:11 +00003796 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003797 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003798 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003799 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003800 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003801
Evan Cheng43cd9e32010-04-01 06:04:33 +00003802 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003803 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003804 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003805
3806 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003807 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003808 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3809 if (!TRI->needsStackRealignment(MF))
3810 while (NewAlign > Align &&
3811 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3812 NewAlign /= 2;
3813
Evan Cheng43cd9e32010-04-01 06:04:33 +00003814 if (NewAlign > Align) {
3815 // Give the stack frame object a larger alignment if needed.
3816 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3817 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3818 Align = NewAlign;
3819 }
3820 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003821
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003822 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003823 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003824 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003825 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003826 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003827 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003828 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003829
Evan Cheng79e2ca92012-12-10 23:21:26 +00003830 if (VTSize > Size) {
3831 // Issuing an unaligned load / store pair that overlaps with the previous
3832 // pair. Adjust the offset accordingly.
3833 assert(i == NumMemOps-1 && i != 0);
3834 SrcOff -= VTSize - Size;
3835 DstOff -= VTSize - Size;
3836 }
3837
Evan Cheng43cd9e32010-04-01 06:04:33 +00003838 if (CopyFromStr &&
3839 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003840 // It's unlikely a store of a vector immediate can be done in a single
3841 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003842 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003843 // FIXME: Handle other cases where store of vector immediate is done in
3844 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003845 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003846 if (Value.getNode())
3847 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003848 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003849 DstPtrInfo.getWithOffset(DstOff), isVol,
3850 false, Align);
3851 }
3852
3853 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003854 // The type might not be legal for the target. This should only happen
3855 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003856 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3857 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003858 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003859 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003860 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003861 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003862 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003863 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003864 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003865 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003866 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003867 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3868 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003869 }
3870 OutChains.push_back(Store);
3871 SrcOff += VTSize;
3872 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003873 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003874 }
3875
Craig Topper48d114b2014-04-26 18:35:24 +00003876 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003877}
3878
Andrew Trickef9de2a2013-05-25 02:42:55 +00003879static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003880 SDValue Chain, SDValue Dst,
3881 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003882 unsigned Align, bool isVol,
3883 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003884 MachinePointerInfo DstPtrInfo,
3885 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003886 // Turn a memmove of undef to nop.
3887 if (Src.getOpcode() == ISD::UNDEF)
3888 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003889
3890 // Expand memmove to a series of load and store ops if the size operand falls
3891 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003892 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003893 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003894 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003895 MachineFunction &MF = DAG.getMachineFunction();
3896 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003897 bool OptSize = MF.getFunction()->getAttributes().
3898 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003899 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3900 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3901 DstAlignCanChange = true;
3902 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3903 if (Align > SrcAlign)
3904 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003905 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003906
Evan Cheng4c014c82010-04-01 18:19:11 +00003907 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003908 (DstAlignCanChange ? 0 : Align), SrcAlign,
3909 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003910 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003911
Evan Cheng43cd9e32010-04-01 06:04:33 +00003912 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003913 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003914 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003915 if (NewAlign > Align) {
3916 // Give the stack frame object a larger alignment if needed.
3917 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3918 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3919 Align = NewAlign;
3920 }
3921 }
Dan Gohman714663a2008-05-29 19:42:22 +00003922
Evan Cheng43cd9e32010-04-01 06:04:33 +00003923 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003924 SmallVector<SDValue, 8> LoadValues;
3925 SmallVector<SDValue, 8> LoadChains;
3926 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003927 unsigned NumMemOps = MemOps.size();
3928 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003929 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003930 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003931 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00003932
Dale Johannesenabf66b82009-02-03 22:26:09 +00003933 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003934 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003935 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003936 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003937 LoadValues.push_back(Value);
3938 LoadChains.push_back(Value.getValue(1));
3939 SrcOff += VTSize;
3940 }
Craig Topper48d114b2014-04-26 18:35:24 +00003941 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00003942 OutChains.clear();
3943 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003944 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003945 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003946 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003947
Dale Johannesenabf66b82009-02-03 22:26:09 +00003948 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003949 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003950 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003951 OutChains.push_back(Store);
3952 DstOff += VTSize;
3953 }
3954
Craig Topper48d114b2014-04-26 18:35:24 +00003955 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00003956}
3957
Serge Pavlov8ec39992013-09-17 16:24:42 +00003958/// \brief Lower the call to 'memset' intrinsic function into a series of store
3959/// operations.
3960///
3961/// \param DAG Selection DAG where lowered code is placed.
3962/// \param dl Link to corresponding IR location.
3963/// \param Chain Control flow dependency.
3964/// \param Dst Pointer to destination memory location.
3965/// \param Src Value of byte to write into the memory.
3966/// \param Size Number of bytes to write.
3967/// \param Align Alignment of the destination in bytes.
3968/// \param isVol True if destination is volatile.
3969/// \param DstPtrInfo IR information on the memory pointer.
3970/// \returns New head in the control flow, if lowering was successful, empty
3971/// SDValue otherwise.
3972///
3973/// The function tries to replace 'llvm.memset' intrinsic with several store
3974/// operations and value calculation code. This is usually profitable for small
3975/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003976static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003977 SDValue Chain, SDValue Dst,
3978 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003979 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003980 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003981 // Turn a memset of undef to nop.
3982 if (Src.getOpcode() == ISD::UNDEF)
3983 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003984
3985 // Expand memset to a series of load/store ops if the size operand
3986 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003987 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003988 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003989 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003990 MachineFunction &MF = DAG.getMachineFunction();
3991 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003992 bool OptSize = MF.getFunction()->getAttributes().
3993 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003994 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3995 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3996 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003997 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003998 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003999 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004000 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004001 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004002 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004003
Evan Cheng43cd9e32010-04-01 06:04:33 +00004004 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004005 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004006 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004007 if (NewAlign > Align) {
4008 // Give the stack frame object a larger alignment if needed.
4009 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4010 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4011 Align = NewAlign;
4012 }
4013 }
4014
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004015 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004016 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004017 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004018
4019 // Find the largest store and generate the bit pattern for it.
4020 EVT LargestVT = MemOps[0];
4021 for (unsigned i = 1; i < NumMemOps; i++)
4022 if (MemOps[i].bitsGT(LargestVT))
4023 LargestVT = MemOps[i];
4024 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4025
Dan Gohman544ab2c2008-04-12 04:36:06 +00004026 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004027 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004028 unsigned VTSize = VT.getSizeInBits() / 8;
4029 if (VTSize > Size) {
4030 // Issuing an unaligned load / store pair that overlaps with the previous
4031 // pair. Adjust the offset accordingly.
4032 assert(i == NumMemOps-1 && i != 0);
4033 DstOff -= VTSize - Size;
4034 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004035
4036 // If this store is smaller than the largest store see whether we can get
4037 // the smaller value for free with a truncate.
4038 SDValue Value = MemSetValue;
4039 if (VT.bitsLT(LargestVT)) {
4040 if (!LargestVT.isVector() && !VT.isVector() &&
4041 TLI.isTruncateFree(LargestVT, VT))
4042 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4043 else
4044 Value = getMemsetValue(Src, VT, DAG, dl);
4045 }
4046 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004047 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004048 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004049 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004050 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004051 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004052 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004053 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004054 }
4055
Craig Topper48d114b2014-04-26 18:35:24 +00004056 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004057}
4058
Andrew Trickef9de2a2013-05-25 02:42:55 +00004059SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004060 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004061 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004062 MachinePointerInfo DstPtrInfo,
4063 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004064 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004065
4066 // Check to see if we should lower the memcpy to loads and stores first.
4067 // For cases within the target-specified limits, this is the best choice.
4068 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4069 if (ConstantSize) {
4070 // Memcpy with size zero? Just return the original chain.
4071 if (ConstantSize->isNullValue())
4072 return Chain;
4073
Evan Cheng43cd9e32010-04-01 06:04:33 +00004074 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4075 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004076 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004077 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004078 return Result;
4079 }
4080
4081 // Then check to see if we should lower the memcpy with target-specific
4082 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004083 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004084 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004085 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004086 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004087 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004088 return Result;
4089
4090 // If we really need inline code and the target declined to provide it,
4091 // use a (potentially long) sequence of loads and stores.
4092 if (AlwaysInline) {
4093 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004094 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004095 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004096 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004097 }
4098
Dan Gohmanf38547c2010-04-05 20:24:08 +00004099 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4100 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4101 // respect volatile, so they may do things like read or write memory
4102 // beyond the given memory regions. But fixing this isn't easy, and most
4103 // people don't care.
4104
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004105 const TargetLowering *TLI = TM.getTargetLowering();
4106
Dan Gohman544ab2c2008-04-12 04:36:06 +00004107 // Emit a library call.
4108 TargetLowering::ArgListTy Args;
4109 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004110 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004111 Entry.Node = Dst; Args.push_back(Entry);
4112 Entry.Node = Src; Args.push_back(Entry);
4113 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004114 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004115 TargetLowering::
4116 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004117 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004118 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00004119 /*isTailCall=*/false,
4120 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004121 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4122 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004123 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004124 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004125
Dan Gohman544ab2c2008-04-12 04:36:06 +00004126 return CallResult.second;
4127}
4128
Andrew Trickef9de2a2013-05-25 02:42:55 +00004129SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004130 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004131 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004132 MachinePointerInfo DstPtrInfo,
4133 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004134 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004135
Dan Gohman714663a2008-05-29 19:42:22 +00004136 // Check to see if we should lower the memmove to loads and stores first.
4137 // For cases within the target-specified limits, this is the best choice.
4138 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4139 if (ConstantSize) {
4140 // Memmove with size zero? Just return the original chain.
4141 if (ConstantSize->isNullValue())
4142 return Chain;
4143
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004144 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004145 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004146 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004147 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004148 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004149 return Result;
4150 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004151
4152 // Then check to see if we should lower the memmove with target-specific
4153 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004154 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004155 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004156 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004157 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004158 return Result;
4159
Mon P Wangbf862242010-04-06 08:27:51 +00004160 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4161 // not be safe. See memcpy above for more details.
4162
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004163 const TargetLowering *TLI = TM.getTargetLowering();
4164
Dan Gohman544ab2c2008-04-12 04:36:06 +00004165 // Emit a library call.
4166 TargetLowering::ArgListTy Args;
4167 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004168 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004169 Entry.Node = Dst; Args.push_back(Entry);
4170 Entry.Node = Src; Args.push_back(Entry);
4171 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004172 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004173 TargetLowering::
4174 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004175 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004176 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004177 /*isTailCall=*/false,
4178 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004179 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4180 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004181 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004182 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004183
Dan Gohman544ab2c2008-04-12 04:36:06 +00004184 return CallResult.second;
4185}
4186
Andrew Trickef9de2a2013-05-25 02:42:55 +00004187SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004188 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004189 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004190 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004191 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004192
4193 // Check to see if we should lower the memset to stores first.
4194 // For cases within the target-specified limits, this is the best choice.
4195 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4196 if (ConstantSize) {
4197 // Memset with size zero? Just return the original chain.
4198 if (ConstantSize->isNullValue())
4199 return Chain;
4200
Mon P Wangc576ee92010-04-04 03:10:48 +00004201 SDValue Result =
4202 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004203 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004204
Gabor Greiff304a7a2008-08-28 21:40:38 +00004205 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004206 return Result;
4207 }
4208
4209 // Then check to see if we should lower the memset with target-specific
4210 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004211 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004212 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004213 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004214 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004215 return Result;
4216
Wesley Peck527da1b2010-11-23 03:31:01 +00004217 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004218 const TargetLowering *TLI = TM.getTargetLowering();
4219 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004220 TargetLowering::ArgListTy Args;
4221 TargetLowering::ArgListEntry Entry;
4222 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4223 Args.push_back(Entry);
4224 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004225 if (Src.getValueType().bitsGT(MVT::i32))
4226 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004227 else
Owen Anderson9f944592009-08-11 20:47:22 +00004228 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004229 Entry.Node = Src;
4230 Entry.Ty = Type::getInt32Ty(*getContext());
4231 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004232 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004233 Entry.Node = Size;
4234 Entry.Ty = IntPtrTy;
4235 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004236 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004237 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004238 TargetLowering::
4239 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004240 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004241 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004242 /*isTailCall=*/false,
4243 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004244 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4245 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004246 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004247 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004248
Dan Gohman544ab2c2008-04-12 04:36:06 +00004249 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004250}
4251
Andrew Trickef9de2a2013-05-25 02:42:55 +00004252SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004253 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004254 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004255 AtomicOrdering SuccessOrdering,
4256 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004257 SynchronizationScope SynchScope) {
4258 FoldingSetNodeID ID;
4259 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004260 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004261 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004262 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004263 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4264 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4265 return SDValue(E, 0);
4266 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004267
4268 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4269 // SDNode doesn't have access to it. This memory will be "leaked" when
4270 // the node is deallocated, but recovered when the allocator is released.
4271 // If the number of operands is less than 5 we use AtomicSDNode's internal
4272 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004273 unsigned NumOps = Ops.size();
4274 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4275 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004276
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004277 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4278 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004279 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004280 SuccessOrdering, FailureOrdering,
4281 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004282 CSEMap.InsertNode(N, IP);
4283 AllNodes.push_back(N);
4284 return SDValue(N, 0);
4285}
4286
4287SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004288 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004289 MachineMemOperand *MMO,
4290 AtomicOrdering Ordering,
4291 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004292 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004293 Ordering, SynchScope);
4294}
4295
4296SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004297 SDValue Chain, SDValue Ptr, SDValue Cmp,
4298 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004299 unsigned Alignment,
Tim Northovere94a5182014-03-11 10:48:52 +00004300 AtomicOrdering SuccessOrdering,
4301 AtomicOrdering FailureOrdering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004302 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004303 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4304 Alignment = getEVTAlignment(MemVT);
4305
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004306 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004307
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004308 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004309 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004310 // FIXME: Volatile isn't really correct; we should keep track of atomic
4311 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004312 unsigned Flags = MachineMemOperand::MOVolatile;
4313 if (Opcode != ISD::ATOMIC_STORE)
4314 Flags |= MachineMemOperand::MOLoad;
4315 if (Opcode != ISD::ATOMIC_LOAD)
4316 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004317
4318 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004319 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004320
Eli Friedmanadec5872011-07-29 03:05:32 +00004321 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004322 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004323}
4324
Andrew Trickef9de2a2013-05-25 02:42:55 +00004325SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004326 SDValue Chain,
4327 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004328 SDValue Swp, MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004329 AtomicOrdering SuccessOrdering,
4330 AtomicOrdering FailureOrdering,
Eli Friedmanadec5872011-07-29 03:05:32 +00004331 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004332 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4333 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4334
Owen Anderson53aa7a92009-08-10 22:56:29 +00004335 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004336
Owen Anderson9f944592009-08-11 20:47:22 +00004337 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004338 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004339 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, SuccessOrdering,
Tim Northovere94a5182014-03-11 10:48:52 +00004340 FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004341}
4342
Andrew Trickef9de2a2013-05-25 02:42:55 +00004343SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004344 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004345 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004346 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004347 unsigned Alignment,
4348 AtomicOrdering Ordering,
4349 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004350 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4351 Alignment = getEVTAlignment(MemVT);
4352
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004353 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004354 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004355 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004356 // For now, atomics are considered to be volatile always, and they are
4357 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004358 // FIXME: Volatile isn't really correct; we should keep track of atomic
4359 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004360 unsigned Flags = MachineMemOperand::MOVolatile;
4361 if (Opcode != ISD::ATOMIC_STORE)
4362 Flags |= MachineMemOperand::MOLoad;
4363 if (Opcode != ISD::ATOMIC_LOAD)
4364 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004365
4366 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004367 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004368 MemVT.getStoreSize(), Alignment);
4369
Eli Friedmanadec5872011-07-29 03:05:32 +00004370 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4371 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004372}
4373
Andrew Trickef9de2a2013-05-25 02:42:55 +00004374SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004375 SDValue Chain,
4376 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004377 MachineMemOperand *MMO,
4378 AtomicOrdering Ordering,
4379 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004380 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4381 Opcode == ISD::ATOMIC_LOAD_SUB ||
4382 Opcode == ISD::ATOMIC_LOAD_AND ||
4383 Opcode == ISD::ATOMIC_LOAD_OR ||
4384 Opcode == ISD::ATOMIC_LOAD_XOR ||
4385 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004386 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004387 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004388 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004389 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004390 Opcode == ISD::ATOMIC_SWAP ||
4391 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004392 "Invalid Atomic Op");
4393
Owen Anderson53aa7a92009-08-10 22:56:29 +00004394 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004395
Eli Friedman342e8df2011-08-24 20:50:09 +00004396 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4397 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004398 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004399 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004400}
4401
Andrew Trickef9de2a2013-05-25 02:42:55 +00004402SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004403 EVT VT, SDValue Chain,
4404 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004405 MachineMemOperand *MMO,
4406 AtomicOrdering Ordering,
4407 SynchronizationScope SynchScope) {
4408 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4409
4410 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004411 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004412 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004413}
4414
Duncan Sands739a0542008-07-02 17:40:58 +00004415/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004416SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4417 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004418 return Ops[0];
4419
Owen Anderson53aa7a92009-08-10 22:56:29 +00004420 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004421 VTs.reserve(Ops.size());
4422 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004423 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004424 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004425}
4426
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004427SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004428SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004429 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004430 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004431 unsigned Align, bool Vol,
4432 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004433 if (Align == 0) // Ensure that codegen never sees alignment 0
4434 Align = getEVTAlignment(MemVT);
4435
4436 MachineFunction &MF = getMachineFunction();
4437 unsigned Flags = 0;
4438 if (WriteMem)
4439 Flags |= MachineMemOperand::MOStore;
4440 if (ReadMem)
4441 Flags |= MachineMemOperand::MOLoad;
4442 if (Vol)
4443 Flags |= MachineMemOperand::MOVolatile;
4444 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004445 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004446
Craig Topper206fcd42014-04-26 19:29:41 +00004447 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004448}
4449
4450SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004451SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004452 ArrayRef<SDValue> Ops, EVT MemVT,
4453 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004454 assert((Opcode == ISD::INTRINSIC_VOID ||
4455 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004456 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004457 Opcode == ISD::LIFETIME_START ||
4458 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004459 (Opcode <= INT_MAX &&
4460 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4461 "Opcode is not a memory-accessing opcode!");
4462
Dale Johannesen839acbb2009-01-29 00:47:48 +00004463 // Memoize the node unless it returns a flag.
4464 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004465 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004466 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004467 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004468 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004469 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004470 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4471 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004472 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004473 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004474
Jack Carter170a5f22013-09-09 22:02:08 +00004475 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4476 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004477 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004478 CSEMap.InsertNode(N, IP);
4479 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004480 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4481 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004482 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004483 }
4484 AllNodes.push_back(N);
4485 return SDValue(N, 0);
4486}
4487
Chris Lattnerea952f02010-09-21 17:24:05 +00004488/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4489/// MachinePointerInfo record from it. This is particularly useful because the
4490/// code generator has many cases where it doesn't bother passing in a
4491/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4492static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4493 // If this is FI+Offset, we can model it.
4494 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4495 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4496
4497 // If this is (FI+Offset1)+Offset2, we can model it.
4498 if (Ptr.getOpcode() != ISD::ADD ||
4499 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4500 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4501 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004502
Chris Lattnerea952f02010-09-21 17:24:05 +00004503 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4504 return MachinePointerInfo::getFixedStack(FI, Offset+
4505 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4506}
4507
4508/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4509/// MachinePointerInfo record from it. This is particularly useful because the
4510/// code generator has many cases where it doesn't bother passing in a
4511/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4512static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4513 // If the 'Offset' value isn't a constant, we can't handle this.
4514 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4515 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4516 if (OffsetOp.getOpcode() == ISD::UNDEF)
4517 return InferPointerInfo(Ptr);
4518 return MachinePointerInfo();
4519}
Wesley Peck527da1b2010-11-23 03:31:01 +00004520
Chris Lattnerea952f02010-09-21 17:24:05 +00004521
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004522SDValue
4523SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004524 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004525 SDValue Ptr, SDValue Offset,
4526 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004527 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004528 unsigned Alignment, const MDNode *TBAAInfo,
4529 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004530 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004531 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004532 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4533 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004534
Dan Gohman48b185d2009-09-25 20:36:54 +00004535 unsigned Flags = MachineMemOperand::MOLoad;
4536 if (isVolatile)
4537 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004538 if (isNonTemporal)
4539 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004540 if (isInvariant)
4541 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004542
Chris Lattnerea952f02010-09-21 17:24:05 +00004543 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4544 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004545 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004546 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004547
Chris Lattnerea952f02010-09-21 17:24:05 +00004548 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004549 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004550 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004551 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004552 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004553}
4554
4555SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004556SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004557 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004558 SDValue Ptr, SDValue Offset, EVT MemVT,
4559 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004560 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004561 ExtType = ISD::NON_EXTLOAD;
4562 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004563 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004564 } else {
4565 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004566 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4567 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004568 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004569 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004570 assert(VT.isVector() == MemVT.isVector() &&
4571 "Cannot use trunc store to convert to or from a vector!");
4572 assert((!VT.isVector() ||
4573 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4574 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004575 }
4576
4577 bool Indexed = AM != ISD::UNINDEXED;
4578 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4579 "Unindexed load with an offset!");
4580
4581 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004582 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004583 SDValue Ops[] = { Chain, Ptr, Offset };
4584 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004585 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004586 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004587 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004588 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004589 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004590 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004591 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004592 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4593 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004594 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004595 }
Jack Carter170a5f22013-09-09 22:02:08 +00004596 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4597 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004598 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004599 CSEMap.InsertNode(N, IP);
4600 AllNodes.push_back(N);
4601 return SDValue(N, 0);
4602}
4603
Andrew Trickef9de2a2013-05-25 02:42:55 +00004604SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004605 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004606 MachinePointerInfo PtrInfo,
4607 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004608 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004609 const MDNode *TBAAInfo,
4610 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004611 SDValue Undef = getUNDEF(Ptr.getValueType());
4612 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004613 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4614 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004615}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004616
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004617SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4618 SDValue Chain, SDValue Ptr,
4619 MachineMemOperand *MMO) {
4620 SDValue Undef = getUNDEF(Ptr.getValueType());
4621 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4622 VT, MMO);
4623}
4624
Andrew Trickef9de2a2013-05-25 02:42:55 +00004625SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004626 SDValue Chain, SDValue Ptr,
4627 MachinePointerInfo PtrInfo, EVT MemVT,
4628 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004629 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004630 SDValue Undef = getUNDEF(Ptr.getValueType());
4631 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004632 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004633 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004634}
4635
4636
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004637SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4638 SDValue Chain, SDValue Ptr, EVT MemVT,
4639 MachineMemOperand *MMO) {
4640 SDValue Undef = getUNDEF(Ptr.getValueType());
4641 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4642 MemVT, MMO);
4643}
4644
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004645SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004646SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004647 SDValue Offset, ISD::MemIndexedMode AM) {
4648 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4649 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4650 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004651 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004652 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004653 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004654 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004655}
4656
Andrew Trickef9de2a2013-05-25 02:42:55 +00004657SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004658 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004659 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004660 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004661 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004662 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004663 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004664 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004665
Dan Gohman48b185d2009-09-25 20:36:54 +00004666 unsigned Flags = MachineMemOperand::MOStore;
4667 if (isVolatile)
4668 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004669 if (isNonTemporal)
4670 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004671
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004672 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004673 PtrInfo = InferPointerInfo(Ptr);
4674
4675 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004676 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004677 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004678 Val.getValueType().getStoreSize(), Alignment,
4679 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004680
4681 return getStore(Chain, dl, Val, Ptr, MMO);
4682}
4683
Andrew Trickef9de2a2013-05-25 02:42:55 +00004684SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004685 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004686 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004687 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004688 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004689 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004690 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004691 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4692 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004693 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004694 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004695 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004696 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004697 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004698 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004699 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4700 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004701 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004702 }
Jack Carter170a5f22013-09-09 22:02:08 +00004703 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4704 dl.getDebugLoc(), VTs,
4705 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004706 CSEMap.InsertNode(N, IP);
4707 AllNodes.push_back(N);
4708 return SDValue(N, 0);
4709}
4710
Andrew Trickef9de2a2013-05-25 02:42:55 +00004711SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004712 SDValue Ptr, MachinePointerInfo PtrInfo,
4713 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004714 unsigned Alignment,
4715 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004716 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004717 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004718 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4719 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004720
Dan Gohman48b185d2009-09-25 20:36:54 +00004721 unsigned Flags = MachineMemOperand::MOStore;
4722 if (isVolatile)
4723 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004724 if (isNonTemporal)
4725 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004726
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004727 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004728 PtrInfo = InferPointerInfo(Ptr);
4729
4730 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004731 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004732 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4733 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004734
4735 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4736}
4737
Andrew Trickef9de2a2013-05-25 02:42:55 +00004738SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004739 SDValue Ptr, EVT SVT,
4740 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004741 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004742
Michael Ilsemand5f91512012-09-10 16:56:31 +00004743 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004744 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004745 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004746 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004747
Dan Gohmancecad352009-12-14 23:40:38 +00004748 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4749 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004750 assert(VT.isInteger() == SVT.isInteger() &&
4751 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004752 assert(VT.isVector() == SVT.isVector() &&
4753 "Cannot use trunc store to convert to or from a vector!");
4754 assert((!VT.isVector() ||
4755 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4756 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004757
Owen Anderson9f944592009-08-11 20:47:22 +00004758 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004759 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004760 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4761 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004762 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004763 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004764 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004765 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004766 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004767 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004768 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4769 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004770 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004771 }
Jack Carter170a5f22013-09-09 22:02:08 +00004772 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4773 dl.getDebugLoc(), VTs,
4774 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004775 CSEMap.InsertNode(N, IP);
4776 AllNodes.push_back(N);
4777 return SDValue(N, 0);
4778}
4779
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004780SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004781SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004782 SDValue Offset, ISD::MemIndexedMode AM) {
4783 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4784 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4785 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004786 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004787 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4788 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004789 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004790 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004791 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004792 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004793 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004795 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004796
Jack Carter170a5f22013-09-09 22:02:08 +00004797 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4798 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004799 ST->isTruncatingStore(),
4800 ST->getMemoryVT(),
4801 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004802 CSEMap.InsertNode(N, IP);
4803 AllNodes.push_back(N);
4804 return SDValue(N, 0);
4805}
4806
Andrew Trickef9de2a2013-05-25 02:42:55 +00004807SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004808 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004809 SDValue SV,
4810 unsigned Align) {
4811 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00004812 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004813}
4814
Andrew Trickef9de2a2013-05-25 02:42:55 +00004815SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00004816 ArrayRef<SDUse> Ops) {
4817 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004818 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00004819 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004820 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4821 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004822 default: break;
4823 }
4824
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004825 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004826 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00004827 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00004828 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004829}
4830
Andrew Trickef9de2a2013-05-25 02:42:55 +00004831SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00004832 ArrayRef<SDValue> Ops) {
4833 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00004834 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004835 case 0: return getNode(Opcode, DL, VT);
4836 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4837 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4838 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004839 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004840 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004841
Chris Lattnerb0713c72005-04-09 03:27:28 +00004842 switch (Opcode) {
4843 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004844 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004845 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004846 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4847 "LHS and RHS of condition must have same type!");
4848 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4849 "True and False arms of SelectCC must have same type!");
4850 assert(Ops[2].getValueType() == VT &&
4851 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004852 break;
4853 }
4854 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004855 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004856 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4857 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004858 break;
4859 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004860 }
4861
Chris Lattner566307f2005-05-14 07:42:29 +00004862 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004863 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004864 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004865
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004866 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004867 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004868 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004869 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004870
Bill Wendling022d18f2009-12-18 23:32:53 +00004871 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004872 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004873
Jack Carter170a5f22013-09-09 22:02:08 +00004874 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004875 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004876 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004877 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004878 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004879 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004880 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004881
Chris Lattnerb0713c72005-04-09 03:27:28 +00004882 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004883#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004884 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004885#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004886 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004887}
4888
Andrew Trickef9de2a2013-05-25 02:42:55 +00004889SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00004890 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
4891 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00004892}
4893
Andrew Trickef9de2a2013-05-25 02:42:55 +00004894SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00004895 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004896 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00004897 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00004898
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004899#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004900 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004901 // FIXME: figure out how to safely handle things like
4902 // int foo(int x) { return 1 << (x & 255); }
4903 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004904 case ISD::SRA_PARTS:
4905 case ISD::SRL_PARTS:
4906 case ISD::SHL_PARTS:
4907 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004908 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
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 else if (N3.getOpcode() == ISD::AND)
4911 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4912 // If the and is only masking out bits that cannot effect the shift,
4913 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004914 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004915 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004916 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004917 }
4918 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004919 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004920#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004921
Chris Lattnerf9c19152005-08-25 19:12:10 +00004922 // Memoize the node unless it returns a flag.
4923 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00004924 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004925 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004926 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004927 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004928 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004929 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004930 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004931
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004932 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004933 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4934 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004935 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004936 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4937 DL.getDebugLoc(), VTList, Ops[0],
4938 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004939 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004940 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4941 DL.getDebugLoc(), VTList, Ops[0],
4942 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004943 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004944 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004945 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004946 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004947 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004948 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004949 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004950 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4951 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004952 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004953 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4954 DL.getDebugLoc(), VTList, Ops[0],
4955 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004956 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004957 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4958 DL.getDebugLoc(), VTList, Ops[0],
4959 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004960 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004961 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004962 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004963 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004964 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004965 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004966#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004967 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004968#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004969 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004970}
4971
Andrew Trickef9de2a2013-05-25 02:42:55 +00004972SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Topper48d114b2014-04-26 18:35:24 +00004973 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004974}
4975
Andrew Trickef9de2a2013-05-25 02:42:55 +00004976SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004977 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004978 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00004979 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004980}
4981
Andrew Trickef9de2a2013-05-25 02:42:55 +00004982SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004983 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004984 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00004985 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004986}
4987
Andrew Trickef9de2a2013-05-25 02:42:55 +00004988SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004989 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004990 SDValue Ops[] = { N1, N2, N3 };
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) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004997 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00004998 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004999}
5000
Andrew Trickef9de2a2013-05-25 02:42:55 +00005001SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005002 SDValue N1, SDValue N2, SDValue N3,
5003 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005004 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005005 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005006}
5007
Owen Anderson53aa7a92009-08-10 22:56:29 +00005008SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005009 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005010}
5011
Owen Anderson53aa7a92009-08-10 22:56:29 +00005012SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005013 FoldingSetNodeID ID;
5014 ID.AddInteger(2U);
5015 ID.AddInteger(VT1.getRawBits());
5016 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005017
Craig Topperc0196b12014-04-14 00:51:57 +00005018 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005019 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005020 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005021 EVT *Array = Allocator.Allocate<EVT>(2);
5022 Array[0] = VT1;
5023 Array[1] = VT2;
5024 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5025 VTListMap.InsertNode(Result, IP);
5026 }
5027 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005028}
Dan Gohman17059682008-07-17 19:10:17 +00005029
Owen Anderson53aa7a92009-08-10 22:56:29 +00005030SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005031 FoldingSetNodeID ID;
5032 ID.AddInteger(3U);
5033 ID.AddInteger(VT1.getRawBits());
5034 ID.AddInteger(VT2.getRawBits());
5035 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005036
Craig Topperc0196b12014-04-14 00:51:57 +00005037 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005038 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005039 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005040 EVT *Array = Allocator.Allocate<EVT>(3);
5041 Array[0] = VT1;
5042 Array[1] = VT2;
5043 Array[2] = VT3;
5044 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5045 VTListMap.InsertNode(Result, IP);
5046 }
5047 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005048}
5049
Owen Anderson53aa7a92009-08-10 22:56:29 +00005050SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005051 FoldingSetNodeID ID;
5052 ID.AddInteger(4U);
5053 ID.AddInteger(VT1.getRawBits());
5054 ID.AddInteger(VT2.getRawBits());
5055 ID.AddInteger(VT3.getRawBits());
5056 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005057
Craig Topperc0196b12014-04-14 00:51:57 +00005058 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005059 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005060 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005061 EVT *Array = Allocator.Allocate<EVT>(4);
5062 Array[0] = VT1;
5063 Array[1] = VT2;
5064 Array[2] = VT3;
5065 Array[3] = VT4;
5066 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5067 VTListMap.InsertNode(Result, IP);
5068 }
5069 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005070}
5071
Craig Topperabb4ac72014-04-16 06:10:51 +00005072SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5073 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005074 FoldingSetNodeID ID;
5075 ID.AddInteger(NumVTs);
5076 for (unsigned index = 0; index < NumVTs; index++) {
5077 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005078 }
5079
Craig Topperc0196b12014-04-14 00:51:57 +00005080 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005081 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005082 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005083 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005084 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005085 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5086 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005087 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005088 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005089}
5090
5091
Chris Lattnerf34156e2006-01-28 09:32:45 +00005092/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5093/// specified operands. If the resultant node already exists in the DAG,
5094/// this does not modify the specified node, instead it returns the node that
5095/// already exists. If the resultant node does not exist in the DAG, the
5096/// input node is returned. As a degenerate case, if you specify the same
5097/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005098SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005099 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005100
Chris Lattnerf34156e2006-01-28 09:32:45 +00005101 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005102 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005103
Chris Lattnerf34156e2006-01-28 09:32:45 +00005104 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005105 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005106 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005107 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005108
Dan Gohmanebeccb42008-07-21 22:38:59 +00005109 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005110 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005111 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005112 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005113
Chris Lattnerf34156e2006-01-28 09:32:45 +00005114 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005115 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005116
Chris Lattnerf34156e2006-01-28 09:32:45 +00005117 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005118 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005119 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005120}
5121
Dan Gohman92c11ac2010-06-18 15:30:29 +00005122SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005123 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005124
Chris Lattnerf34156e2006-01-28 09:32:45 +00005125 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005126 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005127 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005128
Chris Lattnerf34156e2006-01-28 09:32:45 +00005129 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005130 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005131 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005132 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005133
Dan Gohmanebeccb42008-07-21 22:38:59 +00005134 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005135 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005136 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005137 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005138
Chris Lattnerf34156e2006-01-28 09:32:45 +00005139 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005140 if (N->OperandList[0] != Op1)
5141 N->OperandList[0].set(Op1);
5142 if (N->OperandList[1] != Op2)
5143 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005144
Chris Lattnerf34156e2006-01-28 09:32:45 +00005145 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005146 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005147 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005148}
5149
Dan Gohman92c11ac2010-06-18 15:30:29 +00005150SDNode *SelectionDAG::
5151UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005152 SDValue Ops[] = { Op1, Op2, Op3 };
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) {
5159 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005160 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005161}
5162
Dan Gohman92c11ac2010-06-18 15:30:29 +00005163SDNode *SelectionDAG::
5164UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005165 SDValue Op3, SDValue Op4, SDValue Op5) {
5166 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005167 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005168}
5169
Dan Gohman92c11ac2010-06-18 15:30:29 +00005170SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005171UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5172 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005173 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005174 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005175
Chris Lattnerf34156e2006-01-28 09:32:45 +00005176 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005177 bool AnyChange = false;
5178 for (unsigned i = 0; i != NumOps; ++i) {
5179 if (Ops[i] != N->getOperand(i)) {
5180 AnyChange = true;
5181 break;
5182 }
5183 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005184
Chris Lattnerf34156e2006-01-28 09:32:45 +00005185 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005186 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005187
Chris Lattnerf34156e2006-01-28 09:32:45 +00005188 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005189 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005190 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005191 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005192
Dan Gohman2f83b472008-05-02 00:05:03 +00005193 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005194 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005195 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005196 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005197
Chris Lattnerf34156e2006-01-28 09:32:45 +00005198 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005199 for (unsigned i = 0; i != NumOps; ++i)
5200 if (N->OperandList[i] != Ops[i])
5201 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005202
5203 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005204 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005205 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005206}
5207
Dan Gohman91697632008-07-07 20:57:48 +00005208/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005209/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005210void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005211 // Unlike the code in MorphNodeTo that does this, we don't need to
5212 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005213 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5214 SDUse &Use = *I++;
5215 Use.set(SDValue());
5216 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005217}
Chris Lattner19732782005-08-16 18:17:10 +00005218
Dan Gohman17059682008-07-17 19:10:17 +00005219/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5220/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005221///
Dan Gohman17059682008-07-17 19:10:17 +00005222SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005223 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005224 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005225 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +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) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005230 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005231 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005232 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005233}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005234
Dan Gohman17059682008-07-17 19:10:17 +00005235SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005236 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005237 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005238 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005239 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005240 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005241}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005242
Dan Gohman17059682008-07-17 19:10:17 +00005243SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005244 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005245 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005246 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005247 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005248 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005249}
Chris Lattner466fece2005-08-21 22:30:30 +00005250
Dan Gohman17059682008-07-17 19:10:17 +00005251SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005252 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005253 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005254 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005255}
5256
Dan Gohman17059682008-07-17 19:10:17 +00005257SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005258 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005259 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005260 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005261}
5262
Dan Gohman17059682008-07-17 19:10:17 +00005263SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005264 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005265 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005266 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005267}
5268
Dan Gohman17059682008-07-17 19:10:17 +00005269SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005270 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005271 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005272 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005273 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005274}
5275
Bill Wendling2d598632008-12-01 23:28:22 +00005276SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005277 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005278 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005279 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005280 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005281}
5282
Scott Michelcf0da6c2009-02-17 22:15:04 +00005283SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005284 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005285 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005286 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005287 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005288 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005289}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005290
Scott Michelcf0da6c2009-02-17 22:15:04 +00005291SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005292 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005293 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005294 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005295 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005296 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005297}
5298
Dan Gohman17059682008-07-17 19:10:17 +00005299SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005300 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005301 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005302 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005303 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005304 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005305 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005306}
5307
Dan Gohman17059682008-07-17 19:10:17 +00005308SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005309 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005310 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005311 SDValue Op3) {
5312 SDVTList VTs = getVTList(VT1, VT2, VT3);
5313 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005314 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005315}
5316
5317SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005318 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005319 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005320 // Reset the NodeID to -1.
5321 N->setNodeId(-1);
5322 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005323}
5324
Andrew Trickef9de2a2013-05-25 02:42:55 +00005325/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005326/// the line number information on the merged node since it is not possible to
5327/// preserve the information that operation is associated with multiple lines.
5328/// This will make the debugger working better at -O0, were there is a higher
5329/// probability having other instructions associated with that line.
5330///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005331/// For IROrder, we keep the smaller of the two
5332SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005333 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005334 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5335 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005336 N->setDebugLoc(DebugLoc());
5337 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005338 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5339 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005340 return N;
5341}
5342
Chris Lattneraf197502010-02-28 21:36:14 +00005343/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005344/// return type, opcode, and operands.
5345///
5346/// Note that MorphNodeTo returns the resultant node. If there is already a
5347/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005348/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005349///
5350/// Using MorphNodeTo is faster than creating a new node and swapping it in
5351/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005352/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005353/// the node's users.
5354///
5355SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005356 SDVTList VTs, ArrayRef<SDValue> Ops) {
5357 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005358 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005359 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005360 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005361 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005362 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005363 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005364 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005365 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005366
Dan Gohmand3fe1742008-09-13 01:54:27 +00005367 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005368 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005369
Dan Gohman17059682008-07-17 19:10:17 +00005370 // Start the morphing.
5371 N->NodeType = Opc;
5372 N->ValueList = VTs.VTs;
5373 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005374
Dan Gohman17059682008-07-17 19:10:17 +00005375 // Clear the operands list, updating used nodes to remove this from their
5376 // use list. Keep track of any operands that become dead as a result.
5377 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005378 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5379 SDUse &Use = *I++;
5380 SDNode *Used = Use.getNode();
5381 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005382 if (Used->use_empty())
5383 DeadNodeSet.insert(Used);
5384 }
5385
Dan Gohman48b185d2009-09-25 20:36:54 +00005386 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5387 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005388 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005389 // If NumOps is larger than the # of operands we can have in a
5390 // MachineSDNode, reallocate the operand list.
5391 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5392 if (MN->OperandsNeedDelete)
5393 delete[] MN->OperandList;
5394 if (NumOps > array_lengthof(MN->LocalOperands))
5395 // We're creating a final node that will live unmorphed for the
5396 // remainder of the current SelectionDAG iteration, so we can allocate
5397 // the operands directly out of a pool with no recycling metadata.
5398 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005399 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005400 else
Craig Topper131de822014-04-27 19:21:16 +00005401 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005402 MN->OperandsNeedDelete = false;
5403 } else
Craig Topper131de822014-04-27 19:21:16 +00005404 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005405 } else {
5406 // If NumOps is larger than the # of operands we currently have, reallocate
5407 // the operand list.
5408 if (NumOps > N->NumOperands) {
5409 if (N->OperandsNeedDelete)
5410 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005411 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005412 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005413 } else
Craig Topper131de822014-04-27 19:21:16 +00005414 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005415 }
5416
5417 // Delete any nodes that are still dead after adding the uses for the
5418 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005419 if (!DeadNodeSet.empty()) {
5420 SmallVector<SDNode *, 16> DeadNodes;
5421 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5422 E = DeadNodeSet.end(); I != E; ++I)
5423 if ((*I)->use_empty())
5424 DeadNodes.push_back(*I);
5425 RemoveDeadNodes(DeadNodes);
5426 }
Dan Gohman91697632008-07-07 20:57:48 +00005427
Dan Gohman17059682008-07-17 19:10:17 +00005428 if (IP)
5429 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005430 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005431}
5432
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005433
Dan Gohman32f71d72009-09-25 18:54:59 +00005434/// getMachineNode - These are used for target selectors to create a new node
5435/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005436///
Dan Gohman32f71d72009-09-25 18:54:59 +00005437/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005438/// node of the specified opcode and operands, it returns that node instead of
5439/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005440MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005441SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005442 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005443 return getMachineNode(Opcode, dl, VTs, None);
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, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005448 SDVTList VTs = getVTList(VT);
5449 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005450 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005451}
Bill Wendlinga434d932009-01-29 09:01:55 +00005452
Dan Gohmana22f2d82009-10-10 01:29:16 +00005453MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005454SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005455 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005456 SDVTList VTs = getVTList(VT);
5457 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005458 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005459}
Bill Wendlinga434d932009-01-29 09:01:55 +00005460
Dan Gohmana22f2d82009-10-10 01:29:16 +00005461MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005462SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005463 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005464 SDVTList VTs = getVTList(VT);
5465 SDValue Ops[] = { Op1, Op2, Op3 };
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 VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005471 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005472 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005473 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005474}
Bill Wendlinga434d932009-01-29 09:01:55 +00005475
Dan Gohmana22f2d82009-10-10 01:29:16 +00005476MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005477SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005478 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005479 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005480}
Bill Wendlinga434d932009-01-29 09:01:55 +00005481
Dan Gohmana22f2d82009-10-10 01:29:16 +00005482MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005483SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005484 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005485 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005486 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005487 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005488}
Bill Wendlinga434d932009-01-29 09:01:55 +00005489
Dan Gohmana22f2d82009-10-10 01:29:16 +00005490MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005491SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005492 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005493 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005494 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005495 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005496}
5497
Dan Gohmana22f2d82009-10-10 01:29:16 +00005498MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005499SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005500 EVT VT1, EVT VT2, SDValue Op1,
5501 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005502 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005503 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005504 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005505}
5506
Dan Gohmana22f2d82009-10-10 01:29:16 +00005507MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005508SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005509 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005510 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005511 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005512 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005513}
Bill Wendlinga434d932009-01-29 09:01:55 +00005514
Dan Gohmana22f2d82009-10-10 01:29:16 +00005515MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005516SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005517 EVT VT1, EVT VT2, EVT VT3,
5518 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005519 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005520 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005521 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005522}
Bill Wendlinga434d932009-01-29 09:01:55 +00005523
Dan Gohmana22f2d82009-10-10 01:29:16 +00005524MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005525SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005526 EVT VT1, EVT VT2, EVT VT3,
5527 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005528 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005529 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005530 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005531}
Bill Wendlinga434d932009-01-29 09:01:55 +00005532
Dan Gohmana22f2d82009-10-10 01:29:16 +00005533MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005534SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005535 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005536 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005537 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005538 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005539}
Bill Wendlinga434d932009-01-29 09:01:55 +00005540
Dan Gohmana22f2d82009-10-10 01:29:16 +00005541MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005542SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005543 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005544 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005545 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005546 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005547}
5548
Dan Gohmana22f2d82009-10-10 01:29:16 +00005549MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005550SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005551 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005552 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005553 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005554 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005555}
5556
Dan Gohmana22f2d82009-10-10 01:29:16 +00005557MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005558SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005559 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005560 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005561 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005562 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005563 const SDValue *Ops = OpsArray.data();
5564 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005565
5566 if (DoCSE) {
5567 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005568 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005569 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005570 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005571 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005572 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005573 }
5574
5575 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005576 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5577 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005578
5579 // Initialize the operands list.
5580 if (NumOps > array_lengthof(N->LocalOperands))
5581 // We're creating a final node that will live unmorphed for the
5582 // remainder of the current SelectionDAG iteration, so we can allocate
5583 // the operands directly out of a pool with no recycling metadata.
5584 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5585 Ops, NumOps);
5586 else
5587 N->InitOperands(N->LocalOperands, Ops, NumOps);
5588 N->OperandsNeedDelete = false;
5589
5590 if (DoCSE)
5591 CSEMap.InsertNode(N, IP);
5592
5593 AllNodes.push_back(N);
5594#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005595 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005596#endif
5597 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005598}
Evan Chengd3f1db92006-02-09 07:15:23 +00005599
Dan Gohmanac33a902009-08-19 18:16:17 +00005600/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005601/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005602SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005603SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005604 SDValue Operand) {
5605 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005606 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005607 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005608 return SDValue(Subreg, 0);
5609}
5610
Bob Wilson2a45a652009-10-08 18:49:46 +00005611/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005612/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005613SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005614SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005615 SDValue Operand, SDValue Subreg) {
5616 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005617 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005618 VT, Operand, Subreg, SRIdxVal);
5619 return SDValue(Result, 0);
5620}
5621
Evan Cheng31604a62008-03-22 01:55:50 +00005622/// getNodeIfExists - Get the specified node if it's already available, or
5623/// else return NULL.
5624SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Craig Topper633d99b2014-04-27 23:22:43 +00005625 ArrayRef<SDValue> Ops) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005626 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005627 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005628 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005629 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005630 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005631 return E;
5632 }
Craig Topperc0196b12014-04-14 00:51:57 +00005633 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005634}
5635
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005636/// getDbgValue - Creates a SDDbgValue node.
5637///
Adrian Prantl32da8892014-04-25 20:49:25 +00005638/// SDNode
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005639SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005640SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
5641 bool IsIndirect, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005642 DebugLoc DL, unsigned O) {
Adrian Prantl32da8892014-04-25 20:49:25 +00005643 return new (Allocator) SDDbgValue(MDPtr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005644}
5645
Adrian Prantl32da8892014-04-25 20:49:25 +00005646/// Constant
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005647SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005648SelectionDAG::getConstantDbgValue(MDNode *MDPtr, const Value *C,
5649 uint64_t Off,
5650 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005651 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5652}
5653
Adrian Prantl32da8892014-04-25 20:49:25 +00005654/// FrameIndex
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005655SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005656SelectionDAG::getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5657 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005658 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5659}
5660
Dan Gohman7d099f72010-03-03 21:33:37 +00005661namespace {
5662
Dan Gohman9cc886b2010-03-04 19:11:28 +00005663/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005664/// pointed to by a use iterator is deleted, increment the use iterator
5665/// so that it doesn't dangle.
5666///
Dan Gohman7d099f72010-03-03 21:33:37 +00005667class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005668 SDNode::use_iterator &UI;
5669 SDNode::use_iterator &UE;
5670
Craig Topper7b883b32014-03-08 06:31:39 +00005671 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005672 // Increment the iterator as needed.
5673 while (UI != UE && N == *UI)
5674 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005675 }
5676
5677public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005678 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005679 SDNode::use_iterator &ui,
5680 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005681 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005682};
5683
5684}
5685
Evan Cheng445b91a2006-08-07 22:13:29 +00005686/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005687/// This can cause recursive merging of nodes in the DAG.
5688///
Chris Lattner76858912008-02-03 03:35:22 +00005689/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005690///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005691void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005692 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005693 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005694 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005695 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005696
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005697 // Iterate over all the existing uses of From. New uses will be added
5698 // to the beginning of the use list, which we avoid visiting.
5699 // This specifically avoids visiting uses of From that arise while the
5700 // replacement is happening, because any such uses would be the result
5701 // of CSE: If an existing node looks like From after one of its operands
5702 // is replaced by To, we don't want to replace of all its users with To
5703 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005704 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005705 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005706 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005707 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005708
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005709 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005710 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005711
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005712 // A user can appear in a use list multiple times, and when this
5713 // happens the uses are usually next to each other in the list.
5714 // To help reduce the number of CSE recomputations, process all
5715 // the uses of this user that we can find this way.
5716 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005717 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005718 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005719 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005720 } while (UI != UE && *UI == User);
5721
5722 // Now that we have modified User, add it back to the CSE maps. If it
5723 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005724 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005725 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005726
5727 // If we just RAUW'd the root, take note.
5728 if (FromN == getRoot())
5729 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005730}
5731
5732/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5733/// This can cause recursive merging of nodes in the DAG.
5734///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005735/// This version assumes that for each value of From, there is a
5736/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005737///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005738void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005739#ifndef NDEBUG
5740 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5741 assert((!From->hasAnyUseOfValue(i) ||
5742 From->getValueType(i) == To->getValueType(i)) &&
5743 "Cannot use this version of ReplaceAllUsesWith!");
5744#endif
Dan Gohman17059682008-07-17 19:10:17 +00005745
5746 // Handle the trivial case.
5747 if (From == To)
5748 return;
5749
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005750 // Iterate over just the existing users of From. See the comments in
5751 // the ReplaceAllUsesWith above.
5752 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005753 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005754 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005755 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005756
Chris Lattner373f0482005-08-26 18:36:28 +00005757 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005758 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005759
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005760 // A user can appear in a use list multiple times, and when this
5761 // happens the uses are usually next to each other in the list.
5762 // To help reduce the number of CSE recomputations, process all
5763 // the uses of this user that we can find this way.
5764 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005765 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005766 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005767 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005768 } while (UI != UE && *UI == User);
5769
5770 // Now that we have modified User, add it back to the CSE maps. If it
5771 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005772 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005773 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005774
5775 // If we just RAUW'd the root, take note.
5776 if (From == getRoot().getNode())
5777 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005778}
5779
Chris Lattner373f0482005-08-26 18:36:28 +00005780/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5781/// This can cause recursive merging of nodes in the DAG.
5782///
5783/// This version can replace From with any result values. To must match the
5784/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005785void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005786 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005787 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005788
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005789 // Iterate over just the existing users of From. See the comments in
5790 // the ReplaceAllUsesWith above.
5791 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005792 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005793 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005794 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005795
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005796 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005797 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005798
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005799 // A user can appear in a use list multiple times, and when this
5800 // happens the uses are usually next to each other in the list.
5801 // To help reduce the number of CSE recomputations, process all
5802 // the uses of this user that we can find this way.
5803 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005804 SDUse &Use = UI.getUse();
5805 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005806 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005807 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005808 } while (UI != UE && *UI == User);
5809
5810 // Now that we have modified User, add it back to the CSE maps. If it
5811 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005812 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005813 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005814
5815 // If we just RAUW'd the root, take note.
5816 if (From == getRoot().getNode())
5817 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005818}
5819
Chris Lattner375e1a72006-02-17 21:58:01 +00005820/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005821/// uses of other values produced by From.getNode() alone. The Deleted
5822/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005823void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005824 // Handle the really simple, really trivial case efficiently.
5825 if (From == To) return;
5826
Chris Lattner375e1a72006-02-17 21:58:01 +00005827 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005828 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005829 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005830 return;
5831 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005832
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005833 // Iterate over just the existing users of From. See the comments in
5834 // the ReplaceAllUsesWith above.
5835 SDNode::use_iterator UI = From.getNode()->use_begin(),
5836 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005837 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005838 while (UI != UE) {
5839 SDNode *User = *UI;
5840 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005841
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005842 // A user can appear in a use list multiple times, and when this
5843 // happens the uses are usually next to each other in the list.
5844 // To help reduce the number of CSE recomputations, process all
5845 // the uses of this user that we can find this way.
5846 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005847 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005848
5849 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005850 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005851 ++UI;
5852 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005853 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005854
5855 // If this node hasn't been modified yet, it's still in the CSE maps,
5856 // so remove its old self from the CSE maps.
5857 if (!UserRemovedFromCSEMaps) {
5858 RemoveNodeFromCSEMaps(User);
5859 UserRemovedFromCSEMaps = true;
5860 }
5861
5862 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005863 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005864 } while (UI != UE && *UI == User);
5865
5866 // We are iterating over all uses of the From node, so if a use
5867 // doesn't use the specific value, no changes are made.
5868 if (!UserRemovedFromCSEMaps)
5869 continue;
5870
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005871 // Now that we have modified User, add it back to the CSE maps. If it
5872 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005873 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005874 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005875
5876 // If we just RAUW'd the root, take note.
5877 if (From == getRoot())
5878 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005879}
5880
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005881namespace {
5882 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5883 /// to record information about a use.
5884 struct UseMemo {
5885 SDNode *User;
5886 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005887 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005888 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005889
5890 /// operator< - Sort Memos by User.
5891 bool operator<(const UseMemo &L, const UseMemo &R) {
5892 return (intptr_t)L.User < (intptr_t)R.User;
5893 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005894}
5895
Dan Gohman17059682008-07-17 19:10:17 +00005896/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005897/// uses of other values produced by From.getNode() alone. The same value
5898/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005899/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005900void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5901 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005902 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005903 // Handle the simple, trivial case efficiently.
5904 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005905 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005906
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005907 // Read up all the uses and make records of them. This helps
5908 // processing new uses that are introduced during the
5909 // replacement process.
5910 SmallVector<UseMemo, 4> Uses;
5911 for (unsigned i = 0; i != Num; ++i) {
5912 unsigned FromResNo = From[i].getResNo();
5913 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005914 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005915 E = FromNode->use_end(); UI != E; ++UI) {
5916 SDUse &Use = UI.getUse();
5917 if (Use.getResNo() == FromResNo) {
5918 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005919 Uses.push_back(Memo);
5920 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005921 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005922 }
Dan Gohman17059682008-07-17 19:10:17 +00005923
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005924 // Sort the uses, so that all the uses from a given User are together.
5925 std::sort(Uses.begin(), Uses.end());
5926
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005927 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5928 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005929 // We know that this user uses some value of From. If it is the right
5930 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005931 SDNode *User = Uses[UseIndex].User;
5932
5933 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005934 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005935
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005936 // The Uses array is sorted, so all the uses for a given User
5937 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005938 // To help reduce the number of CSE recomputations, process all
5939 // the uses of this user that we can find this way.
5940 do {
5941 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005942 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005943 ++UseIndex;
5944
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005945 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005946 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5947
Dan Gohman17059682008-07-17 19:10:17 +00005948 // Now that we have modified User, add it back to the CSE maps. If it
5949 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005950 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005951 }
5952}
5953
Evan Cheng9631a602006-08-01 08:20:41 +00005954/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005955/// based on their topological order. It returns the maximum id and a vector
5956/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005957unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005958
Dan Gohman86aa16a2008-09-30 18:30:35 +00005959 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005960
Dan Gohman86aa16a2008-09-30 18:30:35 +00005961 // SortedPos tracks the progress of the algorithm. Nodes before it are
5962 // sorted, nodes after it are unsorted. When the algorithm completes
5963 // it is at the end of the list.
5964 allnodes_iterator SortedPos = allnodes_begin();
5965
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005966 // Visit all the nodes. Move nodes with no operands to the front of
5967 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005968 // operand count. Before we do this, the Node Id fields of the nodes
5969 // may contain arbitrary values. After, the Node Id fields for nodes
5970 // before SortedPos will contain the topological sort index, and the
5971 // Node Id fields for nodes At SortedPos and after will contain the
5972 // count of outstanding operands.
5973 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5974 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005975 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005976 unsigned Degree = N->getNumOperands();
5977 if (Degree == 0) {
5978 // A node with no uses, add it to the result array immediately.
5979 N->setNodeId(DAGSize++);
5980 allnodes_iterator Q = N;
5981 if (Q != SortedPos)
5982 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005983 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005984 ++SortedPos;
5985 } else {
5986 // Temporarily use the Node Id as scratch space for the degree count.
5987 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005988 }
5989 }
5990
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005991 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005992 // such that by the time the end is reached all nodes will be sorted.
5993 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5994 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00005995 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00005996 // N is in sorted position, so all its uses have one less operand
5997 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005998 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5999 UI != UE; ++UI) {
6000 SDNode *P = *UI;
6001 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006002 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006003 --Degree;
6004 if (Degree == 0) {
6005 // All of P's operands are sorted, so P may sorted now.
6006 P->setNodeId(DAGSize++);
6007 if (P != SortedPos)
6008 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006009 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006010 ++SortedPos;
6011 } else {
6012 // Update P's outstanding operand count.
6013 P->setNodeId(Degree);
6014 }
6015 }
David Greene3b2a68c2010-01-20 00:59:23 +00006016 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006017#ifndef NDEBUG
6018 SDNode *S = ++I;
6019 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00006020 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00006021#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006022 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006023 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006024 }
6025
6026 assert(SortedPos == AllNodes.end() &&
6027 "Topological sort incomplete!");
6028 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6029 "First node in topological sort is not the entry token!");
6030 assert(AllNodes.front().getNodeId() == 0 &&
6031 "First node in topological sort has non-zero id!");
6032 assert(AllNodes.front().getNumOperands() == 0 &&
6033 "First node in topological sort has operands!");
6034 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6035 "Last node in topologic sort has unexpected id!");
6036 assert(AllNodes.back().use_empty() &&
6037 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006038 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006039 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006040}
6041
Evan Cheng563fe3c2010-03-25 01:38:16 +00006042/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6043/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006044void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6045 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006046 if (SD)
6047 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006048}
Evan Cheng29eefc12006-07-27 06:39:06 +00006049
Devang Patelefc6b162011-01-25 23:27:42 +00006050/// TransferDbgValues - Transfer SDDbgValues.
6051void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6052 if (From == To || !From.getNode()->getHasDebugValue())
6053 return;
6054 SDNode *FromNode = From.getNode();
6055 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006056 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006057 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006058 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006059 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006060 SDDbgValue *Dbg = *I;
6061 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6062 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
Adrian Prantl32da8892014-04-25 20:49:25 +00006063 Dbg->isIndirect(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006064 Dbg->getOffset(), Dbg->getDebugLoc(),
6065 Dbg->getOrder());
6066 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006067 }
6068 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006069 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006070 E = ClonedDVs.end(); I != E; ++I)
6071 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006072}
6073
Jim Laskeyd66e6162005-08-17 20:08:02 +00006074//===----------------------------------------------------------------------===//
6075// SDNode Class
6076//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006077
Chris Lattner3bf17b62007-02-04 02:41:42 +00006078HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006079 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006080}
6081
Andrew Trickef9de2a2013-05-25 02:42:55 +00006082GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6083 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006084 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006085 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006086 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006087}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006088
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006089AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6090 SDValue X, unsigned SrcAS,
6091 unsigned DestAS)
6092 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6093 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6094
Andrew Trickef9de2a2013-05-25 02:42:55 +00006095MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6096 EVT memvt, MachineMemOperand *mmo)
6097 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006098 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006099 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006100 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006101 assert(isNonTemporal() == MMO->isNonTemporal() &&
6102 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006103 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006104}
6105
Andrew Trickef9de2a2013-05-25 02:42:55 +00006106MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006107 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6108 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006109 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006110 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006111 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006112 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6113 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006114}
6115
Jim Laskeyf576b422006-10-27 23:46:08 +00006116/// Profile - Gather unique data for the node.
6117///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006118void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006119 AddNodeIDNode(ID, this);
6120}
6121
Owen Anderson3b1665e2009-08-25 22:27:22 +00006122namespace {
6123 struct EVTArray {
6124 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006125
Owen Anderson3b1665e2009-08-25 22:27:22 +00006126 EVTArray() {
6127 VTs.reserve(MVT::LAST_VALUETYPE);
6128 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6129 VTs.push_back(MVT((MVT::SimpleValueType)i));
6130 }
6131 };
6132}
6133
Owen Anderson53aa7a92009-08-10 22:56:29 +00006134static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006135static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006136static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006137
Chris Lattner88fa11c2005-11-08 23:30:28 +00006138/// getValueTypeList - Return a pointer to the specified value type.
6139///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006140const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006141 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006142 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006143 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006144 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006145 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006146 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006147 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006148 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006149}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006150
Chris Lattner40e79822005-01-12 18:37:47 +00006151/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6152/// indicated value. This method ignores uses of other values defined by this
6153/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006154bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006155 assert(Value < getNumValues() && "Bad value!");
6156
Roman Levenstein51f532f2008-04-07 10:06:32 +00006157 // TODO: Only iterate over uses of a given value of the node
6158 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006159 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006160 if (NUses == 0)
6161 return false;
6162 --NUses;
6163 }
Chris Lattner40e79822005-01-12 18:37:47 +00006164 }
6165
6166 // Found exactly the right number of uses?
6167 return NUses == 0;
6168}
6169
6170
Evan Cheng358c3d12007-08-02 05:29:38 +00006171/// hasAnyUseOfValue - Return true if there are any use of the indicated
6172/// value. This method ignores uses of other values defined by this operation.
6173bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6174 assert(Value < getNumValues() && "Bad value!");
6175
Dan Gohman7a510c22008-07-09 22:39:01 +00006176 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006177 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006178 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006179
6180 return false;
6181}
6182
6183
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006184/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006185///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006186bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006187 bool Seen = false;
6188 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006189 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006190 if (User == this)
6191 Seen = true;
6192 else
6193 return false;
6194 }
6195
6196 return Seen;
6197}
6198
Evan Cheng9456dd82006-11-03 07:31:32 +00006199/// isOperand - Return true if this node is an operand of N.
6200///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006201bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006202 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6203 if (*this == N->getOperand(i))
6204 return true;
6205 return false;
6206}
6207
Evan Cheng567d2e52008-03-04 00:41:45 +00006208bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006209 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006210 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006211 return true;
6212 return false;
6213}
Evan Chengd37645c2006-02-05 06:29:23 +00006214
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006215/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006216/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006217/// side-effecting instructions on any chain path. In practice, this looks
6218/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006219/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006220bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006221 unsigned Depth) const {
6222 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006223
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006224 // Don't search too deeply, we just want to be able to see through
6225 // TokenFactor's etc.
6226 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006227
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006228 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006229 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006230 if (getOpcode() == ISD::TokenFactor) {
6231 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006232 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6233 return false;
6234 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006235 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006236
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006237 // Loads don't have side effects, look through them.
6238 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6239 if (!Ld->isVolatile())
6240 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6241 }
6242 return false;
6243}
6244
Lang Hames5a004992011-07-07 04:31:51 +00006245/// hasPredecessor - Return true if N is a predecessor of this node.
6246/// N is either an operand of this node, or can be reached by recursively
6247/// traversing up the operands.
6248/// NOTE: This is an expensive method. Use it carefully.
6249bool SDNode::hasPredecessor(const SDNode *N) const {
6250 SmallPtrSet<const SDNode *, 32> Visited;
6251 SmallVector<const SDNode *, 16> Worklist;
6252 return hasPredecessorHelper(N, Visited, Worklist);
6253}
Dan Gohmancd139c02009-10-28 03:44:30 +00006254
Craig Topperb94011f2013-07-14 04:42:23 +00006255bool
6256SDNode::hasPredecessorHelper(const SDNode *N,
6257 SmallPtrSet<const SDNode *, 32> &Visited,
6258 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006259 if (Visited.empty()) {
6260 Worklist.push_back(this);
6261 } else {
6262 // Take a look in the visited set. If we've already encountered this node
6263 // we needn't search further.
6264 if (Visited.count(N))
6265 return true;
6266 }
6267
6268 // Haven't visited N yet. Continue the search.
6269 while (!Worklist.empty()) {
6270 const SDNode *M = Worklist.pop_back_val();
6271 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6272 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006273 if (Visited.insert(Op))
6274 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006275 if (Op == N)
6276 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006277 }
Lang Hames5a004992011-07-07 04:31:51 +00006278 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006279
6280 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006281}
6282
Evan Cheng5d9fd972006-10-04 00:56:09 +00006283uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6284 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006285 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006286}
6287
Mon P Wang32f8bb92009-11-30 02:42:02 +00006288SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6289 assert(N->getNumValues() == 1 &&
6290 "Can't unroll a vector with multiple results!");
6291
6292 EVT VT = N->getValueType(0);
6293 unsigned NE = VT.getVectorNumElements();
6294 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006295 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006296
6297 SmallVector<SDValue, 8> Scalars;
6298 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6299
6300 // If ResNE is 0, fully unroll the vector op.
6301 if (ResNE == 0)
6302 ResNE = NE;
6303 else if (NE > ResNE)
6304 NE = ResNE;
6305
6306 unsigned i;
6307 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006308 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006309 SDValue Operand = N->getOperand(j);
6310 EVT OperandVT = Operand.getValueType();
6311 if (OperandVT.isVector()) {
6312 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006313 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006314 EVT OperandEltVT = OperandVT.getVectorElementType();
6315 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6316 OperandEltVT,
6317 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006318 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006319 } else {
6320 // A scalar operand; just use it as is.
6321 Operands[j] = Operand;
6322 }
6323 }
6324
6325 switch (N->getOpcode()) {
6326 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006327 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006328 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006329 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006330 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006331 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006332 case ISD::SHL:
6333 case ISD::SRA:
6334 case ISD::SRL:
6335 case ISD::ROTL:
6336 case ISD::ROTR:
6337 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006338 getShiftAmountOperand(Operands[0].getValueType(),
6339 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006340 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006341 case ISD::SIGN_EXTEND_INREG:
6342 case ISD::FP_ROUND_INREG: {
6343 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6344 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6345 Operands[0],
6346 getValueType(ExtVT)));
6347 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006348 }
6349 }
6350
6351 for (; i < ResNE; ++i)
6352 Scalars.push_back(getUNDEF(EltVT));
6353
6354 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006355 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006356}
6357
Evan Chengf5938d52009-12-09 01:36:00 +00006358
Wesley Peck527da1b2010-11-23 03:31:01 +00006359/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6360/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006361/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006362bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006363 unsigned Bytes, int Dist) const {
6364 if (LD->getChain() != Base->getChain())
6365 return false;
6366 EVT VT = LD->getValueType(0);
6367 if (VT.getSizeInBits() / 8 != Bytes)
6368 return false;
6369
6370 SDValue Loc = LD->getOperand(1);
6371 SDValue BaseLoc = Base->getOperand(1);
6372 if (Loc.getOpcode() == ISD::FrameIndex) {
6373 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6374 return false;
6375 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6376 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6377 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6378 int FS = MFI->getObjectSize(FI);
6379 int BFS = MFI->getObjectSize(BFI);
6380 if (FS != BFS || FS != (int)Bytes) return false;
6381 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6382 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006383
6384 // Handle X+C
6385 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6386 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6387 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006388
Craig Topperc0196b12014-04-14 00:51:57 +00006389 const GlobalValue *GV1 = nullptr;
6390 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006391 int64_t Offset1 = 0;
6392 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006393 const TargetLowering *TLI = TM.getTargetLowering();
6394 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6395 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006396 if (isGA1 && isGA2 && GV1 == GV2)
6397 return Offset1 == (Offset2 + Dist*Bytes);
6398 return false;
6399}
6400
6401
Evan Cheng34a23ea2009-12-09 01:04:59 +00006402/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6403/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006404unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006405 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006406 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006407 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006408 const TargetLowering *TLI = TM.getTargetLowering();
6409 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006410 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006411 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00006412 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006413 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006414 unsigned AlignBits = KnownZero.countTrailingOnes();
6415 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6416 if (Align)
6417 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006418 }
Evan Chengd938faf2009-12-09 01:53:58 +00006419
Evan Cheng34a23ea2009-12-09 01:04:59 +00006420 // If this is a direct reference to a stack slot, use information about the
6421 // stack slot's alignment.
6422 int FrameIdx = 1 << 31;
6423 int64_t FrameOffset = 0;
6424 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6425 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006426 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006427 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006428 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006429 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6430 FrameOffset = Ptr.getConstantOperandVal(1);
6431 }
6432
6433 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006434 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006435 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6436 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006437 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006438 }
6439
6440 return 0;
6441}
6442
Juergen Ributzkab3487102013-11-19 21:20:17 +00006443/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6444/// which is split (or expanded) into two not necessarily identical pieces.
6445std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6446 // Currently all types are split in half.
6447 EVT LoVT, HiVT;
6448 if (!VT.isVector()) {
6449 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6450 } else {
6451 unsigned NumElements = VT.getVectorNumElements();
6452 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6453 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6454 NumElements/2);
6455 }
6456 return std::make_pair(LoVT, HiVT);
6457}
6458
6459/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6460/// low/high part.
6461std::pair<SDValue, SDValue>
6462SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6463 const EVT &HiVT) {
6464 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6465 N.getValueType().getVectorNumElements() &&
6466 "More vector elements requested than available!");
6467 SDValue Lo, Hi;
6468 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6469 getConstant(0, TLI->getVectorIdxTy()));
6470 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6471 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6472 return std::make_pair(Lo, Hi);
6473}
6474
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006475void SelectionDAG::ExtractVectorElements(SDValue Op,
6476 SmallVectorImpl<SDValue> &Args,
6477 unsigned Start, unsigned Count) {
6478 EVT VT = Op.getValueType();
6479 if (Count == 0)
6480 Count = VT.getVectorNumElements();
6481
6482 EVT EltVT = VT.getVectorElementType();
6483 EVT IdxTy = TLI->getVectorIdxTy();
6484 SDLoc SL(Op);
6485 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6486 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6487 Op, getConstant(i, IdxTy)));
6488 }
6489}
6490
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006491// getAddressSpace - Return the address space this GlobalAddress belongs to.
6492unsigned GlobalAddressSDNode::getAddressSpace() const {
6493 return getGlobal()->getType()->getAddressSpace();
6494}
6495
6496
Chris Lattner229907c2011-07-18 04:54:35 +00006497Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006498 if (isMachineConstantPoolEntry())
6499 return Val.MachineCPVal->getType();
6500 return Val.ConstVal->getType();
6501}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006502
Bob Wilson85cefe82009-03-02 23:24:16 +00006503bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6504 APInt &SplatUndef,
6505 unsigned &SplatBitSize,
6506 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006507 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006508 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006509 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006510 assert(VT.isVector() && "Expected a vector type");
6511 unsigned sz = VT.getSizeInBits();
6512 if (MinSplatBits > sz)
6513 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006514
Bob Wilson85cefe82009-03-02 23:24:16 +00006515 SplatValue = APInt(sz, 0);
6516 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006517
Bob Wilson85cefe82009-03-02 23:24:16 +00006518 // Get the bits. Bits with undefined values (when the corresponding element
6519 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6520 // in SplatValue. If any of the values are not constant, give up and return
6521 // false.
6522 unsigned int nOps = getNumOperands();
6523 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6524 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006525
6526 for (unsigned j = 0; j < nOps; ++j) {
6527 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006528 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006529 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006530
Bob Wilson85cefe82009-03-02 23:24:16 +00006531 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006532 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006533 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006534 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006535 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006536 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006537 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006538 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006539 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006540 }
6541
Bob Wilson85cefe82009-03-02 23:24:16 +00006542 // The build_vector is all constants or undefs. Find the smallest element
6543 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006544
Bob Wilson85cefe82009-03-02 23:24:16 +00006545 HasAnyUndefs = (SplatUndef != 0);
6546 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006547
Bob Wilson85cefe82009-03-02 23:24:16 +00006548 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006549 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6550 APInt LowValue = SplatValue.trunc(HalfSize);
6551 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6552 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006553
Bob Wilson85cefe82009-03-02 23:24:16 +00006554 // If the two halves do not match (ignoring undef bits), stop here.
6555 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6556 MinSplatBits > HalfSize)
6557 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006558
Bob Wilson85cefe82009-03-02 23:24:16 +00006559 SplatValue = HighValue | LowValue;
6560 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006561
Bob Wilson85cefe82009-03-02 23:24:16 +00006562 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006563 }
6564
Bob Wilson85cefe82009-03-02 23:24:16 +00006565 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006566 return true;
6567}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006568
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006569ConstantSDNode *BuildVectorSDNode::getConstantSplatValue() const {
Matt Arsenault985b9de2014-03-17 18:58:01 +00006570 SDValue Op0 = getOperand(0);
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006571 if (Op0.getOpcode() != ISD::Constant)
6572 return nullptr;
6573
6574 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
6575 if (getOperand(i) != Op0)
Matt Arsenault985b9de2014-03-17 18:58:01 +00006576 return nullptr;
Matt Arsenault985b9de2014-03-17 18:58:01 +00006577
6578 return cast<ConstantSDNode>(Op0);
6579}
6580
Juergen Ributzka73844052014-01-13 20:51:35 +00006581bool BuildVectorSDNode::isConstant() const {
6582 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6583 unsigned Opc = getOperand(i).getOpcode();
6584 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6585 return false;
6586 }
6587 return true;
6588}
6589
Owen Anderson53aa7a92009-08-10 22:56:29 +00006590bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006591 // Find the first non-undef value in the shuffle mask.
6592 unsigned i, e;
6593 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6594 /* search */;
6595
Nate Begeman39b59db2009-04-29 18:13:31 +00006596 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006597
Nate Begeman5f829d82009-04-29 05:20:52 +00006598 // Make sure all remaining elements are either undef or the same as the first
6599 // non-undef value.
6600 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006601 if (Mask[i] >= 0 && Mask[i] != Idx)
6602 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006603 return true;
6604}
David Greene09851602010-01-20 20:13:31 +00006605
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006606#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006607static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006608 SmallPtrSet<const SDNode*, 32> &Visited,
6609 SmallPtrSet<const SDNode*, 32> &Checked) {
6610 // If this node has already been checked, don't check it again.
6611 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006612 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006613
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006614 // If a node has already been visited on this depth-first walk, reject it as
6615 // a cycle.
6616 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006617 dbgs() << "Offending node:\n";
6618 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006619 errs() << "Detected cycle in SelectionDAG\n";
6620 abort();
David Greene09851602010-01-20 20:13:31 +00006621 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006622
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006623 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6624 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006625
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006626 Checked.insert(N);
6627 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006628}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006629#endif
David Greene09851602010-01-20 20:13:31 +00006630
6631void llvm::checkForCycles(const llvm::SDNode *N) {
6632#ifdef XDEBUG
Alp Toker6a033742013-10-29 02:35:28 +00006633 assert(N && "Checking nonexistent SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006634 SmallPtrSet<const SDNode*, 32> visited;
6635 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006636 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006637#endif
6638}
6639
6640void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6641 checkForCycles(DAG->getRoot().getNode());
6642}