blob: 537a2589fcaee76465b0e13d4a2371dbde56d226 [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"
Eric Christopherd9134482014-08-04 21:25:23 +000049#include "llvm/Target/TargetSubtargetInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000050#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000051#include <cmath>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000052
Chris Lattner560b5e42004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000054
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000065
Jim Laskeyd66e6162005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000076}
77
Owen Anderson53aa7a92009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Dale Johannesend246b2c2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000089}
90
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000094
Evan Chengc70e33c2006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000098 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +000099 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Evan Chengc70e33c2006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000105
Chris Lattnerc2d28112006-03-25 22:57:01 +0000106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000109
Chris Lattnerc2d28112006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000112
Chris Lattnerc2d28112006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000114 // elements. We have to be a bit careful here, as the type of the constant
115 // may not be the same as the type of the vector elements due to type
116 // legalization (the elements are promoted to a legal type for the target and
117 // a vector of a type may be legal when the base element type is not).
118 // We only want to check enough bits to cover the vector elements, because
119 // we care if the resultant vector is all ones, not whether the individual
120 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000125 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000128 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000129 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Chris Lattnerc2d28112006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000133 // undefs. Even with the above element type twiddling, this should be OK, as
134 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Chenga6789912006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000146 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +0000147 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chenga6789912006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Kevin Qin93d45ec2014-06-24 05:37:27 +0000152 bool IsAllUndef = true;
153 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
154 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
155 continue;
156 IsAllUndef = false;
157 // Do not accept build_vectors that aren't all constants or which have non-0
158 // elements. We have to be a bit careful here, as the type of the constant
159 // may not be the same as the type of the vector elements due to type
160 // legalization (the elements are promoted to a legal type for the target
161 // and a vector of a type may be legal when the base element type is not).
162 // We only want to check enough bits to cover the vector elements, because
163 // we care if the resultant vector is all zeros, not whether the individual
164 // constants are.
165 SDValue Zero = N->getOperand(i);
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
168 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
169 return false;
170 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
171 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
172 return false;
173 } else
174 return false;
175 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000176
Evan Chengc70e33c2006-03-27 06:58:47 +0000177 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000178 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000179 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000180 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000181}
182
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000183/// \brief Return true if the specified node is a BUILD_VECTOR node of
184/// all ConstantSDNode or undef.
185bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
186 if (N->getOpcode() != ISD::BUILD_VECTOR)
187 return false;
188
189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
190 SDValue Op = N->getOperand(i);
191 if (Op.getOpcode() == ISD::UNDEF)
192 continue;
193 if (!isa<ConstantSDNode>(Op))
194 return false;
195 }
196 return true;
197}
198
Evan Cheng6200c222008-02-18 23:04:32 +0000199/// isScalarToVector - Return true if the specified node is a
200/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
201/// element is not an undef.
202bool ISD::isScalarToVector(const SDNode *N) {
203 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
204 return true;
205
206 if (N->getOpcode() != ISD::BUILD_VECTOR)
207 return false;
208 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
209 return false;
210 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000211 if (NumElems == 1)
212 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000213 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000214 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000215 if (V.getOpcode() != ISD::UNDEF)
216 return false;
217 }
218 return true;
219}
220
Nadav Rotema62368c2012-07-15 08:38:23 +0000221/// allOperandsUndef - Return true if the node has at least one operand
222/// and all operands of the specified node are ISD::UNDEF.
223bool ISD::allOperandsUndef(const SDNode *N) {
224 // Return false if the node has no operands.
225 // This is "logically inconsistent" with the definition of "all" but
226 // is probably the desired behavior.
227 if (N->getNumOperands() == 0)
228 return false;
229
230 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
231 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
232 return false;
233
234 return true;
235}
236
Matt Arsenaultbd223422015-01-14 01:35:17 +0000237ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000238 switch (ExtType) {
239 case ISD::EXTLOAD:
Matt Arsenaultbd223422015-01-14 01:35:17 +0000240 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000241 case ISD::SEXTLOAD:
242 return ISD::SIGN_EXTEND;
243 case ISD::ZEXTLOAD:
244 return ISD::ZERO_EXTEND;
245 default:
246 break;
247 }
248
249 llvm_unreachable("Invalid LoadExtType");
250}
251
Chris Lattner061a1ea2005-01-07 07:46:32 +0000252/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
253/// when given the operation for (X op Y).
254ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
255 // To perform this operation, we just need to swap the L and G bits of the
256 // operation.
257 unsigned OldL = (Operation >> 2) & 1;
258 unsigned OldG = (Operation >> 1) & 1;
259 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
260 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000261 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000262}
263
264/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
265/// 'op' is a valid SetCC operation.
266ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
267 unsigned Operation = Op;
268 if (isInteger)
269 Operation ^= 7; // Flip L, G, E bits, but not U.
270 else
271 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000272
Chris Lattner061a1ea2005-01-07 07:46:32 +0000273 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000274 Operation &= ~8; // Don't let N and U bits get set.
275
Chris Lattner061a1ea2005-01-07 07:46:32 +0000276 return ISD::CondCode(Operation);
277}
278
279
280/// isSignedOp - For an integer comparison, return 1 if the comparison is a
281/// signed operation and 2 if the result is an unsigned comparison. Return zero
282/// if the operation does not depend on the sign of the input (setne and seteq).
283static int isSignedOp(ISD::CondCode Opcode) {
284 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000285 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000286 case ISD::SETEQ:
287 case ISD::SETNE: return 0;
288 case ISD::SETLT:
289 case ISD::SETLE:
290 case ISD::SETGT:
291 case ISD::SETGE: return 1;
292 case ISD::SETULT:
293 case ISD::SETULE:
294 case ISD::SETUGT:
295 case ISD::SETUGE: return 2;
296 }
297}
298
299/// getSetCCOrOperation - Return the result of a logical OR between different
300/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
301/// returns SETCC_INVALID if it is not possible to represent the resultant
302/// comparison.
303ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
304 bool isInteger) {
305 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
306 // Cannot fold a signed integer setcc with an unsigned integer setcc.
307 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000308
Chris Lattner061a1ea2005-01-07 07:46:32 +0000309 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000310
Chris Lattner061a1ea2005-01-07 07:46:32 +0000311 // If the N and U bits get set then the resultant comparison DOES suddenly
312 // care about orderedness, and is true when ordered.
313 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000314 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000315
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000316 // Canonicalize illegal integer setcc's.
317 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
318 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Chris Lattner061a1ea2005-01-07 07:46:32 +0000320 return ISD::CondCode(Op);
321}
322
323/// getSetCCAndOperation - Return the result of a logical AND between different
324/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
325/// function returns zero if it is not possible to represent the resultant
326/// comparison.
327ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
328 bool isInteger) {
329 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
330 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000331 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000332
333 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000334 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Chris Lattner393d96a2006-04-27 05:01:07 +0000336 // Canonicalize illegal integer setcc's.
337 if (isInteger) {
338 switch (Result) {
339 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000340 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000341 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000342 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
343 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
344 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000345 }
346 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000347
Chris Lattner393d96a2006-04-27 05:01:07 +0000348 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000349}
350
Jim Laskeyd66e6162005-08-17 20:08:02 +0000351//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000352// SDNode Profile Support
353//===----------------------------------------------------------------------===//
354
Jim Laskeybd0f0882006-10-27 23:52:51 +0000355/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
356///
Jim Laskeyf576b422006-10-27 23:46:08 +0000357static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
358 ID.AddInteger(OpC);
359}
360
361/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
362/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000363static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000364 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000365}
366
Jim Laskeybd0f0882006-10-27 23:52:51 +0000367/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
368///
Jim Laskeyf576b422006-10-27 23:46:08 +0000369static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000370 ArrayRef<SDValue> Ops) {
371 for (auto& Op : Ops) {
372 ID.AddPointer(Op.getNode());
373 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000374 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000375}
376
Dan Gohman768f2c92008-07-07 18:26:29 +0000377/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
378///
379static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000380 ArrayRef<SDUse> Ops) {
381 for (auto& Op : Ops) {
382 ID.AddPointer(Op.getNode());
383 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000384 }
385}
386
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000387static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
388 bool exact) {
389 ID.AddBoolean(nuw);
390 ID.AddBoolean(nsw);
391 ID.AddBoolean(exact);
392}
393
394/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
395static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
396 bool nuw, bool nsw, bool exact) {
397 if (isBinOpWithFlags(Opcode))
398 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
399}
400
Craig Topper633d99b2014-04-27 23:22:43 +0000401static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
402 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000403 AddNodeIDOpcode(ID, OpC);
404 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000405 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000406}
407
Duncan Sands835bdca2008-10-27 15:30:53 +0000408/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
409/// the NodeID data.
410static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000411 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000412 case ISD::TargetExternalSymbol:
413 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000414 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000415 default: break; // Normal nodes don't need extra info.
416 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000417 case ISD::Constant: {
418 const ConstantSDNode *C = cast<ConstantSDNode>(N);
419 ID.AddPointer(C->getConstantIntValue());
420 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000421 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000422 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000423 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000424 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000425 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000426 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000427 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000428 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000429 case ISD::GlobalAddress:
430 case ISD::TargetGlobalTLSAddress:
431 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000432 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000433 ID.AddPointer(GA->getGlobal());
434 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000435 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000436 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 break;
438 }
439 case ISD::BasicBlock:
440 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
441 break;
442 case ISD::Register:
443 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
444 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000445 case ISD::RegisterMask:
446 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
447 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000448 case ISD::SRCVALUE:
449 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
450 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000451 case ISD::FrameIndex:
452 case ISD::TargetFrameIndex:
453 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
454 break;
455 case ISD::JumpTable:
456 case ISD::TargetJumpTable:
457 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000458 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000459 break;
460 case ISD::ConstantPool:
461 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000462 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000463 ID.AddInteger(CP->getAlignment());
464 ID.AddInteger(CP->getOffset());
465 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000466 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000467 else
468 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000469 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000470 break;
471 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000472 case ISD::TargetIndex: {
473 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
474 ID.AddInteger(TI->getIndex());
475 ID.AddInteger(TI->getOffset());
476 ID.AddInteger(TI->getTargetFlags());
477 break;
478 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000479 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000480 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000481 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000482 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000483 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000484 break;
485 }
486 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000487 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000488 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000489 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000490 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000491 break;
492 }
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000493 case ISD::SDIV:
494 case ISD::UDIV:
495 case ISD::SRA:
496 case ISD::SRL:
497 case ISD::MUL:
498 case ISD::ADD:
499 case ISD::SUB:
500 case ISD::SHL: {
501 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
502 AddBinaryNodeIDCustom(ID, N->getOpcode(), BinNode->hasNoUnsignedWrap(),
503 BinNode->hasNoSignedWrap(), BinNode->isExact());
504 break;
505 }
Dan Gohman12f24902008-12-23 21:37:04 +0000506 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000507 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000508 case ISD::ATOMIC_SWAP:
509 case ISD::ATOMIC_LOAD_ADD:
510 case ISD::ATOMIC_LOAD_SUB:
511 case ISD::ATOMIC_LOAD_AND:
512 case ISD::ATOMIC_LOAD_OR:
513 case ISD::ATOMIC_LOAD_XOR:
514 case ISD::ATOMIC_LOAD_NAND:
515 case ISD::ATOMIC_LOAD_MIN:
516 case ISD::ATOMIC_LOAD_MAX:
517 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000518 case ISD::ATOMIC_LOAD_UMAX:
519 case ISD::ATOMIC_LOAD:
520 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000521 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000522 ID.AddInteger(AT->getMemoryVT().getRawBits());
523 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000524 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
525 break;
526 }
527 case ISD::PREFETCH: {
528 const MemSDNode *PF = cast<MemSDNode>(N);
529 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000530 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000531 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000532 case ISD::VECTOR_SHUFFLE: {
533 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000534 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000535 i != e; ++i)
536 ID.AddInteger(SVN->getMaskElt(i));
537 break;
538 }
Dan Gohman6c938802009-10-30 01:27:03 +0000539 case ISD::TargetBlockAddress:
540 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000541 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
542 ID.AddPointer(BA->getBlockAddress());
543 ID.AddInteger(BA->getOffset());
544 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000545 break;
546 }
Mon P Wang6a490372008-06-25 08:15:39 +0000547 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000548
549 // Target specific memory nodes could also have address spaces to check.
550 if (N->isTargetMemoryOpcode())
551 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000552}
553
Duncan Sands835bdca2008-10-27 15:30:53 +0000554/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
555/// data.
556static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
557 AddNodeIDOpcode(ID, N->getOpcode());
558 // Add the return value info.
559 AddNodeIDValueTypes(ID, N->getVTList());
560 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000561 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000562
563 // Handle SDNode leafs with special info.
564 AddNodeIDCustom(ID, N);
565}
566
Dan Gohman2da2bed2008-08-20 15:58:01 +0000567/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000568/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000569/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000570///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000571static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000572encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000573 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000574 assert((ConvType & 3) == ConvType &&
575 "ConvType may not require more than 2 bits!");
576 assert((AM & 7) == AM &&
577 "AM may not require more than 3 bits!");
578 return ConvType |
579 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000580 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000581 (isNonTemporal << 6) |
582 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000583}
584
Jim Laskeyf576b422006-10-27 23:46:08 +0000585//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000586// SelectionDAG Class
587//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000588
Duncan Sands835bdca2008-10-27 15:30:53 +0000589/// doNotCSE - Return true if CSE should not be performed for this node.
590static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000591 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000592 return true; // Never CSE anything that produces a flag.
593
594 switch (N->getOpcode()) {
595 default: break;
596 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000597 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000598 return true; // Never CSE these nodes.
599 }
600
601 // Check that remaining values produced are not flags.
602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000603 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000604 return true; // Never CSE anything that produces a flag.
605
606 return false;
607}
608
Chris Lattner9c667932005-01-07 21:09:16 +0000609/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000610/// SelectionDAG.
611void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000612 // Create a dummy node (which is not added to allnodes), that adds a reference
613 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000614 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000615
Chris Lattner8927c872006-08-04 17:45:20 +0000616 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000617
Chris Lattner8927c872006-08-04 17:45:20 +0000618 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000619 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000620 if (I->use_empty())
621 DeadNodes.push_back(I);
622
Dan Gohman91697632008-07-07 20:57:48 +0000623 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000624
Dan Gohman91697632008-07-07 20:57:48 +0000625 // If the root changed (e.g. it was a dead load, update the root).
626 setRoot(Dummy.getValue());
627}
628
629/// RemoveDeadNodes - This method deletes the unreachable nodes in the
630/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000631void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000632
Chris Lattner8927c872006-08-04 17:45:20 +0000633 // Process the worklist, deleting the nodes and adding their uses to the
634 // worklist.
635 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000636 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000637
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000638 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000639 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000640
Chris Lattner8927c872006-08-04 17:45:20 +0000641 // Take the node out of the appropriate CSE map.
642 RemoveNodeFromCSEMaps(N);
643
644 // Next, brutally remove the operand list. This is safe to do, as there are
645 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000646 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
647 SDUse &Use = *I++;
648 SDNode *Operand = Use.getNode();
649 Use.set(SDValue());
650
Chris Lattner8927c872006-08-04 17:45:20 +0000651 // Now that we removed this operand, see if there are no uses of it left.
652 if (Operand->use_empty())
653 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000654 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000655
Dan Gohman534c8a22009-01-19 22:39:36 +0000656 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000657 }
Chris Lattner9c667932005-01-07 21:09:16 +0000658}
659
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000660void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000661 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000662
663 // Create a dummy node that adds a reference to the root node, preventing
664 // it from being deleted. (This matters if the root is an operand of the
665 // dead node.)
666 HandleSDNode Dummy(getRoot());
667
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000668 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000669}
670
Chris Lattnerc738d002005-08-29 21:59:31 +0000671void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000672 // First take this out of the appropriate CSE map.
673 RemoveNodeFromCSEMaps(N);
674
Scott Michelcf0da6c2009-02-17 22:15:04 +0000675 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000676 // AllNodes list, and delete the node.
677 DeleteNodeNotInCSEMaps(N);
678}
679
680void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000681 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
682 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000683
Dan Gohman86aa16a2008-09-30 18:30:35 +0000684 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000685 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000686
Dan Gohman534c8a22009-01-19 22:39:36 +0000687 DeallocateNode(N);
688}
689
Frederic Riss8ad4f492014-11-11 21:21:08 +0000690void SDDbgInfo::erase(const SDNode *Node) {
691 DbgValMapType::iterator I = DbgValMap.find(Node);
692 if (I == DbgValMap.end())
693 return;
694 for (auto &Val: I->second)
695 Val->setIsInvalidated();
696 DbgValMap.erase(I);
697}
698
Dan Gohman534c8a22009-01-19 22:39:36 +0000699void SelectionDAG::DeallocateNode(SDNode *N) {
700 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000701 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000702
Dan Gohman534c8a22009-01-19 22:39:36 +0000703 // Set the opcode to DELETED_NODE to help catch bugs when node
704 // memory is reallocated.
705 N->NodeType = ISD::DELETED_NODE;
706
Dan Gohman2e834902008-08-26 01:44:34 +0000707 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000708
Frederic Riss8ad4f492014-11-11 21:21:08 +0000709 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
710 // them and forget about that node.
711 DbgInfo->erase(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000712}
713
Chandler Carruth41b20e72014-07-22 04:07:55 +0000714#ifndef NDEBUG
715/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
716static void VerifySDNode(SDNode *N) {
717 switch (N->getOpcode()) {
718 default:
719 break;
720 case ISD::BUILD_PAIR: {
721 EVT VT = N->getValueType(0);
722 assert(N->getNumValues() == 1 && "Too many results!");
723 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
724 "Wrong return type!");
725 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
726 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
727 "Mismatched operand types!");
728 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
729 "Wrong operand type!");
730 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
731 "Wrong return type size");
732 break;
733 }
734 case ISD::BUILD_VECTOR: {
735 assert(N->getNumValues() == 1 && "Too many results!");
736 assert(N->getValueType(0).isVector() && "Wrong return type!");
737 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
738 "Wrong number of operands!");
739 EVT EltVT = N->getValueType(0).getVectorElementType();
740 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
741 assert((I->getValueType() == EltVT ||
742 (EltVT.isInteger() && I->getValueType().isInteger() &&
743 EltVT.bitsLE(I->getValueType()))) &&
744 "Wrong operand type!");
745 assert(I->getValueType() == N->getOperand(0).getValueType() &&
746 "Operands must all have the same type");
747 }
748 break;
749 }
750 }
751}
752#endif // NDEBUG
753
754/// \brief Insert a newly allocated node into the DAG.
755///
756/// Handles insertion into the all nodes list and CSE map, as well as
757/// verification and other common operations when a new node is allocated.
758void SelectionDAG::InsertNode(SDNode *N) {
759 AllNodes.push_back(N);
760#ifndef NDEBUG
761 VerifySDNode(N);
762#endif
763}
764
Chris Lattner19732782005-08-16 18:17:10 +0000765/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
766/// correspond to it. This is useful when we're about to delete or repurpose
767/// the node. We don't want future request for structurally identical nodes
768/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000769bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000770 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000771 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000772 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000773 case ISD::CONDCODE:
774 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
775 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000776 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
777 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000778 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000779 case ISD::ExternalSymbol:
780 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000781 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000782 case ISD::TargetExternalSymbol: {
783 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
784 Erased = TargetExternalSymbols.erase(
785 std::pair<std::string,unsigned char>(ESN->getSymbol(),
786 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000787 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000788 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000789 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000790 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000791 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000792 Erased = ExtendedValueTypeNodes.erase(VT);
793 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000794 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
795 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000796 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000797 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000798 }
Chris Lattner9c667932005-01-07 21:09:16 +0000799 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000800 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000801 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
802 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000803 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000804 break;
805 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000806#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000807 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000808 // flag result (which cannot be CSE'd) or is one of the special cases that are
809 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000810 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000811 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000812 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000813 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000814 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000815 }
816#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000817 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000818}
819
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000820/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
821/// maps and modified in place. Add it back to the CSE maps, unless an identical
822/// node already exists, in which case transfer all its users to the existing
823/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000824///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000825void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000826SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000827 // For node types that aren't CSE'd, just act as if no identical node
828 // already exists.
829 if (!doNotCSE(N)) {
830 SDNode *Existing = CSEMap.GetOrInsertNode(N);
831 if (Existing != N) {
832 // If there was already an existing matching node, use ReplaceAllUsesWith
833 // to replace the dead one with the existing one. This can cause
834 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000835 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000836
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000837 // N is now dead. Inform the listeners and delete it.
838 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
839 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000840 DeleteNodeNotInCSEMaps(N);
841 return;
842 }
843 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000844
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000845 // If the node doesn't already exist, we updated it. Inform listeners.
846 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
847 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000848}
849
Chris Lattnerf34156e2006-01-28 09:32:45 +0000850/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000851/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000852/// return null, otherwise return a pointer to the slot it would take. If a
853/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000854SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000855 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000856 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000857 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000858
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000859 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000860 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000861 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000862 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000863 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000864 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000865}
866
867/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000868/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000869/// return null, otherwise return a pointer to the slot it would take. If a
870/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000871SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000872 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000873 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000874 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000875 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000876
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000877 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000878 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000879 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000880 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000881 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000882 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000883}
884
885
886/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000887/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000888/// return null, otherwise return a pointer to the slot it would take. If a
889/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000890SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000891 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000892 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000893 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000894
Jim Laskeyf576b422006-10-27 23:46:08 +0000895 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000896 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000897 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000898 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000899 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000900}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000901
Owen Anderson53aa7a92009-08-10 22:56:29 +0000902/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000903/// given type.
904///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000905unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000906 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000907 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000908 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000909
Eric Christopher1e845f22014-10-08 21:08:32 +0000910 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000911}
Chris Lattner9c667932005-01-07 21:09:16 +0000912
Dale Johannesen8ba713212009-02-07 02:15:05 +0000913// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000914SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000915 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000916 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
917 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
918 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000919 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000920 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000921}
922
Eric Christopher8d07f442014-10-08 01:57:58 +0000923void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000924 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000925 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000926 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000927 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000928}
929
Chris Lattner600d3082003-08-11 14:57:33 +0000930SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000931 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000932 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000933 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000934}
935
936void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000937 assert(&*AllNodes.begin() == &EntryNode);
938 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000939 while (!AllNodes.empty())
940 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000941}
942
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000943BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
944 SDVTList VTs, SDValue N1,
945 SDValue N2, bool nuw, bool nsw,
946 bool exact) {
947 if (isBinOpWithFlags(Opcode)) {
948 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
949 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
950 FN->setHasNoUnsignedWrap(nuw);
951 FN->setHasNoSignedWrap(nsw);
952 FN->setIsExact(exact);
953
954 return FN;
955 }
956
957 BinarySDNode *N = new (NodeAllocator)
958 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
959 return N;
960}
961
Dan Gohmane1a9a782008-08-27 23:52:12 +0000962void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000963 allnodes_clear();
964 OperandAllocator.Reset();
965 CSEMap.clear();
966
967 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000968 ExternalSymbols.clear();
969 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000970 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000971 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000972 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000973 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000974
Craig Topperc0196b12014-04-14 00:51:57 +0000975 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000976 AllNodes.push_back(&EntryNode);
977 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000978 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000979}
980
Andrew Trickef9de2a2013-05-25 02:42:55 +0000981SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000982 return VT.bitsGT(Op.getValueType()) ?
983 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
984 getNode(ISD::TRUNCATE, DL, VT, Op);
985}
986
Andrew Trickef9de2a2013-05-25 02:42:55 +0000987SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000988 return VT.bitsGT(Op.getValueType()) ?
989 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
990 getNode(ISD::TRUNCATE, DL, VT, Op);
991}
992
Andrew Trickef9de2a2013-05-25 02:42:55 +0000993SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000994 return VT.bitsGT(Op.getValueType()) ?
995 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
996 getNode(ISD::TRUNCATE, DL, VT, Op);
997}
998
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000999SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1000 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001001 if (VT.bitsLE(Op.getValueType()))
1002 return getNode(ISD::TRUNCATE, SL, VT, Op);
1003
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001004 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001005 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1006}
1007
Andrew Trickef9de2a2013-05-25 02:42:55 +00001008SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001009 assert(!VT.isVector() &&
1010 "getZeroExtendInReg should use the vector element type instead of "
1011 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001012 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001013 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1014 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001015 VT.getSizeInBits());
1016 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1017 getConstant(Imm, Op.getValueType()));
1018}
1019
Chandler Carruth0b666e02014-07-10 12:32:32 +00001020SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1021 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1022 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1023 "The sizes of the input and result must match in order to perform the "
1024 "extend in-register.");
1025 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1026 "The destination vector type must have fewer lanes than the input.");
1027 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1028}
1029
1030SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1031 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1032 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1033 "The sizes of the input and result must match in order to perform the "
1034 "extend in-register.");
1035 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1036 "The destination vector type must have fewer lanes than the input.");
1037 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1038}
1039
Chandler Carruthafe4b252014-07-09 10:58:18 +00001040SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1041 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001042 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1043 "The sizes of the input and result must match in order to perform the "
1044 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001045 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1046 "The destination vector type must have fewer lanes than the input.");
1047 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1048}
1049
Bob Wilsonc5890052009-01-22 17:39:32 +00001050/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1051///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001052SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001053 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001054 SDValue NegOne =
1055 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001056 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1057}
1058
Pete Cooper7fd1d722014-05-12 23:26:58 +00001059SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1060 EVT EltVT = VT.getScalarType();
1061 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001062 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001063 case TargetLowering::ZeroOrOneBooleanContent:
1064 case TargetLowering::UndefinedBooleanContent:
1065 TrueValue = getConstant(1, VT);
1066 break;
1067 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1068 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1069 VT);
1070 break;
1071 }
1072 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1073}
1074
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001075SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001076 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001077 assert((EltVT.getSizeInBits() >= 64 ||
1078 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1079 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001080 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001081}
1082
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001083SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1084{
1085 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001086}
1087
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001088SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1089 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001090 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001091
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001092 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001093 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001094
Duncan Sandsf2641e12011-09-06 19:07:46 +00001095 // In some cases the vector type is legal but the element type is illegal and
1096 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1097 // inserted value (the type does not need to match the vector element type).
1098 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001099 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001100 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001101 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001102 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1103 Elt = ConstantInt::get(*getContext(), NewVal);
1104 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001105 // In other cases the element type is illegal and needs to be expanded, for
1106 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1107 // the value into n parts and use a vector type with n-times the elements.
1108 // Then bitcast to the type requested.
1109 // Legalizing constants too early makes the DAGCombiner's job harder so we
1110 // only legalize if the DAG tells us we must produce legal types.
1111 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1112 TLI->getTypeAction(*getContext(), EltVT) ==
1113 TargetLowering::TypeExpandInteger) {
1114 APInt NewVal = Elt->getValue();
1115 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1116 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1117 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1118 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1119
1120 // Check the temporary vector is the correct size. If this fails then
1121 // getTypeToTransformTo() probably returned a type whose size (in bits)
1122 // isn't a power-of-2 factor of the requested type size.
1123 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1124
1125 SmallVector<SDValue, 2> EltParts;
1126 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1127 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1128 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001129 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001130 }
1131
1132 // EltParts is currently in little endian order. If we actually want
1133 // big-endian order then reverse it now.
1134 if (TLI->isBigEndian())
1135 std::reverse(EltParts.begin(), EltParts.end());
1136
1137 // The elements must be reversed when the element order is different
1138 // to the endianness of the elements (because the BITCAST is itself a
1139 // vector shuffle in this situation). However, we do not need any code to
1140 // perform this reversal because getConstant() is producing a vector
1141 // splat.
1142 // This situation occurs in MIPS MSA.
1143
1144 SmallVector<SDValue, 8> Ops;
1145 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1146 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1147
1148 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1149 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001150 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001151 return Result;
1152 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001153
1154 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1155 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001156 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001157 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001158 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001159 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001160 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001161 void *IP = nullptr;
1162 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001163 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001164 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001165 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001166
Dan Gohman7a7742c2007-12-12 22:21:26 +00001167 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001168 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001169 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001170 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001171 }
1172
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001173 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001174 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001175 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001176 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001177 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001178 }
1179 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001180}
1181
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001182SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Eric Christopher1e845f22014-10-08 21:08:32 +00001183 return getConstant(Val, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001184}
1185
1186
Owen Anderson53aa7a92009-08-10 22:56:29 +00001187SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001188 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001189}
1190
Owen Anderson53aa7a92009-08-10 22:56:29 +00001191SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001192 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001193
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001194 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001195
Chris Lattner381dddc2005-02-17 20:17:32 +00001196 // Do the map lookup using the actual bit pattern for the floating point
1197 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1198 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001199 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001200 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001201 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001202 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001203 void *IP = nullptr;
1204 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001205 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001206 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001207 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001208
Evan Cheng9458e6a2007-06-29 21:36:04 +00001209 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001210 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001211 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001212 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001213 }
1214
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001215 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001216 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001217 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001218 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001219 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001220 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001221 }
1222 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001223}
1224
Owen Anderson53aa7a92009-08-10 22:56:29 +00001225SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001226 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001227 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001228 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001229 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001230 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001231 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1232 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001233 bool ignored;
1234 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001235 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001236 &ignored);
1237 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001238 } else
1239 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001240}
1241
Andrew Trickef9de2a2013-05-25 02:42:55 +00001242SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001243 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001244 bool isTargetGA,
1245 unsigned char TargetFlags) {
1246 assert((TargetFlags == 0 || isTargetGA) &&
1247 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001248
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001249 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001250 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001251 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001252 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001253
Chris Lattner8e34f982009-06-25 21:21:14 +00001254 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001255 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001256 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1257 else
1258 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001259
Jim Laskeyf576b422006-10-27 23:46:08 +00001260 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001261 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001262 ID.AddPointer(GV);
1263 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001264 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001265 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001266 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001267 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001268 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001269
Andrew Trickef9de2a2013-05-25 02:42:55 +00001270 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1271 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001272 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001273 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001274 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001275 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001276}
1277
Owen Anderson53aa7a92009-08-10 22:56:29 +00001278SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001279 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001280 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001281 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001282 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001283 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001284 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001285 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001286
Dan Gohman01c65a22010-03-18 18:49:47 +00001287 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001288 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001289 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001290 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001291}
1292
Owen Anderson53aa7a92009-08-10 22:56:29 +00001293SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001294 unsigned char TargetFlags) {
1295 assert((TargetFlags == 0 || isTarget) &&
1296 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001297 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001298 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001299 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001300 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001301 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001302 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001304 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001305
Dan Gohman01c65a22010-03-18 18:49:47 +00001306 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1307 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001308 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001309 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001310 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001311}
1312
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001313SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001314 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001315 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001316 unsigned char TargetFlags) {
1317 assert((TargetFlags == 0 || isTarget) &&
1318 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001319 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001320 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001321 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001322 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001323 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001324 ID.AddInteger(Alignment);
1325 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001326 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001327 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001328 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001329 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001330 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001331
Dan Gohman01c65a22010-03-18 18:49:47 +00001332 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1333 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001334 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001335 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001336 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001337}
1338
Chris Lattner061a1ea2005-01-07 07:46:32 +00001339
Owen Anderson53aa7a92009-08-10 22:56:29 +00001340SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001341 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001342 bool isTarget,
1343 unsigned char TargetFlags) {
1344 assert((TargetFlags == 0 || isTarget) &&
1345 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001346 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001347 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001348 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001349 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001350 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001351 ID.AddInteger(Alignment);
1352 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001353 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001354 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001355 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001356 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001358
Dan Gohman01c65a22010-03-18 18:49:47 +00001359 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1360 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001361 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001362 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001363 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001364}
1365
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001366SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1367 unsigned char TargetFlags) {
1368 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001369 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001370 ID.AddInteger(Index);
1371 ID.AddInteger(Offset);
1372 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001373 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001374 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1375 return SDValue(E, 0);
1376
1377 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1378 TargetFlags);
1379 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001380 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001381 return SDValue(N, 0);
1382}
1383
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001384SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001385 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001386 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001387 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001388 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001390 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001391
Dan Gohman01c65a22010-03-18 18:49:47 +00001392 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001393 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001394 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001395 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001396}
1397
Owen Anderson53aa7a92009-08-10 22:56:29 +00001398SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001399 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1400 ValueTypeNodes.size())
1401 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001402
Duncan Sands13237ac2008-06-06 12:08:01 +00001403 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001404 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001405
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001406 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001407 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001408 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001409 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001410}
1411
Owen Anderson53aa7a92009-08-10 22:56:29 +00001412SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001413 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001414 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001415 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001416 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001417 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001418}
1419
Owen Anderson53aa7a92009-08-10 22:56:29 +00001420SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001421 unsigned char TargetFlags) {
1422 SDNode *&N =
1423 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1424 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001425 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001426 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001427 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001428 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001429}
1430
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001431SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001432 if ((unsigned)Cond >= CondCodeNodes.size())
1433 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001434
Craig Topperc0196b12014-04-14 00:51:57 +00001435 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001436 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001437 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001438 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001439 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001440
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001441 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001442}
1443
Nate Begeman5f829d82009-04-29 05:20:52 +00001444// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1445// the shuffle mask M that point at N1 to point at N2, and indices that point
1446// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001447static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1448 std::swap(N1, N2);
1449 int NElts = M.size();
1450 for (int i = 0; i != NElts; ++i) {
1451 if (M[i] >= NElts)
1452 M[i] -= NElts;
1453 else if (M[i] >= 0)
1454 M[i] += NElts;
1455 }
1456}
1457
Andrew Trickef9de2a2013-05-25 02:42:55 +00001458SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001459 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001460 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1461 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001462
1463 // Canonicalize shuffle undef, undef -> undef
1464 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001465 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001466
Eric Christopherdfda92b2009-08-22 00:40:45 +00001467 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001468 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001469 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001470 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001471 for (unsigned i = 0; i != NElts; ++i) {
1472 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001473 MaskVec.push_back(Mask[i]);
1474 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001475
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001476 // Canonicalize shuffle v, v -> v, undef
1477 if (N1 == N2) {
1478 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001479 for (unsigned i = 0; i != NElts; ++i)
1480 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001481 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001482
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001483 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1484 if (N1.getOpcode() == ISD::UNDEF)
1485 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001486
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001487 // Canonicalize all index into lhs, -> shuffle lhs, undef
1488 // Canonicalize all index into rhs, -> shuffle rhs, undef
1489 bool AllLHS = true, AllRHS = true;
1490 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001491 for (unsigned i = 0; i != NElts; ++i) {
1492 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001493 if (N2Undef)
1494 MaskVec[i] = -1;
1495 else
1496 AllLHS = false;
1497 } else if (MaskVec[i] >= 0) {
1498 AllRHS = false;
1499 }
1500 }
1501 if (AllLHS && AllRHS)
1502 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001503 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001504 N2 = getUNDEF(VT);
1505 if (AllRHS) {
1506 N1 = getUNDEF(VT);
1507 commuteShuffle(N1, N2, MaskVec);
1508 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001509 // Reset our undef status after accounting for the mask.
1510 N2Undef = N2.getOpcode() == ISD::UNDEF;
1511 // Re-check whether both sides ended up undef.
1512 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1513 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001514
Craig Topper9a39b072013-08-08 08:03:12 +00001515 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001516 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001517 for (unsigned i = 0; i != NElts; ++i) {
1518 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001519 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001520 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001521 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001522 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001523
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001524 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001525 if (N2Undef) {
1526 SDValue V = N1;
1527
1528 // Look through any bitcasts. We check that these don't change the number
1529 // (and size) of elements and just changes their types.
1530 while (V.getOpcode() == ISD::BITCAST)
1531 V = V->getOperand(0);
1532
1533 // A splat should always show up as a build vector node.
1534 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001535 BitVector UndefElements;
1536 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001537 // If this is a splat of an undef, shuffling it is also undef.
1538 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1539 return getUNDEF(VT);
1540
Andrea Di Biagio83814752015-01-24 11:54:29 +00001541 bool SameNumElts =
1542 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1543
Chandler Carruth142e9662014-07-08 08:45:38 +00001544 // We only have a splat which can skip shuffles if there is a splatted
1545 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001546 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001547 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1548 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001549 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001550 return N1;
1551 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1552 if (C->isNullValue())
1553 return N1;
1554 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001555
1556 // If the shuffle itself creates a constant splat, build the vector
1557 // directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001558 if (AllSame && SameNumElts) {
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001559 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1560 if (isa<ConstantSDNode>(Splatted) || isa<ConstantFPSDNode>(Splatted)) {
1561 SmallVector<SDValue, 8> Ops;
Andrea Di Biagio83814752015-01-24 11:54:29 +00001562 for (unsigned i = 0; i != NElts; ++i)
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001563 Ops.push_back(Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001564
1565 SDValue NewBV =
1566 getNode(ISD::BUILD_VECTOR, dl, BV->getValueType(0), Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001567
1568 // We may have jumped through bitcasts, so the type of the
1569 // BUILD_VECTOR may not match the type of the shuffle.
1570 if (BV->getValueType(0) != VT)
1571 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1572 return NewBV;
1573 }
1574 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001575 }
1576 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001577
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001578 FoldingSetNodeID ID;
1579 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001580 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001581 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001582 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001583
Craig Topperc0196b12014-04-14 00:51:57 +00001584 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001585 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001586 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001587
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001588 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1589 // SDNode doesn't have access to it. This memory will be "leaked" when
1590 // the node is deallocated, but recovered when the NodeAllocator is released.
1591 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1592 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001593
Dan Gohman01c65a22010-03-18 18:49:47 +00001594 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001595 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1596 dl.getDebugLoc(), N1, N2,
1597 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001598 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001599 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001600 return SDValue(N, 0);
1601}
1602
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001603SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1604 MVT VT = SV.getSimpleValueType(0);
1605 unsigned NumElems = VT.getVectorNumElements();
1606 SmallVector<int, 8> MaskVec;
1607
1608 for (unsigned i = 0; i != NumElems; ++i) {
1609 int Idx = SV.getMaskElt(i);
1610 if (Idx >= 0) {
1611 if (Idx < (int)NumElems)
1612 Idx += NumElems;
1613 else
1614 Idx -= NumElems;
1615 }
1616 MaskVec.push_back(Idx);
1617 }
1618
1619 SDValue Op0 = SV.getOperand(0);
1620 SDValue Op1 = SV.getOperand(1);
1621 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1622}
1623
Andrew Trickef9de2a2013-05-25 02:42:55 +00001624SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001625 SDValue Val, SDValue DTy,
1626 SDValue STy, SDValue Rnd, SDValue Sat,
1627 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001628 // If the src and dest types are the same and the conversion is between
1629 // integer types of the same sign or two floats, no conversion is necessary.
1630 if (DTy == STy &&
1631 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001632 return Val;
1633
1634 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001635 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001636 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001637 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001638 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001639 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001640
Jack Carter170a5f22013-09-09 22:02:08 +00001641 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1642 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001643 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001644 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001645 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001646 return SDValue(N, 0);
1647}
1648
Owen Anderson53aa7a92009-08-10 22:56:29 +00001649SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001650 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001651 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001652 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001653 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001654 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001655 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001656
Dan Gohman01c65a22010-03-18 18:49:47 +00001657 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001658 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001659 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001660 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001661}
1662
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001663SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1664 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001665 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001666 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001667 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001668 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1669 return SDValue(E, 0);
1670
1671 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1672 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001673 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001674 return SDValue(N, 0);
1675}
1676
Andrew Trickef9de2a2013-05-25 02:42:55 +00001677SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001678 FoldingSetNodeID ID;
1679 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001680 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001681 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001682 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001683 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001684 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001685
Jack Carter170a5f22013-09-09 22:02:08 +00001686 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1687 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001688 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001689 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001690 return SDValue(N, 0);
1691}
1692
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001693
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001694SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001695 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001696 bool isTarget,
1697 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001698 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1699
1700 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001701 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001702 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001703 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001704 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001705 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001706 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001707 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001708
Michael Liaoabb87d42012-09-12 21:43:09 +00001709 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1710 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001711 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001712 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001713 return SDValue(N, 0);
1714}
1715
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001716SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001717 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001718 "SrcValue is not a pointer?");
1719
Jim Laskeyf576b422006-10-27 23:46:08 +00001720 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001721 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001722 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001723
Craig Topperc0196b12014-04-14 00:51:57 +00001724 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001725 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001726 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001727
Dan Gohman01c65a22010-03-18 18:49:47 +00001728 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001729 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001730 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001731 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001732}
1733
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001734/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1735SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1736 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001737 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001738 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001739
Craig Topperc0196b12014-04-14 00:51:57 +00001740 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001741 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1742 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001743
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001744 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1745 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001746 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001747 return SDValue(N, 0);
1748}
1749
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001750/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1751SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1752 unsigned SrcAS, unsigned DestAS) {
1753 SDValue Ops[] = {Ptr};
1754 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001755 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001756 ID.AddInteger(SrcAS);
1757 ID.AddInteger(DestAS);
1758
Craig Topperc0196b12014-04-14 00:51:57 +00001759 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001760 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1761 return SDValue(E, 0);
1762
1763 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1764 dl.getDebugLoc(),
1765 VT, Ptr, SrcAS, DestAS);
1766 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001767 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001768 return SDValue(N, 0);
1769}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001770
Duncan Sands41826032009-01-31 15:50:11 +00001771/// getShiftAmountOperand - Return the specified value casted to
1772/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001773SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001774 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001775 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001776 if (OpTy == ShTy || OpTy.isVector()) return Op;
1777
1778 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001779 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001780}
1781
Chris Lattner9eb7a822007-10-15 17:47:20 +00001782/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1783/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001784SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001785 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001786 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001787 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001788 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001789 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001790
David Greene1fbe0542009-11-12 20:49:22 +00001791 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001792 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001793}
1794
Duncan Sands445071c2008-12-09 21:33:20 +00001795/// CreateStackTemporary - Create a stack temporary suitable for holding
1796/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001797SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001798 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1799 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001800 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1801 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001802 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001803 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1804 TD->getPrefTypeAlignment(Ty2));
1805
1806 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001807 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001808 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001809}
1810
Owen Anderson53aa7a92009-08-10 22:56:29 +00001811SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001812 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001813 // These setcc operations always fold.
1814 switch (Cond) {
1815 default: break;
1816 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001817 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001818 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001819 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001820 TargetLowering::BooleanContent Cnt =
1821 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001822 return getConstant(
1823 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1824 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001825
Chris Lattner393d96a2006-04-27 05:01:07 +00001826 case ISD::SETOEQ:
1827 case ISD::SETOGT:
1828 case ISD::SETOGE:
1829 case ISD::SETOLT:
1830 case ISD::SETOLE:
1831 case ISD::SETONE:
1832 case ISD::SETO:
1833 case ISD::SETUO:
1834 case ISD::SETUEQ:
1835 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001836 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001837 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001838 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001839
Gabor Greiff304a7a2008-08-28 21:40:38 +00001840 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001841 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001842 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001843 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001844
Chris Lattner061a1ea2005-01-07 07:46:32 +00001845 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001846 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001847 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1848 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001849 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1850 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1851 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1852 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1853 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1854 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1855 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1856 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001857 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001858 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001859 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001860 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1861 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001862 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001863 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001864 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001865 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001866 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001867 // fall through
1868 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001869 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001870 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001871 // fall through
1872 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001873 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001874 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001875 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001876 // fall through
1877 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001878 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001879 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001880 // fall through
1881 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001882 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001883 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001884 // fall through
1885 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001886 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001887 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001888 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001889 // fall through
1890 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001891 R==APFloat::cmpEqual, VT);
1892 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1893 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1894 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1895 R==APFloat::cmpEqual, VT);
1896 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1897 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1898 R==APFloat::cmpLessThan, VT);
1899 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1900 R==APFloat::cmpUnordered, VT);
1901 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1902 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001903 }
1904 } else {
1905 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001906 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1907 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001908 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001909 return SDValue();
1910
1911 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001912 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001913 }
1914
Chris Lattnerd47675e2005-08-09 20:20:18 +00001915 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001916 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001917}
1918
Dan Gohman1f372ed2008-02-25 21:11:39 +00001919/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1920/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001921bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001922 // This predicate is not safe for vector operations.
1923 if (Op.getValueType().isVector())
1924 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001925
Dan Gohman1d459e42009-12-11 21:31:27 +00001926 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001927 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1928}
1929
Dan Gohman309d3d52007-06-22 14:59:07 +00001930/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1931/// this predicate to simplify operations downstream. Mask is known to be zero
1932/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001933bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001934 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001935 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001936 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001937 return (KnownZero & Mask) == Mask;
1938}
1939
Jay Foada0653a32014-05-14 21:14:37 +00001940/// Determine which bits of Op are known to be either zero or one and return
1941/// them in the KnownZero/KnownOne bitsets.
1942void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1943 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001944 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001945
Dan Gohmanf990faf2008-02-13 00:35:47 +00001946 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001947 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001948 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001949
Dan Gohmanf990faf2008-02-13 00:35:47 +00001950 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001951
1952 switch (Op.getOpcode()) {
1953 case ISD::Constant:
1954 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001955 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1956 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001957 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001958 case ISD::AND:
1959 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001960 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1961 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001962
1963 // Output known-1 bits are only known if set in both the LHS & RHS.
1964 KnownOne &= KnownOne2;
1965 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1966 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001967 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001968 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001969 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1970 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001971
Dan Gohman309d3d52007-06-22 14:59:07 +00001972 // Output known-0 bits are only known if clear in both the LHS & RHS.
1973 KnownZero &= KnownZero2;
1974 // Output known-1 are known to be set if set in either the LHS | RHS.
1975 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001976 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001977 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001978 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1979 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001980
Dan Gohman309d3d52007-06-22 14:59:07 +00001981 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001982 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001983 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1984 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1985 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001986 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001987 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001988 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00001989 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1990 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001991
1992 // If low bits are zero in either operand, output low known-0 bits.
1993 // Also compute a conserative estimate for high known-0 bits.
1994 // More trickiness is possible, but this is sufficient for the
1995 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001996 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001997 unsigned TrailZ = KnownZero.countTrailingOnes() +
1998 KnownZero2.countTrailingOnes();
1999 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002000 KnownZero2.countLeadingOnes(),
2001 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002002
2003 TrailZ = std::min(TrailZ, BitWidth);
2004 LeadZ = std::min(LeadZ, BitWidth);
2005 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2006 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002007 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002008 }
2009 case ISD::UDIV: {
2010 // For the purposes of computing leading zeros we can conservatively
2011 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002012 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002013 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002014 unsigned LeadZ = KnownZero2.countLeadingOnes();
2015
Jay Foad25a5e4c2010-12-01 08:53:58 +00002016 KnownOne2.clearAllBits();
2017 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002018 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002019 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2020 if (RHSUnknownLeadingOnes != BitWidth)
2021 LeadZ = std::min(BitWidth,
2022 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002023
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002024 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002025 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002026 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002027 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002028 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2029 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002030
Dan Gohman309d3d52007-06-22 14:59:07 +00002031 // Only known if known in both the LHS and RHS.
2032 KnownOne &= KnownOne2;
2033 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002034 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002035 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002036 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2037 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002038
Dan Gohman309d3d52007-06-22 14:59:07 +00002039 // Only known if known in both the LHS and RHS.
2040 KnownOne &= KnownOne2;
2041 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002042 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002043 case ISD::SADDO:
2044 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002045 case ISD::SSUBO:
2046 case ISD::USUBO:
2047 case ISD::SMULO:
2048 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002049 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002050 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002051 // The boolean result conforms to getBooleanContents.
2052 // If we know the result of a setcc has the top bits zero, use this info.
2053 // We know that we have an integer-based boolean since these operations
2054 // are only available for integer.
2055 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2056 TargetLowering::ZeroOrOneBooleanContent &&
2057 BitWidth > 1)
2058 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2059 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002060 case ISD::SETCC:
2061 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002062 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2063 TargetLowering::ZeroOrOneBooleanContent &&
2064 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002065 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002066 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002067 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002068 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002069 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002070 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002071
2072 // If the shift count is an invalid immediate, don't do anything.
2073 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002074 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002075
Jay Foada0653a32014-05-14 21:14:37 +00002076 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002077 KnownZero <<= ShAmt;
2078 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002079 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002080 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002081 }
Jay Foad5a29c362014-05-15 12:12:55 +00002082 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002083 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002084 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002085 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002086 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002087
Dan Gohman9db0aa82008-02-26 18:50:50 +00002088 // If the shift count is an invalid immediate, don't do anything.
2089 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002090 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002091
Jay Foada0653a32014-05-14 21:14:37 +00002092 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002093 KnownZero = KnownZero.lshr(ShAmt);
2094 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002095
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002096 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002097 KnownZero |= HighBits; // High bits known zero.
2098 }
Jay Foad5a29c362014-05-15 12:12:55 +00002099 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002100 case ISD::SRA:
2101 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002102 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002103
Dan Gohman9db0aa82008-02-26 18:50:50 +00002104 // If the shift count is an invalid immediate, don't do anything.
2105 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002106 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002107
Dan Gohman309d3d52007-06-22 14:59:07 +00002108 // If any of the demanded bits are produced by the sign extension, we also
2109 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002110 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002111
Jay Foada0653a32014-05-14 21:14:37 +00002112 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002113 KnownZero = KnownZero.lshr(ShAmt);
2114 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002115
Dan Gohman309d3d52007-06-22 14:59:07 +00002116 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002117 APInt SignBit = APInt::getSignBit(BitWidth);
2118 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002119
Dan Gohmanb717fda2008-02-20 16:30:17 +00002120 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002121 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002122 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002123 KnownOne |= HighBits; // New bits are known one.
2124 }
2125 }
Jay Foad5a29c362014-05-15 12:12:55 +00002126 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002127 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002128 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002129 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002130
2131 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002132 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002133 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002134
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002135 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002136 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002137
Dan Gohman309d3d52007-06-22 14:59:07 +00002138 // If the sign extended bits are demanded, we know that the sign
2139 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002140 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002141 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002142 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002143
Jay Foada0653a32014-05-14 21:14:37 +00002144 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002145 KnownOne &= InputDemandedBits;
2146 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002147
Dan Gohman309d3d52007-06-22 14:59:07 +00002148 // If the sign bit of the input is known set or clear, then we know the
2149 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002150 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002151 KnownZero |= NewBits;
2152 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002153 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002154 KnownOne |= NewBits;
2155 KnownZero &= ~NewBits;
2156 } else { // Input sign bit unknown
2157 KnownZero &= ~NewBits;
2158 KnownOne &= ~NewBits;
2159 }
Jay Foad5a29c362014-05-15 12:12:55 +00002160 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002161 }
2162 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002163 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002164 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002165 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002166 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002167 unsigned LowBits = Log2_32(BitWidth)+1;
2168 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002169 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002170 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002171 }
2172 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002173 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002174 // If this is a ZEXTLoad and we are looking at the loaded value.
2175 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002176 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002177 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002178 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002179 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002180 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002181 }
Jay Foad5a29c362014-05-15 12:12:55 +00002182 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002183 }
2184 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002185 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002186 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002187 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002188 KnownZero = KnownZero.trunc(InBits);
2189 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002190 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002191 KnownZero = KnownZero.zext(BitWidth);
2192 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002193 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002194 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002195 }
2196 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002197 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002198 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002199 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002200
Jay Foad583abbc2010-12-07 08:25:19 +00002201 KnownZero = KnownZero.trunc(InBits);
2202 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002203 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002204
2205 // Note if the sign bit is known to be zero or one.
2206 bool SignBitKnownZero = KnownZero.isNegative();
2207 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002208
Jay Foad583abbc2010-12-07 08:25:19 +00002209 KnownZero = KnownZero.zext(BitWidth);
2210 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002211
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002212 // If the sign bit is known zero or one, the top bits match.
2213 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002214 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002215 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002216 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002217 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002218 }
2219 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002220 EVT InVT = Op.getOperand(0).getValueType();
2221 unsigned InBits = InVT.getScalarType().getSizeInBits();
2222 KnownZero = KnownZero.trunc(InBits);
2223 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002224 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002225 KnownZero = KnownZero.zext(BitWidth);
2226 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002227 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002228 }
2229 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002230 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002231 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002232 KnownZero = KnownZero.zext(InBits);
2233 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002234 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002235 KnownZero = KnownZero.trunc(BitWidth);
2236 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002237 break;
2238 }
2239 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002240 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002241 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002242 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002243 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002244 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002245 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002246 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002247 case ISD::FGETSIGN:
2248 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002249 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002250 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002251
Dan Gohman72ec3f42008-04-28 17:02:21 +00002252 case ISD::SUB: {
2253 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2254 // We know that the top bits of C-X are clear if X contains less bits
2255 // than C (i.e. no wrap-around can happen). For example, 20-X is
2256 // positive if we can prove that X is >= 0 and < 16.
2257 if (CLHS->getAPIntValue().isNonNegative()) {
2258 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2259 // NLZ can't be BitWidth with no sign bit
2260 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002261 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002262
2263 // If all of the MaskV bits are known to be zero, then we know the
2264 // output top bits are zero, because we now know that the output is
2265 // from [0-C].
2266 if ((KnownZero2 & MaskV) == MaskV) {
2267 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2268 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002269 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002270 }
2271 }
2272 }
2273 }
2274 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002275 case ISD::ADD:
2276 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002277 // Output known-0 bits are known if clear or set in both the low clear bits
2278 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2279 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002280 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002281 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2282
Jay Foada0653a32014-05-14 21:14:37 +00002283 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002284 KnownZeroOut = std::min(KnownZeroOut,
2285 KnownZero2.countTrailingOnes());
2286
Chris Lattner440b2802010-12-19 20:38:28 +00002287 if (Op.getOpcode() == ISD::ADD) {
2288 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002289 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002290 }
2291
2292 // With ADDE, a carry bit may be added in, so we can only use this
2293 // information if we know (at least) that the low two bits are clear. We
2294 // then return to the caller that the low bit is unknown but that other bits
2295 // are known zero.
2296 if (KnownZeroOut >= 2) // ADDE
2297 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002298 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002299 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002300 case ISD::SREM:
2301 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002302 const APInt &RA = Rem->getAPIntValue().abs();
2303 if (RA.isPowerOf2()) {
2304 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002305 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002306
Duncan Sands33274982010-01-29 09:45:26 +00002307 // The low bits of the first operand are unchanged by the srem.
2308 KnownZero = KnownZero2 & LowBits;
2309 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002310
Duncan Sands33274982010-01-29 09:45:26 +00002311 // If the first operand is non-negative or has all low bits zero, then
2312 // the upper bits are all zero.
2313 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2314 KnownZero |= ~LowBits;
2315
2316 // If the first operand is negative and not all low bits are zero, then
2317 // the upper bits are all one.
2318 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2319 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002320 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002321 }
2322 }
Jay Foad5a29c362014-05-15 12:12:55 +00002323 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002324 case ISD::UREM: {
2325 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002326 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002327 if (RA.isPowerOf2()) {
2328 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002329 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2330
2331 // The upper bits are all zero, the lower ones are unchanged.
2332 KnownZero = KnownZero2 | ~LowBits;
2333 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002334 break;
2335 }
2336 }
2337
2338 // Since the result is less than or equal to either operand, any leading
2339 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002340 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2341 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002342
2343 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2344 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002345 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002346 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002347 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002348 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002349 case ISD::EXTRACT_ELEMENT: {
2350 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2351 const unsigned Index =
2352 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2353 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2354
2355 // Remove low part of known bits mask
2356 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2357 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2358
2359 // Remove high part of known bit mask
2360 KnownZero = KnownZero.trunc(BitWidth);
2361 KnownOne = KnownOne.trunc(BitWidth);
2362 break;
2363 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002364 case ISD::FrameIndex:
2365 case ISD::TargetFrameIndex:
2366 if (unsigned Align = InferPtrAlignment(Op)) {
2367 // The low bits are known zero if the pointer is aligned.
2368 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002369 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002370 }
2371 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002372
Dan Gohman309d3d52007-06-22 14:59:07 +00002373 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002374 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2375 break;
2376 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 case ISD::INTRINSIC_WO_CHAIN:
2378 case ISD::INTRINSIC_W_CHAIN:
2379 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002380 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002381 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002382 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002383 }
Jay Foad5a29c362014-05-15 12:12:55 +00002384
2385 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002386}
2387
2388/// ComputeNumSignBits - Return the number of times the sign bit of the
2389/// register is replicated into the other bits. We know that at least 1 bit
2390/// is always equal to the sign bit (itself), but other cases can give us
2391/// information. For example, immediately after an "SRA X, 2", we know that
2392/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002393unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002394 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002395 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002396 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002397 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002398 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002399
Dan Gohman309d3d52007-06-22 14:59:07 +00002400 if (Depth == 6)
2401 return 1; // Limit search depth.
2402
2403 switch (Op.getOpcode()) {
2404 default: break;
2405 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002406 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002407 return VTBits-Tmp+1;
2408 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002409 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002410 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002411
Dan Gohman309d3d52007-06-22 14:59:07 +00002412 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002413 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002414 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002415 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002416
Dan Gohman309d3d52007-06-22 14:59:07 +00002417 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002418 Tmp =
2419 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002420 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002421
Dan Gohman309d3d52007-06-22 14:59:07 +00002422 case ISD::SIGN_EXTEND_INREG:
2423 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002424 Tmp =
2425 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002427
Dan Gohman309d3d52007-06-22 14:59:07 +00002428 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2429 return std::max(Tmp, Tmp2);
2430
2431 case ISD::SRA:
2432 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2433 // SRA X, C -> adds C sign bits.
2434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002435 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002436 if (Tmp > VTBits) Tmp = VTBits;
2437 }
2438 return Tmp;
2439 case ISD::SHL:
2440 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2441 // shl destroys sign bits.
2442 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002443 if (C->getZExtValue() >= VTBits || // Bad shift.
2444 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2445 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 }
2447 break;
2448 case ISD::AND:
2449 case ISD::OR:
2450 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002451 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002452 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002453 if (Tmp != 1) {
2454 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2455 FirstAnswer = std::min(Tmp, Tmp2);
2456 // We computed what we know about the sign bits as our first
2457 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002458 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002459 }
2460 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002461
2462 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002463 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002464 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002465 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002466 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002467
2468 case ISD::SADDO:
2469 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002470 case ISD::SSUBO:
2471 case ISD::USUBO:
2472 case ISD::SMULO:
2473 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002474 if (Op.getResNo() != 1)
2475 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002476 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002477 // If setcc returns 0/-1, all bits are sign bits.
2478 // We know that we have an integer-based boolean since these operations
2479 // are only available for integer.
2480 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2481 TargetLowering::ZeroOrNegativeOneBooleanContent)
2482 return VTBits;
2483 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002484 case ISD::SETCC:
2485 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002486 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002487 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002488 return VTBits;
2489 break;
2490 case ISD::ROTL:
2491 case ISD::ROTR:
2492 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002493 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002494
Dan Gohman309d3d52007-06-22 14:59:07 +00002495 // Handle rotate right by N like a rotate left by 32-N.
2496 if (Op.getOpcode() == ISD::ROTR)
2497 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2498
2499 // If we aren't rotating out all of the known-in sign bits, return the
2500 // number that are left. This handles rotl(sext(x), 1) for example.
2501 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2502 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2503 }
2504 break;
2505 case ISD::ADD:
2506 // Add can have at most one carry bit. Thus we know that the output
2507 // is, at worst, one more bit than the inputs.
2508 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2509 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002510
Dan Gohman309d3d52007-06-22 14:59:07 +00002511 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002512 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002513 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002514 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002515 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002516
Dan Gohman309d3d52007-06-22 14:59:07 +00002517 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2518 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002519 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002520 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002521
Dan Gohman309d3d52007-06-22 14:59:07 +00002522 // If we are subtracting one from a positive number, there is no carry
2523 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002524 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002525 return Tmp;
2526 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002527
Dan Gohman309d3d52007-06-22 14:59:07 +00002528 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2529 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002530 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002531
Dan Gohman309d3d52007-06-22 14:59:07 +00002532 case ISD::SUB:
2533 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2534 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002535
Dan Gohman309d3d52007-06-22 14:59:07 +00002536 // Handle NEG.
2537 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002538 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002539 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002540 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002541 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2542 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002543 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002544 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002545
Dan Gohman309d3d52007-06-22 14:59:07 +00002546 // If the input is known to be positive (the sign bit is known clear),
2547 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002548 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002549 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002550
Dan Gohman309d3d52007-06-22 14:59:07 +00002551 // Otherwise, we treat this like a SUB.
2552 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002553
Dan Gohman309d3d52007-06-22 14:59:07 +00002554 // Sub can have at most one carry bit. Thus we know that the output
2555 // is, at worst, one more bit than the inputs.
2556 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2557 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002558 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002559 case ISD::TRUNCATE:
2560 // FIXME: it's tricky to do anything useful for this, but it is an important
2561 // case for targets like X86.
2562 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002563 case ISD::EXTRACT_ELEMENT: {
2564 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2565 const int BitWidth = Op.getValueType().getSizeInBits();
2566 const int Items =
2567 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2568
2569 // Get reverse index (starting from 1), Op1 value indexes elements from
2570 // little end. Sign starts at big end.
2571 const int rIndex = Items - 1 -
2572 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2573
2574 // If the sign portion ends in our element the substraction gives correct
2575 // result. Otherwise it gives either negative or > bitwidth result
2576 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2577 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002578 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002579
Nadav Rotem4536d582013-03-20 22:53:44 +00002580 // If we are looking at the loaded value of the SDNode.
2581 if (Op.getResNo() == 0) {
2582 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2583 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2584 unsigned ExtType = LD->getExtensionType();
2585 switch (ExtType) {
2586 default: break;
2587 case ISD::SEXTLOAD: // '17' bits known
2588 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2589 return VTBits-Tmp+1;
2590 case ISD::ZEXTLOAD: // '16' bits known
2591 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2592 return VTBits-Tmp;
2593 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002594 }
2595 }
2596
2597 // Allow the target to implement this method for its nodes.
2598 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002599 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002600 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2601 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002602 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002603 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002604 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002605
Dan Gohman309d3d52007-06-22 14:59:07 +00002606 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2607 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002608 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002609 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002610
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002611 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002612 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002613 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002614 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002615 Mask = KnownOne;
2616 } else {
2617 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002618 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002619 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002620
Dan Gohman309d3d52007-06-22 14:59:07 +00002621 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2622 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002623 Mask = ~Mask;
2624 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002625 // Return # leading zeros. We use 'min' here in case Val was zero before
2626 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002627 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002628}
2629
Chris Lattner46c01a32011-02-13 22:25:43 +00002630/// isBaseWithConstantOffset - Return true if the specified operand is an
2631/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2632/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2633/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002634/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002635bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2636 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2637 !isa<ConstantSDNode>(Op.getOperand(1)))
2638 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002639
2640 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002641 !MaskedValueIsZero(Op.getOperand(0),
2642 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2643 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002644
Chris Lattner46c01a32011-02-13 22:25:43 +00002645 return true;
2646}
2647
2648
Dan Gohmand0d5e682009-09-03 20:34:31 +00002649bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2650 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002651 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002652 return true;
2653
2654 // If the value is a constant, we can obviously see if it is a NaN or not.
2655 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2656 return !C->getValueAPF().isNaN();
2657
2658 // TODO: Recognize more cases here.
2659
2660 return false;
2661}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002662
Dan Gohman38605212010-02-24 06:52:40 +00002663bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2664 // If the value is a constant, we can obviously see if it is a zero or not.
2665 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2666 return !C->isZero();
2667
2668 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002669 switch (Op.getOpcode()) {
2670 default: break;
2671 case ISD::OR:
2672 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2673 return !C->isNullValue();
2674 break;
2675 }
Dan Gohman38605212010-02-24 06:52:40 +00002676
2677 return false;
2678}
2679
2680bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2681 // Check the obvious case.
2682 if (A == B) return true;
2683
2684 // For for negative and positive zero.
2685 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2686 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2687 if (CA->isZero() && CB->isZero()) return true;
2688
2689 // Otherwise they may not be equal.
2690 return false;
2691}
2692
Chris Lattner061a1ea2005-01-07 07:46:32 +00002693/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002694///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002695SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002696 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002697 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002698 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002699 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002700 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002701
Jack Carter170a5f22013-09-09 22:02:08 +00002702 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2703 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002704 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002705
Chandler Carruth41b20e72014-07-22 04:07:55 +00002706 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002707 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002708}
2709
Andrew Trickef9de2a2013-05-25 02:42:55 +00002710SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002711 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002712 // Constant fold unary operations with an integer constant operand. Even
2713 // opaque constant will be folded, because the folding of unary operations
2714 // doesn't create new constants with different values. Nevertheless, the
2715 // opaque flag is preserved during folding to prevent future folding with
2716 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002717 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002718 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002719 switch (Opcode) {
2720 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002721 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002722 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2723 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002724 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002725 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002726 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002727 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2728 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002729 case ISD::UINT_TO_FP:
2730 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002731 APFloat apf(EVTToAPFloatSemantics(VT),
2732 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002733 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002734 Opcode==ISD::SINT_TO_FP,
2735 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002736 return getConstantFP(apf, VT);
2737 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002738 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002739 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
2740 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002741 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002742 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002743 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002744 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002745 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002746 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002747 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2748 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002749 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002750 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2751 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002752 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002753 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002754 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2755 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002756 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002757 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002758 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2759 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002760 }
2761 }
2762
Dale Johannesen446b9002007-08-31 23:34:27 +00002763 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002764 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002765 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002766 switch (Opcode) {
2767 case ISD::FNEG:
2768 V.changeSign();
2769 return getConstantFP(V, VT);
2770 case ISD::FABS:
2771 V.clearSign();
2772 return getConstantFP(V, VT);
2773 case ISD::FCEIL: {
2774 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2775 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002776 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002777 break;
2778 }
2779 case ISD::FTRUNC: {
2780 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2781 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002782 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002783 break;
2784 }
2785 case ISD::FFLOOR: {
2786 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2787 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002788 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002789 break;
2790 }
2791 case ISD::FP_EXTEND: {
2792 bool ignored;
2793 // This can return overflow, underflow, or inexact; we don't care.
2794 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002795 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002796 APFloat::rmNearestTiesToEven, &ignored);
2797 return getConstantFP(V, VT);
2798 }
2799 case ISD::FP_TO_SINT:
2800 case ISD::FP_TO_UINT: {
2801 integerPart x[2];
2802 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002803 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002804 // FIXME need to be more flexible about rounding mode.
2805 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2806 Opcode==ISD::FP_TO_SINT,
2807 APFloat::rmTowardZero, &ignored);
2808 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002809 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002810 APInt api(VT.getSizeInBits(), x);
2811 return getConstant(api, VT);
2812 }
2813 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002814 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
2815 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), VT);
2816 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002817 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2818 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2819 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2820 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002821 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002822 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002823
Jim Grosbachf7502c42014-07-18 00:40:52 +00002824 // Constant fold unary operations with a vector integer operand.
2825 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002826 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002827 switch (Opcode) {
2828 default:
2829 // FIXME: Entirely reasonable to perform folding of other unary
2830 // operations here as the need arises.
2831 break;
2832 case ISD::UINT_TO_FP:
2833 case ISD::SINT_TO_FP: {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002834 SmallVector<SDValue, 8> Ops;
2835 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2836 SDValue OpN = BV->getOperand(i);
2837 // Let the above scalar folding handle the conversion of each
2838 // element.
2839 OpN = getNode(ISD::SINT_TO_FP, DL, VT.getVectorElementType(),
2840 OpN);
2841 Ops.push_back(OpN);
2842 }
2843 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Jim Grosbachf7502c42014-07-18 00:40:52 +00002844 }
2845 }
2846 }
2847 }
2848
Gabor Greiff304a7a2008-08-28 21:40:38 +00002849 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002850 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002851 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002852 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002853 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002854 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002855 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002856 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002857 assert(VT.isFloatingPoint() &&
2858 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002859 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002860 assert((!VT.isVector() ||
2861 VT.getVectorNumElements() ==
2862 Operand.getValueType().getVectorNumElements()) &&
2863 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002864 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002865 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002866 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002867 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002868 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002869 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002870 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002871 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2872 "Invalid sext node, dst < src!");
2873 assert((!VT.isVector() ||
2874 VT.getVectorNumElements() ==
2875 Operand.getValueType().getVectorNumElements()) &&
2876 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002877 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2878 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2879 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002880 // sext(undef) = 0, because the top bits will all be the same.
2881 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002882 break;
2883 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002884 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002885 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002886 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002887 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2888 "Invalid zext node, dst < src!");
2889 assert((!VT.isVector() ||
2890 VT.getVectorNumElements() ==
2891 Operand.getValueType().getVectorNumElements()) &&
2892 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002893 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002894 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002895 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002896 else if (OpOpcode == ISD::UNDEF)
2897 // zext(undef) = 0, because the top bits will be zero.
2898 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002899 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002900 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002901 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002902 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002903 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002904 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2905 "Invalid anyext node, dst < src!");
2906 assert((!VT.isVector() ||
2907 VT.getVectorNumElements() ==
2908 Operand.getValueType().getVectorNumElements()) &&
2909 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002910
Dan Gohman08837892010-06-18 00:08:30 +00002911 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2912 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002913 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002914 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002915 else if (OpOpcode == ISD::UNDEF)
2916 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002917
2918 // (ext (trunx x)) -> x
2919 if (OpOpcode == ISD::TRUNCATE) {
2920 SDValue OpOp = Operand.getNode()->getOperand(0);
2921 if (OpOp.getValueType() == VT)
2922 return OpOp;
2923 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002924 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002925 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002926 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002927 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002928 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002929 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2930 "Invalid truncate node, src < dst!");
2931 assert((!VT.isVector() ||
2932 VT.getVectorNumElements() ==
2933 Operand.getValueType().getVectorNumElements()) &&
2934 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002935 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002936 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002937 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2938 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002939 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002940 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2941 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002942 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002943 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002944 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002945 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002946 }
Craig Topper201c1a32012-01-15 01:05:11 +00002947 if (OpOpcode == ISD::UNDEF)
2948 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002949 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002950 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002951 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002952 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002953 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002954 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002955 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2956 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002957 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002958 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002959 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002960 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002961 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002962 (VT.getVectorElementType() == Operand.getValueType() ||
2963 (VT.getVectorElementType().isInteger() &&
2964 Operand.getValueType().isInteger() &&
2965 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002966 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002967 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002968 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002969 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2970 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2971 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2972 Operand.getConstantOperandVal(1) == 0 &&
2973 Operand.getOperand(0).getValueType() == VT)
2974 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002975 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002976 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002977 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002978 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002979 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002980 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002981 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002982 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002983 break;
2984 case ISD::FABS:
2985 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002986 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002987 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002988 }
2989
Chris Lattnerf9c19152005-08-25 19:12:10 +00002990 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002991 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002992 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002993 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002994 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002995 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002996 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002997 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002998 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002999
Jack Carter170a5f22013-09-09 22:02:08 +00003000 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3001 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003002 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003003 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003004 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3005 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003006 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003007
Chandler Carruth41b20e72014-07-22 04:07:55 +00003008 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003009 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003010}
3011
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003012SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
3013 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003014 // If the opcode is a target-specific ISD node, there's nothing we can
3015 // do here and the operand rules may not line up with the below, so
3016 // bail early.
3017 if (Opcode >= ISD::BUILTIN_OP_END)
3018 return SDValue();
3019
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003020 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3021 SmallVector<SDValue, 4> Outputs;
3022 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00003023
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003024 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3025 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003026 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3027 return SDValue();
3028
3029 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003030 // Scalar instruction.
3031 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003032 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003033 // For vectors extract each constant element into Inputs so we can constant
3034 // fold them individually.
3035 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3036 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3037 if (!BV1 || !BV2)
3038 return SDValue();
3039
3040 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3041
3042 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3043 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3044 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3045 if (!V1 || !V2) // Not a constant, bail.
3046 return SDValue();
3047
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003048 if (V1->isOpaque() || V2->isOpaque())
3049 return SDValue();
3050
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003051 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3052 // FIXME: This is valid and could be handled by truncating the APInts.
3053 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3054 return SDValue();
3055
3056 Inputs.push_back(std::make_pair(V1, V2));
3057 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003058 }
3059
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003060 // We have a number of constant values, constant fold them element by element.
3061 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3062 const APInt &C1 = Inputs[I].first->getAPIntValue();
3063 const APInt &C2 = Inputs[I].second->getAPIntValue();
3064
3065 switch (Opcode) {
3066 case ISD::ADD:
3067 Outputs.push_back(getConstant(C1 + C2, SVT));
3068 break;
3069 case ISD::SUB:
3070 Outputs.push_back(getConstant(C1 - C2, SVT));
3071 break;
3072 case ISD::MUL:
3073 Outputs.push_back(getConstant(C1 * C2, SVT));
3074 break;
3075 case ISD::UDIV:
3076 if (!C2.getBoolValue())
3077 return SDValue();
3078 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3079 break;
3080 case ISD::UREM:
3081 if (!C2.getBoolValue())
3082 return SDValue();
3083 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3084 break;
3085 case ISD::SDIV:
3086 if (!C2.getBoolValue())
3087 return SDValue();
3088 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3089 break;
3090 case ISD::SREM:
3091 if (!C2.getBoolValue())
3092 return SDValue();
3093 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3094 break;
3095 case ISD::AND:
3096 Outputs.push_back(getConstant(C1 & C2, SVT));
3097 break;
3098 case ISD::OR:
3099 Outputs.push_back(getConstant(C1 | C2, SVT));
3100 break;
3101 case ISD::XOR:
3102 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3103 break;
3104 case ISD::SHL:
3105 Outputs.push_back(getConstant(C1 << C2, SVT));
3106 break;
3107 case ISD::SRL:
3108 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3109 break;
3110 case ISD::SRA:
3111 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3112 break;
3113 case ISD::ROTL:
3114 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3115 break;
3116 case ISD::ROTR:
3117 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3118 break;
3119 default:
3120 return SDValue();
3121 }
3122 }
3123
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003124 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3125 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003126
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003127 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003128 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003129 return Outputs.back();
3130
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003131 // We may have a vector type but a scalar result. Create a splat.
3132 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3133
3134 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003135 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003136}
3137
Andrew Trickef9de2a2013-05-25 02:42:55 +00003138SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003139 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003140 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3141 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003142 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003143 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003144 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003145 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3146 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003147 // Fold trivial token factors.
3148 if (N1.getOpcode() == ISD::EntryToken) return N2;
3149 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003150 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003151 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003152 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003153 // Concat of UNDEFs is UNDEF.
3154 if (N1.getOpcode() == ISD::UNDEF &&
3155 N2.getOpcode() == ISD::UNDEF)
3156 return getUNDEF(VT);
3157
Dan Gohman550c9af2008-08-14 20:04:46 +00003158 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3159 // one big BUILD_VECTOR.
3160 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3161 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003162 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3163 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003164 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003165 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003166 }
3167 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003168 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003169 assert(VT.isInteger() && "This operator does not apply to FP types!");
3170 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003171 N1.getValueType() == VT && "Binary operator types must match!");
3172 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3173 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003174 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003175 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003176 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3177 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003178 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003179 case ISD::OR:
3180 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003181 case ISD::ADD:
3182 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003183 assert(VT.isInteger() && "This operator does not apply to FP types!");
3184 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003185 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003186 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3187 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003188 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003189 return N1;
3190 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003191 case ISD::UDIV:
3192 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003193 case ISD::MULHU:
3194 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003195 case ISD::MUL:
3196 case ISD::SDIV:
3197 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003198 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003199 assert(N1.getValueType() == N2.getValueType() &&
3200 N1.getValueType() == VT && "Binary operator types must match!");
3201 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003202 case ISD::FADD:
3203 case ISD::FSUB:
3204 case ISD::FMUL:
3205 case ISD::FDIV:
3206 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003207 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003208 if (Opcode == ISD::FADD) {
3209 // 0+x --> x
3210 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3211 if (CFP->getValueAPF().isZero())
3212 return N2;
3213 // x+0 --> x
3214 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3215 if (CFP->getValueAPF().isZero())
3216 return N1;
3217 } else if (Opcode == ISD::FSUB) {
3218 // x-0 --> x
3219 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3220 if (CFP->getValueAPF().isZero())
3221 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003222 } else if (Opcode == ISD::FMUL) {
3223 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3224 SDValue V = N2;
3225
3226 // If the first operand isn't the constant, try the second
3227 if (!CFP) {
3228 CFP = dyn_cast<ConstantFPSDNode>(N2);
3229 V = N1;
3230 }
3231
3232 if (CFP) {
3233 // 0*x --> 0
3234 if (CFP->isZero())
3235 return SDValue(CFP,0);
3236 // 1*x --> x
3237 if (CFP->isExactlyValue(1.0))
3238 return V;
3239 }
Dan Gohman1275e282009-01-23 19:10:37 +00003240 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003241 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003242 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003243 assert(N1.getValueType() == N2.getValueType() &&
3244 N1.getValueType() == VT && "Binary operator types must match!");
3245 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003246 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3247 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003248 N1.getValueType().isFloatingPoint() &&
3249 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003250 "Invalid FCOPYSIGN!");
3251 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003252 case ISD::SHL:
3253 case ISD::SRA:
3254 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003255 case ISD::ROTL:
3256 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003257 assert(VT == N1.getValueType() &&
3258 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003259 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003260 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003261 assert((!VT.isVector() || VT == N2.getValueType()) &&
3262 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003263 // Verify that the shift amount VT is bit enough to hold valid shift
3264 // amounts. This catches things like trying to shift an i1024 value by an
3265 // i8, which is easy to fall into in generic code that uses
3266 // TLI.getShiftAmount().
3267 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003268 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003269 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003270
3271 // Always fold shifts of i1 values so the code generator doesn't need to
3272 // handle them. Since we know the size of the shift has to be less than the
3273 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003274 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003275 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003276 if (N2C && N2C->isNullValue())
3277 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003278 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003279 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003280 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003281 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003282 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003283 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003284 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003285 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003286 "type is vector!");
3287 assert((!EVT.isVector() ||
3288 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3289 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003290 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003291 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003292 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003293 break;
3294 }
Chris Lattner72733e52008-01-17 07:00:52 +00003295 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003296 assert(VT.isFloatingPoint() &&
3297 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003298 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003299 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003300 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003301 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003302 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003303 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003304 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003305 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003306 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003307 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003308 assert(!EVT.isVector() &&
3309 "AssertSExt/AssertZExt type should be the vector element type "
3310 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003311 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003312 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003313 break;
3314 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003315 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003316 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003317 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003318 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003319 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003320 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003321 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003322 "type is vector!");
3323 assert((!EVT.isVector() ||
3324 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3325 "Vector element counts must match in SIGN_EXTEND_INREG");
3326 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003327 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003328
Chris Lattner16713612008-01-22 19:09:33 +00003329 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003330 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003331 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003332 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003333 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003334 return getConstant(Val, VT);
3335 }
Chris Lattner16713612008-01-22 19:09:33 +00003336 break;
3337 }
3338 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003339 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3340 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003341 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003342
Chris Lattner16713612008-01-22 19:09:33 +00003343 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3344 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003345 if (N2C &&
3346 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003347 N1.getNumOperands() > 0) {
3348 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003349 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003350 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003351 N1.getOperand(N2C->getZExtValue() / Factor),
3352 getConstant(N2C->getZExtValue() % Factor,
3353 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003354 }
3355
3356 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3357 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003358 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3359 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003360
3361 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003362 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003363 // are promoted and implicitly truncated, and the result implicitly
3364 // extended. Make that explicit here.
3365 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003366
Bob Wilson59dbbb22009-04-13 22:05:19 +00003367 return Elt;
3368 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003369
Chris Lattner16713612008-01-22 19:09:33 +00003370 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3371 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003372 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003373 // If the indices are the same, return the inserted element else
3374 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003375 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003376 SDValue N1Op2 = N1.getOperand(2);
3377 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3378
3379 if (N1Op2C && N2C) {
3380 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3381 if (VT == N1.getOperand(1).getValueType())
3382 return N1.getOperand(1);
3383 else
3384 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3385 }
3386
Dale Johannesendb393622009-02-03 01:55:44 +00003387 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003388 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003389 }
Chris Lattner16713612008-01-22 19:09:33 +00003390 break;
3391 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003392 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003393 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3394 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003395 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003396 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003397
Chris Lattner16713612008-01-22 19:09:33 +00003398 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3399 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003400 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003401 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003402 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003403
Chris Lattner16713612008-01-22 19:09:33 +00003404 // EXTRACT_ELEMENT of a constant int is also very common.
3405 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003406 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003407 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003408 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3409 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003410 }
3411 break;
David Greeneb6f16112011-01-26 15:38:49 +00003412 case ISD::EXTRACT_SUBVECTOR: {
3413 SDValue Index = N2;
3414 if (VT.isSimple() && N1.getValueType().isSimple()) {
3415 assert(VT.isVector() && N1.getValueType().isVector() &&
3416 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003417 assert(VT.getVectorElementType() ==
3418 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003419 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003420 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003421 "Extract subvector must be from larger vector to smaller vector!");
3422
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003423 if (isa<ConstantSDNode>(Index.getNode())) {
3424 assert((VT.getVectorNumElements() +
3425 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003426 <= N1.getValueType().getVectorNumElements())
3427 && "Extract subvector overflow!");
3428 }
3429
3430 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003431 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003432 return N1;
3433 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003434 break;
Chris Lattner16713612008-01-22 19:09:33 +00003435 }
David Greeneb6f16112011-01-26 15:38:49 +00003436 }
Chris Lattner16713612008-01-22 19:09:33 +00003437
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003438 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003439 if (SDValue SV =
3440 FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode()))
3441 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003442
3443 // Canonicalize constant to RHS if commutative.
3444 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3445 std::swap(N1C, N2C);
3446 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003447 }
3448
Chris Lattner16713612008-01-22 19:09:33 +00003449 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003450 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003451 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3452 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003453 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003454 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003455 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003456 std::swap(N1CFP, N2CFP);
3457 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003458 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003459 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3460 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003461 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003462 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003463 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003464 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003465 return getConstantFP(V1, VT);
3466 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003467 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003468 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003469 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003470 return getConstantFP(V1, VT);
3471 break;
3472 case ISD::FMUL:
3473 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003474 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003475 return getConstantFP(V1, VT);
3476 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003477 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003478 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003479 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3480 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003481 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003482 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003483 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003484 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003485 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003486 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3487 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003488 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003489 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003490 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003491 case ISD::FCOPYSIGN:
3492 V1.copySign(V2);
3493 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003494 default: break;
3495 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003496 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003497
3498 if (Opcode == ISD::FP_ROUND) {
3499 APFloat V = N1CFP->getValueAPF(); // make copy
3500 bool ignored;
3501 // This can return overflow, underflow, or inexact; we don't care.
3502 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003503 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003504 APFloat::rmNearestTiesToEven, &ignored);
3505 return getConstantFP(V, VT);
3506 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003507 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003508
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003509 // Canonicalize an UNDEF to the RHS, even over a constant.
3510 if (N1.getOpcode() == ISD::UNDEF) {
3511 if (isCommutativeBinOp(Opcode)) {
3512 std::swap(N1, N2);
3513 } else {
3514 switch (Opcode) {
3515 case ISD::FP_ROUND_INREG:
3516 case ISD::SIGN_EXTEND_INREG:
3517 case ISD::SUB:
3518 case ISD::FSUB:
3519 case ISD::FDIV:
3520 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003521 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003522 return N1; // fold op(undef, arg2) -> undef
3523 case ISD::UDIV:
3524 case ISD::SDIV:
3525 case ISD::UREM:
3526 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003527 case ISD::SRL:
3528 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003529 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003530 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3531 // For vectors, we can't easily build an all zero vector, just return
3532 // the LHS.
3533 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003534 }
3535 }
3536 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003537
3538 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003539 if (N2.getOpcode() == ISD::UNDEF) {
3540 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003541 case ISD::XOR:
3542 if (N1.getOpcode() == ISD::UNDEF)
3543 // Handle undef ^ undef -> 0 special case. This is a common
3544 // idiom (misuse).
3545 return getConstant(0, VT);
3546 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003547 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003548 case ISD::ADDC:
3549 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003550 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003551 case ISD::UDIV:
3552 case ISD::SDIV:
3553 case ISD::UREM:
3554 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003555 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003556 case ISD::FADD:
3557 case ISD::FSUB:
3558 case ISD::FMUL:
3559 case ISD::FDIV:
3560 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003561 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003562 return N2;
3563 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003564 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003565 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003566 case ISD::SRL:
3567 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003568 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003569 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3570 // For vectors, we can't easily build an all zero vector, just return
3571 // the LHS.
3572 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003573 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003574 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003575 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003576 // For vectors, we can't easily build an all one vector, just return
3577 // the LHS.
3578 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003579 case ISD::SRA:
3580 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003581 }
3582 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003583
Chris Lattner724f7ee2005-05-11 18:57:39 +00003584 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003585 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003586 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003587 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003588 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003589 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003590 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003591 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003592 if (BinOpHasFlags)
3593 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003594 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003595 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003596 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003597
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003598 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3599
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003600 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003601 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003602
3603 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003604 }
3605
Chandler Carruth41b20e72014-07-22 04:07:55 +00003606 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003607 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003608}
3609
Andrew Trickef9de2a2013-05-25 02:42:55 +00003610SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003611 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003612 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003613 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003614 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003615 case ISD::FMA: {
3616 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3617 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3618 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3619 if (N1CFP && N2CFP && N3CFP) {
3620 APFloat V1 = N1CFP->getValueAPF();
3621 const APFloat &V2 = N2CFP->getValueAPF();
3622 const APFloat &V3 = N3CFP->getValueAPF();
3623 APFloat::opStatus s =
3624 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003625 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Owen Anderson32baf992013-05-09 22:27:13 +00003626 return getConstantFP(V1, VT);
3627 }
3628 break;
3629 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003630 case ISD::CONCAT_VECTORS:
3631 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3632 // one big BUILD_VECTOR.
3633 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3634 N2.getOpcode() == ISD::BUILD_VECTOR &&
3635 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003636 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3637 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003638 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3639 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003640 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003641 }
3642 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003643 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003644 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003645 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003646 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003647 break;
3648 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003649 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003650 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003651 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003652 return N2; // select true, X, Y -> X
3653 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003654 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003655
3656 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003657 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003658 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003659 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003660 case ISD::INSERT_SUBVECTOR: {
3661 SDValue Index = N3;
3662 if (VT.isSimple() && N1.getValueType().isSimple()
3663 && N2.getValueType().isSimple()) {
3664 assert(VT.isVector() && N1.getValueType().isVector() &&
3665 N2.getValueType().isVector() &&
3666 "Insert subvector VTs must be a vectors");
3667 assert(VT == N1.getValueType() &&
3668 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003669 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003670 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003671 if (isa<ConstantSDNode>(Index.getNode())) {
3672 assert((N2.getValueType().getVectorNumElements() +
3673 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003674 <= VT.getVectorNumElements())
3675 && "Insert subvector overflow!");
3676 }
3677
3678 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003679 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003680 return N2;
3681 }
3682 break;
3683 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003684 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003685 // Fold bit_convert nodes from a type to themselves.
3686 if (N1.getValueType() == VT)
3687 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003688 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003689 }
3690
Chris Lattnerf9c19152005-08-25 19:12:10 +00003691 // Memoize node if it doesn't produce a flag.
3692 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003693 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003694 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003695 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003696 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003697 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003698 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003699 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003700 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003701
Jack Carter170a5f22013-09-09 22:02:08 +00003702 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3703 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003704 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003705 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003706 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3707 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003708 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003709
Chandler Carruth41b20e72014-07-22 04:07:55 +00003710 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003711 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003712}
3713
Andrew Trickef9de2a2013-05-25 02:42:55 +00003714SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003715 SDValue N1, SDValue N2, SDValue N3,
3716 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003717 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003718 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003719}
3720
Andrew Trickef9de2a2013-05-25 02:42:55 +00003721SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003722 SDValue N1, SDValue N2, SDValue N3,
3723 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003724 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003725 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003726}
3727
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003728/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3729/// the incoming stack arguments to be loaded from the stack.
3730SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3731 SmallVector<SDValue, 8> ArgChains;
3732
3733 // Include the original chain at the beginning of the list. When this is
3734 // used by target LowerCall hooks, this helps legalize find the
3735 // CALLSEQ_BEGIN node.
3736 ArgChains.push_back(Chain);
3737
3738 // Add a chain value for each stack argument.
3739 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3740 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3741 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3742 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3743 if (FI->getIndex() < 0)
3744 ArgChains.push_back(SDValue(L, 1));
3745
3746 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003747 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003748}
3749
Dan Gohman544ab2c2008-04-12 04:36:06 +00003750/// getMemsetValue - Vectorized representation of the memset value
3751/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003752static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003753 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003754 assert(Value.getOpcode() != ISD::UNDEF);
3755
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003756 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003758 assert(C->getAPIntValue().getBitWidth() == 8);
3759 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003760 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003761 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003762 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003763 }
Evan Chengef377ad2008-05-15 08:39:06 +00003764
Dale Johannesenabf66b82009-02-03 22:26:09 +00003765 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003766 if (NumBits > 8) {
3767 // Use a multiplication with 0x010101... to extend the input to the
3768 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003769 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003770 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003771 }
3772
3773 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003774}
3775
Dan Gohman544ab2c2008-04-12 04:36:06 +00003776/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3777/// used when a memcpy is turned into a memset when the source is a constant
3778/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003779static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003780 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003781 // Handle vector with all elements zero.
3782 if (Str.empty()) {
3783 if (VT.isInteger())
3784 return DAG.getConstant(0, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003785 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003786 return DAG.getConstantFP(0.0, VT);
3787 else if (VT.isVector()) {
3788 unsigned NumElts = VT.getVectorNumElements();
3789 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003790 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003791 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3792 EltVT, NumElts)));
3793 } else
3794 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003795 }
3796
Duncan Sands13237ac2008-06-06 12:08:01 +00003797 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003798 unsigned NumVTBits = VT.getSizeInBits();
3799 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003800 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3801
Evan Chengc8444b12013-01-10 22:13:27 +00003802 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003803 if (TLI.isLittleEndian()) {
3804 for (unsigned i = 0; i != NumBytes; ++i)
3805 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3806 } else {
3807 for (unsigned i = 0; i != NumBytes; ++i)
3808 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003809 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003810
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003811 // If the "cost" of materializing the integer immediate is less than the cost
3812 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003813 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3814 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003815 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003816 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003817}
3818
Scott Michelcf0da6c2009-02-17 22:15:04 +00003819/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003820///
Andrew Tricke2431c62013-05-25 03:08:10 +00003821static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003822 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003823 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003824 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003825 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003826}
3827
Evan Chengef377ad2008-05-15 08:39:06 +00003828/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3829///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003830static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003831 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003832 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003833 if (Src.getOpcode() == ISD::GlobalAddress)
3834 G = cast<GlobalAddressSDNode>(Src);
3835 else if (Src.getOpcode() == ISD::ADD &&
3836 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3837 Src.getOperand(1).getOpcode() == ISD::Constant) {
3838 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003839 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003840 }
3841 if (!G)
3842 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003843
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003844 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003845}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003846
Evan Cheng43cd9e32010-04-01 06:04:33 +00003847/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3848/// to replace the memset / memcpy. Return true if the number of memory ops
3849/// is below the threshold. It returns the types of the sequence of
3850/// memory ops to perform memset / memcpy by reference.
3851static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003852 unsigned Limit, uint64_t Size,
3853 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003854 bool IsMemset,
3855 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003856 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003857 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003858 SelectionDAG &DAG,
3859 const TargetLowering &TLI) {
3860 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3861 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003862 // If 'SrcAlign' is zero, that means the memory operation does not need to
3863 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3864 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3865 // is the specified alignment of the memory operation. If it is zero, that
3866 // means it's possible to change the alignment of the destination.
3867 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3868 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003869 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003870 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003871 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003872
Chris Lattner28dc6c12010-03-07 07:45:08 +00003873 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003874 unsigned AS = 0;
3875 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003876 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003877 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003878 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003879 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003880 case 0: VT = MVT::i64; break;
3881 case 4: VT = MVT::i32; break;
3882 case 2: VT = MVT::i16; break;
3883 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003884 }
3885 }
3886
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003887 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003888 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003889 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003890 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003891
Duncan Sands11dd4242008-06-08 20:54:56 +00003892 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003893 VT = LVT;
3894 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003895
Dan Gohman544ab2c2008-04-12 04:36:06 +00003896 unsigned NumMemOps = 0;
3897 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003898 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003899 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003900 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003901 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003902 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003903
3904 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003905 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003906 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003907 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003908 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003909 Found = true;
3910 else if (NewVT == MVT::i64 &&
3911 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003912 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003913 // i64 is usually not legal on 32-bit targets, but f64 may be.
3914 NewVT = MVT::f64;
3915 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003916 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003917 }
3918
Evan Cheng04e55182012-12-12 00:42:09 +00003919 if (!Found) {
3920 do {
3921 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3922 if (NewVT == MVT::i8)
3923 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003924 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003925 }
3926 NewVTSize = NewVT.getSizeInBits() / 8;
3927
Evan Cheng79e2ca92012-12-10 23:21:26 +00003928 // If the new VT cannot cover all of the remaining bits, then consider
3929 // issuing a (or a pair of) unaligned and overlapping load / store.
3930 // FIXME: Only does this for 64-bit or more since we don't have proper
3931 // cost model for unaligned load / store.
3932 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003933 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003934 if (NumMemOps && AllowOverlap &&
3935 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003936 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003937 VTSize = Size;
3938 else {
3939 VT = NewVT;
3940 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003941 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003942 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003943
Evan Chengb7d3d032012-12-12 20:43:23 +00003944 if (++NumMemOps > Limit)
3945 return false;
3946
Dan Gohman544ab2c2008-04-12 04:36:06 +00003947 MemOps.push_back(VT);
3948 Size -= VTSize;
3949 }
3950
3951 return true;
3952}
3953
Andrew Trickef9de2a2013-05-25 02:42:55 +00003954static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003955 SDValue Chain, SDValue Dst,
3956 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003957 unsigned Align, bool isVol,
3958 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003959 MachinePointerInfo DstPtrInfo,
3960 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003961 // Turn a memcpy of undef to nop.
3962 if (Src.getOpcode() == ISD::UNDEF)
3963 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003964
Dan Gohman714663a2008-05-29 19:42:22 +00003965 // Expand memcpy to a series of load and store ops if the size operand falls
3966 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003967 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3968 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003970 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003971 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003972 MachineFunction &MF = DAG.getMachineFunction();
3973 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003974 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003975 MF.getFunction()->getAttributes().
3976 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003977 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3978 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3979 DstAlignCanChange = true;
3980 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3981 if (Align > SrcAlign)
3982 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003983 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003984 bool CopyFromStr = isMemSrcFromString(Src, Str);
3985 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003986 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003987
Evan Cheng4c014c82010-04-01 18:19:11 +00003988 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003989 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003990 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003991 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003992 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003993
Evan Cheng43cd9e32010-04-01 06:04:33 +00003994 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003995 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003996 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003997
3998 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003999 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004000 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004001 if (!TRI->needsStackRealignment(MF))
4002 while (NewAlign > Align &&
4003 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4004 NewAlign /= 2;
4005
Evan Cheng43cd9e32010-04-01 06:04:33 +00004006 if (NewAlign > Align) {
4007 // Give the stack frame object a larger alignment if needed.
4008 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4009 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4010 Align = NewAlign;
4011 }
4012 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004013
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004014 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004015 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004016 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004017 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004018 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004019 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004020 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004021
Evan Cheng79e2ca92012-12-10 23:21:26 +00004022 if (VTSize > Size) {
4023 // Issuing an unaligned load / store pair that overlaps with the previous
4024 // pair. Adjust the offset accordingly.
4025 assert(i == NumMemOps-1 && i != 0);
4026 SrcOff -= VTSize - Size;
4027 DstOff -= VTSize - Size;
4028 }
4029
Evan Cheng43cd9e32010-04-01 06:04:33 +00004030 if (CopyFromStr &&
4031 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004032 // It's unlikely a store of a vector immediate can be done in a single
4033 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004034 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004035 // FIXME: Handle other cases where store of vector immediate is done in
4036 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004037 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004038 if (Value.getNode())
4039 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004040 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004041 DstPtrInfo.getWithOffset(DstOff), isVol,
4042 false, Align);
4043 }
4044
4045 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004046 // The type might not be legal for the target. This should only happen
4047 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004048 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4049 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004050 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004051 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004052 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004053 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004054 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004055 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004056 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004057 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004058 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004059 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4060 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004061 }
4062 OutChains.push_back(Store);
4063 SrcOff += VTSize;
4064 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004065 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004066 }
4067
Craig Topper48d114b2014-04-26 18:35:24 +00004068 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004069}
4070
Andrew Trickef9de2a2013-05-25 02:42:55 +00004071static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004072 SDValue Chain, SDValue Dst,
4073 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004074 unsigned Align, bool isVol,
4075 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004076 MachinePointerInfo DstPtrInfo,
4077 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004078 // Turn a memmove of undef to nop.
4079 if (Src.getOpcode() == ISD::UNDEF)
4080 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004081
4082 // Expand memmove to a series of load and store ops if the size operand falls
4083 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004084 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004085 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004086 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004087 MachineFunction &MF = DAG.getMachineFunction();
4088 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004089 bool OptSize = MF.getFunction()->getAttributes().
4090 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004091 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4092 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4093 DstAlignCanChange = true;
4094 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4095 if (Align > SrcAlign)
4096 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004097 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004098
Evan Cheng4c014c82010-04-01 18:19:11 +00004099 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004100 (DstAlignCanChange ? 0 : Align), SrcAlign,
4101 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004102 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004103
Evan Cheng43cd9e32010-04-01 06:04:33 +00004104 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004105 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004106 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004107 if (NewAlign > Align) {
4108 // Give the stack frame object a larger alignment if needed.
4109 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4110 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4111 Align = NewAlign;
4112 }
4113 }
Dan Gohman714663a2008-05-29 19:42:22 +00004114
Evan Cheng43cd9e32010-04-01 06:04:33 +00004115 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004116 SmallVector<SDValue, 8> LoadValues;
4117 SmallVector<SDValue, 8> LoadChains;
4118 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004119 unsigned NumMemOps = MemOps.size();
4120 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004121 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004122 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004123 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004124
Dale Johannesenabf66b82009-02-03 22:26:09 +00004125 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004126 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004127 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004128 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004129 LoadValues.push_back(Value);
4130 LoadChains.push_back(Value.getValue(1));
4131 SrcOff += VTSize;
4132 }
Craig Topper48d114b2014-04-26 18:35:24 +00004133 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004134 OutChains.clear();
4135 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004136 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004137 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004138 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004139
Dale Johannesenabf66b82009-02-03 22:26:09 +00004140 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004141 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004142 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004143 OutChains.push_back(Store);
4144 DstOff += VTSize;
4145 }
4146
Craig Topper48d114b2014-04-26 18:35:24 +00004147 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004148}
4149
Serge Pavlov8ec39992013-09-17 16:24:42 +00004150/// \brief Lower the call to 'memset' intrinsic function into a series of store
4151/// operations.
4152///
4153/// \param DAG Selection DAG where lowered code is placed.
4154/// \param dl Link to corresponding IR location.
4155/// \param Chain Control flow dependency.
4156/// \param Dst Pointer to destination memory location.
4157/// \param Src Value of byte to write into the memory.
4158/// \param Size Number of bytes to write.
4159/// \param Align Alignment of the destination in bytes.
4160/// \param isVol True if destination is volatile.
4161/// \param DstPtrInfo IR information on the memory pointer.
4162/// \returns New head in the control flow, if lowering was successful, empty
4163/// SDValue otherwise.
4164///
4165/// The function tries to replace 'llvm.memset' intrinsic with several store
4166/// operations and value calculation code. This is usually profitable for small
4167/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004168static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004169 SDValue Chain, SDValue Dst,
4170 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004171 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004172 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004173 // Turn a memset of undef to nop.
4174 if (Src.getOpcode() == ISD::UNDEF)
4175 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004176
4177 // Expand memset to a series of load/store ops if the size operand
4178 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004179 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004180 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004181 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004182 MachineFunction &MF = DAG.getMachineFunction();
4183 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004184 bool OptSize = MF.getFunction()->getAttributes().
4185 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004186 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4187 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4188 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004189 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004190 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004191 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004192 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004193 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004194 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004195
Evan Cheng43cd9e32010-04-01 06:04:33 +00004196 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004197 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004198 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004199 if (NewAlign > Align) {
4200 // Give the stack frame object a larger alignment if needed.
4201 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4202 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4203 Align = NewAlign;
4204 }
4205 }
4206
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004207 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004208 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004209 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004210
4211 // Find the largest store and generate the bit pattern for it.
4212 EVT LargestVT = MemOps[0];
4213 for (unsigned i = 1; i < NumMemOps; i++)
4214 if (MemOps[i].bitsGT(LargestVT))
4215 LargestVT = MemOps[i];
4216 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4217
Dan Gohman544ab2c2008-04-12 04:36:06 +00004218 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004219 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004220 unsigned VTSize = VT.getSizeInBits() / 8;
4221 if (VTSize > Size) {
4222 // Issuing an unaligned load / store pair that overlaps with the previous
4223 // pair. Adjust the offset accordingly.
4224 assert(i == NumMemOps-1 && i != 0);
4225 DstOff -= VTSize - Size;
4226 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004227
4228 // If this store is smaller than the largest store see whether we can get
4229 // the smaller value for free with a truncate.
4230 SDValue Value = MemSetValue;
4231 if (VT.bitsLT(LargestVT)) {
4232 if (!LargestVT.isVector() && !VT.isVector() &&
4233 TLI.isTruncateFree(LargestVT, VT))
4234 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4235 else
4236 Value = getMemsetValue(Src, VT, DAG, dl);
4237 }
4238 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004239 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004240 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004241 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004242 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004243 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004244 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004245 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004246 }
4247
Craig Topper48d114b2014-04-26 18:35:24 +00004248 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004249}
4250
Andrew Trickef9de2a2013-05-25 02:42:55 +00004251SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004252 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004253 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004254 MachinePointerInfo DstPtrInfo,
4255 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004256 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004257
4258 // Check to see if we should lower the memcpy to loads and stores first.
4259 // For cases within the target-specified limits, this is the best choice.
4260 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4261 if (ConstantSize) {
4262 // Memcpy with size zero? Just return the original chain.
4263 if (ConstantSize->isNullValue())
4264 return Chain;
4265
Evan Cheng43cd9e32010-04-01 06:04:33 +00004266 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4267 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004268 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004269 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004270 return Result;
4271 }
4272
4273 // Then check to see if we should lower the memcpy with target-specific
4274 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004275 SDValue Result =
Alexey Samsonove229ec52014-08-20 21:40:15 +00004276 TSI->EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
4277 isVol, AlwaysInline, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004278 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004279 return Result;
4280
4281 // If we really need inline code and the target declined to provide it,
4282 // use a (potentially long) sequence of loads and stores.
4283 if (AlwaysInline) {
4284 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004285 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004286 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004287 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004288 }
4289
Dan Gohmanf38547c2010-04-05 20:24:08 +00004290 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4291 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4292 // respect volatile, so they may do things like read or write memory
4293 // beyond the given memory regions. But fixing this isn't easy, and most
4294 // people don't care.
4295
Dan Gohman544ab2c2008-04-12 04:36:06 +00004296 // Emit a library call.
4297 TargetLowering::ArgListTy Args;
4298 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004299 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004300 Entry.Node = Dst; Args.push_back(Entry);
4301 Entry.Node = Src; Args.push_back(Entry);
4302 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004303 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004304 TargetLowering::CallLoweringInfo CLI(*this);
4305 CLI.setDebugLoc(dl).setChain(Chain)
4306 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4307 Type::getVoidTy(*getContext()),
4308 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004309 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004310 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004311 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004312
Dan Gohman544ab2c2008-04-12 04:36:06 +00004313 return CallResult.second;
4314}
4315
Andrew Trickef9de2a2013-05-25 02:42:55 +00004316SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004317 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004318 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004319 MachinePointerInfo DstPtrInfo,
4320 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004321 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004322
Dan Gohman714663a2008-05-29 19:42:22 +00004323 // Check to see if we should lower the memmove to loads and stores first.
4324 // For cases within the target-specified limits, this is the best choice.
4325 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4326 if (ConstantSize) {
4327 // Memmove with size zero? Just return the original chain.
4328 if (ConstantSize->isNullValue())
4329 return Chain;
4330
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004331 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004332 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004333 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004334 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004335 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004336 return Result;
4337 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004338
4339 // Then check to see if we should lower the memmove with target-specific
4340 // code. If the target chooses to do this, this is the next best.
Alexey Samsonove229ec52014-08-20 21:40:15 +00004341 SDValue Result = TSI->EmitTargetCodeForMemmove(
4342 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004343 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004344 return Result;
4345
Mon P Wangbf862242010-04-06 08:27:51 +00004346 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4347 // not be safe. See memcpy above for more details.
4348
Dan Gohman544ab2c2008-04-12 04:36:06 +00004349 // Emit a library call.
4350 TargetLowering::ArgListTy Args;
4351 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004352 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004353 Entry.Node = Dst; Args.push_back(Entry);
4354 Entry.Node = Src; Args.push_back(Entry);
4355 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004356 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004357 TargetLowering::CallLoweringInfo CLI(*this);
4358 CLI.setDebugLoc(dl).setChain(Chain)
4359 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4360 Type::getVoidTy(*getContext()),
4361 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004362 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004363 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004364 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004365
Dan Gohman544ab2c2008-04-12 04:36:06 +00004366 return CallResult.second;
4367}
4368
Andrew Trickef9de2a2013-05-25 02:42:55 +00004369SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004370 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004371 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004372 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004373 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004374
4375 // Check to see if we should lower the memset to stores first.
4376 // For cases within the target-specified limits, this is the best choice.
4377 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4378 if (ConstantSize) {
4379 // Memset with size zero? Just return the original chain.
4380 if (ConstantSize->isNullValue())
4381 return Chain;
4382
Mon P Wangc576ee92010-04-04 03:10:48 +00004383 SDValue Result =
4384 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004385 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004386
Gabor Greiff304a7a2008-08-28 21:40:38 +00004387 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004388 return Result;
4389 }
4390
4391 // Then check to see if we should lower the memset with target-specific
4392 // code. If the target chooses to do this, this is the next best.
Alexey Samsonove229ec52014-08-20 21:40:15 +00004393 SDValue Result = TSI->EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src,
4394 Size, Align, isVol, DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004395 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004396 return Result;
4397
Wesley Peck527da1b2010-11-23 03:31:01 +00004398 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004399 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004400 TargetLowering::ArgListTy Args;
4401 TargetLowering::ArgListEntry Entry;
4402 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4403 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004404 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004405 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004406 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004407 Entry.Node = Size;
4408 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004409 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004410
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004411 // FIXME: pass in SDLoc
4412 TargetLowering::CallLoweringInfo CLI(*this);
4413 CLI.setDebugLoc(dl).setChain(Chain)
4414 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4415 Type::getVoidTy(*getContext()),
4416 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004417 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004418 .setDiscardResult();
4419
4420 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004421 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004422}
4423
Andrew Trickef9de2a2013-05-25 02:42:55 +00004424SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004425 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004426 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004427 AtomicOrdering SuccessOrdering,
4428 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004429 SynchronizationScope SynchScope) {
4430 FoldingSetNodeID ID;
4431 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004432 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004433 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004434 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004435 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4436 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4437 return SDValue(E, 0);
4438 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004439
4440 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4441 // SDNode doesn't have access to it. This memory will be "leaked" when
4442 // the node is deallocated, but recovered when the allocator is released.
4443 // If the number of operands is less than 5 we use AtomicSDNode's internal
4444 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004445 unsigned NumOps = Ops.size();
4446 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4447 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004448
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004449 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4450 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004451 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004452 SuccessOrdering, FailureOrdering,
4453 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004454 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004455 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004456 return SDValue(N, 0);
4457}
4458
4459SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004460 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004461 MachineMemOperand *MMO,
4462 AtomicOrdering Ordering,
4463 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004464 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004465 Ordering, SynchScope);
4466}
4467
Tim Northover420a2162014-06-13 14:24:07 +00004468SDValue SelectionDAG::getAtomicCmpSwap(
4469 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4470 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4471 unsigned Alignment, AtomicOrdering SuccessOrdering,
4472 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4473 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4474 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4475 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4476
Dan Gohman48b185d2009-09-25 20:36:54 +00004477 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4478 Alignment = getEVTAlignment(MemVT);
4479
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004480 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004481
Eli Friedmane978d2f2011-09-07 02:23:42 +00004482 // FIXME: Volatile isn't really correct; we should keep track of atomic
4483 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004484 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004485 Flags |= MachineMemOperand::MOLoad;
4486 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004487
4488 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004489 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004490
Tim Northover420a2162014-06-13 14:24:07 +00004491 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4492 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004493}
4494
Tim Northover420a2162014-06-13 14:24:07 +00004495SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4496 SDVTList VTs, SDValue Chain, SDValue Ptr,
4497 SDValue Cmp, SDValue Swp,
4498 MachineMemOperand *MMO,
4499 AtomicOrdering SuccessOrdering,
4500 AtomicOrdering FailureOrdering,
4501 SynchronizationScope SynchScope) {
4502 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4503 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004504 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4505
Dale Johannesen839acbb2009-01-29 00:47:48 +00004506 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004507 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4508 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004509}
4510
Andrew Trickef9de2a2013-05-25 02:42:55 +00004511SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004512 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004513 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004514 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004515 unsigned Alignment,
4516 AtomicOrdering Ordering,
4517 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004518 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4519 Alignment = getEVTAlignment(MemVT);
4520
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004521 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004522 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004523 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004524 // For now, atomics are considered to be volatile always, and they are
4525 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004526 // FIXME: Volatile isn't really correct; we should keep track of atomic
4527 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004528 unsigned Flags = MachineMemOperand::MOVolatile;
4529 if (Opcode != ISD::ATOMIC_STORE)
4530 Flags |= MachineMemOperand::MOLoad;
4531 if (Opcode != ISD::ATOMIC_LOAD)
4532 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004533
4534 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004535 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004536 MemVT.getStoreSize(), Alignment);
4537
Eli Friedmanadec5872011-07-29 03:05:32 +00004538 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4539 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004540}
4541
Andrew Trickef9de2a2013-05-25 02:42:55 +00004542SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004543 SDValue Chain,
4544 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004545 MachineMemOperand *MMO,
4546 AtomicOrdering Ordering,
4547 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004548 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4549 Opcode == ISD::ATOMIC_LOAD_SUB ||
4550 Opcode == ISD::ATOMIC_LOAD_AND ||
4551 Opcode == ISD::ATOMIC_LOAD_OR ||
4552 Opcode == ISD::ATOMIC_LOAD_XOR ||
4553 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004554 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004555 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004556 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004557 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004558 Opcode == ISD::ATOMIC_SWAP ||
4559 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004560 "Invalid Atomic Op");
4561
Owen Anderson53aa7a92009-08-10 22:56:29 +00004562 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004563
Eli Friedman342e8df2011-08-24 20:50:09 +00004564 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4565 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004566 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004567 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004568}
4569
Andrew Trickef9de2a2013-05-25 02:42:55 +00004570SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004571 EVT VT, SDValue Chain,
4572 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004573 MachineMemOperand *MMO,
4574 AtomicOrdering Ordering,
4575 SynchronizationScope SynchScope) {
4576 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4577
4578 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004579 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004580 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004581}
4582
Duncan Sands739a0542008-07-02 17:40:58 +00004583/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004584SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4585 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004586 return Ops[0];
4587
Owen Anderson53aa7a92009-08-10 22:56:29 +00004588 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004589 VTs.reserve(Ops.size());
4590 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004591 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004592 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004593}
4594
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004595SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004596SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004597 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004598 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004599 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004600 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004601 if (Align == 0) // Ensure that codegen never sees alignment 0
4602 Align = getEVTAlignment(MemVT);
4603
4604 MachineFunction &MF = getMachineFunction();
4605 unsigned Flags = 0;
4606 if (WriteMem)
4607 Flags |= MachineMemOperand::MOStore;
4608 if (ReadMem)
4609 Flags |= MachineMemOperand::MOLoad;
4610 if (Vol)
4611 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004612 if (!Size)
4613 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004614 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004615 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004616
Craig Topper206fcd42014-04-26 19:29:41 +00004617 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004618}
4619
4620SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004621SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004622 ArrayRef<SDValue> Ops, EVT MemVT,
4623 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004624 assert((Opcode == ISD::INTRINSIC_VOID ||
4625 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004626 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004627 Opcode == ISD::LIFETIME_START ||
4628 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004629 (Opcode <= INT_MAX &&
4630 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4631 "Opcode is not a memory-accessing opcode!");
4632
Dale Johannesen839acbb2009-01-29 00:47:48 +00004633 // Memoize the node unless it returns a flag.
4634 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004635 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004636 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004637 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004638 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004639 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4641 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004642 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004643 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004644
Jack Carter170a5f22013-09-09 22:02:08 +00004645 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4646 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004647 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004648 CSEMap.InsertNode(N, IP);
4649 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004650 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4651 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004652 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004653 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004654 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004655 return SDValue(N, 0);
4656}
4657
Chris Lattnerea952f02010-09-21 17:24:05 +00004658/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4659/// MachinePointerInfo record from it. This is particularly useful because the
4660/// code generator has many cases where it doesn't bother passing in a
4661/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4662static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4663 // If this is FI+Offset, we can model it.
4664 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4665 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4666
4667 // If this is (FI+Offset1)+Offset2, we can model it.
4668 if (Ptr.getOpcode() != ISD::ADD ||
4669 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4670 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4671 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004672
Chris Lattnerea952f02010-09-21 17:24:05 +00004673 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4674 return MachinePointerInfo::getFixedStack(FI, Offset+
4675 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4676}
4677
4678/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4679/// MachinePointerInfo record from it. This is particularly useful because the
4680/// code generator has many cases where it doesn't bother passing in a
4681/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4682static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4683 // If the 'Offset' value isn't a constant, we can't handle this.
4684 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4685 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4686 if (OffsetOp.getOpcode() == ISD::UNDEF)
4687 return InferPointerInfo(Ptr);
4688 return MachinePointerInfo();
4689}
Wesley Peck527da1b2010-11-23 03:31:01 +00004690
Chris Lattnerea952f02010-09-21 17:24:05 +00004691
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004692SDValue
4693SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004694 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004695 SDValue Ptr, SDValue Offset,
4696 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004697 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004698 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004699 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004700 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004701 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004702 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4703 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004704
Dan Gohman48b185d2009-09-25 20:36:54 +00004705 unsigned Flags = MachineMemOperand::MOLoad;
4706 if (isVolatile)
4707 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004708 if (isNonTemporal)
4709 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004710 if (isInvariant)
4711 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004712
Chris Lattnerea952f02010-09-21 17:24:05 +00004713 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4714 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004715 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004716 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004717
Chris Lattnerea952f02010-09-21 17:24:05 +00004718 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004719 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004720 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004721 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004722 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004723}
4724
4725SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004726SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004727 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004728 SDValue Ptr, SDValue Offset, EVT MemVT,
4729 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004730 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004731 ExtType = ISD::NON_EXTLOAD;
4732 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004733 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004734 } else {
4735 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004736 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4737 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004738 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004739 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004740 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004741 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004742 assert((!VT.isVector() ||
4743 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004744 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004745 }
4746
4747 bool Indexed = AM != ISD::UNINDEXED;
4748 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4749 "Unindexed load with an offset!");
4750
4751 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004752 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004753 SDValue Ops[] = { Chain, Ptr, Offset };
4754 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004755 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004756 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004757 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004758 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004759 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004760 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004761 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004762 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4763 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004764 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004765 }
Jack Carter170a5f22013-09-09 22:02:08 +00004766 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4767 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004768 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004769 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004770 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004771 return SDValue(N, 0);
4772}
4773
Andrew Trickef9de2a2013-05-25 02:42:55 +00004774SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004775 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004776 MachinePointerInfo PtrInfo,
4777 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004778 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004779 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004780 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004781 SDValue Undef = getUNDEF(Ptr.getValueType());
4782 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004783 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004784 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004785}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004786
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004787SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4788 SDValue Chain, SDValue Ptr,
4789 MachineMemOperand *MMO) {
4790 SDValue Undef = getUNDEF(Ptr.getValueType());
4791 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4792 VT, MMO);
4793}
4794
Andrew Trickef9de2a2013-05-25 02:42:55 +00004795SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004796 SDValue Chain, SDValue Ptr,
4797 MachinePointerInfo PtrInfo, EVT MemVT,
4798 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004799 bool isInvariant, unsigned Alignment,
4800 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004801 SDValue Undef = getUNDEF(Ptr.getValueType());
4802 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004803 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4804 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004805}
4806
4807
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004808SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4809 SDValue Chain, SDValue Ptr, EVT MemVT,
4810 MachineMemOperand *MMO) {
4811 SDValue Undef = getUNDEF(Ptr.getValueType());
4812 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4813 MemVT, MMO);
4814}
4815
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004816SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004817SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004818 SDValue Offset, ISD::MemIndexedMode AM) {
4819 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4820 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4821 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004822 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004823 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004824 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004825 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004826}
4827
Andrew Trickef9de2a2013-05-25 02:42:55 +00004828SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004829 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004830 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004831 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004832 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004833 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004834 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004835 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004836
Dan Gohman48b185d2009-09-25 20:36:54 +00004837 unsigned Flags = MachineMemOperand::MOStore;
4838 if (isVolatile)
4839 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004840 if (isNonTemporal)
4841 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004842
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004843 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004844 PtrInfo = InferPointerInfo(Ptr);
4845
4846 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004847 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004848 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004849 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004850 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004851
4852 return getStore(Chain, dl, Val, Ptr, MMO);
4853}
4854
Andrew Trickef9de2a2013-05-25 02:42:55 +00004855SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004856 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004857 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004858 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004859 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004860 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004861 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004862 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4863 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004864 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004865 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004866 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004867 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004868 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004869 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004870 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4871 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004872 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004873 }
Jack Carter170a5f22013-09-09 22:02:08 +00004874 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4875 dl.getDebugLoc(), VTs,
4876 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004877 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004878 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004879 return SDValue(N, 0);
4880}
4881
Andrew Trickef9de2a2013-05-25 02:42:55 +00004882SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004883 SDValue Ptr, MachinePointerInfo PtrInfo,
4884 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004885 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004886 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004887 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004888 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004889 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4890 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004891
Dan Gohman48b185d2009-09-25 20:36:54 +00004892 unsigned Flags = MachineMemOperand::MOStore;
4893 if (isVolatile)
4894 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004895 if (isNonTemporal)
4896 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004897
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004898 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004899 PtrInfo = InferPointerInfo(Ptr);
4900
4901 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004902 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004903 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004904 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004905
4906 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4907}
4908
Andrew Trickef9de2a2013-05-25 02:42:55 +00004909SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004910 SDValue Ptr, EVT SVT,
4911 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004912 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004913
Michael Ilsemand5f91512012-09-10 16:56:31 +00004914 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004915 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004916 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004917 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004918
Dan Gohmancecad352009-12-14 23:40:38 +00004919 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4920 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004921 assert(VT.isInteger() == SVT.isInteger() &&
4922 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004923 assert(VT.isVector() == SVT.isVector() &&
4924 "Cannot use trunc store to convert to or from a vector!");
4925 assert((!VT.isVector() ||
4926 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4927 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004928
Owen Anderson9f944592009-08-11 20:47:22 +00004929 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004930 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004931 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4932 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004933 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004934 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004935 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004936 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004937 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004938 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004939 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4940 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004941 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004942 }
Jack Carter170a5f22013-09-09 22:02:08 +00004943 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4944 dl.getDebugLoc(), VTs,
4945 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004946 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004947 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004948 return SDValue(N, 0);
4949}
4950
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004951SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004952SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004953 SDValue Offset, ISD::MemIndexedMode AM) {
4954 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4955 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4956 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004957 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004958 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4959 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004960 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004961 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004962 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004963 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004964 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004965 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004966 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004967
Jack Carter170a5f22013-09-09 22:02:08 +00004968 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4969 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004970 ST->isTruncatingStore(),
4971 ST->getMemoryVT(),
4972 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004973 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004974 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004975 return SDValue(N, 0);
4976}
4977
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00004978SDValue
4979SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00004980 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
4981 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00004982
4983 SDVTList VTs = getVTList(VT, MVT::Other);
4984 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
4985 FoldingSetNodeID ID;
4986 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
4987 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00004988 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00004989 MMO->isVolatile(),
4990 MMO->isNonTemporal(),
4991 MMO->isInvariant()));
4992 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4993 void *IP = nullptr;
4994 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4995 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
4996 return SDValue(E, 0);
4997 }
4998 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
4999 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005000 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005001 CSEMap.InsertNode(N, IP);
5002 InsertNode(N);
5003 return SDValue(N, 0);
5004}
5005
5006SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005007 SDValue Ptr, SDValue Mask, EVT MemVT,
5008 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005009 assert(Chain.getValueType() == MVT::Other &&
5010 "Invalid chain type");
5011 EVT VT = Val.getValueType();
5012 SDVTList VTs = getVTList(MVT::Other);
5013 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5014 FoldingSetNodeID ID;
5015 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5016 ID.AddInteger(VT.getRawBits());
5017 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5018 MMO->isNonTemporal(), MMO->isInvariant()));
5019 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5020 void *IP = nullptr;
5021 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5022 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5023 return SDValue(E, 0);
5024 }
5025 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5026 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005027 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005028 CSEMap.InsertNode(N, IP);
5029 InsertNode(N);
5030 return SDValue(N, 0);
5031}
5032
Andrew Trickef9de2a2013-05-25 02:42:55 +00005033SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005034 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005035 SDValue SV,
5036 unsigned Align) {
5037 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005038 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005039}
5040
Andrew Trickef9de2a2013-05-25 02:42:55 +00005041SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005042 ArrayRef<SDUse> Ops) {
5043 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005044 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005045 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005046 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5047 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005048 default: break;
5049 }
5050
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005051 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005052 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005053 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005054 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005055}
5056
Andrew Trickef9de2a2013-05-25 02:42:55 +00005057SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005058 ArrayRef<SDValue> Ops) {
5059 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005060 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005061 case 0: return getNode(Opcode, DL, VT);
5062 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5063 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5064 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005065 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005066 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005067
Chris Lattnerb0713c72005-04-09 03:27:28 +00005068 switch (Opcode) {
5069 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005070 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005071 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005072 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5073 "LHS and RHS of condition must have same type!");
5074 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5075 "True and False arms of SelectCC must have same type!");
5076 assert(Ops[2].getValueType() == VT &&
5077 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005078 break;
5079 }
5080 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005081 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005082 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5083 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005084 break;
5085 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005086 }
5087
Chris Lattner566307f2005-05-14 07:42:29 +00005088 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005089 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005090 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005091
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005092 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005093 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005094 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005095 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005096
Bill Wendling022d18f2009-12-18 23:32:53 +00005097 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005098 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005099
Jack Carter170a5f22013-09-09 22:02:08 +00005100 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005101 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005102 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005103 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005104 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005105 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005106 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005107
Chandler Carruth41b20e72014-07-22 04:07:55 +00005108 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005109 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005110}
5111
Andrew Trickef9de2a2013-05-25 02:42:55 +00005112SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005113 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5114 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005115}
5116
Andrew Trickef9de2a2013-05-25 02:42:55 +00005117SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005118 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005119 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005120 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005121
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005122#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005123 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005124 // FIXME: figure out how to safely handle things like
5125 // int foo(int x) { return 1 << (x & 255); }
5126 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005127 case ISD::SRA_PARTS:
5128 case ISD::SRL_PARTS:
5129 case ISD::SHL_PARTS:
5130 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005131 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005132 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005133 else if (N3.getOpcode() == ISD::AND)
5134 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5135 // If the and is only masking out bits that cannot effect the shift,
5136 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005137 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005138 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005139 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005140 }
5141 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005142 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005143#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005144
Chris Lattnerf9c19152005-08-25 19:12:10 +00005145 // Memoize the node unless it returns a flag.
5146 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005147 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005148 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005149 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005150 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005151 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005152 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005153 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005154
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005155 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005156 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5157 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005158 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005159 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5160 DL.getDebugLoc(), VTList, Ops[0],
5161 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005162 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005163 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5164 DL.getDebugLoc(), VTList, Ops[0],
5165 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005166 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005167 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005168 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005169 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005170 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005171 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005172 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005173 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5174 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005175 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005176 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5177 DL.getDebugLoc(), VTList, Ops[0],
5178 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005179 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005180 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5181 DL.getDebugLoc(), VTList, Ops[0],
5182 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005183 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005184 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005185 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005186 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005187 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005188 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005189 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005190}
5191
Andrew Trickef9de2a2013-05-25 02:42:55 +00005192SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005193 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005194}
5195
Andrew Trickef9de2a2013-05-25 02:42:55 +00005196SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005197 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005198 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005199 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005200}
5201
Andrew Trickef9de2a2013-05-25 02:42:55 +00005202SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005203 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005204 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005205 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005206}
5207
Andrew Trickef9de2a2013-05-25 02:42:55 +00005208SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005209 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005210 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005211 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005212}
5213
Andrew Trickef9de2a2013-05-25 02:42:55 +00005214SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005215 SDValue N1, SDValue N2, SDValue N3,
5216 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005217 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005218 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005219}
5220
Andrew Trickef9de2a2013-05-25 02:42:55 +00005221SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005222 SDValue N1, SDValue N2, SDValue N3,
5223 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005224 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005225 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005226}
5227
Owen Anderson53aa7a92009-08-10 22:56:29 +00005228SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005229 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005230}
5231
Owen Anderson53aa7a92009-08-10 22:56:29 +00005232SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005233 FoldingSetNodeID ID;
5234 ID.AddInteger(2U);
5235 ID.AddInteger(VT1.getRawBits());
5236 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005237
Craig Topperc0196b12014-04-14 00:51:57 +00005238 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005239 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005240 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005241 EVT *Array = Allocator.Allocate<EVT>(2);
5242 Array[0] = VT1;
5243 Array[1] = VT2;
5244 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5245 VTListMap.InsertNode(Result, IP);
5246 }
5247 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005248}
Dan Gohman17059682008-07-17 19:10:17 +00005249
Owen Anderson53aa7a92009-08-10 22:56:29 +00005250SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005251 FoldingSetNodeID ID;
5252 ID.AddInteger(3U);
5253 ID.AddInteger(VT1.getRawBits());
5254 ID.AddInteger(VT2.getRawBits());
5255 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005256
Craig Topperc0196b12014-04-14 00:51:57 +00005257 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005258 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005259 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005260 EVT *Array = Allocator.Allocate<EVT>(3);
5261 Array[0] = VT1;
5262 Array[1] = VT2;
5263 Array[2] = VT3;
5264 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5265 VTListMap.InsertNode(Result, IP);
5266 }
5267 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005268}
5269
Owen Anderson53aa7a92009-08-10 22:56:29 +00005270SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005271 FoldingSetNodeID ID;
5272 ID.AddInteger(4U);
5273 ID.AddInteger(VT1.getRawBits());
5274 ID.AddInteger(VT2.getRawBits());
5275 ID.AddInteger(VT3.getRawBits());
5276 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005277
Craig Topperc0196b12014-04-14 00:51:57 +00005278 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005279 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005280 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005281 EVT *Array = Allocator.Allocate<EVT>(4);
5282 Array[0] = VT1;
5283 Array[1] = VT2;
5284 Array[2] = VT3;
5285 Array[3] = VT4;
5286 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5287 VTListMap.InsertNode(Result, IP);
5288 }
5289 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005290}
5291
Craig Topperabb4ac72014-04-16 06:10:51 +00005292SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5293 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005294 FoldingSetNodeID ID;
5295 ID.AddInteger(NumVTs);
5296 for (unsigned index = 0; index < NumVTs; index++) {
5297 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005298 }
5299
Craig Topperc0196b12014-04-14 00:51:57 +00005300 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005301 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005302 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005303 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005304 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005305 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5306 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005307 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005308 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005309}
5310
5311
Chris Lattnerf34156e2006-01-28 09:32:45 +00005312/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5313/// specified operands. If the resultant node already exists in the DAG,
5314/// this does not modify the specified node, instead it returns the node that
5315/// already exists. If the resultant node does not exist in the DAG, the
5316/// input node is returned. As a degenerate case, if you specify the same
5317/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005318SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005319 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005320
Chris Lattnerf34156e2006-01-28 09:32:45 +00005321 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005322 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005323
Chris Lattnerf34156e2006-01-28 09:32:45 +00005324 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005325 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005326 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005327 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005328
Dan Gohmanebeccb42008-07-21 22:38:59 +00005329 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005330 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005331 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005332 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005333
Chris Lattnerf34156e2006-01-28 09:32:45 +00005334 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005335 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005336
Chris Lattnerf34156e2006-01-28 09:32:45 +00005337 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005338 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005339 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005340}
5341
Dan Gohman92c11ac2010-06-18 15:30:29 +00005342SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005343 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005344
Chris Lattnerf34156e2006-01-28 09:32:45 +00005345 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005346 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005347 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005348
Chris Lattnerf34156e2006-01-28 09:32:45 +00005349 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005350 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005351 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005352 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005353
Dan Gohmanebeccb42008-07-21 22:38:59 +00005354 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005355 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005356 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005357 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005358
Chris Lattnerf34156e2006-01-28 09:32:45 +00005359 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005360 if (N->OperandList[0] != Op1)
5361 N->OperandList[0].set(Op1);
5362 if (N->OperandList[1] != Op2)
5363 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005364
Chris Lattnerf34156e2006-01-28 09:32:45 +00005365 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005366 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005367 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005368}
5369
Dan Gohman92c11ac2010-06-18 15:30:29 +00005370SDNode *SelectionDAG::
5371UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005372 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005373 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005374}
5375
Dan Gohman92c11ac2010-06-18 15:30:29 +00005376SDNode *SelectionDAG::
5377UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005378 SDValue Op3, SDValue Op4) {
5379 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005380 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005381}
5382
Dan Gohman92c11ac2010-06-18 15:30:29 +00005383SDNode *SelectionDAG::
5384UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005385 SDValue Op3, SDValue Op4, SDValue Op5) {
5386 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005387 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005388}
5389
Dan Gohman92c11ac2010-06-18 15:30:29 +00005390SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005391UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5392 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005393 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005394 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005395
Chris Lattnerf34156e2006-01-28 09:32:45 +00005396 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005397 bool AnyChange = false;
5398 for (unsigned i = 0; i != NumOps; ++i) {
5399 if (Ops[i] != N->getOperand(i)) {
5400 AnyChange = true;
5401 break;
5402 }
5403 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005404
Chris Lattnerf34156e2006-01-28 09:32:45 +00005405 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005406 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005407
Chris Lattnerf34156e2006-01-28 09:32:45 +00005408 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005409 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005410 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005411 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005412
Dan Gohman2f83b472008-05-02 00:05:03 +00005413 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005414 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005415 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005416 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005417
Chris Lattnerf34156e2006-01-28 09:32:45 +00005418 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005419 for (unsigned i = 0; i != NumOps; ++i)
5420 if (N->OperandList[i] != Ops[i])
5421 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005422
5423 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005424 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005425 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005426}
5427
Dan Gohman91697632008-07-07 20:57:48 +00005428/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005429/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005430void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005431 // Unlike the code in MorphNodeTo that does this, we don't need to
5432 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005433 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5434 SDUse &Use = *I++;
5435 Use.set(SDValue());
5436 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005437}
Chris Lattner19732782005-08-16 18:17:10 +00005438
Dan Gohman17059682008-07-17 19:10:17 +00005439/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5440/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005441///
Dan Gohman17059682008-07-17 19:10:17 +00005442SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005443 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005444 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005445 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005446}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005447
Dan Gohman17059682008-07-17 19:10:17 +00005448SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005449 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005450 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005451 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005452 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005453}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005454
Dan Gohman17059682008-07-17 19:10:17 +00005455SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005456 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005457 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005458 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005459 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005460 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005461}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005462
Dan Gohman17059682008-07-17 19:10:17 +00005463SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005464 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005465 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005466 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005467 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005468 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005469}
Chris Lattner466fece2005-08-21 22:30:30 +00005470
Dan Gohman17059682008-07-17 19:10:17 +00005471SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005472 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005473 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005474 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005475}
5476
Dan Gohman17059682008-07-17 19:10:17 +00005477SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005478 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005479 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005480 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005481}
5482
Dan Gohman17059682008-07-17 19:10:17 +00005483SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005484 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005485 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005486 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005487}
5488
Dan Gohman17059682008-07-17 19:10:17 +00005489SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005490 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005491 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005492 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005493 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005494}
5495
Bill Wendling2d598632008-12-01 23:28:22 +00005496SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005497 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005498 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005499 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005500 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005501}
5502
Scott Michelcf0da6c2009-02-17 22:15:04 +00005503SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005504 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005505 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005506 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005507 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005508 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005509}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005510
Scott Michelcf0da6c2009-02-17 22:15:04 +00005511SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005512 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005513 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005514 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005515 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005516 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005517}
5518
Dan Gohman17059682008-07-17 19:10:17 +00005519SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005520 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005521 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005522 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005523 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005524 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005525 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005526}
5527
Dan Gohman17059682008-07-17 19:10:17 +00005528SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005529 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005530 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005531 SDValue Op3) {
5532 SDVTList VTs = getVTList(VT1, VT2, VT3);
5533 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005534 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005535}
5536
5537SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005538 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005539 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005540 // Reset the NodeID to -1.
5541 N->setNodeId(-1);
5542 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005543}
5544
Andrew Trickef9de2a2013-05-25 02:42:55 +00005545/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005546/// the line number information on the merged node since it is not possible to
5547/// preserve the information that operation is associated with multiple lines.
5548/// This will make the debugger working better at -O0, were there is a higher
5549/// probability having other instructions associated with that line.
5550///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005551/// For IROrder, we keep the smaller of the two
5552SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005553 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005554 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5555 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005556 N->setDebugLoc(DebugLoc());
5557 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005558 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5559 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005560 return N;
5561}
5562
Chris Lattneraf197502010-02-28 21:36:14 +00005563/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005564/// return type, opcode, and operands.
5565///
5566/// Note that MorphNodeTo returns the resultant node. If there is already a
5567/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005568/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005569///
5570/// Using MorphNodeTo is faster than creating a new node and swapping it in
5571/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005572/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005573/// the node's users.
5574///
Chandler Carruth356665a2014-08-01 22:09:43 +00005575/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5576/// As a consequence it isn't appropriate to use from within the DAG combiner or
5577/// the legalizer which maintain worklists that would need to be updated when
5578/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005579SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005580 SDVTList VTs, ArrayRef<SDValue> Ops) {
5581 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005582 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005583 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005584 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005585 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005586 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005587 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005588 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005589 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005590
Dan Gohmand3fe1742008-09-13 01:54:27 +00005591 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005592 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005593
Dan Gohman17059682008-07-17 19:10:17 +00005594 // Start the morphing.
5595 N->NodeType = Opc;
5596 N->ValueList = VTs.VTs;
5597 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005598
Dan Gohman17059682008-07-17 19:10:17 +00005599 // Clear the operands list, updating used nodes to remove this from their
5600 // use list. Keep track of any operands that become dead as a result.
5601 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005602 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5603 SDUse &Use = *I++;
5604 SDNode *Used = Use.getNode();
5605 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005606 if (Used->use_empty())
5607 DeadNodeSet.insert(Used);
5608 }
5609
Dan Gohman48b185d2009-09-25 20:36:54 +00005610 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5611 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005612 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005613 // If NumOps is larger than the # of operands we can have in a
5614 // MachineSDNode, reallocate the operand list.
5615 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5616 if (MN->OperandsNeedDelete)
5617 delete[] MN->OperandList;
5618 if (NumOps > array_lengthof(MN->LocalOperands))
5619 // We're creating a final node that will live unmorphed for the
5620 // remainder of the current SelectionDAG iteration, so we can allocate
5621 // the operands directly out of a pool with no recycling metadata.
5622 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005623 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005624 else
Craig Topper131de822014-04-27 19:21:16 +00005625 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005626 MN->OperandsNeedDelete = false;
5627 } else
Craig Topper131de822014-04-27 19:21:16 +00005628 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005629 } else {
5630 // If NumOps is larger than the # of operands we currently have, reallocate
5631 // the operand list.
5632 if (NumOps > N->NumOperands) {
5633 if (N->OperandsNeedDelete)
5634 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005635 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005636 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005637 } else
Craig Topper131de822014-04-27 19:21:16 +00005638 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005639 }
5640
5641 // Delete any nodes that are still dead after adding the uses for the
5642 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005643 if (!DeadNodeSet.empty()) {
5644 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005645 for (SDNode *N : DeadNodeSet)
5646 if (N->use_empty())
5647 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005648 RemoveDeadNodes(DeadNodes);
5649 }
Dan Gohman91697632008-07-07 20:57:48 +00005650
Dan Gohman17059682008-07-17 19:10:17 +00005651 if (IP)
5652 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005653 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005654}
5655
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005656
Dan Gohman32f71d72009-09-25 18:54:59 +00005657/// getMachineNode - These are used for target selectors to create a new node
5658/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005659///
Dan Gohman32f71d72009-09-25 18:54:59 +00005660/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005661/// node of the specified opcode and operands, it returns that node instead of
5662/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005663MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005664SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005665 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005666 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005667}
Bill Wendlinga434d932009-01-29 09:01:55 +00005668
Dan Gohmana22f2d82009-10-10 01:29:16 +00005669MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005670SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005671 SDVTList VTs = getVTList(VT);
5672 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005673 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005674}
Bill Wendlinga434d932009-01-29 09:01:55 +00005675
Dan Gohmana22f2d82009-10-10 01:29:16 +00005676MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005677SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005678 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005679 SDVTList VTs = getVTList(VT);
5680 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005681 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005682}
Bill Wendlinga434d932009-01-29 09:01:55 +00005683
Dan Gohmana22f2d82009-10-10 01:29:16 +00005684MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005685SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005686 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005687 SDVTList VTs = getVTList(VT);
5688 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005689 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005690}
Bill Wendlinga434d932009-01-29 09:01:55 +00005691
Dan Gohmana22f2d82009-10-10 01:29:16 +00005692MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005693SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005694 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005695 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005696 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005697}
Bill Wendlinga434d932009-01-29 09:01:55 +00005698
Dan Gohmana22f2d82009-10-10 01:29:16 +00005699MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005700SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005701 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005702 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005703}
Bill Wendlinga434d932009-01-29 09:01:55 +00005704
Dan Gohmana22f2d82009-10-10 01:29:16 +00005705MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005706SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005707 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005708 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005709 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005710 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005711}
Bill Wendlinga434d932009-01-29 09:01:55 +00005712
Dan Gohmana22f2d82009-10-10 01:29:16 +00005713MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005714SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005715 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005716 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005717 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005718 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005719}
5720
Dan Gohmana22f2d82009-10-10 01:29:16 +00005721MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005722SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005723 EVT VT1, EVT VT2, SDValue Op1,
5724 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005725 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005726 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005727 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005728}
5729
Dan Gohmana22f2d82009-10-10 01:29:16 +00005730MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005731SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005732 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005733 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005734 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005735 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005736}
Bill Wendlinga434d932009-01-29 09:01:55 +00005737
Dan Gohmana22f2d82009-10-10 01:29:16 +00005738MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005739SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005740 EVT VT1, EVT VT2, EVT VT3,
5741 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005742 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005743 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005744 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005745}
Bill Wendlinga434d932009-01-29 09:01:55 +00005746
Dan Gohmana22f2d82009-10-10 01:29:16 +00005747MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005748SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005749 EVT VT1, EVT VT2, EVT VT3,
5750 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005751 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005752 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005753 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005754}
Bill Wendlinga434d932009-01-29 09:01:55 +00005755
Dan Gohmana22f2d82009-10-10 01:29:16 +00005756MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005757SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005758 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005759 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005760 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005761 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005762}
Bill Wendlinga434d932009-01-29 09:01:55 +00005763
Dan Gohmana22f2d82009-10-10 01:29:16 +00005764MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005765SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005766 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005767 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005768 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005769 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005770}
5771
Dan Gohmana22f2d82009-10-10 01:29:16 +00005772MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005773SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005774 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005775 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005776 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005777 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005778}
5779
Dan Gohmana22f2d82009-10-10 01:29:16 +00005780MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005781SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005782 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005783 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005784 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005785 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005786 const SDValue *Ops = OpsArray.data();
5787 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005788
5789 if (DoCSE) {
5790 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005791 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005792 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005793 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005794 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005795 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005796 }
5797
5798 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005799 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5800 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005801
5802 // Initialize the operands list.
5803 if (NumOps > array_lengthof(N->LocalOperands))
5804 // We're creating a final node that will live unmorphed for the
5805 // remainder of the current SelectionDAG iteration, so we can allocate
5806 // the operands directly out of a pool with no recycling metadata.
5807 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5808 Ops, NumOps);
5809 else
5810 N->InitOperands(N->LocalOperands, Ops, NumOps);
5811 N->OperandsNeedDelete = false;
5812
5813 if (DoCSE)
5814 CSEMap.InsertNode(N, IP);
5815
Chandler Carruth41b20e72014-07-22 04:07:55 +00005816 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005817 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005818}
Evan Chengd3f1db92006-02-09 07:15:23 +00005819
Dan Gohmanac33a902009-08-19 18:16:17 +00005820/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005821/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005822SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005823SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005824 SDValue Operand) {
5825 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005826 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005827 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005828 return SDValue(Subreg, 0);
5829}
5830
Bob Wilson2a45a652009-10-08 18:49:46 +00005831/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005832/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005833SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005834SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005835 SDValue Operand, SDValue Subreg) {
5836 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005837 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005838 VT, Operand, Subreg, SRIdxVal);
5839 return SDValue(Result, 0);
5840}
5841
Evan Cheng31604a62008-03-22 01:55:50 +00005842/// getNodeIfExists - Get the specified node if it's already available, or
5843/// else return NULL.
5844SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005845 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5846 bool exact) {
5847 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005848 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005849 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005850 if (isBinOpWithFlags(Opcode))
5851 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005852 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005853 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005854 return E;
5855 }
Craig Topperc0196b12014-04-14 00:51:57 +00005856 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005857}
5858
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005859/// getDbgValue - Creates a SDDbgValue node.
5860///
Adrian Prantl32da8892014-04-25 20:49:25 +00005861/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005862SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5863 unsigned R, bool IsIndirect, uint64_t Off,
5864 DebugLoc DL, unsigned O) {
5865 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005866}
5867
Adrian Prantl32da8892014-04-25 20:49:25 +00005868/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005869SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5870 const Value *C, uint64_t Off,
5871 DebugLoc DL, unsigned O) {
5872 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005873}
5874
Adrian Prantl32da8892014-04-25 20:49:25 +00005875/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005876SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
5877 unsigned FI, uint64_t Off,
5878 DebugLoc DL, unsigned O) {
5879 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005880}
5881
Dan Gohman7d099f72010-03-03 21:33:37 +00005882namespace {
5883
Dan Gohman9cc886b2010-03-04 19:11:28 +00005884/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005885/// pointed to by a use iterator is deleted, increment the use iterator
5886/// so that it doesn't dangle.
5887///
Dan Gohman7d099f72010-03-03 21:33:37 +00005888class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005889 SDNode::use_iterator &UI;
5890 SDNode::use_iterator &UE;
5891
Craig Topper7b883b32014-03-08 06:31:39 +00005892 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005893 // Increment the iterator as needed.
5894 while (UI != UE && N == *UI)
5895 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005896 }
5897
5898public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005899 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005900 SDNode::use_iterator &ui,
5901 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005902 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005903};
5904
5905}
5906
Evan Cheng445b91a2006-08-07 22:13:29 +00005907/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005908/// This can cause recursive merging of nodes in the DAG.
5909///
Chris Lattner76858912008-02-03 03:35:22 +00005910/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005911///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005912void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005913 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005914 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005915 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005916 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005917
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005918 // Iterate over all the existing uses of From. New uses will be added
5919 // to the beginning of the use list, which we avoid visiting.
5920 // This specifically avoids visiting uses of From that arise while the
5921 // replacement is happening, because any such uses would be the result
5922 // of CSE: If an existing node looks like From after one of its operands
5923 // is replaced by To, we don't want to replace of all its users with To
5924 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005925 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005926 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005927 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005928 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005929
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005930 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005931 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005932
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005933 // A user can appear in a use list multiple times, and when this
5934 // happens the uses are usually next to each other in the list.
5935 // To help reduce the number of CSE recomputations, process all
5936 // the uses of this user that we can find this way.
5937 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005938 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005939 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005940 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005941 } while (UI != UE && *UI == User);
5942
5943 // Now that we have modified User, add it back to the CSE maps. If it
5944 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005945 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005946 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005947
5948 // If we just RAUW'd the root, take note.
5949 if (FromN == getRoot())
5950 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005951}
5952
5953/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5954/// This can cause recursive merging of nodes in the DAG.
5955///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005956/// This version assumes that for each value of From, there is a
5957/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005958///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005959void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005960#ifndef NDEBUG
5961 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5962 assert((!From->hasAnyUseOfValue(i) ||
5963 From->getValueType(i) == To->getValueType(i)) &&
5964 "Cannot use this version of ReplaceAllUsesWith!");
5965#endif
Dan Gohman17059682008-07-17 19:10:17 +00005966
5967 // Handle the trivial case.
5968 if (From == To)
5969 return;
5970
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005971 // Iterate over just the existing users of From. See the comments in
5972 // the ReplaceAllUsesWith above.
5973 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005974 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005975 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005976 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005977
Chris Lattner373f0482005-08-26 18:36:28 +00005978 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005979 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005980
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005981 // A user can appear in a use list multiple times, and when this
5982 // happens the uses are usually next to each other in the list.
5983 // To help reduce the number of CSE recomputations, process all
5984 // the uses of this user that we can find this way.
5985 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005986 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005987 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005988 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005989 } while (UI != UE && *UI == User);
5990
5991 // Now that we have modified User, add it back to the CSE maps. If it
5992 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005993 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005994 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005995
5996 // If we just RAUW'd the root, take note.
5997 if (From == getRoot().getNode())
5998 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005999}
6000
Chris Lattner373f0482005-08-26 18:36:28 +00006001/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6002/// This can cause recursive merging of nodes in the DAG.
6003///
6004/// This version can replace From with any result values. To must match the
6005/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006006void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006007 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006008 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006009
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006010 // Iterate over just the existing users of From. See the comments in
6011 // the ReplaceAllUsesWith above.
6012 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006013 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006014 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006015 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006016
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006017 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006018 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006019
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006020 // A user can appear in a use list multiple times, and when this
6021 // happens the uses are usually next to each other in the list.
6022 // To help reduce the number of CSE recomputations, process all
6023 // the uses of this user that we can find this way.
6024 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006025 SDUse &Use = UI.getUse();
6026 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006027 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006028 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006029 } while (UI != UE && *UI == User);
6030
6031 // Now that we have modified User, add it back to the CSE maps. If it
6032 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006033 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006034 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006035
6036 // If we just RAUW'd the root, take note.
6037 if (From == getRoot().getNode())
6038 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006039}
6040
Chris Lattner375e1a72006-02-17 21:58:01 +00006041/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006042/// uses of other values produced by From.getNode() alone. The Deleted
6043/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006044void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006045 // Handle the really simple, really trivial case efficiently.
6046 if (From == To) return;
6047
Chris Lattner375e1a72006-02-17 21:58:01 +00006048 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006049 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006050 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006051 return;
6052 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006053
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006054 // Iterate over just the existing users of From. See the comments in
6055 // the ReplaceAllUsesWith above.
6056 SDNode::use_iterator UI = From.getNode()->use_begin(),
6057 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006058 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006059 while (UI != UE) {
6060 SDNode *User = *UI;
6061 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006062
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006063 // A user can appear in a use list multiple times, and when this
6064 // happens the uses are usually next to each other in the list.
6065 // To help reduce the number of CSE recomputations, process all
6066 // the uses of this user that we can find this way.
6067 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006068 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006069
6070 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006071 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006072 ++UI;
6073 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006074 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006075
6076 // If this node hasn't been modified yet, it's still in the CSE maps,
6077 // so remove its old self from the CSE maps.
6078 if (!UserRemovedFromCSEMaps) {
6079 RemoveNodeFromCSEMaps(User);
6080 UserRemovedFromCSEMaps = true;
6081 }
6082
6083 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006084 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006085 } while (UI != UE && *UI == User);
6086
6087 // We are iterating over all uses of the From node, so if a use
6088 // doesn't use the specific value, no changes are made.
6089 if (!UserRemovedFromCSEMaps)
6090 continue;
6091
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006092 // Now that we have modified User, add it back to the CSE maps. If it
6093 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006094 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006095 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006096
6097 // If we just RAUW'd the root, take note.
6098 if (From == getRoot())
6099 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006100}
6101
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006102namespace {
6103 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6104 /// to record information about a use.
6105 struct UseMemo {
6106 SDNode *User;
6107 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006108 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006109 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006110
6111 /// operator< - Sort Memos by User.
6112 bool operator<(const UseMemo &L, const UseMemo &R) {
6113 return (intptr_t)L.User < (intptr_t)R.User;
6114 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006115}
6116
Dan Gohman17059682008-07-17 19:10:17 +00006117/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006118/// uses of other values produced by From.getNode() alone. The same value
6119/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006120/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006121void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6122 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006123 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006124 // Handle the simple, trivial case efficiently.
6125 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006126 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006127
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006128 // Read up all the uses and make records of them. This helps
6129 // processing new uses that are introduced during the
6130 // replacement process.
6131 SmallVector<UseMemo, 4> Uses;
6132 for (unsigned i = 0; i != Num; ++i) {
6133 unsigned FromResNo = From[i].getResNo();
6134 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006135 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006136 E = FromNode->use_end(); UI != E; ++UI) {
6137 SDUse &Use = UI.getUse();
6138 if (Use.getResNo() == FromResNo) {
6139 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006140 Uses.push_back(Memo);
6141 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006142 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006143 }
Dan Gohman17059682008-07-17 19:10:17 +00006144
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006145 // Sort the uses, so that all the uses from a given User are together.
6146 std::sort(Uses.begin(), Uses.end());
6147
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006148 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6149 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006150 // We know that this user uses some value of From. If it is the right
6151 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006152 SDNode *User = Uses[UseIndex].User;
6153
6154 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006155 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006156
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006157 // The Uses array is sorted, so all the uses for a given User
6158 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006159 // To help reduce the number of CSE recomputations, process all
6160 // the uses of this user that we can find this way.
6161 do {
6162 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006163 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006164 ++UseIndex;
6165
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006166 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006167 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6168
Dan Gohman17059682008-07-17 19:10:17 +00006169 // Now that we have modified User, add it back to the CSE maps. If it
6170 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006171 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006172 }
6173}
6174
Evan Cheng9631a602006-08-01 08:20:41 +00006175/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006176/// based on their topological order. It returns the maximum id and a vector
6177/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006178unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006179
Dan Gohman86aa16a2008-09-30 18:30:35 +00006180 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006181
Dan Gohman86aa16a2008-09-30 18:30:35 +00006182 // SortedPos tracks the progress of the algorithm. Nodes before it are
6183 // sorted, nodes after it are unsorted. When the algorithm completes
6184 // it is at the end of the list.
6185 allnodes_iterator SortedPos = allnodes_begin();
6186
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006187 // Visit all the nodes. Move nodes with no operands to the front of
6188 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006189 // operand count. Before we do this, the Node Id fields of the nodes
6190 // may contain arbitrary values. After, the Node Id fields for nodes
6191 // before SortedPos will contain the topological sort index, and the
6192 // Node Id fields for nodes At SortedPos and after will contain the
6193 // count of outstanding operands.
6194 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6195 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006196 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006197 unsigned Degree = N->getNumOperands();
6198 if (Degree == 0) {
6199 // A node with no uses, add it to the result array immediately.
6200 N->setNodeId(DAGSize++);
6201 allnodes_iterator Q = N;
6202 if (Q != SortedPos)
6203 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006204 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006205 ++SortedPos;
6206 } else {
6207 // Temporarily use the Node Id as scratch space for the degree count.
6208 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006209 }
6210 }
6211
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006212 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006213 // such that by the time the end is reached all nodes will be sorted.
6214 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6215 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006216 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006217 // N is in sorted position, so all its uses have one less operand
6218 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006219 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6220 UI != UE; ++UI) {
6221 SDNode *P = *UI;
6222 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006223 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006224 --Degree;
6225 if (Degree == 0) {
6226 // All of P's operands are sorted, so P may sorted now.
6227 P->setNodeId(DAGSize++);
6228 if (P != SortedPos)
6229 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006230 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006231 ++SortedPos;
6232 } else {
6233 // Update P's outstanding operand count.
6234 P->setNodeId(Degree);
6235 }
6236 }
David Greene3b2a68c2010-01-20 00:59:23 +00006237 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006238#ifndef NDEBUG
6239 SDNode *S = ++I;
6240 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006241 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006242 dbgs() << "Checking if this is due to cycles\n";
6243 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006244#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006245 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006246 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006247 }
6248
6249 assert(SortedPos == AllNodes.end() &&
6250 "Topological sort incomplete!");
6251 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6252 "First node in topological sort is not the entry token!");
6253 assert(AllNodes.front().getNodeId() == 0 &&
6254 "First node in topological sort has non-zero id!");
6255 assert(AllNodes.front().getNumOperands() == 0 &&
6256 "First node in topological sort has operands!");
6257 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6258 "Last node in topologic sort has unexpected id!");
6259 assert(AllNodes.back().use_empty() &&
6260 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006261 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006262 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006263}
6264
Evan Cheng563fe3c2010-03-25 01:38:16 +00006265/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6266/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006267void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006268 if (SD) {
6269 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006270 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006271 }
6272 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006273}
Evan Cheng29eefc12006-07-27 06:39:06 +00006274
Devang Patelefc6b162011-01-25 23:27:42 +00006275/// TransferDbgValues - Transfer SDDbgValues.
6276void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6277 if (From == To || !From.getNode()->getHasDebugValue())
6278 return;
6279 SDNode *FromNode = From.getNode();
6280 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006281 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006282 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006283 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006284 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006285 SDDbgValue *Dbg = *I;
6286 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006287 SDDbgValue *Clone =
6288 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6289 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6290 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006291 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006292 }
6293 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006294 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006295 E = ClonedDVs.end(); I != E; ++I)
6296 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006297}
6298
Jim Laskeyd66e6162005-08-17 20:08:02 +00006299//===----------------------------------------------------------------------===//
6300// SDNode Class
6301//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006302
Chris Lattner3bf17b62007-02-04 02:41:42 +00006303HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006304 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006305}
6306
Andrew Trickef9de2a2013-05-25 02:42:55 +00006307GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6308 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006309 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006310 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006311 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006312}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006313
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006314AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6315 SDValue X, unsigned SrcAS,
6316 unsigned DestAS)
6317 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6318 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6319
Andrew Trickef9de2a2013-05-25 02:42:55 +00006320MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6321 EVT memvt, MachineMemOperand *mmo)
6322 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006323 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006324 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006325 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006326 assert(isNonTemporal() == MMO->isNonTemporal() &&
6327 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006328 // We check here that the size of the memory operand fits within the size of
6329 // the MMO. This is because the MMO might indicate only a possible address
6330 // range instead of specifying the affected memory addresses precisely.
6331 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006332}
6333
Andrew Trickef9de2a2013-05-25 02:42:55 +00006334MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006335 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6336 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006337 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006338 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006339 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006340 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006341 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006342}
6343
Jim Laskeyf576b422006-10-27 23:46:08 +00006344/// Profile - Gather unique data for the node.
6345///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006346void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006347 AddNodeIDNode(ID, this);
6348}
6349
Owen Anderson3b1665e2009-08-25 22:27:22 +00006350namespace {
6351 struct EVTArray {
6352 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006353
Owen Anderson3b1665e2009-08-25 22:27:22 +00006354 EVTArray() {
6355 VTs.reserve(MVT::LAST_VALUETYPE);
6356 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6357 VTs.push_back(MVT((MVT::SimpleValueType)i));
6358 }
6359 };
6360}
6361
Owen Anderson53aa7a92009-08-10 22:56:29 +00006362static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006363static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006364static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006365
Chris Lattner88fa11c2005-11-08 23:30:28 +00006366/// getValueTypeList - Return a pointer to the specified value type.
6367///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006368const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006369 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006370 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006371 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006372 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006373 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006374 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006375 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006376 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006377}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006378
Chris Lattner40e79822005-01-12 18:37:47 +00006379/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6380/// indicated value. This method ignores uses of other values defined by this
6381/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006382bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006383 assert(Value < getNumValues() && "Bad value!");
6384
Roman Levenstein51f532f2008-04-07 10:06:32 +00006385 // TODO: Only iterate over uses of a given value of the node
6386 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006387 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006388 if (NUses == 0)
6389 return false;
6390 --NUses;
6391 }
Chris Lattner40e79822005-01-12 18:37:47 +00006392 }
6393
6394 // Found exactly the right number of uses?
6395 return NUses == 0;
6396}
6397
6398
Evan Cheng358c3d12007-08-02 05:29:38 +00006399/// hasAnyUseOfValue - Return true if there are any use of the indicated
6400/// value. This method ignores uses of other values defined by this operation.
6401bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6402 assert(Value < getNumValues() && "Bad value!");
6403
Dan Gohman7a510c22008-07-09 22:39:01 +00006404 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006405 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006406 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006407
6408 return false;
6409}
6410
6411
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006412/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006413///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006414bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006415 bool Seen = false;
6416 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006417 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006418 if (User == this)
6419 Seen = true;
6420 else
6421 return false;
6422 }
6423
6424 return Seen;
6425}
6426
Evan Cheng9456dd82006-11-03 07:31:32 +00006427/// isOperand - Return true if this node is an operand of N.
6428///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006429bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006430 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6431 if (*this == N->getOperand(i))
6432 return true;
6433 return false;
6434}
6435
Evan Cheng567d2e52008-03-04 00:41:45 +00006436bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006437 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006438 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006439 return true;
6440 return false;
6441}
Evan Chengd37645c2006-02-05 06:29:23 +00006442
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006443/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006444/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006445/// side-effecting instructions on any chain path. In practice, this looks
6446/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006447/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006448bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006449 unsigned Depth) const {
6450 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006451
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006452 // Don't search too deeply, we just want to be able to see through
6453 // TokenFactor's etc.
6454 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006455
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006456 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006457 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006458 if (getOpcode() == ISD::TokenFactor) {
6459 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006460 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6461 return false;
6462 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006463 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006464
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006465 // Loads don't have side effects, look through them.
6466 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6467 if (!Ld->isVolatile())
6468 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6469 }
6470 return false;
6471}
6472
Lang Hames5a004992011-07-07 04:31:51 +00006473/// hasPredecessor - Return true if N is a predecessor of this node.
6474/// N is either an operand of this node, or can be reached by recursively
6475/// traversing up the operands.
6476/// NOTE: This is an expensive method. Use it carefully.
6477bool SDNode::hasPredecessor(const SDNode *N) const {
6478 SmallPtrSet<const SDNode *, 32> Visited;
6479 SmallVector<const SDNode *, 16> Worklist;
6480 return hasPredecessorHelper(N, Visited, Worklist);
6481}
Dan Gohmancd139c02009-10-28 03:44:30 +00006482
Craig Topperb94011f2013-07-14 04:42:23 +00006483bool
6484SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006485 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006486 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006487 if (Visited.empty()) {
6488 Worklist.push_back(this);
6489 } else {
6490 // Take a look in the visited set. If we've already encountered this node
6491 // we needn't search further.
6492 if (Visited.count(N))
6493 return true;
6494 }
6495
6496 // Haven't visited N yet. Continue the search.
6497 while (!Worklist.empty()) {
6498 const SDNode *M = Worklist.pop_back_val();
6499 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6500 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006501 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006502 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006503 if (Op == N)
6504 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006505 }
Lang Hames5a004992011-07-07 04:31:51 +00006506 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006507
6508 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006509}
6510
Evan Cheng5d9fd972006-10-04 00:56:09 +00006511uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6512 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006513 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006514}
6515
Mon P Wang32f8bb92009-11-30 02:42:02 +00006516SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6517 assert(N->getNumValues() == 1 &&
6518 "Can't unroll a vector with multiple results!");
6519
6520 EVT VT = N->getValueType(0);
6521 unsigned NE = VT.getVectorNumElements();
6522 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006523 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006524
6525 SmallVector<SDValue, 8> Scalars;
6526 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6527
6528 // If ResNE is 0, fully unroll the vector op.
6529 if (ResNE == 0)
6530 ResNE = NE;
6531 else if (NE > ResNE)
6532 NE = ResNE;
6533
6534 unsigned i;
6535 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006536 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006537 SDValue Operand = N->getOperand(j);
6538 EVT OperandVT = Operand.getValueType();
6539 if (OperandVT.isVector()) {
6540 // A vector operand; extract a single element.
6541 EVT OperandEltVT = OperandVT.getVectorElementType();
6542 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6543 OperandEltVT,
6544 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006545 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006546 } else {
6547 // A scalar operand; just use it as is.
6548 Operands[j] = Operand;
6549 }
6550 }
6551
6552 switch (N->getOpcode()) {
6553 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006554 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006555 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006556 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006557 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006558 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006559 case ISD::SHL:
6560 case ISD::SRA:
6561 case ISD::SRL:
6562 case ISD::ROTL:
6563 case ISD::ROTR:
6564 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006565 getShiftAmountOperand(Operands[0].getValueType(),
6566 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006567 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006568 case ISD::SIGN_EXTEND_INREG:
6569 case ISD::FP_ROUND_INREG: {
6570 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6571 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6572 Operands[0],
6573 getValueType(ExtVT)));
6574 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006575 }
6576 }
6577
6578 for (; i < ResNE; ++i)
6579 Scalars.push_back(getUNDEF(EltVT));
6580
6581 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006582 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006583}
6584
Evan Chengf5938d52009-12-09 01:36:00 +00006585
Wesley Peck527da1b2010-11-23 03:31:01 +00006586/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6587/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006588/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006589bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006590 unsigned Bytes, int Dist) const {
6591 if (LD->getChain() != Base->getChain())
6592 return false;
6593 EVT VT = LD->getValueType(0);
6594 if (VT.getSizeInBits() / 8 != Bytes)
6595 return false;
6596
6597 SDValue Loc = LD->getOperand(1);
6598 SDValue BaseLoc = Base->getOperand(1);
6599 if (Loc.getOpcode() == ISD::FrameIndex) {
6600 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6601 return false;
6602 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6603 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6604 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6605 int FS = MFI->getObjectSize(FI);
6606 int BFS = MFI->getObjectSize(BFI);
6607 if (FS != BFS || FS != (int)Bytes) return false;
6608 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6609 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006610
Sanjay Patel7129c102014-12-16 21:57:18 +00006611 // Handle X + C.
6612 if (isBaseWithConstantOffset(Loc)) {
6613 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6614 if (Loc.getOperand(0) == BaseLoc) {
6615 // If the base location is a simple address with no offset itself, then
6616 // the second load's first add operand should be the base address.
6617 if (LocOffset == Dist * (int)Bytes)
6618 return true;
6619 } else if (isBaseWithConstantOffset(BaseLoc)) {
6620 // The base location itself has an offset, so subtract that value from the
6621 // second load's offset before comparing to distance * size.
6622 int64_t BOffset =
6623 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6624 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6625 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6626 return true;
6627 }
6628 }
6629 }
Craig Topperc0196b12014-04-14 00:51:57 +00006630 const GlobalValue *GV1 = nullptr;
6631 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006632 int64_t Offset1 = 0;
6633 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006634 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6635 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006636 if (isGA1 && isGA2 && GV1 == GV2)
6637 return Offset1 == (Offset2 + Dist*Bytes);
6638 return false;
6639}
6640
6641
Evan Cheng34a23ea2009-12-09 01:04:59 +00006642/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6643/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006644unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006645 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006646 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006647 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006648 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006649 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006650 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +00006651 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6652 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006653 unsigned AlignBits = KnownZero.countTrailingOnes();
6654 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6655 if (Align)
6656 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006657 }
Evan Chengd938faf2009-12-09 01:53:58 +00006658
Evan Cheng34a23ea2009-12-09 01:04:59 +00006659 // If this is a direct reference to a stack slot, use information about the
6660 // stack slot's alignment.
6661 int FrameIdx = 1 << 31;
6662 int64_t FrameOffset = 0;
6663 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6664 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006665 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006666 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006667 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006668 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6669 FrameOffset = Ptr.getConstantOperandVal(1);
6670 }
6671
6672 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006673 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006674 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6675 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006676 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006677 }
6678
6679 return 0;
6680}
6681
Juergen Ributzkab3487102013-11-19 21:20:17 +00006682/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6683/// which is split (or expanded) into two not necessarily identical pieces.
6684std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6685 // Currently all types are split in half.
6686 EVT LoVT, HiVT;
6687 if (!VT.isVector()) {
6688 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6689 } else {
6690 unsigned NumElements = VT.getVectorNumElements();
6691 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6692 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6693 NumElements/2);
6694 }
6695 return std::make_pair(LoVT, HiVT);
6696}
6697
6698/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6699/// low/high part.
6700std::pair<SDValue, SDValue>
6701SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6702 const EVT &HiVT) {
6703 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6704 N.getValueType().getVectorNumElements() &&
6705 "More vector elements requested than available!");
6706 SDValue Lo, Hi;
6707 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6708 getConstant(0, TLI->getVectorIdxTy()));
6709 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6710 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6711 return std::make_pair(Lo, Hi);
6712}
6713
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006714void SelectionDAG::ExtractVectorElements(SDValue Op,
6715 SmallVectorImpl<SDValue> &Args,
6716 unsigned Start, unsigned Count) {
6717 EVT VT = Op.getValueType();
6718 if (Count == 0)
6719 Count = VT.getVectorNumElements();
6720
6721 EVT EltVT = VT.getVectorElementType();
6722 EVT IdxTy = TLI->getVectorIdxTy();
6723 SDLoc SL(Op);
6724 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6725 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6726 Op, getConstant(i, IdxTy)));
6727 }
6728}
6729
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006730// getAddressSpace - Return the address space this GlobalAddress belongs to.
6731unsigned GlobalAddressSDNode::getAddressSpace() const {
6732 return getGlobal()->getType()->getAddressSpace();
6733}
6734
6735
Chris Lattner229907c2011-07-18 04:54:35 +00006736Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006737 if (isMachineConstantPoolEntry())
6738 return Val.MachineCPVal->getType();
6739 return Val.ConstVal->getType();
6740}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006741
Bob Wilson85cefe82009-03-02 23:24:16 +00006742bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6743 APInt &SplatUndef,
6744 unsigned &SplatBitSize,
6745 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006746 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006747 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006748 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006749 assert(VT.isVector() && "Expected a vector type");
6750 unsigned sz = VT.getSizeInBits();
6751 if (MinSplatBits > sz)
6752 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006753
Bob Wilson85cefe82009-03-02 23:24:16 +00006754 SplatValue = APInt(sz, 0);
6755 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006756
Bob Wilson85cefe82009-03-02 23:24:16 +00006757 // Get the bits. Bits with undefined values (when the corresponding element
6758 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6759 // in SplatValue. If any of the values are not constant, give up and return
6760 // false.
6761 unsigned int nOps = getNumOperands();
6762 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6763 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006764
6765 for (unsigned j = 0; j < nOps; ++j) {
6766 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006767 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006768 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006769
Bob Wilson85cefe82009-03-02 23:24:16 +00006770 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006771 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006772 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006773 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006774 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006775 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006776 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006777 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006778 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006779 }
6780
Bob Wilson85cefe82009-03-02 23:24:16 +00006781 // The build_vector is all constants or undefs. Find the smallest element
6782 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006783
Bob Wilson85cefe82009-03-02 23:24:16 +00006784 HasAnyUndefs = (SplatUndef != 0);
6785 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006786
Bob Wilson85cefe82009-03-02 23:24:16 +00006787 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006788 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6789 APInt LowValue = SplatValue.trunc(HalfSize);
6790 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6791 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006792
Bob Wilson85cefe82009-03-02 23:24:16 +00006793 // If the two halves do not match (ignoring undef bits), stop here.
6794 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6795 MinSplatBits > HalfSize)
6796 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006797
Bob Wilson85cefe82009-03-02 23:24:16 +00006798 SplatValue = HighValue | LowValue;
6799 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006800
Bob Wilson85cefe82009-03-02 23:24:16 +00006801 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006802 }
6803
Bob Wilson85cefe82009-03-02 23:24:16 +00006804 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006805 return true;
6806}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006807
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006808SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6809 if (UndefElements) {
6810 UndefElements->clear();
6811 UndefElements->resize(getNumOperands());
6812 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006813 SDValue Splatted;
6814 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6815 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006816 if (Op.getOpcode() == ISD::UNDEF) {
6817 if (UndefElements)
6818 (*UndefElements)[i] = true;
6819 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006820 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006821 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006822 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006823 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006824 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006825
Chandler Carruthb844e722014-07-08 07:19:55 +00006826 if (!Splatted) {
6827 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6828 "Can only have a splat without a constant for all undefs.");
6829 return getOperand(0);
6830 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006831
Chandler Carruthb844e722014-07-08 07:19:55 +00006832 return Splatted;
6833}
6834
6835ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006836BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006837 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006838 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006839}
6840
6841ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006842BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006843 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006844 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006845}
6846
Juergen Ributzka73844052014-01-13 20:51:35 +00006847bool BuildVectorSDNode::isConstant() const {
6848 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6849 unsigned Opc = getOperand(i).getOpcode();
6850 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6851 return false;
6852 }
6853 return true;
6854}
6855
Owen Anderson53aa7a92009-08-10 22:56:29 +00006856bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006857 // Find the first non-undef value in the shuffle mask.
6858 unsigned i, e;
6859 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6860 /* search */;
6861
Nate Begeman39b59db2009-04-29 18:13:31 +00006862 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006863
Nate Begeman5f829d82009-04-29 05:20:52 +00006864 // Make sure all remaining elements are either undef or the same as the first
6865 // non-undef value.
6866 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006867 if (Mask[i] >= 0 && Mask[i] != Idx)
6868 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006869 return true;
6870}
David Greene09851602010-01-20 20:13:31 +00006871
Adam Nemetb4690e32014-05-31 16:23:20 +00006872#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00006873static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006874 SmallPtrSetImpl<const SDNode*> &Visited,
6875 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00006876 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006877 // If this node has already been checked, don't check it again.
6878 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006879 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006880
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006881 // If a node has already been visited on this depth-first walk, reject it as
6882 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00006883 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006884 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006885 dbgs() << "Offending node:\n";
6886 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006887 abort();
David Greene09851602010-01-20 20:13:31 +00006888 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006889
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006890 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00006891 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00006892
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006893 Checked.insert(N);
6894 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006895}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006896#endif
David Greene09851602010-01-20 20:13:31 +00006897
Adam Nemet7d394302014-05-31 16:23:17 +00006898void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00006899 const llvm::SelectionDAG *DAG,
6900 bool force) {
6901#ifndef NDEBUG
6902 bool check = force;
David Greene09851602010-01-20 20:13:31 +00006903#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00006904 check = true;
6905#endif // XDEBUG
6906 if (check) {
6907 assert(N && "Checking nonexistent SDNode");
6908 SmallPtrSet<const SDNode*, 32> visited;
6909 SmallPtrSet<const SDNode*, 32> checked;
6910 checkForCyclesHelper(N, visited, checked, DAG);
6911 }
6912#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00006913}
6914
Adam Nemetb4690e32014-05-31 16:23:20 +00006915void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6916 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00006917}