blob: 5f43d818dc7a62d968b6014b5e07b88bd6b056d2 [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,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000367 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf17b4222007-02-04 07:28:00 +0000368 for (; NumOps; --NumOps, ++Ops) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000369 ID.AddPointer(Ops->getNode());
Gabor Greifabfdf922008-08-26 22:36:50 +0000370 ID.AddInteger(Ops->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,
377 const SDUse *Ops, unsigned NumOps) {
378 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000379 ID.AddPointer(Ops->getNode());
380 ID.AddInteger(Ops->getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000381 }
382}
383
Jim Laskeyf576b422006-10-27 23:46:08 +0000384static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelcf0da6c2009-02-17 22:15:04 +0000385 unsigned short OpC, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000386 const SDValue *OpList, unsigned N) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000387 AddNodeIDOpcode(ID, OpC);
388 AddNodeIDValueTypes(ID, VTList);
389 AddNodeIDOperands(ID, OpList, N);
390}
391
Duncan Sands835bdca2008-10-27 15:30:53 +0000392/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
393/// the NodeID data.
394static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000395 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000396 case ISD::TargetExternalSymbol:
397 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000398 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000399 default: break; // Normal nodes don't need extra info.
400 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000401 case ISD::Constant: {
402 const ConstantSDNode *C = cast<ConstantSDNode>(N);
403 ID.AddPointer(C->getConstantIntValue());
404 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000405 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000406 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000407 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000408 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000409 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000410 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000411 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000412 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000413 case ISD::GlobalAddress:
414 case ISD::TargetGlobalTLSAddress:
415 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000416 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000417 ID.AddPointer(GA->getGlobal());
418 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000419 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000420 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000421 break;
422 }
423 case ISD::BasicBlock:
424 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
425 break;
426 case ISD::Register:
427 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
428 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000429 case ISD::RegisterMask:
430 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
431 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000432 case ISD::SRCVALUE:
433 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
434 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000435 case ISD::FrameIndex:
436 case ISD::TargetFrameIndex:
437 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
438 break;
439 case ISD::JumpTable:
440 case ISD::TargetJumpTable:
441 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000442 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000443 break;
444 case ISD::ConstantPool:
445 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000446 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000447 ID.AddInteger(CP->getAlignment());
448 ID.AddInteger(CP->getOffset());
449 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000450 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000451 else
452 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000453 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000454 break;
455 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000456 case ISD::TargetIndex: {
457 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
458 ID.AddInteger(TI->getIndex());
459 ID.AddInteger(TI->getOffset());
460 ID.AddInteger(TI->getTargetFlags());
461 break;
462 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000463 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000464 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000465 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000466 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000467 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000468 break;
469 }
470 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000471 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000472 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000473 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000474 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000475 break;
476 }
Dan Gohman12f24902008-12-23 21:37:04 +0000477 case ISD::ATOMIC_CMP_SWAP:
478 case ISD::ATOMIC_SWAP:
479 case ISD::ATOMIC_LOAD_ADD:
480 case ISD::ATOMIC_LOAD_SUB:
481 case ISD::ATOMIC_LOAD_AND:
482 case ISD::ATOMIC_LOAD_OR:
483 case ISD::ATOMIC_LOAD_XOR:
484 case ISD::ATOMIC_LOAD_NAND:
485 case ISD::ATOMIC_LOAD_MIN:
486 case ISD::ATOMIC_LOAD_MAX:
487 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000488 case ISD::ATOMIC_LOAD_UMAX:
489 case ISD::ATOMIC_LOAD:
490 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000491 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000492 ID.AddInteger(AT->getMemoryVT().getRawBits());
493 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000494 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
495 break;
496 }
497 case ISD::PREFETCH: {
498 const MemSDNode *PF = cast<MemSDNode>(N);
499 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000500 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000501 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000502 case ISD::VECTOR_SHUFFLE: {
503 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000504 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000505 i != e; ++i)
506 ID.AddInteger(SVN->getMaskElt(i));
507 break;
508 }
Dan Gohman6c938802009-10-30 01:27:03 +0000509 case ISD::TargetBlockAddress:
510 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000511 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
512 ID.AddPointer(BA->getBlockAddress());
513 ID.AddInteger(BA->getOffset());
514 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000515 break;
516 }
Mon P Wang6a490372008-06-25 08:15:39 +0000517 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000518
519 // Target specific memory nodes could also have address spaces to check.
520 if (N->isTargetMemoryOpcode())
521 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000522}
523
Duncan Sands835bdca2008-10-27 15:30:53 +0000524/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
525/// data.
526static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
527 AddNodeIDOpcode(ID, N->getOpcode());
528 // Add the return value info.
529 AddNodeIDValueTypes(ID, N->getVTList());
530 // Add the operand info.
531 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
532
533 // Handle SDNode leafs with special info.
534 AddNodeIDCustom(ID, N);
535}
536
Dan Gohman2da2bed2008-08-20 15:58:01 +0000537/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000538/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000539/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000540///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000541static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000542encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000543 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000544 assert((ConvType & 3) == ConvType &&
545 "ConvType may not require more than 2 bits!");
546 assert((AM & 7) == AM &&
547 "AM may not require more than 3 bits!");
548 return ConvType |
549 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000550 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000551 (isNonTemporal << 6) |
552 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000553}
554
Jim Laskeyf576b422006-10-27 23:46:08 +0000555//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000556// SelectionDAG Class
557//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000558
Duncan Sands835bdca2008-10-27 15:30:53 +0000559/// doNotCSE - Return true if CSE should not be performed for this node.
560static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000561 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000562 return true; // Never CSE anything that produces a flag.
563
564 switch (N->getOpcode()) {
565 default: break;
566 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000567 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000568 return true; // Never CSE these nodes.
569 }
570
571 // Check that remaining values produced are not flags.
572 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000573 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000574 return true; // Never CSE anything that produces a flag.
575
576 return false;
577}
578
Chris Lattner9c667932005-01-07 21:09:16 +0000579/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000580/// SelectionDAG.
581void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000582 // Create a dummy node (which is not added to allnodes), that adds a reference
583 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000584 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000585
Chris Lattner8927c872006-08-04 17:45:20 +0000586 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000587
Chris Lattner8927c872006-08-04 17:45:20 +0000588 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000589 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000590 if (I->use_empty())
591 DeadNodes.push_back(I);
592
Dan Gohman91697632008-07-07 20:57:48 +0000593 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000594
Dan Gohman91697632008-07-07 20:57:48 +0000595 // If the root changed (e.g. it was a dead load, update the root).
596 setRoot(Dummy.getValue());
597}
598
599/// RemoveDeadNodes - This method deletes the unreachable nodes in the
600/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000601void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000602
Chris Lattner8927c872006-08-04 17:45:20 +0000603 // Process the worklist, deleting the nodes and adding their uses to the
604 // worklist.
605 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000606 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000607
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000608 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000609 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000610
Chris Lattner8927c872006-08-04 17:45:20 +0000611 // Take the node out of the appropriate CSE map.
612 RemoveNodeFromCSEMaps(N);
613
614 // Next, brutally remove the operand list. This is safe to do, as there are
615 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000616 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
617 SDUse &Use = *I++;
618 SDNode *Operand = Use.getNode();
619 Use.set(SDValue());
620
Chris Lattner8927c872006-08-04 17:45:20 +0000621 // Now that we removed this operand, see if there are no uses of it left.
622 if (Operand->use_empty())
623 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000624 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000625
Dan Gohman534c8a22009-01-19 22:39:36 +0000626 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000627 }
Chris Lattner9c667932005-01-07 21:09:16 +0000628}
629
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000630void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000631 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000632
633 // Create a dummy node that adds a reference to the root node, preventing
634 // it from being deleted. (This matters if the root is an operand of the
635 // dead node.)
636 HandleSDNode Dummy(getRoot());
637
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000638 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000639}
640
Chris Lattnerc738d002005-08-29 21:59:31 +0000641void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000642 // First take this out of the appropriate CSE map.
643 RemoveNodeFromCSEMaps(N);
644
Scott Michelcf0da6c2009-02-17 22:15:04 +0000645 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000646 // AllNodes list, and delete the node.
647 DeleteNodeNotInCSEMaps(N);
648}
649
650void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000651 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
652 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000653
Dan Gohman86aa16a2008-09-30 18:30:35 +0000654 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000655 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000656
Dan Gohman534c8a22009-01-19 22:39:36 +0000657 DeallocateNode(N);
658}
659
660void SelectionDAG::DeallocateNode(SDNode *N) {
661 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000662 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000663
Dan Gohman534c8a22009-01-19 22:39:36 +0000664 // Set the opcode to DELETED_NODE to help catch bugs when node
665 // memory is reallocated.
666 N->NodeType = ISD::DELETED_NODE;
667
Dan Gohman2e834902008-08-26 01:44:34 +0000668 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000669
Evan Cheng563fe3c2010-03-25 01:38:16 +0000670 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000671 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000672 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
673 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000674}
675
Chris Lattner19732782005-08-16 18:17:10 +0000676/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
677/// correspond to it. This is useful when we're about to delete or repurpose
678/// the node. We don't want future request for structurally identical nodes
679/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000680bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000681 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000682 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000683 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000684 case ISD::CONDCODE:
685 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
686 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000687 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
688 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000689 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000690 case ISD::ExternalSymbol:
691 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000692 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000693 case ISD::TargetExternalSymbol: {
694 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
695 Erased = TargetExternalSymbols.erase(
696 std::pair<std::string,unsigned char>(ESN->getSymbol(),
697 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000698 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000699 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000700 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000701 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000702 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000703 Erased = ExtendedValueTypeNodes.erase(VT);
704 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000705 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
706 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000707 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000708 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000709 }
Chris Lattner9c667932005-01-07 21:09:16 +0000710 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000711 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000712 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
713 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000714 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000715 break;
716 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000717#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000718 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000719 // flag result (which cannot be CSE'd) or is one of the special cases that are
720 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000721 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000722 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000723 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000724 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000725 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000726 }
727#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000728 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000729}
730
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000731/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
732/// maps and modified in place. Add it back to the CSE maps, unless an identical
733/// node already exists, in which case transfer all its users to the existing
734/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000735///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000736void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000737SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000738 // For node types that aren't CSE'd, just act as if no identical node
739 // already exists.
740 if (!doNotCSE(N)) {
741 SDNode *Existing = CSEMap.GetOrInsertNode(N);
742 if (Existing != N) {
743 // If there was already an existing matching node, use ReplaceAllUsesWith
744 // to replace the dead one with the existing one. This can cause
745 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000746 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000747
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000748 // N is now dead. Inform the listeners and delete it.
749 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
750 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000751 DeleteNodeNotInCSEMaps(N);
752 return;
753 }
754 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000755
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000756 // If the node doesn't already exist, we updated it. Inform listeners.
757 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
758 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000759}
760
Chris Lattnerf34156e2006-01-28 09:32:45 +0000761/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000762/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000763/// return null, otherwise return a pointer to the slot it would take. If a
764/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000765SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000766 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000767 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000768 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000769
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000770 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000771 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000772 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands835bdca2008-10-27 15:30:53 +0000773 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000774 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000775 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000776}
777
778/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000779/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000780/// return null, otherwise return a pointer to the slot it would take. If a
781/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000782SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000783 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000784 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000785 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000786 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000787
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000788 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000789 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000790 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands835bdca2008-10-27 15:30:53 +0000791 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000792 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000793 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000794}
795
796
797/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000798/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000799/// return null, otherwise return a pointer to the slot it would take. If a
800/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000801SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000802 const SDValue *Ops,unsigned NumOps,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000803 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000804 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000805 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000806
Jim Laskeyf576b422006-10-27 23:46:08 +0000807 FoldingSetNodeID ID;
Dan Gohman92a7f3a2007-06-04 15:49:41 +0000808 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands835bdca2008-10-27 15:30:53 +0000809 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000810 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000811 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000812}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000813
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000814#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000815/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
816static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsb0e39382008-07-21 10:20:31 +0000817 switch (N->getOpcode()) {
818 default:
819 break;
Duncan Sands17e678b2008-10-29 14:22:20 +0000820 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000821 EVT VT = N->getValueType(0);
Duncan Sands17e678b2008-10-29 14:22:20 +0000822 assert(N->getNumValues() == 1 && "Too many results!");
823 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
824 "Wrong return type!");
825 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
826 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
827 "Mismatched operand types!");
828 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
829 "Wrong operand type!");
830 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
831 "Wrong return type size");
832 break;
833 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000834 case ISD::BUILD_VECTOR: {
Duncan Sands17e678b2008-10-29 14:22:20 +0000835 assert(N->getNumValues() == 1 && "Too many results!");
836 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsb0e39382008-07-21 10:20:31 +0000837 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands17e678b2008-10-29 14:22:20 +0000838 "Wrong number of operands!");
Owen Anderson53aa7a92009-08-10 22:56:29 +0000839 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedmanb7910b72011-09-09 21:04:06 +0000840 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindolab93db662009-04-24 12:40:33 +0000841 assert((I->getValueType() == EltVT ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000842 (EltVT.isInteger() && I->getValueType().isInteger() &&
843 EltVT.bitsLE(I->getValueType()))) &&
844 "Wrong operand type!");
Eli Friedmanb7910b72011-09-09 21:04:06 +0000845 assert(I->getValueType() == N->getOperand(0).getValueType() &&
846 "Operands must all have the same type");
847 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000848 break;
849 }
850 }
851}
852
Duncan Sands7c601de2010-11-20 11:25:00 +0000853/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
854static void VerifySDNode(SDNode *N) {
855 // The SDNode allocators cannot be used to allocate nodes with fields that are
856 // not present in an SDNode!
857 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
858 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
859 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
860 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
861 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
862 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
863 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
864 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
865 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
866 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
867 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
868 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
869 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
870 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
871 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
872 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
873 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
874 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
875 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
876
877 VerifyNodeCommon(N);
878}
879
880/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
881/// invalid.
882static void VerifyMachineNode(SDNode *N) {
883 // The MachineNode allocators cannot be used to allocate nodes with fields
884 // that are not present in a MachineNode!
885 // Currently there are no such nodes.
886
887 VerifyNodeCommon(N);
888}
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000889#endif // NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000890
Owen Anderson53aa7a92009-08-10 22:56:29 +0000891/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000892/// given type.
893///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000894unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000895 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000896 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000897 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000898
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000899 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000900}
Chris Lattner9c667932005-01-07 21:09:16 +0000901
Dale Johannesen8ba713212009-02-07 02:15:05 +0000902// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000903SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Craig Topperc0196b12014-04-14 00:51:57 +0000904 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TLI(nullptr), OptLevel(OL),
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000905 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sanders50b80412013-11-15 12:56:49 +0000906 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
Craig Topperc0196b12014-04-14 00:51:57 +0000907 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000908 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000909 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000910}
911
Juergen Ributzka659ce002014-01-28 01:20:14 +0000912void SelectionDAG::init(MachineFunction &mf, const TargetLowering *tli) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000913 MF = &mf;
Juergen Ributzkab3487102013-11-19 21:20:17 +0000914 TLI = tli;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000915 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000916}
917
Chris Lattner600d3082003-08-11 14:57:33 +0000918SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000919 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000920 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000921 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000922}
923
924void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000925 assert(&*AllNodes.begin() == &EntryNode);
926 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000927 while (!AllNodes.empty())
928 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000929}
930
Dan Gohmane1a9a782008-08-27 23:52:12 +0000931void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000932 allnodes_clear();
933 OperandAllocator.Reset();
934 CSEMap.clear();
935
936 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000937 ExternalSymbols.clear();
938 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000939 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000940 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000941 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000942 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000943
Craig Topperc0196b12014-04-14 00:51:57 +0000944 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000945 AllNodes.push_back(&EntryNode);
946 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000947 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000948}
949
Andrew Trickef9de2a2013-05-25 02:42:55 +0000950SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000951 return VT.bitsGT(Op.getValueType()) ?
952 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
953 getNode(ISD::TRUNCATE, DL, VT, Op);
954}
955
Andrew Trickef9de2a2013-05-25 02:42:55 +0000956SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000957 return VT.bitsGT(Op.getValueType()) ?
958 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
959 getNode(ISD::TRUNCATE, DL, VT, Op);
960}
961
Andrew Trickef9de2a2013-05-25 02:42:55 +0000962SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000963 return VT.bitsGT(Op.getValueType()) ?
964 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
965 getNode(ISD::TRUNCATE, DL, VT, Op);
966}
967
Andrew Trickef9de2a2013-05-25 02:42:55 +0000968SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000969 assert(!VT.isVector() &&
970 "getZeroExtendInReg should use the vector element type instead of "
971 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000972 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000973 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
974 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000975 VT.getSizeInBits());
976 return getNode(ISD::AND, DL, Op.getValueType(), Op,
977 getConstant(Imm, Op.getValueType()));
978}
979
Bob Wilsonc5890052009-01-22 17:39:32 +0000980/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
981///
Andrew Trickef9de2a2013-05-25 02:42:55 +0000982SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000983 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +0000984 SDValue NegOne =
985 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +0000986 return getNode(ISD::XOR, DL, VT, Val, NegOne);
987}
988
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000989SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000990 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +0000991 assert((EltVT.getSizeInBits() >= 64 ||
992 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
993 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000994 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +0000995}
996
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000997SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
998{
999 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001000}
1001
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001002SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1003 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001004 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001005
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001006 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001007 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001008
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001009 const TargetLowering *TLI = TM.getTargetLowering();
1010
Duncan Sandsf2641e12011-09-06 19:07:46 +00001011 // In some cases the vector type is legal but the element type is illegal and
1012 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1013 // inserted value (the type does not need to match the vector element type).
1014 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001015 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001016 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001017 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001018 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1019 Elt = ConstantInt::get(*getContext(), NewVal);
1020 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001021 // In other cases the element type is illegal and needs to be expanded, for
1022 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1023 // the value into n parts and use a vector type with n-times the elements.
1024 // Then bitcast to the type requested.
1025 // Legalizing constants too early makes the DAGCombiner's job harder so we
1026 // only legalize if the DAG tells us we must produce legal types.
1027 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1028 TLI->getTypeAction(*getContext(), EltVT) ==
1029 TargetLowering::TypeExpandInteger) {
1030 APInt NewVal = Elt->getValue();
1031 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1032 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1033 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1034 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1035
1036 // Check the temporary vector is the correct size. If this fails then
1037 // getTypeToTransformTo() probably returned a type whose size (in bits)
1038 // isn't a power-of-2 factor of the requested type size.
1039 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1040
1041 SmallVector<SDValue, 2> EltParts;
1042 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1043 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1044 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001045 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001046 }
1047
1048 // EltParts is currently in little endian order. If we actually want
1049 // big-endian order then reverse it now.
1050 if (TLI->isBigEndian())
1051 std::reverse(EltParts.begin(), EltParts.end());
1052
1053 // The elements must be reversed when the element order is different
1054 // to the endianness of the elements (because the BITCAST is itself a
1055 // vector shuffle in this situation). However, we do not need any code to
1056 // perform this reversal because getConstant() is producing a vector
1057 // splat.
1058 // This situation occurs in MIPS MSA.
1059
1060 SmallVector<SDValue, 8> Ops;
1061 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1062 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1063
1064 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1065 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
1066 &Ops[0], Ops.size()));
1067 return Result;
1068 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001069
1070 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1071 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001072 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001073 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001074 AddNodeIDNode(ID, Opc, getVTList(EltVT), nullptr, 0);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001075 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001076 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001077 void *IP = nullptr;
1078 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001079 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001080 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001081 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001082
Dan Gohman7a7742c2007-12-12 22:21:26 +00001083 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001084 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001085 CSEMap.InsertNode(N, IP);
1086 AllNodes.push_back(N);
1087 }
1088
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001089 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001090 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001091 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001092 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001093 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7a7742c2007-12-12 22:21:26 +00001094 }
1095 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001096}
1097
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001098SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001099 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001100}
1101
1102
Owen Anderson53aa7a92009-08-10 22:56:29 +00001103SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001104 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001105}
1106
Owen Anderson53aa7a92009-08-10 22:56:29 +00001107SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001108 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001109
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001110 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001111
Chris Lattner381dddc2005-02-17 20:17:32 +00001112 // Do the map lookup using the actual bit pattern for the floating point
1113 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1114 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001115 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001116 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001117 AddNodeIDNode(ID, Opc, getVTList(EltVT), nullptr, 0);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001118 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001119 void *IP = nullptr;
1120 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001121 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001122 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001123 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001124
Evan Cheng9458e6a2007-06-29 21:36:04 +00001125 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001126 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001127 CSEMap.InsertNode(N, IP);
1128 AllNodes.push_back(N);
1129 }
1130
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001131 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001132 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001133 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001134 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001135 // FIXME SDLoc info might be appropriate here
1136 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohmana8665142007-06-25 16:23:39 +00001137 }
1138 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001139}
1140
Owen Anderson53aa7a92009-08-10 22:56:29 +00001141SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001142 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001143 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001144 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001145 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001146 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001147 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1148 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001149 bool ignored;
1150 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001151 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001152 &ignored);
1153 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001154 } else
1155 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001156}
1157
Andrew Trickef9de2a2013-05-25 02:42:55 +00001158SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001159 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001160 bool isTargetGA,
1161 unsigned char TargetFlags) {
1162 assert((TargetFlags == 0 || isTargetGA) &&
1163 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001164 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001165
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001166 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001167 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001168 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001169 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001170
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001171 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1172 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001173 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001174 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Rafael Espindola24a669d2014-03-27 15:26:56 +00001175 GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasedGlobal());
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001176 }
1177
Chris Lattner8e34f982009-06-25 21:21:14 +00001178 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001179 if (GVar && GVar->isThreadLocal())
1180 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1181 else
1182 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001183
Jim Laskeyf576b422006-10-27 23:46:08 +00001184 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001185 AddNodeIDNode(ID, Opc, getVTList(VT), nullptr, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001186 ID.AddPointer(GV);
1187 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001188 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001189 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001190 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001191 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001192 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001193
Andrew Trickef9de2a2013-05-25 02:42:55 +00001194 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1195 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001196 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001197 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001198 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001199 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001200}
1201
Owen Anderson53aa7a92009-08-10 22:56:29 +00001202SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001203 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001204 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001205 AddNodeIDNode(ID, Opc, getVTList(VT), nullptr, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001206 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001207 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001208 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001209 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001210
Dan Gohman01c65a22010-03-18 18:49:47 +00001211 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001212 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001213 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001214 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001215}
1216
Owen Anderson53aa7a92009-08-10 22:56:29 +00001217SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001218 unsigned char TargetFlags) {
1219 assert((TargetFlags == 0 || isTarget) &&
1220 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001221 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001222 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001223 AddNodeIDNode(ID, Opc, getVTList(VT), nullptr, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001224 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001225 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001226 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001227 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001228 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001229
Dan Gohman01c65a22010-03-18 18:49:47 +00001230 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1231 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001232 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001233 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001234 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001235}
1236
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001237SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001239 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001240 unsigned char TargetFlags) {
1241 assert((TargetFlags == 0 || isTarget) &&
1242 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001243 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001244 Alignment =
1245 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001246 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001247 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001248 AddNodeIDNode(ID, Opc, getVTList(VT), nullptr, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001249 ID.AddInteger(Alignment);
1250 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001251 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001252 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001253 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001254 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001255 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001256
Dan Gohman01c65a22010-03-18 18:49:47 +00001257 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1258 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001259 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001260 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001261 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001262}
1263
Chris Lattner061a1ea2005-01-07 07:46:32 +00001264
Owen Anderson53aa7a92009-08-10 22:56:29 +00001265SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001266 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001267 bool isTarget,
1268 unsigned char TargetFlags) {
1269 assert((TargetFlags == 0 || isTarget) &&
1270 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001271 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001272 Alignment =
1273 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001274 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001275 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001276 AddNodeIDNode(ID, Opc, getVTList(VT), nullptr, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001277 ID.AddInteger(Alignment);
1278 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001279 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001280 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001281 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001282 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001283 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001284
Dan Gohman01c65a22010-03-18 18:49:47 +00001285 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1286 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001287 CSEMap.InsertNode(N, IP);
1288 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001289 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001290}
1291
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001292SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1293 unsigned char TargetFlags) {
1294 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001295 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), nullptr, 0);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001296 ID.AddInteger(Index);
1297 ID.AddInteger(Offset);
1298 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001299 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001300 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1301 return SDValue(E, 0);
1302
1303 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1304 TargetFlags);
1305 CSEMap.InsertNode(N, IP);
1306 AllNodes.push_back(N);
1307 return SDValue(N, 0);
1308}
1309
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001310SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001311 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001312 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), nullptr, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001313 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001314 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001315 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001316 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001317
Dan Gohman01c65a22010-03-18 18:49:47 +00001318 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001319 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001320 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001321 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001322}
1323
Owen Anderson53aa7a92009-08-10 22:56:29 +00001324SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001325 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1326 ValueTypeNodes.size())
1327 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001328
Duncan Sands13237ac2008-06-06 12:08:01 +00001329 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001330 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001331
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001332 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001333 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001334 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001335 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001336}
1337
Owen Anderson53aa7a92009-08-10 22:56:29 +00001338SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001339 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001340 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001341 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001342 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001343 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001344}
1345
Owen Anderson53aa7a92009-08-10 22:56:29 +00001346SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001347 unsigned char TargetFlags) {
1348 SDNode *&N =
1349 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1350 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001351 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001352 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001353 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001354 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001355}
1356
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001358 if ((unsigned)Cond >= CondCodeNodes.size())
1359 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001360
Craig Topperc0196b12014-04-14 00:51:57 +00001361 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001362 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001363 CondCodeNodes[Cond] = N;
1364 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001365 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001366
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001367 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001368}
1369
Nate Begeman5f829d82009-04-29 05:20:52 +00001370// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1371// the shuffle mask M that point at N1 to point at N2, and indices that point
1372// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001373static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1374 std::swap(N1, N2);
1375 int NElts = M.size();
1376 for (int i = 0; i != NElts; ++i) {
1377 if (M[i] >= NElts)
1378 M[i] -= NElts;
1379 else if (M[i] >= 0)
1380 M[i] += NElts;
1381 }
1382}
1383
Andrew Trickef9de2a2013-05-25 02:42:55 +00001384SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001385 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001386 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1387 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001388
1389 // Canonicalize shuffle undef, undef -> undef
1390 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001391 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001392
Eric Christopherdfda92b2009-08-22 00:40:45 +00001393 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001394 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001395 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001396 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001397 for (unsigned i = 0; i != NElts; ++i) {
1398 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001399 MaskVec.push_back(Mask[i]);
1400 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001401
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001402 // Canonicalize shuffle v, v -> v, undef
1403 if (N1 == N2) {
1404 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001405 for (unsigned i = 0; i != NElts; ++i)
1406 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001407 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001408
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001409 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1410 if (N1.getOpcode() == ISD::UNDEF)
1411 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001412
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001413 // Canonicalize all index into lhs, -> shuffle lhs, undef
1414 // Canonicalize all index into rhs, -> shuffle rhs, undef
1415 bool AllLHS = true, AllRHS = true;
1416 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001417 for (unsigned i = 0; i != NElts; ++i) {
1418 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001419 if (N2Undef)
1420 MaskVec[i] = -1;
1421 else
1422 AllLHS = false;
1423 } else if (MaskVec[i] >= 0) {
1424 AllRHS = false;
1425 }
1426 }
1427 if (AllLHS && AllRHS)
1428 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001429 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001430 N2 = getUNDEF(VT);
1431 if (AllRHS) {
1432 N1 = getUNDEF(VT);
1433 commuteShuffle(N1, N2, MaskVec);
1434 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001435
Craig Topper9a39b072013-08-08 08:03:12 +00001436 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001437 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001438 for (unsigned i = 0; i != NElts; ++i) {
1439 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001440 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001441 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001442 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001443
1444 FoldingSetNodeID ID;
1445 SDValue Ops[2] = { N1, N2 };
1446 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5f829d82009-04-29 05:20:52 +00001447 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001448 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001449
Craig Topperc0196b12014-04-14 00:51:57 +00001450 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001451 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001452 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001453
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001454 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1455 // SDNode doesn't have access to it. This memory will be "leaked" when
1456 // the node is deallocated, but recovered when the NodeAllocator is released.
1457 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1458 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001459
Dan Gohman01c65a22010-03-18 18:49:47 +00001460 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001461 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1462 dl.getDebugLoc(), N1, N2,
1463 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001464 CSEMap.InsertNode(N, IP);
1465 AllNodes.push_back(N);
1466 return SDValue(N, 0);
1467}
1468
Andrew Trickef9de2a2013-05-25 02:42:55 +00001469SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001470 SDValue Val, SDValue DTy,
1471 SDValue STy, SDValue Rnd, SDValue Sat,
1472 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001473 // If the src and dest types are the same and the conversion is between
1474 // integer types of the same sign or two floats, no conversion is necessary.
1475 if (DTy == STy &&
1476 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001477 return Val;
1478
1479 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001480 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1481 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Craig Topperc0196b12014-04-14 00:51:57 +00001482 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001483 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001484 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001485
Jack Carter170a5f22013-09-09 22:02:08 +00001486 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1487 dl.getDebugLoc(),
1488 Ops, 5, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001489 CSEMap.InsertNode(N, IP);
1490 AllNodes.push_back(N);
1491 return SDValue(N, 0);
1492}
1493
Owen Anderson53aa7a92009-08-10 22:56:29 +00001494SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001495 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001496 AddNodeIDNode(ID, ISD::Register, getVTList(VT), nullptr, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001497 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001498 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001500 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001501
Dan Gohman01c65a22010-03-18 18:49:47 +00001502 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001503 CSEMap.InsertNode(N, IP);
1504 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001505 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001506}
1507
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001508SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1509 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001510 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), nullptr, 0);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001511 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001512 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1514 return SDValue(E, 0);
1515
1516 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1517 CSEMap.InsertNode(N, IP);
1518 AllNodes.push_back(N);
1519 return SDValue(N, 0);
1520}
1521
Andrew Trickef9de2a2013-05-25 02:42:55 +00001522SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001523 FoldingSetNodeID ID;
1524 SDValue Ops[] = { Root };
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001525 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1526 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001527 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001529 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001530
Jack Carter170a5f22013-09-09 22:02:08 +00001531 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1532 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001533 CSEMap.InsertNode(N, IP);
1534 AllNodes.push_back(N);
1535 return SDValue(N, 0);
1536}
1537
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001538
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001539SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001540 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001541 bool isTarget,
1542 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001543 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1544
1545 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001546 AddNodeIDNode(ID, Opc, getVTList(VT), nullptr, 0);
Dan Gohman6c938802009-10-30 01:27:03 +00001547 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001548 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001549 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001550 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001551 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001552 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001553
Michael Liaoabb87d42012-09-12 21:43:09 +00001554 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1555 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001556 CSEMap.InsertNode(N, IP);
1557 AllNodes.push_back(N);
1558 return SDValue(N, 0);
1559}
1560
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001561SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001562 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001563 "SrcValue is not a pointer?");
1564
Jim Laskeyf576b422006-10-27 23:46:08 +00001565 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001566 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), nullptr, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001567 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001568
Craig Topperc0196b12014-04-14 00:51:57 +00001569 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001570 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001571 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001572
Dan Gohman01c65a22010-03-18 18:49:47 +00001573 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001574 CSEMap.InsertNode(N, IP);
1575 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001576 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001577}
1578
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001579/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1580SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1581 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00001582 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), nullptr, 0);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001583 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001584
Craig Topperc0196b12014-04-14 00:51:57 +00001585 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001586 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1587 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001588
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001589 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1590 CSEMap.InsertNode(N, IP);
1591 AllNodes.push_back(N);
1592 return SDValue(N, 0);
1593}
1594
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001595/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1596SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1597 unsigned SrcAS, unsigned DestAS) {
1598 SDValue Ops[] = {Ptr};
1599 FoldingSetNodeID ID;
1600 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), &Ops[0], 1);
1601 ID.AddInteger(SrcAS);
1602 ID.AddInteger(DestAS);
1603
Craig Topperc0196b12014-04-14 00:51:57 +00001604 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001605 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1606 return SDValue(E, 0);
1607
1608 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1609 dl.getDebugLoc(),
1610 VT, Ptr, SrcAS, DestAS);
1611 CSEMap.InsertNode(N, IP);
1612 AllNodes.push_back(N);
1613 return SDValue(N, 0);
1614}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001615
Duncan Sands41826032009-01-31 15:50:11 +00001616/// getShiftAmountOperand - Return the specified value casted to
1617/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001618SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001619 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001620 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001621 if (OpTy == ShTy || OpTy.isVector()) return Op;
1622
1623 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001624 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001625}
1626
Chris Lattner9eb7a822007-10-15 17:47:20 +00001627/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1628/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001629SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001630 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001631 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001632 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001633 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001634 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001635 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001636
David Greene1fbe0542009-11-12 20:49:22 +00001637 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001638 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001639}
1640
Duncan Sands445071c2008-12-09 21:33:20 +00001641/// CreateStackTemporary - Create a stack temporary suitable for holding
1642/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001643SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001644 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1645 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001646 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1647 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001648 const TargetLowering *TLI = TM.getTargetLowering();
1649 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001650 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1651 TD->getPrefTypeAlignment(Ty2));
1652
1653 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001654 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001655 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001656}
1657
Owen Anderson53aa7a92009-08-10 22:56:29 +00001658SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001659 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001660 // These setcc operations always fold.
1661 switch (Cond) {
1662 default: break;
1663 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001664 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001665 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001666 case ISD::SETTRUE2: {
1667 const TargetLowering *TLI = TM.getTargetLowering();
1668 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1669 return getConstant(
1670 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1671 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001672
Chris Lattner393d96a2006-04-27 05:01:07 +00001673 case ISD::SETOEQ:
1674 case ISD::SETOGT:
1675 case ISD::SETOGE:
1676 case ISD::SETOLT:
1677 case ISD::SETOLE:
1678 case ISD::SETONE:
1679 case ISD::SETO:
1680 case ISD::SETUO:
1681 case ISD::SETUEQ:
1682 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001683 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001684 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001685 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001686
Gabor Greiff304a7a2008-08-28 21:40:38 +00001687 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001688 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001689 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001690 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001691
Chris Lattner061a1ea2005-01-07 07:46:32 +00001692 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001693 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001694 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1695 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001696 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1697 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1698 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1699 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1700 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1701 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1702 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1703 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001704 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001705 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001706 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001707 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1708 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001709 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001710 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001711 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001712 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001713 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001714 // fall through
1715 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001716 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001717 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001718 // fall through
1719 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001720 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001721 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001722 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001723 // fall through
1724 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001725 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001726 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001727 // fall through
1728 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001729 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001730 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001731 // fall through
1732 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001733 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001734 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001735 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001736 // fall through
1737 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001738 R==APFloat::cmpEqual, VT);
1739 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1740 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1741 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1742 R==APFloat::cmpEqual, VT);
1743 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1744 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1745 R==APFloat::cmpLessThan, VT);
1746 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1747 R==APFloat::cmpUnordered, VT);
1748 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1749 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001750 }
1751 } else {
1752 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001753 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1754 MVT CompVT = N1.getValueType().getSimpleVT();
1755 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1756 return SDValue();
1757
1758 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001759 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001760 }
1761
Chris Lattnerd47675e2005-08-09 20:20:18 +00001762 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001763 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001764}
1765
Dan Gohman1f372ed2008-02-25 21:11:39 +00001766/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1767/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001768bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001769 // This predicate is not safe for vector operations.
1770 if (Op.getValueType().isVector())
1771 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001772
Dan Gohman1d459e42009-12-11 21:31:27 +00001773 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001774 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1775}
1776
Dan Gohman309d3d52007-06-22 14:59:07 +00001777/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1778/// this predicate to simplify operations downstream. Mask is known to be zero
1779/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001780bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001781 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001782 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001783 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001784 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001785 return (KnownZero & Mask) == Mask;
1786}
1787
1788/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1789/// known to be either zero or one and return them in the KnownZero/KnownOne
1790/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1791/// processing.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001792void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1793 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001794 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001795 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001796
Dan Gohmanf990faf2008-02-13 00:35:47 +00001797 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001798 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001799 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001800
Dan Gohmanf990faf2008-02-13 00:35:47 +00001801 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001802
1803 switch (Op.getOpcode()) {
1804 case ISD::Constant:
1805 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001806 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1807 KnownZero = ~KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00001808 return;
1809 case ISD::AND:
1810 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001811 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1812 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001813 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1814 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001815
1816 // Output known-1 bits are only known if set in both the LHS & RHS.
1817 KnownOne &= KnownOne2;
1818 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1819 KnownZero |= KnownZero2;
1820 return;
1821 case ISD::OR:
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?");
1826
Dan Gohman309d3d52007-06-22 14:59:07 +00001827 // Output known-0 bits are only known if clear in both the LHS & RHS.
1828 KnownZero &= KnownZero2;
1829 // Output known-1 are known to be set if set in either the LHS | RHS.
1830 KnownOne |= KnownOne2;
1831 return;
1832 case ISD::XOR: {
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 known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001839 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001840 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1841 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1842 KnownZero = KnownZeroOut;
1843 return;
1844 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001845 case ISD::MUL: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001846 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1847 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001848 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1849 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1850
1851 // If low bits are zero in either operand, output low known-0 bits.
1852 // Also compute a conserative estimate for high known-0 bits.
1853 // More trickiness is possible, but this is sufficient for the
1854 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001855 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001856 unsigned TrailZ = KnownZero.countTrailingOnes() +
1857 KnownZero2.countTrailingOnes();
1858 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001859 KnownZero2.countLeadingOnes(),
1860 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001861
1862 TrailZ = std::min(TrailZ, BitWidth);
1863 LeadZ = std::min(LeadZ, BitWidth);
1864 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1865 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001866 return;
1867 }
1868 case ISD::UDIV: {
1869 // For the purposes of computing leading zeros we can conservatively
1870 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001871 // be less than the denominator.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001872 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001873 unsigned LeadZ = KnownZero2.countLeadingOnes();
1874
Jay Foad25a5e4c2010-12-01 08:53:58 +00001875 KnownOne2.clearAllBits();
1876 KnownZero2.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001877 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001878 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1879 if (RHSUnknownLeadingOnes != BitWidth)
1880 LeadZ = std::min(BitWidth,
1881 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001882
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001883 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001884 return;
1885 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001886 case ISD::SELECT:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001887 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1888 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001889 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1890 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1891
Dan Gohman309d3d52007-06-22 14:59:07 +00001892 // Only known if known in both the LHS and RHS.
1893 KnownOne &= KnownOne2;
1894 KnownZero &= KnownZero2;
1895 return;
1896 case ISD::SELECT_CC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001897 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1898 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001899 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1900 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1901
Dan Gohman309d3d52007-06-22 14:59:07 +00001902 // Only known if known in both the LHS and RHS.
1903 KnownOne &= KnownOne2;
1904 KnownZero &= KnownZero2;
1905 return;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001906 case ISD::SADDO:
1907 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001908 case ISD::SSUBO:
1909 case ISD::USUBO:
1910 case ISD::SMULO:
1911 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001912 if (Op.getResNo() != 1)
1913 return;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001914 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001915 case ISD::SETCC:
1916 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001917 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001918 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001919 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001920 return;
1921 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001922 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001923 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001924 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001925
1926 // If the shift count is an invalid immediate, don't do anything.
1927 if (ShAmt >= BitWidth)
1928 return;
1929
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001930 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001931 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman9db0aa82008-02-26 18:50:50 +00001932 KnownZero <<= ShAmt;
1933 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00001934 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00001935 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001936 }
1937 return;
1938 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001939 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001940 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001941 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001942
Dan Gohman9db0aa82008-02-26 18:50:50 +00001943 // If the shift count is an invalid immediate, don't do anything.
1944 if (ShAmt >= BitWidth)
1945 return;
1946
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001947 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001948 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001949 KnownZero = KnownZero.lshr(ShAmt);
1950 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001951
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001952 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001953 KnownZero |= HighBits; // High bits known zero.
1954 }
1955 return;
1956 case ISD::SRA:
1957 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001958 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001959
Dan Gohman9db0aa82008-02-26 18:50:50 +00001960 // If the shift count is an invalid immediate, don't do anything.
1961 if (ShAmt >= BitWidth)
1962 return;
1963
Dan Gohman309d3d52007-06-22 14:59:07 +00001964 // If any of the demanded bits are produced by the sign extension, we also
1965 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001966 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001967
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001968 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001969 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001970 KnownZero = KnownZero.lshr(ShAmt);
1971 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001972
Dan Gohman309d3d52007-06-22 14:59:07 +00001973 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001974 APInt SignBit = APInt::getSignBit(BitWidth);
1975 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001976
Dan Gohmanb717fda2008-02-20 16:30:17 +00001977 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001978 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001979 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001980 KnownOne |= HighBits; // New bits are known one.
1981 }
1982 }
1983 return;
1984 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001985 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001986 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001987
1988 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00001989 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001990 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00001991
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001992 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001993 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001994
Dan Gohman309d3d52007-06-22 14:59:07 +00001995 // If the sign extended bits are demanded, we know that the sign
1996 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00001997 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001998 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00001999 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002000
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002001 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2002 KnownOne &= InputDemandedBits;
2003 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002004 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
2005
Dan Gohman309d3d52007-06-22 14:59:07 +00002006 // If the sign bit of the input is known set or clear, then we know the
2007 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002008 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002009 KnownZero |= NewBits;
2010 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002011 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002012 KnownOne |= NewBits;
2013 KnownZero &= ~NewBits;
2014 } else { // Input sign bit unknown
2015 KnownZero &= ~NewBits;
2016 KnownOne &= ~NewBits;
2017 }
2018 return;
2019 }
2020 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002021 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002022 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002023 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002024 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002025 unsigned LowBits = Log2_32(BitWidth)+1;
2026 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002027 KnownOne.clearAllBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002028 return;
2029 }
2030 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002031 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002032 // If this is a ZEXTLoad and we are looking at the loaded value.
2033 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002034 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002035 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002036 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002037 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002038 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002039 }
2040 return;
2041 }
2042 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002043 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002044 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002045 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002046 KnownZero = KnownZero.trunc(InBits);
2047 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002048 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002049 KnownZero = KnownZero.zext(BitWidth);
2050 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002051 KnownZero |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002052 return;
2053 }
2054 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002055 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002056 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002057 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002058
Jay Foad583abbc2010-12-07 08:25:19 +00002059 KnownZero = KnownZero.trunc(InBits);
2060 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002061 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002062
2063 // Note if the sign bit is known to be zero or one.
2064 bool SignBitKnownZero = KnownZero.isNegative();
2065 bool SignBitKnownOne = KnownOne.isNegative();
2066 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2067 "Sign bit can't be known to be both zero and one!");
2068
Jay Foad583abbc2010-12-07 08:25:19 +00002069 KnownZero = KnownZero.zext(BitWidth);
2070 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002071
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002072 // If the sign bit is known zero or one, the top bits match.
2073 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002074 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002075 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002076 KnownOne |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002077 return;
2078 }
2079 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002080 EVT InVT = Op.getOperand(0).getValueType();
2081 unsigned InBits = InVT.getScalarType().getSizeInBits();
2082 KnownZero = KnownZero.trunc(InBits);
2083 KnownOne = KnownOne.trunc(InBits);
2084 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2085 KnownZero = KnownZero.zext(BitWidth);
2086 KnownOne = KnownOne.zext(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002087 return;
2088 }
2089 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002090 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002091 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002092 KnownZero = KnownZero.zext(InBits);
2093 KnownOne = KnownOne.zext(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002094 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002095 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +00002096 KnownZero = KnownZero.trunc(BitWidth);
2097 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002098 break;
2099 }
2100 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002101 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002102 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002103 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2104 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002105 KnownOne &= (~KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002106 return;
2107 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002108 case ISD::FGETSIGN:
2109 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002110 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerafc8f132007-12-22 21:26:52 +00002111 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002112
Dan Gohman72ec3f42008-04-28 17:02:21 +00002113 case ISD::SUB: {
2114 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2115 // We know that the top bits of C-X are clear if X contains less bits
2116 // than C (i.e. no wrap-around can happen). For example, 20-X is
2117 // positive if we can prove that X is >= 0 and < 16.
2118 if (CLHS->getAPIntValue().isNonNegative()) {
2119 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2120 // NLZ can't be BitWidth with no sign bit
2121 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002122 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002123
2124 // If all of the MaskV bits are known to be zero, then we know the
2125 // output top bits are zero, because we now know that the output is
2126 // from [0-C].
2127 if ((KnownZero2 & MaskV) == MaskV) {
2128 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2129 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002130 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002131 }
2132 }
2133 }
2134 }
2135 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002136 case ISD::ADD:
2137 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002138 // Output known-0 bits are known if clear or set in both the low clear bits
2139 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2140 // low 3 bits clear.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002141 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002142 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002143 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2144
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002145 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002146 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002147 KnownZeroOut = std::min(KnownZeroOut,
2148 KnownZero2.countTrailingOnes());
2149
Chris Lattner440b2802010-12-19 20:38:28 +00002150 if (Op.getOpcode() == ISD::ADD) {
2151 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2152 return;
2153 }
2154
2155 // With ADDE, a carry bit may be added in, so we can only use this
2156 // information if we know (at least) that the low two bits are clear. We
2157 // then return to the caller that the low bit is unknown but that other bits
2158 // are known zero.
2159 if (KnownZeroOut >= 2) // ADDE
2160 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohman309d3d52007-06-22 14:59:07 +00002161 return;
2162 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002163 case ISD::SREM:
2164 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002165 const APInt &RA = Rem->getAPIntValue().abs();
2166 if (RA.isPowerOf2()) {
2167 APInt LowBits = RA - 1;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002168 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002169
Duncan Sands33274982010-01-29 09:45:26 +00002170 // The low bits of the first operand are unchanged by the srem.
2171 KnownZero = KnownZero2 & LowBits;
2172 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002173
Duncan Sands33274982010-01-29 09:45:26 +00002174 // If the first operand is non-negative or has all low bits zero, then
2175 // the upper bits are all zero.
2176 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2177 KnownZero |= ~LowBits;
2178
2179 // If the first operand is negative and not all low bits are zero, then
2180 // the upper bits are all one.
2181 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2182 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002183 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002184 }
2185 }
2186 return;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002187 case ISD::UREM: {
2188 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002189 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002190 if (RA.isPowerOf2()) {
2191 APInt LowBits = (RA - 1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002192 KnownZero |= ~LowBits;
2193 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002194 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2195 break;
2196 }
2197 }
2198
2199 // Since the result is less than or equal to either operand, any leading
2200 // zero bits in either operand must also exist in the result.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002201 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2202 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002203
2204 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2205 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002206 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002207 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002208 return;
Dan Gohman309d3d52007-06-22 14:59:07 +00002209 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002210 case ISD::FrameIndex:
2211 case ISD::TargetFrameIndex:
2212 if (unsigned Align = InferPtrAlignment(Op)) {
2213 // The low bits are known zero if the pointer is aligned.
2214 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2215 return;
2216 }
2217 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002218
Dan Gohman309d3d52007-06-22 14:59:07 +00002219 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002220 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2221 break;
2222 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002223 case ISD::INTRINSIC_WO_CHAIN:
2224 case ISD::INTRINSIC_W_CHAIN:
2225 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002226 // Allow the target to implement this method for its nodes.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002227 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002228 return;
2229 }
2230}
2231
2232/// ComputeNumSignBits - Return the number of times the sign bit of the
2233/// register is replicated into the other bits. We know that at least 1 bit
2234/// is always equal to the sign bit (itself), but other cases can give us
2235/// information. For example, immediately after an "SRA X, 2", we know that
2236/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002237unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002238 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002239 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002240 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002241 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002242 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002243 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002244
Dan Gohman309d3d52007-06-22 14:59:07 +00002245 if (Depth == 6)
2246 return 1; // Limit search depth.
2247
2248 switch (Op.getOpcode()) {
2249 default: break;
2250 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002251 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002252 return VTBits-Tmp+1;
2253 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002254 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002255 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002256
Dan Gohman309d3d52007-06-22 14:59:07 +00002257 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002258 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002259 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002260 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002261
Dan Gohman309d3d52007-06-22 14:59:07 +00002262 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002263 Tmp =
2264 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002265 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002266
Dan Gohman309d3d52007-06-22 14:59:07 +00002267 case ISD::SIGN_EXTEND_INREG:
2268 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002269 Tmp =
2270 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002271 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002272
Dan Gohman309d3d52007-06-22 14:59:07 +00002273 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2274 return std::max(Tmp, Tmp2);
2275
2276 case ISD::SRA:
2277 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2278 // SRA X, C -> adds C sign bits.
2279 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002280 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002281 if (Tmp > VTBits) Tmp = VTBits;
2282 }
2283 return Tmp;
2284 case ISD::SHL:
2285 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2286 // shl destroys sign bits.
2287 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002288 if (C->getZExtValue() >= VTBits || // Bad shift.
2289 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2290 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002291 }
2292 break;
2293 case ISD::AND:
2294 case ISD::OR:
2295 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002296 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002297 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002298 if (Tmp != 1) {
2299 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2300 FirstAnswer = std::min(Tmp, Tmp2);
2301 // We computed what we know about the sign bits as our first
2302 // answer. Now proceed to the generic code that uses
2303 // ComputeMaskedBits, and pick whichever answer is better.
2304 }
2305 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002306
2307 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002308 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002309 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002310 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002311 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002312
2313 case ISD::SADDO:
2314 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002315 case ISD::SSUBO:
2316 case ISD::USUBO:
2317 case ISD::SMULO:
2318 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002319 if (Op.getResNo() != 1)
2320 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002321 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002322 case ISD::SETCC:
2323 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002324 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002325 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002326 return VTBits;
2327 break;
2328 case ISD::ROTL:
2329 case ISD::ROTR:
2330 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002331 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002332
Dan Gohman309d3d52007-06-22 14:59:07 +00002333 // Handle rotate right by N like a rotate left by 32-N.
2334 if (Op.getOpcode() == ISD::ROTR)
2335 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2336
2337 // If we aren't rotating out all of the known-in sign bits, return the
2338 // number that are left. This handles rotl(sext(x), 1) for example.
2339 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2340 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2341 }
2342 break;
2343 case ISD::ADD:
2344 // Add can have at most one carry bit. Thus we know that the output
2345 // is, at worst, one more bit than the inputs.
2346 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2347 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002348
Dan Gohman309d3d52007-06-22 14:59:07 +00002349 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002350 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002351 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002352 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002353 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002354
Dan Gohman309d3d52007-06-22 14:59:07 +00002355 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2356 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002357 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002358 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002359
Dan Gohman309d3d52007-06-22 14:59:07 +00002360 // If we are subtracting one from a positive number, there is no carry
2361 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002362 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002363 return Tmp;
2364 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002365
Dan Gohman309d3d52007-06-22 14:59:07 +00002366 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2367 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002368 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002369
Dan Gohman309d3d52007-06-22 14:59:07 +00002370 case ISD::SUB:
2371 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2372 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002373
Dan Gohman309d3d52007-06-22 14:59:07 +00002374 // Handle NEG.
2375 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002376 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002377 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002378 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002379 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2380 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002381 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002382 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002383
Dan Gohman309d3d52007-06-22 14:59:07 +00002384 // If the input is known to be positive (the sign bit is known clear),
2385 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002386 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002387 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002388
Dan Gohman309d3d52007-06-22 14:59:07 +00002389 // Otherwise, we treat this like a SUB.
2390 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002391
Dan Gohman309d3d52007-06-22 14:59:07 +00002392 // Sub can have at most one carry bit. Thus we know that the output
2393 // is, at worst, one more bit than the inputs.
2394 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2395 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002396 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002397 case ISD::TRUNCATE:
2398 // FIXME: it's tricky to do anything useful for this, but it is an important
2399 // case for targets like X86.
2400 break;
2401 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002402
Nadav Rotem4536d582013-03-20 22:53:44 +00002403 // If we are looking at the loaded value of the SDNode.
2404 if (Op.getResNo() == 0) {
2405 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2406 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2407 unsigned ExtType = LD->getExtensionType();
2408 switch (ExtType) {
2409 default: break;
2410 case ISD::SEXTLOAD: // '17' bits known
2411 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2412 return VTBits-Tmp+1;
2413 case ISD::ZEXTLOAD: // '16' bits known
2414 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2415 return VTBits-Tmp;
2416 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002417 }
2418 }
2419
2420 // Allow the target to implement this method for its nodes.
2421 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002422 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002423 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2424 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002425 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002426 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002427 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002428
Dan Gohman309d3d52007-06-22 14:59:07 +00002429 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2430 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002431 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002432 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002433
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002434 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002435 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002436 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002437 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002438 Mask = KnownOne;
2439 } else {
2440 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002441 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002442 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002443
Dan Gohman309d3d52007-06-22 14:59:07 +00002444 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2445 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002446 Mask = ~Mask;
2447 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002448 // Return # leading zeros. We use 'min' here in case Val was zero before
2449 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002450 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002451}
2452
Chris Lattner46c01a32011-02-13 22:25:43 +00002453/// isBaseWithConstantOffset - Return true if the specified operand is an
2454/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2455/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2456/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002457/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002458bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2459 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2460 !isa<ConstantSDNode>(Op.getOperand(1)))
2461 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002462
2463 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002464 !MaskedValueIsZero(Op.getOperand(0),
2465 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2466 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002467
Chris Lattner46c01a32011-02-13 22:25:43 +00002468 return true;
2469}
2470
2471
Dan Gohmand0d5e682009-09-03 20:34:31 +00002472bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2473 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002474 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002475 return true;
2476
2477 // If the value is a constant, we can obviously see if it is a NaN or not.
2478 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2479 return !C->getValueAPF().isNaN();
2480
2481 // TODO: Recognize more cases here.
2482
2483 return false;
2484}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002485
Dan Gohman38605212010-02-24 06:52:40 +00002486bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2487 // If the value is a constant, we can obviously see if it is a zero or not.
2488 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2489 return !C->isZero();
2490
2491 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002492 switch (Op.getOpcode()) {
2493 default: break;
2494 case ISD::OR:
2495 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2496 return !C->isNullValue();
2497 break;
2498 }
Dan Gohman38605212010-02-24 06:52:40 +00002499
2500 return false;
2501}
2502
2503bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2504 // Check the obvious case.
2505 if (A == B) return true;
2506
2507 // For for negative and positive zero.
2508 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2509 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2510 if (CA->isZero() && CB->isZero()) return true;
2511
2512 // Otherwise they may not be equal.
2513 return false;
2514}
2515
Chris Lattner061a1ea2005-01-07 07:46:32 +00002516/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002517///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002518SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002519 FoldingSetNodeID ID;
Craig Topperc0196b12014-04-14 00:51:57 +00002520 AddNodeIDNode(ID, Opcode, getVTList(VT), nullptr, 0);
2521 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002522 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002523 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002524
Jack Carter170a5f22013-09-09 22:02:08 +00002525 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2526 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002527 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002528
Chris Lattnerfcb16472006-08-11 18:38:11 +00002529 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002530#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002531 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002532#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002533 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002534}
2535
Andrew Trickef9de2a2013-05-25 02:42:55 +00002536SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002537 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002538 // Constant fold unary operations with an integer constant operand. Even
2539 // opaque constant will be folded, because the folding of unary operations
2540 // doesn't create new constants with different values. Nevertheless, the
2541 // opaque flag is preserved during folding to prevent future folding with
2542 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002543 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002544 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002545 switch (Opcode) {
2546 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002547 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002548 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2549 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002550 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002551 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002552 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002553 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2554 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002555 case ISD::UINT_TO_FP:
2556 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002557 APFloat apf(EVTToAPFloatSemantics(VT),
2558 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002559 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002560 Opcode==ISD::SINT_TO_FP,
2561 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002562 return getConstantFP(apf, VT);
2563 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002564 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002565 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002566 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002567 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002568 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002569 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002570 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002571 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2572 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002573 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002574 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2575 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002576 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002577 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002578 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2579 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002580 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002581 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002582 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2583 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002584 }
2585 }
2586
Dale Johannesen446b9002007-08-31 23:34:27 +00002587 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002588 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002589 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002590 switch (Opcode) {
2591 case ISD::FNEG:
2592 V.changeSign();
2593 return getConstantFP(V, VT);
2594 case ISD::FABS:
2595 V.clearSign();
2596 return getConstantFP(V, VT);
2597 case ISD::FCEIL: {
2598 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2599 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002600 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002601 break;
2602 }
2603 case ISD::FTRUNC: {
2604 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2605 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002606 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002607 break;
2608 }
2609 case ISD::FFLOOR: {
2610 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2611 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002612 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002613 break;
2614 }
2615 case ISD::FP_EXTEND: {
2616 bool ignored;
2617 // This can return overflow, underflow, or inexact; we don't care.
2618 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002619 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002620 APFloat::rmNearestTiesToEven, &ignored);
2621 return getConstantFP(V, VT);
2622 }
2623 case ISD::FP_TO_SINT:
2624 case ISD::FP_TO_UINT: {
2625 integerPart x[2];
2626 bool ignored;
2627 assert(integerPartWidth >= 64);
2628 // FIXME need to be more flexible about rounding mode.
2629 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2630 Opcode==ISD::FP_TO_SINT,
2631 APFloat::rmTowardZero, &ignored);
2632 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002633 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002634 APInt api(VT.getSizeInBits(), x);
2635 return getConstant(api, VT);
2636 }
2637 case ISD::BITCAST:
2638 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2639 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2640 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2641 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2642 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002643 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002644 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002645
Gabor Greiff304a7a2008-08-28 21:40:38 +00002646 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002647 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002648 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002649 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002650 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002651 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002652 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002653 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002654 assert(VT.isFloatingPoint() &&
2655 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002656 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002657 assert((!VT.isVector() ||
2658 VT.getVectorNumElements() ==
2659 Operand.getValueType().getVectorNumElements()) &&
2660 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002661 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002662 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002663 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002664 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002665 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002666 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002667 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002668 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2669 "Invalid sext node, dst < src!");
2670 assert((!VT.isVector() ||
2671 VT.getVectorNumElements() ==
2672 Operand.getValueType().getVectorNumElements()) &&
2673 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002674 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2675 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2676 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002677 // sext(undef) = 0, because the top bits will all be the same.
2678 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002679 break;
2680 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002681 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002682 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002683 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002684 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2685 "Invalid zext node, dst < src!");
2686 assert((!VT.isVector() ||
2687 VT.getVectorNumElements() ==
2688 Operand.getValueType().getVectorNumElements()) &&
2689 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002690 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002691 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002692 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002693 else if (OpOpcode == ISD::UNDEF)
2694 // zext(undef) = 0, because the top bits will be zero.
2695 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002696 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002697 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002698 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002699 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002700 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002701 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2702 "Invalid anyext node, dst < src!");
2703 assert((!VT.isVector() ||
2704 VT.getVectorNumElements() ==
2705 Operand.getValueType().getVectorNumElements()) &&
2706 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002707
Dan Gohman08837892010-06-18 00:08:30 +00002708 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2709 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002710 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002711 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002712 else if (OpOpcode == ISD::UNDEF)
2713 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002714
2715 // (ext (trunx x)) -> x
2716 if (OpOpcode == ISD::TRUNCATE) {
2717 SDValue OpOp = Operand.getNode()->getOperand(0);
2718 if (OpOp.getValueType() == VT)
2719 return OpOp;
2720 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002721 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002722 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002723 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002724 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002725 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002726 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2727 "Invalid truncate node, src < dst!");
2728 assert((!VT.isVector() ||
2729 VT.getVectorNumElements() ==
2730 Operand.getValueType().getVectorNumElements()) &&
2731 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002732 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002733 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002734 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2735 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002736 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002737 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2738 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002739 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002740 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002741 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002742 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002743 }
Craig Topper201c1a32012-01-15 01:05:11 +00002744 if (OpOpcode == ISD::UNDEF)
2745 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002746 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002747 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002748 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002749 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002750 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002751 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002752 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2753 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002754 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002755 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002756 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002757 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002758 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002759 (VT.getVectorElementType() == Operand.getValueType() ||
2760 (VT.getVectorElementType().isInteger() &&
2761 Operand.getValueType().isInteger() &&
2762 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002763 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002764 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002765 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002766 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2767 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2768 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2769 Operand.getConstantOperandVal(1) == 0 &&
2770 Operand.getOperand(0).getValueType() == VT)
2771 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002772 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002773 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002774 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002775 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002776 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002777 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002778 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002779 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002780 break;
2781 case ISD::FABS:
2782 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002783 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002784 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002785 }
2786
Chris Lattnerf9c19152005-08-25 19:12:10 +00002787 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002788 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002789 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002790 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002791 SDValue Ops[1] = { Operand };
Chris Lattnerf17b4222007-02-04 07:28:00 +00002792 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Craig Topperc0196b12014-04-14 00:51:57 +00002793 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002795 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002796
Jack Carter170a5f22013-09-09 22:02:08 +00002797 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2798 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002799 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002800 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002801 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2802 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002803 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002804
Chris Lattner061a1ea2005-01-07 07:46:32 +00002805 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002806#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002807 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002808#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002809 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002810}
2811
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002812SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2813 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002814 // If the opcode is a target-specific ISD node, there's nothing we can
2815 // do here and the operand rules may not line up with the below, so
2816 // bail early.
2817 if (Opcode >= ISD::BUILTIN_OP_END)
2818 return SDValue();
2819
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002820 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2821 SmallVector<SDValue, 4> Outputs;
2822 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002823
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002824 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2825 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002826 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2827 return SDValue();
2828
2829 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002830 // Scalar instruction.
2831 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002832 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002833 // For vectors extract each constant element into Inputs so we can constant
2834 // fold them individually.
2835 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2836 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2837 if (!BV1 || !BV2)
2838 return SDValue();
2839
2840 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2841
2842 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2843 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2844 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2845 if (!V1 || !V2) // Not a constant, bail.
2846 return SDValue();
2847
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002848 if (V1->isOpaque() || V2->isOpaque())
2849 return SDValue();
2850
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002851 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2852 // FIXME: This is valid and could be handled by truncating the APInts.
2853 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2854 return SDValue();
2855
2856 Inputs.push_back(std::make_pair(V1, V2));
2857 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002858 }
2859
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002860 // We have a number of constant values, constant fold them element by element.
2861 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2862 const APInt &C1 = Inputs[I].first->getAPIntValue();
2863 const APInt &C2 = Inputs[I].second->getAPIntValue();
2864
2865 switch (Opcode) {
2866 case ISD::ADD:
2867 Outputs.push_back(getConstant(C1 + C2, SVT));
2868 break;
2869 case ISD::SUB:
2870 Outputs.push_back(getConstant(C1 - C2, SVT));
2871 break;
2872 case ISD::MUL:
2873 Outputs.push_back(getConstant(C1 * C2, SVT));
2874 break;
2875 case ISD::UDIV:
2876 if (!C2.getBoolValue())
2877 return SDValue();
2878 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2879 break;
2880 case ISD::UREM:
2881 if (!C2.getBoolValue())
2882 return SDValue();
2883 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2884 break;
2885 case ISD::SDIV:
2886 if (!C2.getBoolValue())
2887 return SDValue();
2888 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2889 break;
2890 case ISD::SREM:
2891 if (!C2.getBoolValue())
2892 return SDValue();
2893 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2894 break;
2895 case ISD::AND:
2896 Outputs.push_back(getConstant(C1 & C2, SVT));
2897 break;
2898 case ISD::OR:
2899 Outputs.push_back(getConstant(C1 | C2, SVT));
2900 break;
2901 case ISD::XOR:
2902 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2903 break;
2904 case ISD::SHL:
2905 Outputs.push_back(getConstant(C1 << C2, SVT));
2906 break;
2907 case ISD::SRL:
2908 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2909 break;
2910 case ISD::SRA:
2911 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2912 break;
2913 case ISD::ROTL:
2914 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2915 break;
2916 case ISD::ROTR:
2917 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2918 break;
2919 default:
2920 return SDValue();
2921 }
2922 }
2923
2924 // Handle the scalar case first.
Silviu Baranga4ad2bc52013-04-25 09:32:33 +00002925 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002926 return Outputs.back();
2927
2928 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002929 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002930 Outputs.size());
Bill Wendling162c26d2008-09-24 10:16:24 +00002931}
2932
Andrew Trickef9de2a2013-05-25 02:42:55 +00002933SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002934 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002935 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2936 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002937 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002938 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002939 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002940 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2941 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002942 // Fold trivial token factors.
2943 if (N1.getOpcode() == ISD::EntryToken) return N2;
2944 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002945 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002946 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002947 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002948 // Concat of UNDEFs is UNDEF.
2949 if (N1.getOpcode() == ISD::UNDEF &&
2950 N2.getOpcode() == ISD::UNDEF)
2951 return getUNDEF(VT);
2952
Dan Gohman550c9af2008-08-14 20:04:46 +00002953 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2954 // one big BUILD_VECTOR.
2955 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2956 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002957 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2958 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002959 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00002960 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00002961 }
2962 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002963 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002964 assert(VT.isInteger() && "This operator does not apply to FP types!");
2965 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002966 N1.getValueType() == VT && "Binary operator types must match!");
2967 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2968 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002969 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002970 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002971 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2972 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002973 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002974 case ISD::OR:
2975 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002976 case ISD::ADD:
2977 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002978 assert(VT.isInteger() && "This operator does not apply to FP types!");
2979 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002980 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002981 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2982 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002983 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002984 return N1;
2985 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002986 case ISD::UDIV:
2987 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00002988 case ISD::MULHU:
2989 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002990 case ISD::MUL:
2991 case ISD::SDIV:
2992 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00002993 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002994 assert(N1.getValueType() == N2.getValueType() &&
2995 N1.getValueType() == VT && "Binary operator types must match!");
2996 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002997 case ISD::FADD:
2998 case ISD::FSUB:
2999 case ISD::FMUL:
3000 case ISD::FDIV:
3001 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003002 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003003 if (Opcode == ISD::FADD) {
3004 // 0+x --> x
3005 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3006 if (CFP->getValueAPF().isZero())
3007 return N2;
3008 // x+0 --> x
3009 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3010 if (CFP->getValueAPF().isZero())
3011 return N1;
3012 } else if (Opcode == ISD::FSUB) {
3013 // x-0 --> x
3014 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3015 if (CFP->getValueAPF().isZero())
3016 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003017 } else if (Opcode == ISD::FMUL) {
3018 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3019 SDValue V = N2;
3020
3021 // If the first operand isn't the constant, try the second
3022 if (!CFP) {
3023 CFP = dyn_cast<ConstantFPSDNode>(N2);
3024 V = N1;
3025 }
3026
3027 if (CFP) {
3028 // 0*x --> 0
3029 if (CFP->isZero())
3030 return SDValue(CFP,0);
3031 // 1*x --> x
3032 if (CFP->isExactlyValue(1.0))
3033 return V;
3034 }
Dan Gohman1275e282009-01-23 19:10:37 +00003035 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003036 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003037 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003038 assert(N1.getValueType() == N2.getValueType() &&
3039 N1.getValueType() == VT && "Binary operator types must match!");
3040 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003041 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3042 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003043 N1.getValueType().isFloatingPoint() &&
3044 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003045 "Invalid FCOPYSIGN!");
3046 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003047 case ISD::SHL:
3048 case ISD::SRA:
3049 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003050 case ISD::ROTL:
3051 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003052 assert(VT == N1.getValueType() &&
3053 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003054 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003055 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003056 assert((!VT.isVector() || VT == N2.getValueType()) &&
3057 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003058 // Verify that the shift amount VT is bit enough to hold valid shift
3059 // amounts. This catches things like trying to shift an i1024 value by an
3060 // i8, which is easy to fall into in generic code that uses
3061 // TLI.getShiftAmount().
3062 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003063 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003064 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003065
3066 // Always fold shifts of i1 values so the code generator doesn't need to
3067 // handle them. Since we know the size of the shift has to be less than the
3068 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003069 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003070 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003071 if (N2C && N2C->isNullValue())
3072 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003073 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003074 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003075 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003076 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003077 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003078 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003079 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003080 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003081 "type is vector!");
3082 assert((!EVT.isVector() ||
3083 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3084 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003085 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003086 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003087 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003088 break;
3089 }
Chris Lattner72733e52008-01-17 07:00:52 +00003090 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003091 assert(VT.isFloatingPoint() &&
3092 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003093 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003094 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003095 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003096 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003097 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003098 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003099 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003100 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003101 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003102 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003103 assert(!EVT.isVector() &&
3104 "AssertSExt/AssertZExt type should be the vector element type "
3105 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003106 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003107 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003108 break;
3109 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003110 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003111 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003112 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003113 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003114 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003115 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003116 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003117 "type is vector!");
3118 assert((!EVT.isVector() ||
3119 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3120 "Vector element counts must match in SIGN_EXTEND_INREG");
3121 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003122 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003123
Chris Lattner16713612008-01-22 19:09:33 +00003124 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003125 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003126 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003127 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003128 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003129 return getConstant(Val, VT);
3130 }
Chris Lattner16713612008-01-22 19:09:33 +00003131 break;
3132 }
3133 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003134 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3135 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003136 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003137
Chris Lattner16713612008-01-22 19:09:33 +00003138 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3139 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003140 if (N2C &&
3141 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003142 N1.getNumOperands() > 0) {
3143 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003144 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003145 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003146 N1.getOperand(N2C->getZExtValue() / Factor),
3147 getConstant(N2C->getZExtValue() % Factor,
3148 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003149 }
3150
3151 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3152 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003153 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3154 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003155
3156 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003157 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003158 // are promoted and implicitly truncated, and the result implicitly
3159 // extended. Make that explicit here.
3160 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003161
Bob Wilson59dbbb22009-04-13 22:05:19 +00003162 return Elt;
3163 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003164
Chris Lattner16713612008-01-22 19:09:33 +00003165 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3166 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003167 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003168 // If the indices are the same, return the inserted element else
3169 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003170 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003171 SDValue N1Op2 = N1.getOperand(2);
3172 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3173
3174 if (N1Op2C && N2C) {
3175 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3176 if (VT == N1.getOperand(1).getValueType())
3177 return N1.getOperand(1);
3178 else
3179 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3180 }
3181
Dale Johannesendb393622009-02-03 01:55:44 +00003182 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003183 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003184 }
Chris Lattner16713612008-01-22 19:09:33 +00003185 break;
3186 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003187 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003188 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3189 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003190 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003191 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003192
Chris Lattner16713612008-01-22 19:09:33 +00003193 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3194 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003195 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003196 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003197 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003198
Chris Lattner16713612008-01-22 19:09:33 +00003199 // EXTRACT_ELEMENT of a constant int is also very common.
3200 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003201 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003202 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003203 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3204 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003205 }
3206 break;
David Greeneb6f16112011-01-26 15:38:49 +00003207 case ISD::EXTRACT_SUBVECTOR: {
3208 SDValue Index = N2;
3209 if (VT.isSimple() && N1.getValueType().isSimple()) {
3210 assert(VT.isVector() && N1.getValueType().isVector() &&
3211 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003212 assert(VT.getVectorElementType() ==
3213 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003214 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003215 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003216 "Extract subvector must be from larger vector to smaller vector!");
3217
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003218 if (isa<ConstantSDNode>(Index.getNode())) {
3219 assert((VT.getVectorNumElements() +
3220 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003221 <= N1.getValueType().getVectorNumElements())
3222 && "Extract subvector overflow!");
3223 }
3224
3225 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003226 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003227 return N1;
3228 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003229 break;
Chris Lattner16713612008-01-22 19:09:33 +00003230 }
David Greeneb6f16112011-01-26 15:38:49 +00003231 }
Chris Lattner16713612008-01-22 19:09:33 +00003232
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003233 // Perform trivial constant folding.
3234 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3235 if (SV.getNode()) return SV;
3236
3237 // Canonicalize constant to RHS if commutative.
3238 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3239 std::swap(N1C, N2C);
3240 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003241 }
3242
Chris Lattner16713612008-01-22 19:09:33 +00003243 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003244 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3245 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003246 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003247 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003248 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003249 std::swap(N1CFP, N2CFP);
3250 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003251 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003252 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3253 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003254 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003255 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003256 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003257 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003258 return getConstantFP(V1, VT);
3259 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003260 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003261 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3262 if (s!=APFloat::opInvalidOp)
3263 return getConstantFP(V1, VT);
3264 break;
3265 case ISD::FMUL:
3266 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3267 if (s!=APFloat::opInvalidOp)
3268 return getConstantFP(V1, VT);
3269 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003270 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003271 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3272 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3273 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003274 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003275 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003276 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3277 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3278 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003279 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003280 case ISD::FCOPYSIGN:
3281 V1.copySign(V2);
3282 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003283 default: break;
3284 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003285 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003286
3287 if (Opcode == ISD::FP_ROUND) {
3288 APFloat V = N1CFP->getValueAPF(); // make copy
3289 bool ignored;
3290 // This can return overflow, underflow, or inexact; we don't care.
3291 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003292 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003293 APFloat::rmNearestTiesToEven, &ignored);
3294 return getConstantFP(V, VT);
3295 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003296 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003297
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003298 // Canonicalize an UNDEF to the RHS, even over a constant.
3299 if (N1.getOpcode() == ISD::UNDEF) {
3300 if (isCommutativeBinOp(Opcode)) {
3301 std::swap(N1, N2);
3302 } else {
3303 switch (Opcode) {
3304 case ISD::FP_ROUND_INREG:
3305 case ISD::SIGN_EXTEND_INREG:
3306 case ISD::SUB:
3307 case ISD::FSUB:
3308 case ISD::FDIV:
3309 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003310 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003311 return N1; // fold op(undef, arg2) -> undef
3312 case ISD::UDIV:
3313 case ISD::SDIV:
3314 case ISD::UREM:
3315 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003316 case ISD::SRL:
3317 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003318 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003319 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3320 // For vectors, we can't easily build an all zero vector, just return
3321 // the LHS.
3322 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003323 }
3324 }
3325 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003326
3327 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003328 if (N2.getOpcode() == ISD::UNDEF) {
3329 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003330 case ISD::XOR:
3331 if (N1.getOpcode() == ISD::UNDEF)
3332 // Handle undef ^ undef -> 0 special case. This is a common
3333 // idiom (misuse).
3334 return getConstant(0, VT);
3335 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003336 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003337 case ISD::ADDC:
3338 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003339 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003340 case ISD::UDIV:
3341 case ISD::SDIV:
3342 case ISD::UREM:
3343 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003344 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003345 case ISD::FADD:
3346 case ISD::FSUB:
3347 case ISD::FMUL:
3348 case ISD::FDIV:
3349 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003350 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003351 return N2;
3352 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003353 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003354 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003355 case ISD::SRL:
3356 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003357 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003358 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3359 // For vectors, we can't easily build an all zero vector, just return
3360 // the LHS.
3361 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003362 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003363 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003364 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003365 // For vectors, we can't easily build an all one vector, just return
3366 // the LHS.
3367 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003368 case ISD::SRA:
3369 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003370 }
3371 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003372
Chris Lattner724f7ee2005-05-11 18:57:39 +00003373 // Memoize this node if possible.
3374 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003375 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003376 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003377 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003378 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003379 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Craig Topperc0196b12014-04-14 00:51:57 +00003380 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003381 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003382 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003383
Jack Carter170a5f22013-09-09 22:02:08 +00003384 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3385 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003386 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003387 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003388 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3389 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003390 }
3391
Chris Lattner061a1ea2005-01-07 07:46:32 +00003392 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003393#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003394 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003395#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003396 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003397}
3398
Andrew Trickef9de2a2013-05-25 02:42:55 +00003399SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003400 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003401 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003402 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003403 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003404 case ISD::FMA: {
3405 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3406 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3407 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3408 if (N1CFP && N2CFP && N3CFP) {
3409 APFloat V1 = N1CFP->getValueAPF();
3410 const APFloat &V2 = N2CFP->getValueAPF();
3411 const APFloat &V3 = N3CFP->getValueAPF();
3412 APFloat::opStatus s =
3413 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3414 if (s != APFloat::opInvalidOp)
3415 return getConstantFP(V1, VT);
3416 }
3417 break;
3418 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003419 case ISD::CONCAT_VECTORS:
3420 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3421 // one big BUILD_VECTOR.
3422 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3423 N2.getOpcode() == ISD::BUILD_VECTOR &&
3424 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003425 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3426 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003427 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3428 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00003429 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00003430 }
3431 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003432 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003433 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003434 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003435 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003436 break;
3437 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003438 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003439 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003440 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003441 return N2; // select true, X, Y -> X
3442 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003443 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003444
3445 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003446 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003447 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003448 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003449 case ISD::INSERT_SUBVECTOR: {
3450 SDValue Index = N3;
3451 if (VT.isSimple() && N1.getValueType().isSimple()
3452 && N2.getValueType().isSimple()) {
3453 assert(VT.isVector() && N1.getValueType().isVector() &&
3454 N2.getValueType().isVector() &&
3455 "Insert subvector VTs must be a vectors");
3456 assert(VT == N1.getValueType() &&
3457 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003458 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003459 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003460 if (isa<ConstantSDNode>(Index.getNode())) {
3461 assert((N2.getValueType().getVectorNumElements() +
3462 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003463 <= VT.getVectorNumElements())
3464 && "Insert subvector overflow!");
3465 }
3466
3467 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003468 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003469 return N2;
3470 }
3471 break;
3472 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003473 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003474 // Fold bit_convert nodes from a type to themselves.
3475 if (N1.getValueType() == VT)
3476 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003477 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003478 }
3479
Chris Lattnerf9c19152005-08-25 19:12:10 +00003480 // Memoize node if it doesn't produce a flag.
3481 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003482 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003483 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003484 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003485 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003486 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Craig Topperc0196b12014-04-14 00:51:57 +00003487 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003488 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003489 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003490
Jack Carter170a5f22013-09-09 22:02:08 +00003491 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3492 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003493 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003494 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003495 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3496 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003497 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003498
Chris Lattner061a1ea2005-01-07 07:46:32 +00003499 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003500#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003501 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003502#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003503 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003504}
3505
Andrew Trickef9de2a2013-05-25 02:42:55 +00003506SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003507 SDValue N1, SDValue N2, SDValue N3,
3508 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003509 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003510 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003511}
3512
Andrew Trickef9de2a2013-05-25 02:42:55 +00003513SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003514 SDValue N1, SDValue N2, SDValue N3,
3515 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003516 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003517 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003518}
3519
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003520/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3521/// the incoming stack arguments to be loaded from the stack.
3522SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3523 SmallVector<SDValue, 8> ArgChains;
3524
3525 // Include the original chain at the beginning of the list. When this is
3526 // used by target LowerCall hooks, this helps legalize find the
3527 // CALLSEQ_BEGIN node.
3528 ArgChains.push_back(Chain);
3529
3530 // Add a chain value for each stack argument.
3531 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3532 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3533 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3534 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3535 if (FI->getIndex() < 0)
3536 ArgChains.push_back(SDValue(L, 1));
3537
3538 // Build a tokenfactor for all the chains.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003539 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003540 &ArgChains[0], ArgChains.size());
3541}
3542
Dan Gohman544ab2c2008-04-12 04:36:06 +00003543/// getMemsetValue - Vectorized representation of the memset value
3544/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003545static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003546 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003547 assert(Value.getOpcode() != ISD::UNDEF);
3548
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003549 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003550 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003551 assert(C->getAPIntValue().getBitWidth() == 8);
3552 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003553 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003554 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003555 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003556 }
Evan Chengef377ad2008-05-15 08:39:06 +00003557
Dale Johannesenabf66b82009-02-03 22:26:09 +00003558 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003559 if (NumBits > 8) {
3560 // Use a multiplication with 0x010101... to extend the input to the
3561 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003562 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003563 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003564 }
3565
3566 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003567}
3568
Dan Gohman544ab2c2008-04-12 04:36:06 +00003569/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3570/// used when a memcpy is turned into a memset when the source is a constant
3571/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003572static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003573 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003574 // Handle vector with all elements zero.
3575 if (Str.empty()) {
3576 if (VT.isInteger())
3577 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003578 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003579 return DAG.getConstantFP(0.0, VT);
3580 else if (VT.isVector()) {
3581 unsigned NumElts = VT.getVectorNumElements();
3582 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003583 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003584 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3585 EltVT, NumElts)));
3586 } else
3587 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003588 }
3589
Duncan Sands13237ac2008-06-06 12:08:01 +00003590 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003591 unsigned NumVTBits = VT.getSizeInBits();
3592 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003593 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3594
Evan Chengc8444b12013-01-10 22:13:27 +00003595 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003596 if (TLI.isLittleEndian()) {
3597 for (unsigned i = 0; i != NumBytes; ++i)
3598 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3599 } else {
3600 for (unsigned i = 0; i != NumBytes; ++i)
3601 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003602 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003603
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003604 // If the "cost" of materializing the integer immediate is less than the cost
3605 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003606 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3607 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003608 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003609 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003610}
3611
Scott Michelcf0da6c2009-02-17 22:15:04 +00003612/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003613///
Andrew Tricke2431c62013-05-25 03:08:10 +00003614static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003615 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003616 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003617 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003618 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003619}
3620
Evan Chengef377ad2008-05-15 08:39:06 +00003621/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3622///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003623static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003624 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003625 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003626 if (Src.getOpcode() == ISD::GlobalAddress)
3627 G = cast<GlobalAddressSDNode>(Src);
3628 else if (Src.getOpcode() == ISD::ADD &&
3629 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3630 Src.getOperand(1).getOpcode() == ISD::Constant) {
3631 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003632 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003633 }
3634 if (!G)
3635 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003636
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003637 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003638}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003639
Evan Cheng43cd9e32010-04-01 06:04:33 +00003640/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3641/// to replace the memset / memcpy. Return true if the number of memory ops
3642/// is below the threshold. It returns the types of the sequence of
3643/// memory ops to perform memset / memcpy by reference.
3644static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003645 unsigned Limit, uint64_t Size,
3646 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003647 bool IsMemset,
3648 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003649 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003650 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003651 SelectionDAG &DAG,
3652 const TargetLowering &TLI) {
3653 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3654 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003655 // If 'SrcAlign' is zero, that means the memory operation does not need to
3656 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3657 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3658 // is the specified alignment of the memory operation. If it is zero, that
3659 // means it's possible to change the alignment of the destination.
3660 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3661 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003662 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003663 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003664 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003665
Chris Lattner28dc6c12010-03-07 07:45:08 +00003666 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003667 unsigned AS = 0;
3668 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
3669 TLI.allowsUnalignedMemoryAccesses(VT, AS)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003670 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003671 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003672 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003673 case 0: VT = MVT::i64; break;
3674 case 4: VT = MVT::i32; break;
3675 case 2: VT = MVT::i16; break;
3676 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003677 }
3678 }
3679
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003680 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003681 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003682 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003683 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003684
Duncan Sands11dd4242008-06-08 20:54:56 +00003685 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003686 VT = LVT;
3687 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003688
Dan Gohman544ab2c2008-04-12 04:36:06 +00003689 unsigned NumMemOps = 0;
3690 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003691 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003692 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003693 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003694 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003695 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003696
3697 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003698 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003699 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003700 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003701 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003702 Found = true;
3703 else if (NewVT == MVT::i64 &&
3704 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003705 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003706 // i64 is usually not legal on 32-bit targets, but f64 may be.
3707 NewVT = MVT::f64;
3708 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003709 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003710 }
3711
Evan Cheng04e55182012-12-12 00:42:09 +00003712 if (!Found) {
3713 do {
3714 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3715 if (NewVT == MVT::i8)
3716 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003717 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003718 }
3719 NewVTSize = NewVT.getSizeInBits() / 8;
3720
Evan Cheng79e2ca92012-12-10 23:21:26 +00003721 // If the new VT cannot cover all of the remaining bits, then consider
3722 // issuing a (or a pair of) unaligned and overlapping load / store.
3723 // FIXME: Only does this for 64-bit or more since we don't have proper
3724 // cost model for unaligned load / store.
3725 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003726 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003727 if (NumMemOps && AllowOverlap &&
3728 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003729 TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003730 VTSize = Size;
3731 else {
3732 VT = NewVT;
3733 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003734 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003735 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003736
Evan Chengb7d3d032012-12-12 20:43:23 +00003737 if (++NumMemOps > Limit)
3738 return false;
3739
Dan Gohman544ab2c2008-04-12 04:36:06 +00003740 MemOps.push_back(VT);
3741 Size -= VTSize;
3742 }
3743
3744 return true;
3745}
3746
Andrew Trickef9de2a2013-05-25 02:42:55 +00003747static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003748 SDValue Chain, SDValue Dst,
3749 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003750 unsigned Align, bool isVol,
3751 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003752 MachinePointerInfo DstPtrInfo,
3753 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003754 // Turn a memcpy of undef to nop.
3755 if (Src.getOpcode() == ISD::UNDEF)
3756 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003757
Dan Gohman714663a2008-05-29 19:42:22 +00003758 // Expand memcpy to a series of load and store ops if the size operand falls
3759 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003760 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3761 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003762 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003763 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003764 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003765 MachineFunction &MF = DAG.getMachineFunction();
3766 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003767 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003768 MF.getFunction()->getAttributes().
3769 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003770 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3771 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3772 DstAlignCanChange = true;
3773 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3774 if (Align > SrcAlign)
3775 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003776 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003777 bool CopyFromStr = isMemSrcFromString(Src, Str);
3778 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003779 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003780
Evan Cheng4c014c82010-04-01 18:19:11 +00003781 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003782 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003783 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003784 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003785 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003786
Evan Cheng43cd9e32010-04-01 06:04:33 +00003787 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003788 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003789 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003790
3791 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003792 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003793 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3794 if (!TRI->needsStackRealignment(MF))
3795 while (NewAlign > Align &&
3796 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3797 NewAlign /= 2;
3798
Evan Cheng43cd9e32010-04-01 06:04:33 +00003799 if (NewAlign > Align) {
3800 // Give the stack frame object a larger alignment if needed.
3801 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3802 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3803 Align = NewAlign;
3804 }
3805 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003806
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003807 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003808 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003809 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003810 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003811 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003812 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003813 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003814
Evan Cheng79e2ca92012-12-10 23:21:26 +00003815 if (VTSize > Size) {
3816 // Issuing an unaligned load / store pair that overlaps with the previous
3817 // pair. Adjust the offset accordingly.
3818 assert(i == NumMemOps-1 && i != 0);
3819 SrcOff -= VTSize - Size;
3820 DstOff -= VTSize - Size;
3821 }
3822
Evan Cheng43cd9e32010-04-01 06:04:33 +00003823 if (CopyFromStr &&
3824 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003825 // It's unlikely a store of a vector immediate can be done in a single
3826 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003827 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003828 // FIXME: Handle other cases where store of vector immediate is done in
3829 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003830 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003831 if (Value.getNode())
3832 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003833 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003834 DstPtrInfo.getWithOffset(DstOff), isVol,
3835 false, Align);
3836 }
3837
3838 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003839 // The type might not be legal for the target. This should only happen
3840 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003841 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3842 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003843 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003844 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003845 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003846 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003847 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003848 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003849 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003850 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003851 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003852 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3853 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003854 }
3855 OutChains.push_back(Store);
3856 SrcOff += VTSize;
3857 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003858 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003859 }
3860
Owen Anderson9f944592009-08-11 20:47:22 +00003861 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003862 &OutChains[0], OutChains.size());
3863}
3864
Andrew Trickef9de2a2013-05-25 02:42:55 +00003865static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003866 SDValue Chain, SDValue Dst,
3867 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003868 unsigned Align, bool isVol,
3869 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003870 MachinePointerInfo DstPtrInfo,
3871 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003872 // Turn a memmove of undef to nop.
3873 if (Src.getOpcode() == ISD::UNDEF)
3874 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003875
3876 // Expand memmove to a series of load and store ops if the size operand falls
3877 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003878 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003879 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003880 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003881 MachineFunction &MF = DAG.getMachineFunction();
3882 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003883 bool OptSize = MF.getFunction()->getAttributes().
3884 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003885 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3886 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3887 DstAlignCanChange = true;
3888 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3889 if (Align > SrcAlign)
3890 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003891 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003892
Evan Cheng4c014c82010-04-01 18:19:11 +00003893 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003894 (DstAlignCanChange ? 0 : Align), SrcAlign,
3895 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003896 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003897
Evan Cheng43cd9e32010-04-01 06:04:33 +00003898 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003899 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003900 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003901 if (NewAlign > Align) {
3902 // Give the stack frame object a larger alignment if needed.
3903 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3904 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3905 Align = NewAlign;
3906 }
3907 }
Dan Gohman714663a2008-05-29 19:42:22 +00003908
Evan Cheng43cd9e32010-04-01 06:04:33 +00003909 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003910 SmallVector<SDValue, 8> LoadValues;
3911 SmallVector<SDValue, 8> LoadChains;
3912 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003913 unsigned NumMemOps = MemOps.size();
3914 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003915 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003916 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003917 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00003918
Dale Johannesenabf66b82009-02-03 22:26:09 +00003919 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003920 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003921 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003922 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003923 LoadValues.push_back(Value);
3924 LoadChains.push_back(Value.getValue(1));
3925 SrcOff += VTSize;
3926 }
Owen Anderson9f944592009-08-11 20:47:22 +00003927 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003928 &LoadChains[0], LoadChains.size());
3929 OutChains.clear();
3930 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003931 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003932 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003933 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003934
Dale Johannesenabf66b82009-02-03 22:26:09 +00003935 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003936 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003937 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003938 OutChains.push_back(Store);
3939 DstOff += VTSize;
3940 }
3941
Owen Anderson9f944592009-08-11 20:47:22 +00003942 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003943 &OutChains[0], OutChains.size());
3944}
3945
Serge Pavlov8ec39992013-09-17 16:24:42 +00003946/// \brief Lower the call to 'memset' intrinsic function into a series of store
3947/// operations.
3948///
3949/// \param DAG Selection DAG where lowered code is placed.
3950/// \param dl Link to corresponding IR location.
3951/// \param Chain Control flow dependency.
3952/// \param Dst Pointer to destination memory location.
3953/// \param Src Value of byte to write into the memory.
3954/// \param Size Number of bytes to write.
3955/// \param Align Alignment of the destination in bytes.
3956/// \param isVol True if destination is volatile.
3957/// \param DstPtrInfo IR information on the memory pointer.
3958/// \returns New head in the control flow, if lowering was successful, empty
3959/// SDValue otherwise.
3960///
3961/// The function tries to replace 'llvm.memset' intrinsic with several store
3962/// operations and value calculation code. This is usually profitable for small
3963/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003964static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003965 SDValue Chain, SDValue Dst,
3966 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003967 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003968 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003969 // Turn a memset of undef to nop.
3970 if (Src.getOpcode() == ISD::UNDEF)
3971 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003972
3973 // Expand memset to a series of load/store ops if the size operand
3974 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003975 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003976 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003977 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003978 MachineFunction &MF = DAG.getMachineFunction();
3979 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003980 bool OptSize = MF.getFunction()->getAttributes().
3981 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003982 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3983 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3984 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003985 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003986 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003987 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00003988 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00003989 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003990 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003991
Evan Cheng43cd9e32010-04-01 06:04:33 +00003992 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003993 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003994 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003995 if (NewAlign > Align) {
3996 // Give the stack frame object a larger alignment if needed.
3997 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3998 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3999 Align = NewAlign;
4000 }
4001 }
4002
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004003 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004004 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004005 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004006
4007 // Find the largest store and generate the bit pattern for it.
4008 EVT LargestVT = MemOps[0];
4009 for (unsigned i = 1; i < NumMemOps; i++)
4010 if (MemOps[i].bitsGT(LargestVT))
4011 LargestVT = MemOps[i];
4012 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4013
Dan Gohman544ab2c2008-04-12 04:36:06 +00004014 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004015 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004016 unsigned VTSize = VT.getSizeInBits() / 8;
4017 if (VTSize > Size) {
4018 // Issuing an unaligned load / store pair that overlaps with the previous
4019 // pair. Adjust the offset accordingly.
4020 assert(i == NumMemOps-1 && i != 0);
4021 DstOff -= VTSize - Size;
4022 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004023
4024 // If this store is smaller than the largest store see whether we can get
4025 // the smaller value for free with a truncate.
4026 SDValue Value = MemSetValue;
4027 if (VT.bitsLT(LargestVT)) {
4028 if (!LargestVT.isVector() && !VT.isVector() &&
4029 TLI.isTruncateFree(LargestVT, VT))
4030 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4031 else
4032 Value = getMemsetValue(Src, VT, DAG, dl);
4033 }
4034 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004035 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004036 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004037 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004038 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004039 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004040 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004041 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004042 }
4043
Owen Anderson9f944592009-08-11 20:47:22 +00004044 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00004045 &OutChains[0], OutChains.size());
4046}
4047
Andrew Trickef9de2a2013-05-25 02:42:55 +00004048SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004049 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004050 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004051 MachinePointerInfo DstPtrInfo,
4052 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004053 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004054
4055 // Check to see if we should lower the memcpy to loads and stores first.
4056 // For cases within the target-specified limits, this is the best choice.
4057 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4058 if (ConstantSize) {
4059 // Memcpy with size zero? Just return the original chain.
4060 if (ConstantSize->isNullValue())
4061 return Chain;
4062
Evan Cheng43cd9e32010-04-01 06:04:33 +00004063 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4064 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004065 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004066 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004067 return Result;
4068 }
4069
4070 // Then check to see if we should lower the memcpy with target-specific
4071 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004072 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004073 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004074 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004075 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004076 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004077 return Result;
4078
4079 // If we really need inline code and the target declined to provide it,
4080 // use a (potentially long) sequence of loads and stores.
4081 if (AlwaysInline) {
4082 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004083 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004084 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004085 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004086 }
4087
Dan Gohmanf38547c2010-04-05 20:24:08 +00004088 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4089 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4090 // respect volatile, so they may do things like read or write memory
4091 // beyond the given memory regions. But fixing this isn't easy, and most
4092 // people don't care.
4093
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004094 const TargetLowering *TLI = TM.getTargetLowering();
4095
Dan Gohman544ab2c2008-04-12 04:36:06 +00004096 // Emit a library call.
4097 TargetLowering::ArgListTy Args;
4098 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004099 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004100 Entry.Node = Dst; Args.push_back(Entry);
4101 Entry.Node = Src; Args.push_back(Entry);
4102 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004103 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004104 TargetLowering::
4105 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004106 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004107 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00004108 /*isTailCall=*/false,
4109 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004110 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4111 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004112 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004113 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004114
Dan Gohman544ab2c2008-04-12 04:36:06 +00004115 return CallResult.second;
4116}
4117
Andrew Trickef9de2a2013-05-25 02:42:55 +00004118SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004119 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004120 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004121 MachinePointerInfo DstPtrInfo,
4122 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004123 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004124
Dan Gohman714663a2008-05-29 19:42:22 +00004125 // Check to see if we should lower the memmove to loads and stores first.
4126 // For cases within the target-specified limits, this is the best choice.
4127 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4128 if (ConstantSize) {
4129 // Memmove with size zero? Just return the original chain.
4130 if (ConstantSize->isNullValue())
4131 return Chain;
4132
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004133 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004134 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004135 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004136 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004137 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004138 return Result;
4139 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004140
4141 // Then check to see if we should lower the memmove with target-specific
4142 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004143 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004144 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004145 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004146 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004147 return Result;
4148
Mon P Wangbf862242010-04-06 08:27:51 +00004149 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4150 // not be safe. See memcpy above for more details.
4151
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004152 const TargetLowering *TLI = TM.getTargetLowering();
4153
Dan Gohman544ab2c2008-04-12 04:36:06 +00004154 // Emit a library call.
4155 TargetLowering::ArgListTy Args;
4156 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004157 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004158 Entry.Node = Dst; Args.push_back(Entry);
4159 Entry.Node = Src; Args.push_back(Entry);
4160 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004161 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004162 TargetLowering::
4163 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004164 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004165 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004166 /*isTailCall=*/false,
4167 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004168 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4169 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004170 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004171 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004172
Dan Gohman544ab2c2008-04-12 04:36:06 +00004173 return CallResult.second;
4174}
4175
Andrew Trickef9de2a2013-05-25 02:42:55 +00004176SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004177 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004178 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004179 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004180 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004181
4182 // Check to see if we should lower the memset to stores first.
4183 // For cases within the target-specified limits, this is the best choice.
4184 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4185 if (ConstantSize) {
4186 // Memset with size zero? Just return the original chain.
4187 if (ConstantSize->isNullValue())
4188 return Chain;
4189
Mon P Wangc576ee92010-04-04 03:10:48 +00004190 SDValue Result =
4191 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004192 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004193
Gabor Greiff304a7a2008-08-28 21:40:38 +00004194 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004195 return Result;
4196 }
4197
4198 // Then check to see if we should lower the memset with target-specific
4199 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004200 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004201 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004202 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004203 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004204 return Result;
4205
Wesley Peck527da1b2010-11-23 03:31:01 +00004206 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004207 const TargetLowering *TLI = TM.getTargetLowering();
4208 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004209 TargetLowering::ArgListTy Args;
4210 TargetLowering::ArgListEntry Entry;
4211 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4212 Args.push_back(Entry);
4213 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004214 if (Src.getValueType().bitsGT(MVT::i32))
4215 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004216 else
Owen Anderson9f944592009-08-11 20:47:22 +00004217 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004218 Entry.Node = Src;
4219 Entry.Ty = Type::getInt32Ty(*getContext());
4220 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004221 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004222 Entry.Node = Size;
4223 Entry.Ty = IntPtrTy;
4224 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004225 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004226 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004227 TargetLowering::
4228 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004229 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004230 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004231 /*isTailCall=*/false,
4232 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004233 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4234 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004235 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004236 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004237
Dan Gohman544ab2c2008-04-12 04:36:06 +00004238 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004239}
4240
Andrew Trickef9de2a2013-05-25 02:42:55 +00004241SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Tim Northovere94a5182014-03-11 10:48:52 +00004242 SDVTList VTList, SDValue *Ops, unsigned NumOps,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004243 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004244 AtomicOrdering SuccessOrdering,
4245 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004246 SynchronizationScope SynchScope) {
4247 FoldingSetNodeID ID;
4248 ID.AddInteger(MemVT.getRawBits());
4249 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4250 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004251 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004252 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4253 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4254 return SDValue(E, 0);
4255 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004256
4257 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4258 // SDNode doesn't have access to it. This memory will be "leaked" when
4259 // the node is deallocated, but recovered when the allocator is released.
4260 // If the number of operands is less than 5 we use AtomicSDNode's internal
4261 // storage.
Craig Topperc0196b12014-04-14 00:51:57 +00004262 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004263
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004264 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4265 dl.getDebugLoc(), VTList, MemVT,
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004266 Ops, DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004267 SuccessOrdering, FailureOrdering,
4268 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004269 CSEMap.InsertNode(N, IP);
4270 AllNodes.push_back(N);
4271 return SDValue(N, 0);
4272}
4273
4274SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Tim Northovere94a5182014-03-11 10:48:52 +00004275 SDVTList VTList, SDValue *Ops, unsigned NumOps,
4276 MachineMemOperand *MMO,
4277 AtomicOrdering Ordering,
4278 SynchronizationScope SynchScope) {
4279 return getAtomic(Opcode, dl, MemVT, VTList, Ops, NumOps, MMO, Ordering,
4280 Ordering, SynchScope);
4281}
4282
4283SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004284 SDValue Chain, SDValue Ptr, SDValue Cmp,
4285 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004286 unsigned Alignment,
Tim Northovere94a5182014-03-11 10:48:52 +00004287 AtomicOrdering SuccessOrdering,
4288 AtomicOrdering FailureOrdering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004289 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004290 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4291 Alignment = getEVTAlignment(MemVT);
4292
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004293 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004294
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004295 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004296 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004297 // FIXME: Volatile isn't really correct; we should keep track of atomic
4298 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004299 unsigned Flags = MachineMemOperand::MOVolatile;
4300 if (Opcode != ISD::ATOMIC_STORE)
4301 Flags |= MachineMemOperand::MOLoad;
4302 if (Opcode != ISD::ATOMIC_LOAD)
4303 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004304
4305 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004306 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004307
Eli Friedmanadec5872011-07-29 03:05:32 +00004308 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004309 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004310}
4311
Andrew Trickef9de2a2013-05-25 02:42:55 +00004312SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004313 SDValue Chain,
4314 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004315 SDValue Swp, MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004316 AtomicOrdering SuccessOrdering,
4317 AtomicOrdering FailureOrdering,
Eli Friedmanadec5872011-07-29 03:05:32 +00004318 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004319 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4320 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4321
Owen Anderson53aa7a92009-08-10 22:56:29 +00004322 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004323
Owen Anderson9f944592009-08-11 20:47:22 +00004324 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004325 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northovere94a5182014-03-11 10:48:52 +00004326 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, SuccessOrdering,
4327 FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004328}
4329
Andrew Trickef9de2a2013-05-25 02:42:55 +00004330SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004331 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004332 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004333 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004334 unsigned Alignment,
4335 AtomicOrdering Ordering,
4336 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004337 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4338 Alignment = getEVTAlignment(MemVT);
4339
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004340 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004341 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004342 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004343 // For now, atomics are considered to be volatile always, and they are
4344 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004345 // FIXME: Volatile isn't really correct; we should keep track of atomic
4346 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004347 unsigned Flags = MachineMemOperand::MOVolatile;
4348 if (Opcode != ISD::ATOMIC_STORE)
4349 Flags |= MachineMemOperand::MOLoad;
4350 if (Opcode != ISD::ATOMIC_LOAD)
4351 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004352
4353 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004354 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004355 MemVT.getStoreSize(), Alignment);
4356
Eli Friedmanadec5872011-07-29 03:05:32 +00004357 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4358 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004359}
4360
Andrew Trickef9de2a2013-05-25 02:42:55 +00004361SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004362 SDValue Chain,
4363 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004364 MachineMemOperand *MMO,
4365 AtomicOrdering Ordering,
4366 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004367 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4368 Opcode == ISD::ATOMIC_LOAD_SUB ||
4369 Opcode == ISD::ATOMIC_LOAD_AND ||
4370 Opcode == ISD::ATOMIC_LOAD_OR ||
4371 Opcode == ISD::ATOMIC_LOAD_XOR ||
4372 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004373 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004374 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004375 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004376 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004377 Opcode == ISD::ATOMIC_SWAP ||
4378 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004379 "Invalid Atomic Op");
4380
Owen Anderson53aa7a92009-08-10 22:56:29 +00004381 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004382
Eli Friedman342e8df2011-08-24 20:50:09 +00004383 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4384 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004385 SDValue Ops[] = {Chain, Ptr, Val};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004386 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004387}
4388
Andrew Trickef9de2a2013-05-25 02:42:55 +00004389SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004390 EVT VT, SDValue Chain,
4391 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004392 MachineMemOperand *MMO,
4393 AtomicOrdering Ordering,
4394 SynchronizationScope SynchScope) {
4395 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4396
4397 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004398 SDValue Ops[] = {Chain, Ptr};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004399 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004400}
4401
Duncan Sands739a0542008-07-02 17:40:58 +00004402/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesenae7992a2009-02-02 20:47:48 +00004403SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004404 SDLoc dl) {
Dale Johannesenae7992a2009-02-02 20:47:48 +00004405 if (NumOps == 1)
4406 return Ops[0];
4407
Owen Anderson53aa7a92009-08-10 22:56:29 +00004408 SmallVector<EVT, 4> VTs;
Dale Johannesenae7992a2009-02-02 20:47:48 +00004409 VTs.reserve(NumOps);
4410 for (unsigned i = 0; i < NumOps; ++i)
4411 VTs.push_back(Ops[i].getValueType());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004412 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesenae7992a2009-02-02 20:47:48 +00004413 Ops, NumOps);
4414}
4415
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004416SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004417SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004418 const EVT *VTs, unsigned NumVTs,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004419 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004420 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004421 unsigned Align, bool Vol,
4422 bool ReadMem, bool WriteMem) {
4423 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004424 MemVT, PtrInfo, Align, Vol,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004425 ReadMem, WriteMem);
4426}
4427
4428SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004429SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004430 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004431 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004432 unsigned Align, bool Vol,
4433 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004434 if (Align == 0) // Ensure that codegen never sees alignment 0
4435 Align = getEVTAlignment(MemVT);
4436
4437 MachineFunction &MF = getMachineFunction();
4438 unsigned Flags = 0;
4439 if (WriteMem)
4440 Flags |= MachineMemOperand::MOStore;
4441 if (ReadMem)
4442 Flags |= MachineMemOperand::MOLoad;
4443 if (Vol)
4444 Flags |= MachineMemOperand::MOVolatile;
4445 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004446 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004447
4448 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4449}
4450
4451SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004452SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohman48b185d2009-09-25 20:36:54 +00004453 const SDValue *Ops, unsigned NumOps,
4454 EVT MemVT, MachineMemOperand *MMO) {
4455 assert((Opcode == ISD::INTRINSIC_VOID ||
4456 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004457 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004458 Opcode == ISD::LIFETIME_START ||
4459 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004460 (Opcode <= INT_MAX &&
4461 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4462 "Opcode is not a memory-accessing opcode!");
4463
Dale Johannesen839acbb2009-01-29 00:47:48 +00004464 // Memoize the node unless it returns a flag.
4465 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004466 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004467 FoldingSetNodeID ID;
4468 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper91244262012-07-30 20:23:19 +00004469 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004470 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004471 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4472 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004473 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004474 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004475
Jack Carter170a5f22013-09-09 22:02:08 +00004476 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4477 dl.getDebugLoc(), VTList, Ops,
4478 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004479 CSEMap.InsertNode(N, IP);
4480 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004481 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4482 dl.getDebugLoc(), VTList, Ops,
4483 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004484 }
4485 AllNodes.push_back(N);
4486 return SDValue(N, 0);
4487}
4488
Chris Lattnerea952f02010-09-21 17:24:05 +00004489/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4490/// MachinePointerInfo record from it. This is particularly useful because the
4491/// code generator has many cases where it doesn't bother passing in a
4492/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4493static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4494 // If this is FI+Offset, we can model it.
4495 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4496 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4497
4498 // If this is (FI+Offset1)+Offset2, we can model it.
4499 if (Ptr.getOpcode() != ISD::ADD ||
4500 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4501 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4502 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004503
Chris Lattnerea952f02010-09-21 17:24:05 +00004504 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4505 return MachinePointerInfo::getFixedStack(FI, Offset+
4506 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4507}
4508
4509/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4510/// MachinePointerInfo record from it. This is particularly useful because the
4511/// code generator has many cases where it doesn't bother passing in a
4512/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4513static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4514 // If the 'Offset' value isn't a constant, we can't handle this.
4515 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4516 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4517 if (OffsetOp.getOpcode() == ISD::UNDEF)
4518 return InferPointerInfo(Ptr);
4519 return MachinePointerInfo();
4520}
Wesley Peck527da1b2010-11-23 03:31:01 +00004521
Chris Lattnerea952f02010-09-21 17:24:05 +00004522
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004523SDValue
4524SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004525 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004526 SDValue Ptr, SDValue Offset,
4527 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004528 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004529 unsigned Alignment, const MDNode *TBAAInfo,
4530 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004531 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004532 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004533 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4534 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004535
Dan Gohman48b185d2009-09-25 20:36:54 +00004536 unsigned Flags = MachineMemOperand::MOLoad;
4537 if (isVolatile)
4538 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004539 if (isNonTemporal)
4540 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004541 if (isInvariant)
4542 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004543
Chris Lattnerea952f02010-09-21 17:24:05 +00004544 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4545 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004546 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004547 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004548
Chris Lattnerea952f02010-09-21 17:24:05 +00004549 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004550 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004551 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004552 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004553 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004554}
4555
4556SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004557SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004558 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004559 SDValue Ptr, SDValue Offset, EVT MemVT,
4560 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004561 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004562 ExtType = ISD::NON_EXTLOAD;
4563 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004564 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004565 } else {
4566 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004567 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4568 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004569 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004570 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004571 assert(VT.isVector() == MemVT.isVector() &&
4572 "Cannot use trunc store to convert to or from a vector!");
4573 assert((!VT.isVector() ||
4574 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4575 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004576 }
4577
4578 bool Indexed = AM != ISD::UNINDEXED;
4579 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4580 "Unindexed load with an offset!");
4581
4582 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004583 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004584 SDValue Ops[] = { Chain, Ptr, Offset };
4585 FoldingSetNodeID ID;
4586 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman08c0a952009-09-23 21:02:20 +00004587 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004588 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004589 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004590 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004591 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004592 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004593 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4594 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004595 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004596 }
Jack Carter170a5f22013-09-09 22:02:08 +00004597 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4598 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004599 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004600 CSEMap.InsertNode(N, IP);
4601 AllNodes.push_back(N);
4602 return SDValue(N, 0);
4603}
4604
Andrew Trickef9de2a2013-05-25 02:42:55 +00004605SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004606 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004607 MachinePointerInfo PtrInfo,
4608 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004609 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004610 const MDNode *TBAAInfo,
4611 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004612 SDValue Undef = getUNDEF(Ptr.getValueType());
4613 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004614 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4615 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004616}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004617
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004618SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4619 SDValue Chain, SDValue Ptr,
4620 MachineMemOperand *MMO) {
4621 SDValue Undef = getUNDEF(Ptr.getValueType());
4622 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4623 VT, MMO);
4624}
4625
Andrew Trickef9de2a2013-05-25 02:42:55 +00004626SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004627 SDValue Chain, SDValue Ptr,
4628 MachinePointerInfo PtrInfo, EVT MemVT,
4629 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004630 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004631 SDValue Undef = getUNDEF(Ptr.getValueType());
4632 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004633 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004634 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004635}
4636
4637
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004638SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4639 SDValue Chain, SDValue Ptr, EVT MemVT,
4640 MachineMemOperand *MMO) {
4641 SDValue Undef = getUNDEF(Ptr.getValueType());
4642 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4643 MemVT, MMO);
4644}
4645
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004646SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004647SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004648 SDValue Offset, ISD::MemIndexedMode AM) {
4649 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4650 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4651 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004652 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004653 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004654 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004655 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004656}
4657
Andrew Trickef9de2a2013-05-25 02:42:55 +00004658SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004659 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004660 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004661 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004662 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004663 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004664 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004665 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004666
Dan Gohman48b185d2009-09-25 20:36:54 +00004667 unsigned Flags = MachineMemOperand::MOStore;
4668 if (isVolatile)
4669 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004670 if (isNonTemporal)
4671 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004672
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004673 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004674 PtrInfo = InferPointerInfo(Ptr);
4675
4676 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004677 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004678 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004679 Val.getValueType().getStoreSize(), Alignment,
4680 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004681
4682 return getStore(Chain, dl, Val, Ptr, MMO);
4683}
4684
Andrew Trickef9de2a2013-05-25 02:42:55 +00004685SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004686 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004687 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004688 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004689 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004690 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004691 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004692 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4693 FoldingSetNodeID ID;
4694 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004695 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004696 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004697 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004698 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004699 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004700 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4701 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004702 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004703 }
Jack Carter170a5f22013-09-09 22:02:08 +00004704 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4705 dl.getDebugLoc(), VTs,
4706 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004707 CSEMap.InsertNode(N, IP);
4708 AllNodes.push_back(N);
4709 return SDValue(N, 0);
4710}
4711
Andrew Trickef9de2a2013-05-25 02:42:55 +00004712SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004713 SDValue Ptr, MachinePointerInfo PtrInfo,
4714 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004715 unsigned Alignment,
4716 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004717 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004718 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004719 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4720 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004721
Dan Gohman48b185d2009-09-25 20:36:54 +00004722 unsigned Flags = MachineMemOperand::MOStore;
4723 if (isVolatile)
4724 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004725 if (isNonTemporal)
4726 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004727
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004728 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004729 PtrInfo = InferPointerInfo(Ptr);
4730
4731 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004732 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004733 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4734 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004735
4736 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4737}
4738
Andrew Trickef9de2a2013-05-25 02:42:55 +00004739SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004740 SDValue Ptr, EVT SVT,
4741 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004742 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004743
Michael Ilsemand5f91512012-09-10 16:56:31 +00004744 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004745 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004746 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004747 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004748
Dan Gohmancecad352009-12-14 23:40:38 +00004749 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4750 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004751 assert(VT.isInteger() == SVT.isInteger() &&
4752 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004753 assert(VT.isVector() == SVT.isVector() &&
4754 "Cannot use trunc store to convert to or from a vector!");
4755 assert((!VT.isVector() ||
4756 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4757 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004758
Owen Anderson9f944592009-08-11 20:47:22 +00004759 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004760 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004761 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4762 FoldingSetNodeID ID;
4763 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004764 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004765 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004766 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004767 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004768 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004769 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4770 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004771 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004772 }
Jack Carter170a5f22013-09-09 22:02:08 +00004773 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4774 dl.getDebugLoc(), VTs,
4775 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004776 CSEMap.InsertNode(N, IP);
4777 AllNodes.push_back(N);
4778 return SDValue(N, 0);
4779}
4780
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004781SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004782SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004783 SDValue Offset, ISD::MemIndexedMode AM) {
4784 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4785 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4786 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004787 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004788 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4789 FoldingSetNodeID ID;
4790 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004791 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004792 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004793 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004794 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004795 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004796 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004797
Jack Carter170a5f22013-09-09 22:02:08 +00004798 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4799 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004800 ST->isTruncatingStore(),
4801 ST->getMemoryVT(),
4802 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004803 CSEMap.InsertNode(N, IP);
4804 AllNodes.push_back(N);
4805 return SDValue(N, 0);
4806}
4807
Andrew Trickef9de2a2013-05-25 02:42:55 +00004808SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004809 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004810 SDValue SV,
4811 unsigned Align) {
4812 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4813 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004814}
4815
Andrew Trickef9de2a2013-05-25 02:42:55 +00004816SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004817 const SDUse *Ops, unsigned NumOps) {
Dan Gohman768f2c92008-07-07 18:26:29 +00004818 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004819 case 0: return getNode(Opcode, DL, VT);
4820 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4821 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4822 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004823 default: break;
4824 }
4825
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004826 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004827 // the regular getNode logic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004828 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004829 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004830}
4831
Andrew Trickef9de2a2013-05-25 02:42:55 +00004832SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004833 const SDValue *Ops, unsigned NumOps) {
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;
4868 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
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(),
4875 VTs, Ops, NumOps);
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(),
4879 VTs, Ops, NumOps);
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,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00004890 ArrayRef<EVT> ResultTys,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004891 const SDValue *Ops, unsigned NumOps) {
Dan Gohmande912e22009-04-09 23:54:40 +00004892 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner3bf4be42006-08-14 23:31:51 +00004893 Ops, NumOps);
4894}
4895
Andrew Trickef9de2a2013-05-25 02:42:55 +00004896SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004897 const EVT *VTs, unsigned NumVTs,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004898 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3bf4be42006-08-14 23:31:51 +00004899 if (NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004900 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4901 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004902}
4903
Andrew Trickef9de2a2013-05-25 02:42:55 +00004904SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004905 const SDValue *Ops, unsigned NumOps) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004906 if (VTList.NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004907 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattnerd5531332005-05-14 06:20:26 +00004908
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004909#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004910 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004911 // FIXME: figure out how to safely handle things like
4912 // int foo(int x) { return 1 << (x & 255); }
4913 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004914 case ISD::SRA_PARTS:
4915 case ISD::SRL_PARTS:
4916 case ISD::SHL_PARTS:
4917 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004918 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004919 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004920 else if (N3.getOpcode() == ISD::AND)
4921 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4922 // If the and is only masking out bits that cannot effect the shift,
4923 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004924 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004925 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004926 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004927 }
4928 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004929 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004930#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004931
Chris Lattnerf9c19152005-08-25 19:12:10 +00004932 // Memoize the node unless it returns a flag.
4933 SDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004934 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004935 FoldingSetNodeID ID;
4936 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Craig Topperc0196b12014-04-14 00:51:57 +00004937 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004938 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004939 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004940
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004941 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004942 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4943 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004944 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004945 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4946 DL.getDebugLoc(), VTList, Ops[0],
4947 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004948 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004949 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4950 DL.getDebugLoc(), VTList, Ops[0],
4951 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004952 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004953 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4954 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004955 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004956 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004957 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004958 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004959 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4960 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004961 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004962 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4963 DL.getDebugLoc(), VTList, Ops[0],
4964 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004965 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004966 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4967 DL.getDebugLoc(), VTList, Ops[0],
4968 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004969 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004970 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4971 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004972 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004973 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004974 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004975#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004976 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004977#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004978 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004979}
4980
Andrew Trickef9de2a2013-05-25 02:42:55 +00004981SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Topperc0196b12014-04-14 00:51:57 +00004982 return getNode(Opcode, DL, VTList, nullptr, 0);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004983}
4984
Andrew Trickef9de2a2013-05-25 02:42:55 +00004985SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004986 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004987 SDValue Ops[] = { N1 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004988 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004989}
4990
Andrew Trickef9de2a2013-05-25 02:42:55 +00004991SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004992 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004993 SDValue Ops[] = { N1, N2 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004994 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004995}
4996
Andrew Trickef9de2a2013-05-25 02:42:55 +00004997SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004998 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004999 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005000 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005001}
5002
Andrew Trickef9de2a2013-05-25 02:42:55 +00005003SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005004 SDValue N1, SDValue N2, SDValue N3,
5005 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005006 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005007 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005008}
5009
Andrew Trickef9de2a2013-05-25 02:42:55 +00005010SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005011 SDValue N1, SDValue N2, SDValue N3,
5012 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005013 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005014 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005015}
5016
Owen Anderson53aa7a92009-08-10 22:56:29 +00005017SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005018 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005019}
5020
Owen Anderson53aa7a92009-08-10 22:56:29 +00005021SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005022 FoldingSetNodeID ID;
5023 ID.AddInteger(2U);
5024 ID.AddInteger(VT1.getRawBits());
5025 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005026
Craig Topperc0196b12014-04-14 00:51:57 +00005027 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005028 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005029 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005030 EVT *Array = Allocator.Allocate<EVT>(2);
5031 Array[0] = VT1;
5032 Array[1] = VT2;
5033 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5034 VTListMap.InsertNode(Result, IP);
5035 }
5036 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005037}
Dan Gohman17059682008-07-17 19:10:17 +00005038
Owen Anderson53aa7a92009-08-10 22:56:29 +00005039SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005040 FoldingSetNodeID ID;
5041 ID.AddInteger(3U);
5042 ID.AddInteger(VT1.getRawBits());
5043 ID.AddInteger(VT2.getRawBits());
5044 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005045
Craig Topperc0196b12014-04-14 00:51:57 +00005046 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005047 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005048 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005049 EVT *Array = Allocator.Allocate<EVT>(3);
5050 Array[0] = VT1;
5051 Array[1] = VT2;
5052 Array[2] = VT3;
5053 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5054 VTListMap.InsertNode(Result, IP);
5055 }
5056 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005057}
5058
Owen Anderson53aa7a92009-08-10 22:56:29 +00005059SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005060 FoldingSetNodeID ID;
5061 ID.AddInteger(4U);
5062 ID.AddInteger(VT1.getRawBits());
5063 ID.AddInteger(VT2.getRawBits());
5064 ID.AddInteger(VT3.getRawBits());
5065 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005066
Craig Topperc0196b12014-04-14 00:51:57 +00005067 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005068 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005069 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005070 EVT *Array = Allocator.Allocate<EVT>(4);
5071 Array[0] = VT1;
5072 Array[1] = VT2;
5073 Array[2] = VT3;
5074 Array[3] = VT4;
5075 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5076 VTListMap.InsertNode(Result, IP);
5077 }
5078 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005079}
5080
Owen Anderson53aa7a92009-08-10 22:56:29 +00005081SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005082 FoldingSetNodeID ID;
5083 ID.AddInteger(NumVTs);
5084 for (unsigned index = 0; index < NumVTs; index++) {
5085 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005086 }
5087
Craig Topperc0196b12014-04-14 00:51:57 +00005088 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005089 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005090 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005091 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5092 std::copy(VTs, VTs + NumVTs, Array);
5093 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5094 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005095 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005096 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005097}
5098
5099
Chris Lattnerf34156e2006-01-28 09:32:45 +00005100/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5101/// specified operands. If the resultant node already exists in the DAG,
5102/// this does not modify the specified node, instead it returns the node that
5103/// already exists. If the resultant node does not exist in the DAG, the
5104/// input node is returned. As a degenerate case, if you specify the same
5105/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005106SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005107 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005108
Chris Lattnerf34156e2006-01-28 09:32:45 +00005109 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005110 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005111
Chris Lattnerf34156e2006-01-28 09:32:45 +00005112 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005113 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005114 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005115 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005116
Dan Gohmanebeccb42008-07-21 22:38:59 +00005117 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005118 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005119 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005120 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005121
Chris Lattnerf34156e2006-01-28 09:32:45 +00005122 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005123 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005124
Chris Lattnerf34156e2006-01-28 09:32:45 +00005125 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005126 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005127 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005128}
5129
Dan Gohman92c11ac2010-06-18 15:30:29 +00005130SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005131 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005132
Chris Lattnerf34156e2006-01-28 09:32:45 +00005133 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005134 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005135 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005136
Chris Lattnerf34156e2006-01-28 09:32:45 +00005137 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005138 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005139 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005140 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005141
Dan Gohmanebeccb42008-07-21 22:38:59 +00005142 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005143 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005144 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005145 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005146
Chris Lattnerf34156e2006-01-28 09:32:45 +00005147 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005148 if (N->OperandList[0] != Op1)
5149 N->OperandList[0].set(Op1);
5150 if (N->OperandList[1] != Op2)
5151 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005152
Chris Lattnerf34156e2006-01-28 09:32:45 +00005153 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005154 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005155 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005156}
5157
Dan Gohman92c11ac2010-06-18 15:30:29 +00005158SDNode *SelectionDAG::
5159UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005160 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005161 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005162}
5163
Dan Gohman92c11ac2010-06-18 15:30:29 +00005164SDNode *SelectionDAG::
5165UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005166 SDValue Op3, SDValue Op4) {
5167 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005168 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005169}
5170
Dan Gohman92c11ac2010-06-18 15:30:29 +00005171SDNode *SelectionDAG::
5172UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005173 SDValue Op3, SDValue Op4, SDValue Op5) {
5174 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005175 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner580b12a2006-01-28 10:09:25 +00005176}
5177
Dan Gohman92c11ac2010-06-18 15:30:29 +00005178SDNode *SelectionDAG::
5179UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00005180 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005181 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005182
Chris Lattnerf34156e2006-01-28 09:32:45 +00005183 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005184 bool AnyChange = false;
5185 for (unsigned i = 0; i != NumOps; ++i) {
5186 if (Ops[i] != N->getOperand(i)) {
5187 AnyChange = true;
5188 break;
5189 }
5190 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005191
Chris Lattnerf34156e2006-01-28 09:32:45 +00005192 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005193 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005194
Chris Lattnerf34156e2006-01-28 09:32:45 +00005195 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005196 void *InsertPos = nullptr;
Chris Lattner97af9d52006-08-08 01:09:31 +00005197 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005198 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005199
Dan Gohman2f83b472008-05-02 00:05:03 +00005200 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005201 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005202 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005203 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005204
Chris Lattnerf34156e2006-01-28 09:32:45 +00005205 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005206 for (unsigned i = 0; i != NumOps; ++i)
5207 if (N->OperandList[i] != Ops[i])
5208 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005209
5210 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005211 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005212 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005213}
5214
Dan Gohman91697632008-07-07 20:57:48 +00005215/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005216/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005217void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005218 // Unlike the code in MorphNodeTo that does this, we don't need to
5219 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005220 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5221 SDUse &Use = *I++;
5222 Use.set(SDValue());
5223 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005224}
Chris Lattner19732782005-08-16 18:17:10 +00005225
Dan Gohman17059682008-07-17 19:10:17 +00005226/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5227/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005228///
Dan Gohman17059682008-07-17 19:10:17 +00005229SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005230 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005231 SDVTList VTs = getVTList(VT);
Craig Topperc0196b12014-04-14 00:51:57 +00005232 return SelectNodeTo(N, MachineOpc, VTs, nullptr, 0);
Chris Lattner45e1ce42005-08-24 23:00:29 +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) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005237 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005238 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005239 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner19732782005-08-16 18:17:10 +00005240}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005241
Dan Gohman17059682008-07-17 19:10:17 +00005242SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005243 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005244 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005245 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005246 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005247 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner19732782005-08-16 18:17:10 +00005248}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005249
Dan Gohman17059682008-07-17 19:10:17 +00005250SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005251 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005252 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005253 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005254 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005255 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner19732782005-08-16 18:17:10 +00005256}
Chris Lattner466fece2005-08-21 22:30:30 +00005257
Dan Gohman17059682008-07-17 19:10:17 +00005258SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005259 EVT VT, const SDValue *Ops,
Evan Cheng849f4bf2006-08-27 08:08:54 +00005260 unsigned NumOps) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005261 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005262 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005263}
5264
Dan Gohman17059682008-07-17 19:10:17 +00005265SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005266 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005267 unsigned NumOps) {
5268 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman17059682008-07-17 19:10:17 +00005269 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005270}
5271
Dan Gohman17059682008-07-17 19:10:17 +00005272SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005273 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005274 SDVTList VTs = getVTList(VT1, VT2);
Craig Topperc0196b12014-04-14 00:51:57 +00005275 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)nullptr, 0);
Dan Gohman22e97072008-07-02 23:23:19 +00005276}
5277
Dan Gohman17059682008-07-17 19:10:17 +00005278SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005279 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005280 const SDValue *Ops, unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005281 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohman17059682008-07-17 19:10:17 +00005282 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005283}
5284
Bill Wendling2d598632008-12-01 23:28:22 +00005285SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005286 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling2d598632008-12-01 23:28:22 +00005287 const SDValue *Ops, unsigned NumOps) {
5288 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5289 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5290}
5291
Scott Michelcf0da6c2009-02-17 22:15:04 +00005292SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005293 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005294 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005295 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005296 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005297 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth683352382006-01-23 21:51:14 +00005298}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005299
Scott Michelcf0da6c2009-02-17 22:15:04 +00005300SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005301 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005302 SDValue Op1, SDValue Op2) {
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 };
Dan Gohman17059682008-07-17 19:10:17 +00005305 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattnerf090f7e2005-11-19 01:44:53 +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,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005310 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005311 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005312 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005313 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005314 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman22e97072008-07-02 23:23:19 +00005315}
5316
Dan Gohman17059682008-07-17 19:10:17 +00005317SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005318 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005319 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005320 SDValue Op3) {
5321 SDVTList VTs = getVTList(VT1, VT2, VT3);
5322 SDValue Ops[] = { Op1, Op2, Op3 };
5323 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5324}
5325
5326SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005327 SDVTList VTs, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005328 unsigned NumOps) {
Chris Lattner625916d2010-02-23 23:01:35 +00005329 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5330 // Reset the NodeID to -1.
5331 N->setNodeId(-1);
5332 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005333}
5334
Andrew Trickef9de2a2013-05-25 02:42:55 +00005335/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005336/// the line number information on the merged node since it is not possible to
5337/// preserve the information that operation is associated with multiple lines.
5338/// This will make the debugger working better at -O0, were there is a higher
5339/// probability having other instructions associated with that line.
5340///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005341/// For IROrder, we keep the smaller of the two
5342SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005343 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005344 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5345 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005346 N->setDebugLoc(DebugLoc());
5347 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005348 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5349 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005350 return N;
5351}
5352
Chris Lattneraf197502010-02-28 21:36:14 +00005353/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005354/// return type, opcode, and operands.
5355///
5356/// Note that MorphNodeTo returns the resultant node. If there is already a
5357/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005358/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005359///
5360/// Using MorphNodeTo is faster than creating a new node and swapping it in
5361/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005362/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005363/// the node's users.
5364///
5365SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005366 SDVTList VTs, const SDValue *Ops,
Dan Gohman17059682008-07-17 19:10:17 +00005367 unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005368 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005369 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005370 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005371 FoldingSetNodeID ID;
5372 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendling022d18f2009-12-18 23:32:53 +00005373 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005374 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005375 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005376
Dan Gohmand3fe1742008-09-13 01:54:27 +00005377 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005378 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005379
Dan Gohman17059682008-07-17 19:10:17 +00005380 // Start the morphing.
5381 N->NodeType = Opc;
5382 N->ValueList = VTs.VTs;
5383 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005384
Dan Gohman17059682008-07-17 19:10:17 +00005385 // Clear the operands list, updating used nodes to remove this from their
5386 // use list. Keep track of any operands that become dead as a result.
5387 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005388 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5389 SDUse &Use = *I++;
5390 SDNode *Used = Use.getNode();
5391 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005392 if (Used->use_empty())
5393 DeadNodeSet.insert(Used);
5394 }
5395
Dan Gohman48b185d2009-09-25 20:36:54 +00005396 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5397 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005398 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005399 // If NumOps is larger than the # of operands we can have in a
5400 // MachineSDNode, reallocate the operand list.
5401 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5402 if (MN->OperandsNeedDelete)
5403 delete[] MN->OperandList;
5404 if (NumOps > array_lengthof(MN->LocalOperands))
5405 // We're creating a final node that will live unmorphed for the
5406 // remainder of the current SelectionDAG iteration, so we can allocate
5407 // the operands directly out of a pool with no recycling metadata.
5408 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman01c65a22010-03-18 18:49:47 +00005409 Ops, NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005410 else
5411 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5412 MN->OperandsNeedDelete = false;
5413 } else
5414 MN->InitOperands(MN->OperandList, Ops, NumOps);
5415 } else {
5416 // If NumOps is larger than the # of operands we currently have, reallocate
5417 // the operand list.
5418 if (NumOps > N->NumOperands) {
5419 if (N->OperandsNeedDelete)
5420 delete[] N->OperandList;
5421 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005422 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005423 } else
Anton Korobeynikov86263672009-10-22 00:15:17 +00005424 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005425 }
5426
5427 // Delete any nodes that are still dead after adding the uses for the
5428 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005429 if (!DeadNodeSet.empty()) {
5430 SmallVector<SDNode *, 16> DeadNodes;
5431 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5432 E = DeadNodeSet.end(); I != E; ++I)
5433 if ((*I)->use_empty())
5434 DeadNodes.push_back(*I);
5435 RemoveDeadNodes(DeadNodes);
5436 }
Dan Gohman91697632008-07-07 20:57:48 +00005437
Dan Gohman17059682008-07-17 19:10:17 +00005438 if (IP)
5439 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005440 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005441}
5442
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005443
Dan Gohman32f71d72009-09-25 18:54:59 +00005444/// getMachineNode - These are used for target selectors to create a new node
5445/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005446///
Dan Gohman32f71d72009-09-25 18:54:59 +00005447/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005448/// node of the specified opcode and operands, it returns that node instead of
5449/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005450MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005451SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005452 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005453 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005454}
Bill Wendlinga434d932009-01-29 09:01:55 +00005455
Dan Gohmana22f2d82009-10-10 01:29:16 +00005456MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005457SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005458 SDVTList VTs = getVTList(VT);
5459 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005460 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005461}
Bill Wendlinga434d932009-01-29 09:01:55 +00005462
Dan Gohmana22f2d82009-10-10 01:29:16 +00005463MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005464SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005465 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005466 SDVTList VTs = getVTList(VT);
5467 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005468 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005469}
Bill Wendlinga434d932009-01-29 09:01:55 +00005470
Dan Gohmana22f2d82009-10-10 01:29:16 +00005471MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005472SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005473 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005474 SDVTList VTs = getVTList(VT);
5475 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005476 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005477}
Bill Wendlinga434d932009-01-29 09:01:55 +00005478
Dan Gohmana22f2d82009-10-10 01:29:16 +00005479MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005480SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005481 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005482 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005483 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005484}
Bill Wendlinga434d932009-01-29 09:01:55 +00005485
Dan Gohmana22f2d82009-10-10 01:29:16 +00005486MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005487SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005488 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005489 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005490}
Bill Wendlinga434d932009-01-29 09:01:55 +00005491
Dan Gohmana22f2d82009-10-10 01:29:16 +00005492MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005493SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005494 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005495 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005496 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005497 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005498}
Bill Wendlinga434d932009-01-29 09:01:55 +00005499
Dan Gohmana22f2d82009-10-10 01:29:16 +00005500MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005501SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005502 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005503 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005504 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005505 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005506}
5507
Dan Gohmana22f2d82009-10-10 01:29:16 +00005508MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005509SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005510 EVT VT1, EVT VT2, SDValue Op1,
5511 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005512 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005513 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005514 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005515}
5516
Dan Gohmana22f2d82009-10-10 01:29:16 +00005517MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005518SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005519 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005520 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005521 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005522 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005523}
Bill Wendlinga434d932009-01-29 09:01:55 +00005524
Dan Gohmana22f2d82009-10-10 01:29:16 +00005525MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005526SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005527 EVT VT1, EVT VT2, EVT VT3,
5528 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005529 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005530 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005531 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005532}
Bill Wendlinga434d932009-01-29 09:01:55 +00005533
Dan Gohmana22f2d82009-10-10 01:29:16 +00005534MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005535SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005536 EVT VT1, EVT VT2, EVT VT3,
5537 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005538 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005539 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005540 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005541}
Bill Wendlinga434d932009-01-29 09:01:55 +00005542
Dan Gohmana22f2d82009-10-10 01:29:16 +00005543MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005544SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005545 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005546 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005547 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005548 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005549}
Bill Wendlinga434d932009-01-29 09:01:55 +00005550
Dan Gohmana22f2d82009-10-10 01:29:16 +00005551MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005552SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005553 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005554 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005555 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005556 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005557}
5558
Dan Gohmana22f2d82009-10-10 01:29:16 +00005559MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005560SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005561 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005562 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005563 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liaob53d8962013-04-19 22:22:57 +00005564 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005565}
5566
Dan Gohmana22f2d82009-10-10 01:29:16 +00005567MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005568SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005569 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005570 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005571 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005572 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005573 const SDValue *Ops = OpsArray.data();
5574 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005575
5576 if (DoCSE) {
5577 FoldingSetNodeID ID;
5578 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
Craig Topperc0196b12014-04-14 00:51:57 +00005579 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005580 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005581 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005582 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005583 }
5584
5585 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005586 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5587 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005588
5589 // Initialize the operands list.
5590 if (NumOps > array_lengthof(N->LocalOperands))
5591 // We're creating a final node that will live unmorphed for the
5592 // remainder of the current SelectionDAG iteration, so we can allocate
5593 // the operands directly out of a pool with no recycling metadata.
5594 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5595 Ops, NumOps);
5596 else
5597 N->InitOperands(N->LocalOperands, Ops, NumOps);
5598 N->OperandsNeedDelete = false;
5599
5600 if (DoCSE)
5601 CSEMap.InsertNode(N, IP);
5602
5603 AllNodes.push_back(N);
5604#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005605 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005606#endif
5607 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005608}
Evan Chengd3f1db92006-02-09 07:15:23 +00005609
Dan Gohmanac33a902009-08-19 18:16:17 +00005610/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005611/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005612SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005613SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005614 SDValue Operand) {
5615 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005616 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005617 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005618 return SDValue(Subreg, 0);
5619}
5620
Bob Wilson2a45a652009-10-08 18:49:46 +00005621/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005622/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005623SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005624SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005625 SDValue Operand, SDValue Subreg) {
5626 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005627 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005628 VT, Operand, Subreg, SRIdxVal);
5629 return SDValue(Result, 0);
5630}
5631
Evan Cheng31604a62008-03-22 01:55:50 +00005632/// getNodeIfExists - Get the specified node if it's already available, or
5633/// else return NULL.
5634SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005635 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005636 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005637 FoldingSetNodeID ID;
5638 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Craig Topperc0196b12014-04-14 00:51:57 +00005639 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005641 return E;
5642 }
Craig Topperc0196b12014-04-14 00:51:57 +00005643 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005644}
5645
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005646/// getDbgValue - Creates a SDDbgValue node.
5647///
5648SDDbgValue *
5649SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5650 DebugLoc DL, unsigned O) {
5651 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5652}
5653
5654SDDbgValue *
Dan Gohmanbcaf6812010-04-15 01:51:59 +00005655SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005656 DebugLoc DL, unsigned O) {
5657 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5658}
5659
5660SDDbgValue *
5661SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5662 DebugLoc DL, unsigned O) {
5663 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5664}
5665
Dan Gohman7d099f72010-03-03 21:33:37 +00005666namespace {
5667
Dan Gohman9cc886b2010-03-04 19:11:28 +00005668/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005669/// pointed to by a use iterator is deleted, increment the use iterator
5670/// so that it doesn't dangle.
5671///
Dan Gohman7d099f72010-03-03 21:33:37 +00005672class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005673 SDNode::use_iterator &UI;
5674 SDNode::use_iterator &UE;
5675
Craig Topper7b883b32014-03-08 06:31:39 +00005676 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005677 // Increment the iterator as needed.
5678 while (UI != UE && N == *UI)
5679 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005680 }
5681
5682public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005683 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005684 SDNode::use_iterator &ui,
5685 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005686 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005687};
5688
5689}
5690
Evan Cheng445b91a2006-08-07 22:13:29 +00005691/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005692/// This can cause recursive merging of nodes in the DAG.
5693///
Chris Lattner76858912008-02-03 03:35:22 +00005694/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005695///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005696void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005697 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005698 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005699 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005700 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005701
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005702 // Iterate over all the existing uses of From. New uses will be added
5703 // to the beginning of the use list, which we avoid visiting.
5704 // This specifically avoids visiting uses of From that arise while the
5705 // replacement is happening, because any such uses would be the result
5706 // of CSE: If an existing node looks like From after one of its operands
5707 // is replaced by To, we don't want to replace of all its users with To
5708 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005709 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005710 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005711 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005712 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005713
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005714 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005715 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005716
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005717 // A user can appear in a use list multiple times, and when this
5718 // happens the uses are usually next to each other in the list.
5719 // To help reduce the number of CSE recomputations, process all
5720 // the uses of this user that we can find this way.
5721 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005722 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005723 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005724 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005725 } while (UI != UE && *UI == User);
5726
5727 // Now that we have modified User, add it back to the CSE maps. If it
5728 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005729 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005730 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005731
5732 // If we just RAUW'd the root, take note.
5733 if (FromN == getRoot())
5734 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005735}
5736
5737/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5738/// This can cause recursive merging of nodes in the DAG.
5739///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005740/// This version assumes that for each value of From, there is a
5741/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005742///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005743void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005744#ifndef NDEBUG
5745 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5746 assert((!From->hasAnyUseOfValue(i) ||
5747 From->getValueType(i) == To->getValueType(i)) &&
5748 "Cannot use this version of ReplaceAllUsesWith!");
5749#endif
Dan Gohman17059682008-07-17 19:10:17 +00005750
5751 // Handle the trivial case.
5752 if (From == To)
5753 return;
5754
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005755 // Iterate over just the existing users of From. See the comments in
5756 // the ReplaceAllUsesWith above.
5757 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005758 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005759 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005760 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005761
Chris Lattner373f0482005-08-26 18:36:28 +00005762 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005763 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005764
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005765 // A user can appear in a use list multiple times, and when this
5766 // happens the uses are usually next to each other in the list.
5767 // To help reduce the number of CSE recomputations, process all
5768 // the uses of this user that we can find this way.
5769 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005770 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005771 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005772 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005773 } while (UI != UE && *UI == User);
5774
5775 // Now that we have modified User, add it back to the CSE maps. If it
5776 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005777 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005778 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005779
5780 // If we just RAUW'd the root, take note.
5781 if (From == getRoot().getNode())
5782 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005783}
5784
Chris Lattner373f0482005-08-26 18:36:28 +00005785/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5786/// This can cause recursive merging of nodes in the DAG.
5787///
5788/// This version can replace From with any result values. To must match the
5789/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005790void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005791 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005792 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005793
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005794 // Iterate over just the existing users of From. See the comments in
5795 // the ReplaceAllUsesWith above.
5796 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005797 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005798 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005799 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005800
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005801 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005802 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005803
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005804 // A user can appear in a use list multiple times, and when this
5805 // happens the uses are usually next to each other in the list.
5806 // To help reduce the number of CSE recomputations, process all
5807 // the uses of this user that we can find this way.
5808 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005809 SDUse &Use = UI.getUse();
5810 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005811 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005812 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005813 } while (UI != UE && *UI == User);
5814
5815 // Now that we have modified User, add it back to the CSE maps. If it
5816 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005817 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005818 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005819
5820 // If we just RAUW'd the root, take note.
5821 if (From == getRoot().getNode())
5822 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005823}
5824
Chris Lattner375e1a72006-02-17 21:58:01 +00005825/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005826/// uses of other values produced by From.getNode() alone. The Deleted
5827/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005828void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005829 // Handle the really simple, really trivial case efficiently.
5830 if (From == To) return;
5831
Chris Lattner375e1a72006-02-17 21:58:01 +00005832 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005833 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005834 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005835 return;
5836 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005837
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005838 // Iterate over just the existing users of From. See the comments in
5839 // the ReplaceAllUsesWith above.
5840 SDNode::use_iterator UI = From.getNode()->use_begin(),
5841 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005842 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005843 while (UI != UE) {
5844 SDNode *User = *UI;
5845 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005846
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005847 // A user can appear in a use list multiple times, and when this
5848 // happens the uses are usually next to each other in the list.
5849 // To help reduce the number of CSE recomputations, process all
5850 // the uses of this user that we can find this way.
5851 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005852 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005853
5854 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005855 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005856 ++UI;
5857 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005858 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005859
5860 // If this node hasn't been modified yet, it's still in the CSE maps,
5861 // so remove its old self from the CSE maps.
5862 if (!UserRemovedFromCSEMaps) {
5863 RemoveNodeFromCSEMaps(User);
5864 UserRemovedFromCSEMaps = true;
5865 }
5866
5867 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005868 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005869 } while (UI != UE && *UI == User);
5870
5871 // We are iterating over all uses of the From node, so if a use
5872 // doesn't use the specific value, no changes are made.
5873 if (!UserRemovedFromCSEMaps)
5874 continue;
5875
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005876 // Now that we have modified User, add it back to the CSE maps. If it
5877 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005878 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005879 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005880
5881 // If we just RAUW'd the root, take note.
5882 if (From == getRoot())
5883 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005884}
5885
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005886namespace {
5887 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5888 /// to record information about a use.
5889 struct UseMemo {
5890 SDNode *User;
5891 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005892 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005893 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005894
5895 /// operator< - Sort Memos by User.
5896 bool operator<(const UseMemo &L, const UseMemo &R) {
5897 return (intptr_t)L.User < (intptr_t)R.User;
5898 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005899}
5900
Dan Gohman17059682008-07-17 19:10:17 +00005901/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005902/// uses of other values produced by From.getNode() alone. The same value
5903/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005904/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005905void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5906 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005907 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005908 // Handle the simple, trivial case efficiently.
5909 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005910 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005911
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005912 // Read up all the uses and make records of them. This helps
5913 // processing new uses that are introduced during the
5914 // replacement process.
5915 SmallVector<UseMemo, 4> Uses;
5916 for (unsigned i = 0; i != Num; ++i) {
5917 unsigned FromResNo = From[i].getResNo();
5918 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005919 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005920 E = FromNode->use_end(); UI != E; ++UI) {
5921 SDUse &Use = UI.getUse();
5922 if (Use.getResNo() == FromResNo) {
5923 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005924 Uses.push_back(Memo);
5925 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005926 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005927 }
Dan Gohman17059682008-07-17 19:10:17 +00005928
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005929 // Sort the uses, so that all the uses from a given User are together.
5930 std::sort(Uses.begin(), Uses.end());
5931
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005932 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5933 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005934 // We know that this user uses some value of From. If it is the right
5935 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005936 SDNode *User = Uses[UseIndex].User;
5937
5938 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005939 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005940
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005941 // The Uses array is sorted, so all the uses for a given User
5942 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005943 // To help reduce the number of CSE recomputations, process all
5944 // the uses of this user that we can find this way.
5945 do {
5946 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005947 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005948 ++UseIndex;
5949
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005950 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005951 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5952
Dan Gohman17059682008-07-17 19:10:17 +00005953 // Now that we have modified User, add it back to the CSE maps. If it
5954 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005955 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005956 }
5957}
5958
Evan Cheng9631a602006-08-01 08:20:41 +00005959/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005960/// based on their topological order. It returns the maximum id and a vector
5961/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005962unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005963
Dan Gohman86aa16a2008-09-30 18:30:35 +00005964 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005965
Dan Gohman86aa16a2008-09-30 18:30:35 +00005966 // SortedPos tracks the progress of the algorithm. Nodes before it are
5967 // sorted, nodes after it are unsorted. When the algorithm completes
5968 // it is at the end of the list.
5969 allnodes_iterator SortedPos = allnodes_begin();
5970
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005971 // Visit all the nodes. Move nodes with no operands to the front of
5972 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005973 // operand count. Before we do this, the Node Id fields of the nodes
5974 // may contain arbitrary values. After, the Node Id fields for nodes
5975 // before SortedPos will contain the topological sort index, and the
5976 // Node Id fields for nodes At SortedPos and after will contain the
5977 // count of outstanding operands.
5978 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5979 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005980 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005981 unsigned Degree = N->getNumOperands();
5982 if (Degree == 0) {
5983 // A node with no uses, add it to the result array immediately.
5984 N->setNodeId(DAGSize++);
5985 allnodes_iterator Q = N;
5986 if (Q != SortedPos)
5987 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005988 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005989 ++SortedPos;
5990 } else {
5991 // Temporarily use the Node Id as scratch space for the degree count.
5992 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005993 }
5994 }
5995
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005996 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005997 // such that by the time the end is reached all nodes will be sorted.
5998 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5999 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00006000 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00006001 // N is in sorted position, so all its uses have one less operand
6002 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006003 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6004 UI != UE; ++UI) {
6005 SDNode *P = *UI;
6006 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006007 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006008 --Degree;
6009 if (Degree == 0) {
6010 // All of P's operands are sorted, so P may sorted now.
6011 P->setNodeId(DAGSize++);
6012 if (P != SortedPos)
6013 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006014 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006015 ++SortedPos;
6016 } else {
6017 // Update P's outstanding operand count.
6018 P->setNodeId(Degree);
6019 }
6020 }
David Greene3b2a68c2010-01-20 00:59:23 +00006021 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006022#ifndef NDEBUG
6023 SDNode *S = ++I;
6024 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00006025 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00006026#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006027 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006028 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006029 }
6030
6031 assert(SortedPos == AllNodes.end() &&
6032 "Topological sort incomplete!");
6033 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6034 "First node in topological sort is not the entry token!");
6035 assert(AllNodes.front().getNodeId() == 0 &&
6036 "First node in topological sort has non-zero id!");
6037 assert(AllNodes.front().getNumOperands() == 0 &&
6038 "First node in topological sort has operands!");
6039 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6040 "Last node in topologic sort has unexpected id!");
6041 assert(AllNodes.back().use_empty() &&
6042 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006043 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006044 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006045}
6046
Evan Cheng563fe3c2010-03-25 01:38:16 +00006047/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6048/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006049void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6050 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006051 if (SD)
6052 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006053}
Evan Cheng29eefc12006-07-27 06:39:06 +00006054
Devang Patelefc6b162011-01-25 23:27:42 +00006055/// TransferDbgValues - Transfer SDDbgValues.
6056void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6057 if (From == To || !From.getNode()->getHasDebugValue())
6058 return;
6059 SDNode *FromNode = From.getNode();
6060 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006061 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006062 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006063 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006064 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006065 SDDbgValue *Dbg = *I;
6066 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6067 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
6068 Dbg->getOffset(), Dbg->getDebugLoc(),
6069 Dbg->getOrder());
6070 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006071 }
6072 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006073 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006074 E = ClonedDVs.end(); I != E; ++I)
6075 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006076}
6077
Jim Laskeyd66e6162005-08-17 20:08:02 +00006078//===----------------------------------------------------------------------===//
6079// SDNode Class
6080//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006081
Chris Lattner3bf17b62007-02-04 02:41:42 +00006082HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006083 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006084}
6085
Andrew Trickef9de2a2013-05-25 02:42:55 +00006086GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6087 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006088 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006089 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006090 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006091}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006092
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006093AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6094 SDValue X, unsigned SrcAS,
6095 unsigned DestAS)
6096 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6097 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6098
Andrew Trickef9de2a2013-05-25 02:42:55 +00006099MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6100 EVT memvt, MachineMemOperand *mmo)
6101 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006102 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006103 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006104 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006105 assert(isNonTemporal() == MMO->isNonTemporal() &&
6106 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006107 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006108}
6109
Andrew Trickef9de2a2013-05-25 02:42:55 +00006110MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peck527da1b2010-11-23 03:31:01 +00006111 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohman48b185d2009-09-25 20:36:54 +00006112 MachineMemOperand *mmo)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006113 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohman48b185d2009-09-25 20:36:54 +00006114 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006115 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006116 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006117 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6118 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006119}
6120
Jim Laskeyf576b422006-10-27 23:46:08 +00006121/// Profile - Gather unique data for the node.
6122///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006123void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006124 AddNodeIDNode(ID, this);
6125}
6126
Owen Anderson3b1665e2009-08-25 22:27:22 +00006127namespace {
6128 struct EVTArray {
6129 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006130
Owen Anderson3b1665e2009-08-25 22:27:22 +00006131 EVTArray() {
6132 VTs.reserve(MVT::LAST_VALUETYPE);
6133 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6134 VTs.push_back(MVT((MVT::SimpleValueType)i));
6135 }
6136 };
6137}
6138
Owen Anderson53aa7a92009-08-10 22:56:29 +00006139static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006140static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006141static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006142
Chris Lattner88fa11c2005-11-08 23:30:28 +00006143/// getValueTypeList - Return a pointer to the specified value type.
6144///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006145const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006146 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006147 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006148 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006149 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006150 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006151 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006152 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006153 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006154}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006155
Chris Lattner40e79822005-01-12 18:37:47 +00006156/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6157/// indicated value. This method ignores uses of other values defined by this
6158/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006159bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006160 assert(Value < getNumValues() && "Bad value!");
6161
Roman Levenstein51f532f2008-04-07 10:06:32 +00006162 // TODO: Only iterate over uses of a given value of the node
6163 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006164 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006165 if (NUses == 0)
6166 return false;
6167 --NUses;
6168 }
Chris Lattner40e79822005-01-12 18:37:47 +00006169 }
6170
6171 // Found exactly the right number of uses?
6172 return NUses == 0;
6173}
6174
6175
Evan Cheng358c3d12007-08-02 05:29:38 +00006176/// hasAnyUseOfValue - Return true if there are any use of the indicated
6177/// value. This method ignores uses of other values defined by this operation.
6178bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6179 assert(Value < getNumValues() && "Bad value!");
6180
Dan Gohman7a510c22008-07-09 22:39:01 +00006181 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006182 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006183 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006184
6185 return false;
6186}
6187
6188
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006189/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006190///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006191bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006192 bool Seen = false;
6193 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006194 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006195 if (User == this)
6196 Seen = true;
6197 else
6198 return false;
6199 }
6200
6201 return Seen;
6202}
6203
Evan Cheng9456dd82006-11-03 07:31:32 +00006204/// isOperand - Return true if this node is an operand of N.
6205///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006206bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006207 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6208 if (*this == N->getOperand(i))
6209 return true;
6210 return false;
6211}
6212
Evan Cheng567d2e52008-03-04 00:41:45 +00006213bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006214 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006215 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006216 return true;
6217 return false;
6218}
Evan Chengd37645c2006-02-05 06:29:23 +00006219
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006220/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006221/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006222/// side-effecting instructions on any chain path. In practice, this looks
6223/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006224/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006225bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006226 unsigned Depth) const {
6227 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006228
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006229 // Don't search too deeply, we just want to be able to see through
6230 // TokenFactor's etc.
6231 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006232
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006233 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006234 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006235 if (getOpcode() == ISD::TokenFactor) {
6236 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006237 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6238 return false;
6239 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006240 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006241
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006242 // Loads don't have side effects, look through them.
6243 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6244 if (!Ld->isVolatile())
6245 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6246 }
6247 return false;
6248}
6249
Lang Hames5a004992011-07-07 04:31:51 +00006250/// hasPredecessor - Return true if N is a predecessor of this node.
6251/// N is either an operand of this node, or can be reached by recursively
6252/// traversing up the operands.
6253/// NOTE: This is an expensive method. Use it carefully.
6254bool SDNode::hasPredecessor(const SDNode *N) const {
6255 SmallPtrSet<const SDNode *, 32> Visited;
6256 SmallVector<const SDNode *, 16> Worklist;
6257 return hasPredecessorHelper(N, Visited, Worklist);
6258}
Dan Gohmancd139c02009-10-28 03:44:30 +00006259
Craig Topperb94011f2013-07-14 04:42:23 +00006260bool
6261SDNode::hasPredecessorHelper(const SDNode *N,
6262 SmallPtrSet<const SDNode *, 32> &Visited,
6263 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006264 if (Visited.empty()) {
6265 Worklist.push_back(this);
6266 } else {
6267 // Take a look in the visited set. If we've already encountered this node
6268 // we needn't search further.
6269 if (Visited.count(N))
6270 return true;
6271 }
6272
6273 // Haven't visited N yet. Continue the search.
6274 while (!Worklist.empty()) {
6275 const SDNode *M = Worklist.pop_back_val();
6276 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6277 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006278 if (Visited.insert(Op))
6279 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006280 if (Op == N)
6281 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006282 }
Lang Hames5a004992011-07-07 04:31:51 +00006283 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006284
6285 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006286}
6287
Evan Cheng5d9fd972006-10-04 00:56:09 +00006288uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6289 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006290 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006291}
6292
Mon P Wang32f8bb92009-11-30 02:42:02 +00006293SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6294 assert(N->getNumValues() == 1 &&
6295 "Can't unroll a vector with multiple results!");
6296
6297 EVT VT = N->getValueType(0);
6298 unsigned NE = VT.getVectorNumElements();
6299 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006300 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006301
6302 SmallVector<SDValue, 8> Scalars;
6303 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6304
6305 // If ResNE is 0, fully unroll the vector op.
6306 if (ResNE == 0)
6307 ResNE = NE;
6308 else if (NE > ResNE)
6309 NE = ResNE;
6310
6311 unsigned i;
6312 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006313 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006314 SDValue Operand = N->getOperand(j);
6315 EVT OperandVT = Operand.getValueType();
6316 if (OperandVT.isVector()) {
6317 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006318 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006319 EVT OperandEltVT = OperandVT.getVectorElementType();
6320 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6321 OperandEltVT,
6322 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006323 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006324 } else {
6325 // A scalar operand; just use it as is.
6326 Operands[j] = Operand;
6327 }
6328 }
6329
6330 switch (N->getOpcode()) {
6331 default:
6332 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6333 &Operands[0], Operands.size()));
6334 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006335 case ISD::VSELECT:
6336 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6337 &Operands[0], Operands.size()));
6338 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006339 case ISD::SHL:
6340 case ISD::SRA:
6341 case ISD::SRL:
6342 case ISD::ROTL:
6343 case ISD::ROTR:
6344 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006345 getShiftAmountOperand(Operands[0].getValueType(),
6346 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006347 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006348 case ISD::SIGN_EXTEND_INREG:
6349 case ISD::FP_ROUND_INREG: {
6350 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6351 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6352 Operands[0],
6353 getValueType(ExtVT)));
6354 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006355 }
6356 }
6357
6358 for (; i < ResNE; ++i)
6359 Scalars.push_back(getUNDEF(EltVT));
6360
6361 return getNode(ISD::BUILD_VECTOR, dl,
6362 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6363 &Scalars[0], Scalars.size());
6364}
6365
Evan Chengf5938d52009-12-09 01:36:00 +00006366
Wesley Peck527da1b2010-11-23 03:31:01 +00006367/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6368/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006369/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006370bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006371 unsigned Bytes, int Dist) const {
6372 if (LD->getChain() != Base->getChain())
6373 return false;
6374 EVT VT = LD->getValueType(0);
6375 if (VT.getSizeInBits() / 8 != Bytes)
6376 return false;
6377
6378 SDValue Loc = LD->getOperand(1);
6379 SDValue BaseLoc = Base->getOperand(1);
6380 if (Loc.getOpcode() == ISD::FrameIndex) {
6381 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6382 return false;
6383 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6384 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6385 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6386 int FS = MFI->getObjectSize(FI);
6387 int BFS = MFI->getObjectSize(BFI);
6388 if (FS != BFS || FS != (int)Bytes) return false;
6389 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6390 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006391
6392 // Handle X+C
6393 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6394 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6395 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006396
Craig Topperc0196b12014-04-14 00:51:57 +00006397 const GlobalValue *GV1 = nullptr;
6398 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006399 int64_t Offset1 = 0;
6400 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006401 const TargetLowering *TLI = TM.getTargetLowering();
6402 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6403 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006404 if (isGA1 && isGA2 && GV1 == GV2)
6405 return Offset1 == (Offset2 + Dist*Bytes);
6406 return false;
6407}
6408
6409
Evan Cheng34a23ea2009-12-09 01:04:59 +00006410/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6411/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006412unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006413 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006414 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006415 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006416 const TargetLowering *TLI = TM.getTargetLowering();
6417 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006418 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006419 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00006420 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006421 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006422 unsigned AlignBits = KnownZero.countTrailingOnes();
6423 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6424 if (Align)
6425 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006426 }
Evan Chengd938faf2009-12-09 01:53:58 +00006427
Evan Cheng34a23ea2009-12-09 01:04:59 +00006428 // If this is a direct reference to a stack slot, use information about the
6429 // stack slot's alignment.
6430 int FrameIdx = 1 << 31;
6431 int64_t FrameOffset = 0;
6432 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6433 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006434 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006435 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006436 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006437 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6438 FrameOffset = Ptr.getConstantOperandVal(1);
6439 }
6440
6441 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006442 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006443 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6444 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006445 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006446 }
6447
6448 return 0;
6449}
6450
Juergen Ributzkab3487102013-11-19 21:20:17 +00006451/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6452/// which is split (or expanded) into two not necessarily identical pieces.
6453std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6454 // Currently all types are split in half.
6455 EVT LoVT, HiVT;
6456 if (!VT.isVector()) {
6457 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6458 } else {
6459 unsigned NumElements = VT.getVectorNumElements();
6460 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6461 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6462 NumElements/2);
6463 }
6464 return std::make_pair(LoVT, HiVT);
6465}
6466
6467/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6468/// low/high part.
6469std::pair<SDValue, SDValue>
6470SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6471 const EVT &HiVT) {
6472 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6473 N.getValueType().getVectorNumElements() &&
6474 "More vector elements requested than available!");
6475 SDValue Lo, Hi;
6476 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6477 getConstant(0, TLI->getVectorIdxTy()));
6478 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6479 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6480 return std::make_pair(Lo, Hi);
6481}
6482
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006483void SelectionDAG::ExtractVectorElements(SDValue Op,
6484 SmallVectorImpl<SDValue> &Args,
6485 unsigned Start, unsigned Count) {
6486 EVT VT = Op.getValueType();
6487 if (Count == 0)
6488 Count = VT.getVectorNumElements();
6489
6490 EVT EltVT = VT.getVectorElementType();
6491 EVT IdxTy = TLI->getVectorIdxTy();
6492 SDLoc SL(Op);
6493 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6494 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6495 Op, getConstant(i, IdxTy)));
6496 }
6497}
6498
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006499// getAddressSpace - Return the address space this GlobalAddress belongs to.
6500unsigned GlobalAddressSDNode::getAddressSpace() const {
6501 return getGlobal()->getType()->getAddressSpace();
6502}
6503
6504
Chris Lattner229907c2011-07-18 04:54:35 +00006505Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006506 if (isMachineConstantPoolEntry())
6507 return Val.MachineCPVal->getType();
6508 return Val.ConstVal->getType();
6509}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006510
Bob Wilson85cefe82009-03-02 23:24:16 +00006511bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6512 APInt &SplatUndef,
6513 unsigned &SplatBitSize,
6514 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006515 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006516 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006517 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006518 assert(VT.isVector() && "Expected a vector type");
6519 unsigned sz = VT.getSizeInBits();
6520 if (MinSplatBits > sz)
6521 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006522
Bob Wilson85cefe82009-03-02 23:24:16 +00006523 SplatValue = APInt(sz, 0);
6524 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006525
Bob Wilson85cefe82009-03-02 23:24:16 +00006526 // Get the bits. Bits with undefined values (when the corresponding element
6527 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6528 // in SplatValue. If any of the values are not constant, give up and return
6529 // false.
6530 unsigned int nOps = getNumOperands();
6531 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6532 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006533
6534 for (unsigned j = 0; j < nOps; ++j) {
6535 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006536 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006537 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006538
Bob Wilson85cefe82009-03-02 23:24:16 +00006539 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006540 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006541 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006542 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006543 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006544 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006545 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006546 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006547 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006548 }
6549
Bob Wilson85cefe82009-03-02 23:24:16 +00006550 // The build_vector is all constants or undefs. Find the smallest element
6551 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006552
Bob Wilson85cefe82009-03-02 23:24:16 +00006553 HasAnyUndefs = (SplatUndef != 0);
6554 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006555
Bob Wilson85cefe82009-03-02 23:24:16 +00006556 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006557 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6558 APInt LowValue = SplatValue.trunc(HalfSize);
6559 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6560 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006561
Bob Wilson85cefe82009-03-02 23:24:16 +00006562 // If the two halves do not match (ignoring undef bits), stop here.
6563 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6564 MinSplatBits > HalfSize)
6565 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006566
Bob Wilson85cefe82009-03-02 23:24:16 +00006567 SplatValue = HighValue | LowValue;
6568 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006569
Bob Wilson85cefe82009-03-02 23:24:16 +00006570 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006571 }
6572
Bob Wilson85cefe82009-03-02 23:24:16 +00006573 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006574 return true;
6575}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006576
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006577ConstantSDNode *BuildVectorSDNode::getConstantSplatValue() const {
Matt Arsenault985b9de2014-03-17 18:58:01 +00006578 SDValue Op0 = getOperand(0);
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006579 if (Op0.getOpcode() != ISD::Constant)
6580 return nullptr;
6581
6582 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
6583 if (getOperand(i) != Op0)
Matt Arsenault985b9de2014-03-17 18:58:01 +00006584 return nullptr;
Matt Arsenault985b9de2014-03-17 18:58:01 +00006585
6586 return cast<ConstantSDNode>(Op0);
6587}
6588
Juergen Ributzka73844052014-01-13 20:51:35 +00006589bool BuildVectorSDNode::isConstant() const {
6590 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6591 unsigned Opc = getOperand(i).getOpcode();
6592 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6593 return false;
6594 }
6595 return true;
6596}
6597
Owen Anderson53aa7a92009-08-10 22:56:29 +00006598bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006599 // Find the first non-undef value in the shuffle mask.
6600 unsigned i, e;
6601 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6602 /* search */;
6603
Nate Begeman39b59db2009-04-29 18:13:31 +00006604 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006605
Nate Begeman5f829d82009-04-29 05:20:52 +00006606 // Make sure all remaining elements are either undef or the same as the first
6607 // non-undef value.
6608 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006609 if (Mask[i] >= 0 && Mask[i] != Idx)
6610 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006611 return true;
6612}
David Greene09851602010-01-20 20:13:31 +00006613
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006614#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006615static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006616 SmallPtrSet<const SDNode*, 32> &Visited,
6617 SmallPtrSet<const SDNode*, 32> &Checked) {
6618 // If this node has already been checked, don't check it again.
6619 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006620 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006621
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006622 // If a node has already been visited on this depth-first walk, reject it as
6623 // a cycle.
6624 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006625 dbgs() << "Offending node:\n";
6626 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006627 errs() << "Detected cycle in SelectionDAG\n";
6628 abort();
David Greene09851602010-01-20 20:13:31 +00006629 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006630
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006631 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6632 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006633
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006634 Checked.insert(N);
6635 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006636}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006637#endif
David Greene09851602010-01-20 20:13:31 +00006638
6639void llvm::checkForCycles(const llvm::SDNode *N) {
6640#ifdef XDEBUG
Alp Toker6a033742013-10-29 02:35:28 +00006641 assert(N && "Checking nonexistent SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006642 SmallPtrSet<const SDNode*, 32> visited;
6643 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006644 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006645#endif
6646}
6647
6648void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6649 checkForCycles(DAG->getRoot().getNode());
6650}