blob: 4a83a3f00b52c45fc465187882a19f92cd1d6a2a [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.
Wesley Peck527da1b2010-11-23 03:31:01 +000099 if (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.
Wesley Peck527da1b2010-11-23 03:31:01 +0000147 if (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 Arsenaultf9a995d2014-03-06 17:34:12 +0000237ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) {
238 switch (ExtType) {
239 case ISD::EXTLOAD:
240 return ISD::ANY_EXTEND;
241 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.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001516 bool Identity = 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;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001519 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001520 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001521 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001522
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001523 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001524 if (N2Undef) {
1525 SDValue V = N1;
1526
1527 // Look through any bitcasts. We check that these don't change the number
1528 // (and size) of elements and just changes their types.
1529 while (V.getOpcode() == ISD::BITCAST)
1530 V = V->getOperand(0);
1531
1532 // A splat should always show up as a build vector node.
1533 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001534 BitVector UndefElements;
1535 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001536 // If this is a splat of an undef, shuffling it is also undef.
1537 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1538 return getUNDEF(VT);
1539
1540 // We only have a splat which can skip shuffles if there is a splatted
1541 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001542 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001543 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1544 // number of elements match or the value splatted is a zero constant.
1545 if (V.getValueType().getVectorNumElements() ==
1546 VT.getVectorNumElements())
1547 return N1;
1548 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1549 if (C->isNullValue())
1550 return N1;
1551 }
1552 }
1553 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001554
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001555 FoldingSetNodeID ID;
1556 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001557 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001558 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001559 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001560
Craig Topperc0196b12014-04-14 00:51:57 +00001561 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001562 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001563 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001564
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001565 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1566 // SDNode doesn't have access to it. This memory will be "leaked" when
1567 // the node is deallocated, but recovered when the NodeAllocator is released.
1568 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1569 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001570
Dan Gohman01c65a22010-03-18 18:49:47 +00001571 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001572 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1573 dl.getDebugLoc(), N1, N2,
1574 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001575 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001576 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001577 return SDValue(N, 0);
1578}
1579
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001580SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1581 MVT VT = SV.getSimpleValueType(0);
1582 unsigned NumElems = VT.getVectorNumElements();
1583 SmallVector<int, 8> MaskVec;
1584
1585 for (unsigned i = 0; i != NumElems; ++i) {
1586 int Idx = SV.getMaskElt(i);
1587 if (Idx >= 0) {
1588 if (Idx < (int)NumElems)
1589 Idx += NumElems;
1590 else
1591 Idx -= NumElems;
1592 }
1593 MaskVec.push_back(Idx);
1594 }
1595
1596 SDValue Op0 = SV.getOperand(0);
1597 SDValue Op1 = SV.getOperand(1);
1598 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1599}
1600
Andrew Trickef9de2a2013-05-25 02:42:55 +00001601SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001602 SDValue Val, SDValue DTy,
1603 SDValue STy, SDValue Rnd, SDValue Sat,
1604 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001605 // If the src and dest types are the same and the conversion is between
1606 // integer types of the same sign or two floats, no conversion is necessary.
1607 if (DTy == STy &&
1608 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001609 return Val;
1610
1611 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001612 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001613 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001614 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001616 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001617
Jack Carter170a5f22013-09-09 22:02:08 +00001618 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1619 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001620 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001621 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001622 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001623 return SDValue(N, 0);
1624}
1625
Owen Anderson53aa7a92009-08-10 22:56:29 +00001626SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001627 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001628 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001629 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001630 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001631 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001632 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001633
Dan Gohman01c65a22010-03-18 18:49:47 +00001634 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001635 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001636 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001637 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001638}
1639
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001640SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1641 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001642 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001643 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001644 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001645 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1646 return SDValue(E, 0);
1647
1648 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1649 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001650 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001651 return SDValue(N, 0);
1652}
1653
Andrew Trickef9de2a2013-05-25 02:42:55 +00001654SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001655 FoldingSetNodeID ID;
1656 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001657 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001658 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001659 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001661 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001662
Jack Carter170a5f22013-09-09 22:02:08 +00001663 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1664 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001665 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001666 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001667 return SDValue(N, 0);
1668}
1669
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001670
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001671SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001672 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001673 bool isTarget,
1674 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001675 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1676
1677 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001678 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001679 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001680 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001681 ID.AddInteger(TargetFlags);
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))
Dan Gohman6c938802009-10-30 01:27:03 +00001684 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001685
Michael Liaoabb87d42012-09-12 21:43:09 +00001686 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1687 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001688 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001689 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001690 return SDValue(N, 0);
1691}
1692
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001693SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001694 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001695 "SrcValue is not a pointer?");
1696
Jim Laskeyf576b422006-10-27 23:46:08 +00001697 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001698 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001699 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001700
Craig Topperc0196b12014-04-14 00:51:57 +00001701 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001702 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001703 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001704
Dan Gohman01c65a22010-03-18 18:49:47 +00001705 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001706 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001707 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001708 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001709}
1710
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001711/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1712SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1713 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001714 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001715 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001716
Craig Topperc0196b12014-04-14 00:51:57 +00001717 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001718 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1719 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001720
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001721 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1722 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001723 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001724 return SDValue(N, 0);
1725}
1726
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001727/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1728SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1729 unsigned SrcAS, unsigned DestAS) {
1730 SDValue Ops[] = {Ptr};
1731 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001732 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001733 ID.AddInteger(SrcAS);
1734 ID.AddInteger(DestAS);
1735
Craig Topperc0196b12014-04-14 00:51:57 +00001736 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001737 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1738 return SDValue(E, 0);
1739
1740 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1741 dl.getDebugLoc(),
1742 VT, Ptr, SrcAS, DestAS);
1743 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001744 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001745 return SDValue(N, 0);
1746}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001747
Duncan Sands41826032009-01-31 15:50:11 +00001748/// getShiftAmountOperand - Return the specified value casted to
1749/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001750SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001751 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001752 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001753 if (OpTy == ShTy || OpTy.isVector()) return Op;
1754
1755 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001756 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001757}
1758
Chris Lattner9eb7a822007-10-15 17:47:20 +00001759/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1760/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001761SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001762 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001763 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001764 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001765 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001766 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001767
David Greene1fbe0542009-11-12 20:49:22 +00001768 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001769 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001770}
1771
Duncan Sands445071c2008-12-09 21:33:20 +00001772/// CreateStackTemporary - Create a stack temporary suitable for holding
1773/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001774SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001775 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1776 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001777 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1778 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001779 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001780 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1781 TD->getPrefTypeAlignment(Ty2));
1782
1783 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001784 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001785 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001786}
1787
Owen Anderson53aa7a92009-08-10 22:56:29 +00001788SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001789 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001790 // These setcc operations always fold.
1791 switch (Cond) {
1792 default: break;
1793 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001794 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001795 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001796 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001797 TargetLowering::BooleanContent Cnt =
1798 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001799 return getConstant(
1800 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1801 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001802
Chris Lattner393d96a2006-04-27 05:01:07 +00001803 case ISD::SETOEQ:
1804 case ISD::SETOGT:
1805 case ISD::SETOGE:
1806 case ISD::SETOLT:
1807 case ISD::SETOLE:
1808 case ISD::SETONE:
1809 case ISD::SETO:
1810 case ISD::SETUO:
1811 case ISD::SETUEQ:
1812 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001813 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001814 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001815 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001816
Gabor Greiff304a7a2008-08-28 21:40:38 +00001817 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001818 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001819 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001820 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001821
Chris Lattner061a1ea2005-01-07 07:46:32 +00001822 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001823 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001824 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1825 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001826 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1827 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1828 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1829 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1830 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1831 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1832 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1833 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001834 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001835 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001836 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001837 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1838 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001839 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001840 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001841 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001842 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001843 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001844 // fall through
1845 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001846 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001847 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001848 // fall through
1849 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001850 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001851 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001852 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001853 // fall through
1854 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001855 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001856 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001857 // fall through
1858 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001859 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001860 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001861 // fall through
1862 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001863 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001864 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001865 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001866 // fall through
1867 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001868 R==APFloat::cmpEqual, VT);
1869 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1870 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1871 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1872 R==APFloat::cmpEqual, VT);
1873 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1874 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1875 R==APFloat::cmpLessThan, VT);
1876 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1877 R==APFloat::cmpUnordered, VT);
1878 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1879 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001880 }
1881 } else {
1882 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001883 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1884 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001885 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001886 return SDValue();
1887
1888 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001889 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001890 }
1891
Chris Lattnerd47675e2005-08-09 20:20:18 +00001892 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001893 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001894}
1895
Dan Gohman1f372ed2008-02-25 21:11:39 +00001896/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1897/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001898bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001899 // This predicate is not safe for vector operations.
1900 if (Op.getValueType().isVector())
1901 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001902
Dan Gohman1d459e42009-12-11 21:31:27 +00001903 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001904 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1905}
1906
Dan Gohman309d3d52007-06-22 14:59:07 +00001907/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1908/// this predicate to simplify operations downstream. Mask is known to be zero
1909/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001910bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001911 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001912 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001913 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001914 return (KnownZero & Mask) == Mask;
1915}
1916
Jay Foada0653a32014-05-14 21:14:37 +00001917/// Determine which bits of Op are known to be either zero or one and return
1918/// them in the KnownZero/KnownOne bitsets.
1919void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1920 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001921 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001922
Dan Gohmanf990faf2008-02-13 00:35:47 +00001923 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001924 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001925 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001926
Dan Gohmanf990faf2008-02-13 00:35:47 +00001927 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001928
1929 switch (Op.getOpcode()) {
1930 case ISD::Constant:
1931 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001932 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1933 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001934 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001935 case ISD::AND:
1936 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001937 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1938 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001939
1940 // Output known-1 bits are only known if set in both the LHS & RHS.
1941 KnownOne &= KnownOne2;
1942 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1943 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001944 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001945 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001946 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1947 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001948
Dan Gohman309d3d52007-06-22 14:59:07 +00001949 // Output known-0 bits are only known if clear in both the LHS & RHS.
1950 KnownZero &= KnownZero2;
1951 // Output known-1 are known to be set if set in either the LHS | RHS.
1952 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001953 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001954 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001955 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1956 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001957
Dan Gohman309d3d52007-06-22 14:59:07 +00001958 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001959 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001960 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1961 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1962 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001963 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001964 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001965 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00001966 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1967 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001968
1969 // If low bits are zero in either operand, output low known-0 bits.
1970 // Also compute a conserative estimate for high known-0 bits.
1971 // More trickiness is possible, but this is sufficient for the
1972 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001973 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001974 unsigned TrailZ = KnownZero.countTrailingOnes() +
1975 KnownZero2.countTrailingOnes();
1976 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001977 KnownZero2.countLeadingOnes(),
1978 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001979
1980 TrailZ = std::min(TrailZ, BitWidth);
1981 LeadZ = std::min(LeadZ, BitWidth);
1982 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1983 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001984 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001985 }
1986 case ISD::UDIV: {
1987 // For the purposes of computing leading zeros we can conservatively
1988 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001989 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00001990 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001991 unsigned LeadZ = KnownZero2.countLeadingOnes();
1992
Jay Foad25a5e4c2010-12-01 08:53:58 +00001993 KnownOne2.clearAllBits();
1994 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00001995 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001996 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1997 if (RHSUnknownLeadingOnes != BitWidth)
1998 LeadZ = std::min(BitWidth,
1999 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002000
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002001 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002002 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002003 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002004 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002005 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2006 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002007
Dan Gohman309d3d52007-06-22 14:59:07 +00002008 // Only known if known in both the LHS and RHS.
2009 KnownOne &= KnownOne2;
2010 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002011 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002012 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002013 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2014 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002015
Dan Gohman309d3d52007-06-22 14:59:07 +00002016 // Only known if known in both the LHS and RHS.
2017 KnownOne &= KnownOne2;
2018 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002019 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002020 case ISD::SADDO:
2021 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002022 case ISD::SSUBO:
2023 case ISD::USUBO:
2024 case ISD::SMULO:
2025 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002026 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002027 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002028 // The boolean result conforms to getBooleanContents.
2029 // If we know the result of a setcc has the top bits zero, use this info.
2030 // We know that we have an integer-based boolean since these operations
2031 // are only available for integer.
2032 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2033 TargetLowering::ZeroOrOneBooleanContent &&
2034 BitWidth > 1)
2035 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2036 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002037 case ISD::SETCC:
2038 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002039 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2040 TargetLowering::ZeroOrOneBooleanContent &&
2041 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002042 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002043 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002044 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002045 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002046 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002047 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002048
2049 // If the shift count is an invalid immediate, don't do anything.
2050 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002051 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002052
Jay Foada0653a32014-05-14 21:14:37 +00002053 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002054 KnownZero <<= ShAmt;
2055 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002056 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002057 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002058 }
Jay Foad5a29c362014-05-15 12:12:55 +00002059 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002060 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002061 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002062 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002063 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002064
Dan Gohman9db0aa82008-02-26 18:50:50 +00002065 // If the shift count is an invalid immediate, don't do anything.
2066 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002067 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002068
Jay Foada0653a32014-05-14 21:14:37 +00002069 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002070 KnownZero = KnownZero.lshr(ShAmt);
2071 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002072
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002073 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002074 KnownZero |= HighBits; // High bits known zero.
2075 }
Jay Foad5a29c362014-05-15 12:12:55 +00002076 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002077 case ISD::SRA:
2078 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002079 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002080
Dan Gohman9db0aa82008-02-26 18:50:50 +00002081 // If the shift count is an invalid immediate, don't do anything.
2082 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002083 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002084
Dan Gohman309d3d52007-06-22 14:59:07 +00002085 // If any of the demanded bits are produced by the sign extension, we also
2086 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002087 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002088
Jay Foada0653a32014-05-14 21:14:37 +00002089 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002090 KnownZero = KnownZero.lshr(ShAmt);
2091 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002092
Dan Gohman309d3d52007-06-22 14:59:07 +00002093 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002094 APInt SignBit = APInt::getSignBit(BitWidth);
2095 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002096
Dan Gohmanb717fda2008-02-20 16:30:17 +00002097 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002098 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002099 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002100 KnownOne |= HighBits; // New bits are known one.
2101 }
2102 }
Jay Foad5a29c362014-05-15 12:12:55 +00002103 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002104 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002105 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002106 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002107
2108 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002109 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002110 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002111
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002112 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002113 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002114
Dan Gohman309d3d52007-06-22 14:59:07 +00002115 // If the sign extended bits are demanded, we know that the sign
2116 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002117 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002118 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002119 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002120
Jay Foada0653a32014-05-14 21:14:37 +00002121 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002122 KnownOne &= InputDemandedBits;
2123 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002124
Dan Gohman309d3d52007-06-22 14:59:07 +00002125 // If the sign bit of the input is known set or clear, then we know the
2126 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002127 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002128 KnownZero |= NewBits;
2129 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002130 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002131 KnownOne |= NewBits;
2132 KnownZero &= ~NewBits;
2133 } else { // Input sign bit unknown
2134 KnownZero &= ~NewBits;
2135 KnownOne &= ~NewBits;
2136 }
Jay Foad5a29c362014-05-15 12:12:55 +00002137 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002138 }
2139 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002140 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002141 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002142 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002143 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002144 unsigned LowBits = Log2_32(BitWidth)+1;
2145 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002146 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002147 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002148 }
2149 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002150 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002151 // If this is a ZEXTLoad and we are looking at the loaded value.
2152 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002153 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002154 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002155 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002156 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002157 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002158 }
Jay Foad5a29c362014-05-15 12:12:55 +00002159 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002160 }
2161 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002162 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002163 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002164 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002165 KnownZero = KnownZero.trunc(InBits);
2166 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002167 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002168 KnownZero = KnownZero.zext(BitWidth);
2169 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002170 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002171 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002172 }
2173 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002174 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002175 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002176 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002177
Jay Foad583abbc2010-12-07 08:25:19 +00002178 KnownZero = KnownZero.trunc(InBits);
2179 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002180 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002181
2182 // Note if the sign bit is known to be zero or one.
2183 bool SignBitKnownZero = KnownZero.isNegative();
2184 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002185
Jay Foad583abbc2010-12-07 08:25:19 +00002186 KnownZero = KnownZero.zext(BitWidth);
2187 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002188
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002189 // If the sign bit is known zero or one, the top bits match.
2190 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002191 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002192 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002193 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002194 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002195 }
2196 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002197 EVT InVT = Op.getOperand(0).getValueType();
2198 unsigned InBits = InVT.getScalarType().getSizeInBits();
2199 KnownZero = KnownZero.trunc(InBits);
2200 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002201 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002202 KnownZero = KnownZero.zext(BitWidth);
2203 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002204 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002205 }
2206 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002207 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002208 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002209 KnownZero = KnownZero.zext(InBits);
2210 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002211 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002212 KnownZero = KnownZero.trunc(BitWidth);
2213 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002214 break;
2215 }
2216 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002217 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002218 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002219 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002220 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002221 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002222 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002223 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002224 case ISD::FGETSIGN:
2225 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002226 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002227 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002228
Dan Gohman72ec3f42008-04-28 17:02:21 +00002229 case ISD::SUB: {
2230 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2231 // We know that the top bits of C-X are clear if X contains less bits
2232 // than C (i.e. no wrap-around can happen). For example, 20-X is
2233 // positive if we can prove that X is >= 0 and < 16.
2234 if (CLHS->getAPIntValue().isNonNegative()) {
2235 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2236 // NLZ can't be BitWidth with no sign bit
2237 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002238 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002239
2240 // If all of the MaskV bits are known to be zero, then we know the
2241 // output top bits are zero, because we now know that the output is
2242 // from [0-C].
2243 if ((KnownZero2 & MaskV) == MaskV) {
2244 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2245 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002246 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002247 }
2248 }
2249 }
2250 }
2251 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002252 case ISD::ADD:
2253 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002254 // Output known-0 bits are known if clear or set in both the low clear bits
2255 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2256 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002257 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002258 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2259
Jay Foada0653a32014-05-14 21:14:37 +00002260 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002261 KnownZeroOut = std::min(KnownZeroOut,
2262 KnownZero2.countTrailingOnes());
2263
Chris Lattner440b2802010-12-19 20:38:28 +00002264 if (Op.getOpcode() == ISD::ADD) {
2265 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002266 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002267 }
2268
2269 // With ADDE, a carry bit may be added in, so we can only use this
2270 // information if we know (at least) that the low two bits are clear. We
2271 // then return to the caller that the low bit is unknown but that other bits
2272 // are known zero.
2273 if (KnownZeroOut >= 2) // ADDE
2274 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002275 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002276 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002277 case ISD::SREM:
2278 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002279 const APInt &RA = Rem->getAPIntValue().abs();
2280 if (RA.isPowerOf2()) {
2281 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002282 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002283
Duncan Sands33274982010-01-29 09:45:26 +00002284 // The low bits of the first operand are unchanged by the srem.
2285 KnownZero = KnownZero2 & LowBits;
2286 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002287
Duncan Sands33274982010-01-29 09:45:26 +00002288 // If the first operand is non-negative or has all low bits zero, then
2289 // the upper bits are all zero.
2290 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2291 KnownZero |= ~LowBits;
2292
2293 // If the first operand is negative and not all low bits are zero, then
2294 // the upper bits are all one.
2295 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2296 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002297 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002298 }
2299 }
Jay Foad5a29c362014-05-15 12:12:55 +00002300 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002301 case ISD::UREM: {
2302 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002303 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002304 if (RA.isPowerOf2()) {
2305 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002306 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2307
2308 // The upper bits are all zero, the lower ones are unchanged.
2309 KnownZero = KnownZero2 | ~LowBits;
2310 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002311 break;
2312 }
2313 }
2314
2315 // Since the result is less than or equal to either operand, any leading
2316 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002317 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2318 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002319
2320 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2321 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002322 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002323 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002324 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002325 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002326 case ISD::FrameIndex:
2327 case ISD::TargetFrameIndex:
2328 if (unsigned Align = InferPtrAlignment(Op)) {
2329 // The low bits are known zero if the pointer is aligned.
2330 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002331 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002332 }
2333 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002334
Dan Gohman309d3d52007-06-22 14:59:07 +00002335 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002336 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2337 break;
2338 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002339 case ISD::INTRINSIC_WO_CHAIN:
2340 case ISD::INTRINSIC_W_CHAIN:
2341 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002342 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002343 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002344 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002345 }
Jay Foad5a29c362014-05-15 12:12:55 +00002346
2347 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002348}
2349
2350/// ComputeNumSignBits - Return the number of times the sign bit of the
2351/// register is replicated into the other bits. We know that at least 1 bit
2352/// is always equal to the sign bit (itself), but other cases can give us
2353/// information. For example, immediately after an "SRA X, 2", we know that
2354/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002355unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002356 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002357 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002358 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002359 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002360 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002361
Dan Gohman309d3d52007-06-22 14:59:07 +00002362 if (Depth == 6)
2363 return 1; // Limit search depth.
2364
2365 switch (Op.getOpcode()) {
2366 default: break;
2367 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002368 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002369 return VTBits-Tmp+1;
2370 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002371 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002372 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002373
Dan Gohman309d3d52007-06-22 14:59:07 +00002374 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002375 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002376 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002378
Dan Gohman309d3d52007-06-22 14:59:07 +00002379 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002380 Tmp =
2381 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002382 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002383
Dan Gohman309d3d52007-06-22 14:59:07 +00002384 case ISD::SIGN_EXTEND_INREG:
2385 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002386 Tmp =
2387 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002388 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002389
Dan Gohman309d3d52007-06-22 14:59:07 +00002390 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2391 return std::max(Tmp, Tmp2);
2392
2393 case ISD::SRA:
2394 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2395 // SRA X, C -> adds C sign bits.
2396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002397 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002398 if (Tmp > VTBits) Tmp = VTBits;
2399 }
2400 return Tmp;
2401 case ISD::SHL:
2402 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2403 // shl destroys sign bits.
2404 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002405 if (C->getZExtValue() >= VTBits || // Bad shift.
2406 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2407 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002408 }
2409 break;
2410 case ISD::AND:
2411 case ISD::OR:
2412 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002413 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002414 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002415 if (Tmp != 1) {
2416 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2417 FirstAnswer = std::min(Tmp, Tmp2);
2418 // We computed what we know about the sign bits as our first
2419 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002420 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002421 }
2422 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002423
2424 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002425 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002427 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002428 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002429
2430 case ISD::SADDO:
2431 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002432 case ISD::SSUBO:
2433 case ISD::USUBO:
2434 case ISD::SMULO:
2435 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002436 if (Op.getResNo() != 1)
2437 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002438 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002439 // If setcc returns 0/-1, all bits are sign bits.
2440 // We know that we have an integer-based boolean since these operations
2441 // are only available for integer.
2442 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2443 TargetLowering::ZeroOrNegativeOneBooleanContent)
2444 return VTBits;
2445 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 case ISD::SETCC:
2447 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002448 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002449 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002450 return VTBits;
2451 break;
2452 case ISD::ROTL:
2453 case ISD::ROTR:
2454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002455 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002456
Dan Gohman309d3d52007-06-22 14:59:07 +00002457 // Handle rotate right by N like a rotate left by 32-N.
2458 if (Op.getOpcode() == ISD::ROTR)
2459 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2460
2461 // If we aren't rotating out all of the known-in sign bits, return the
2462 // number that are left. This handles rotl(sext(x), 1) for example.
2463 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2464 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2465 }
2466 break;
2467 case ISD::ADD:
2468 // Add can have at most one carry bit. Thus we know that the output
2469 // is, at worst, one more bit than the inputs.
2470 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2471 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002472
Dan Gohman309d3d52007-06-22 14:59:07 +00002473 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002474 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002475 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002476 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002477 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002478
Dan Gohman309d3d52007-06-22 14:59:07 +00002479 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2480 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002481 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002482 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002483
Dan Gohman309d3d52007-06-22 14:59:07 +00002484 // If we are subtracting one from a positive number, there is no carry
2485 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002486 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002487 return Tmp;
2488 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002489
Dan Gohman309d3d52007-06-22 14:59:07 +00002490 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2491 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002492 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002493
Dan Gohman309d3d52007-06-22 14:59:07 +00002494 case ISD::SUB:
2495 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2496 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002497
Dan Gohman309d3d52007-06-22 14:59:07 +00002498 // Handle NEG.
2499 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002500 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002501 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002502 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002503 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2504 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002505 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002506 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002507
Dan Gohman309d3d52007-06-22 14:59:07 +00002508 // If the input is known to be positive (the sign bit is known clear),
2509 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002510 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002511 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002512
Dan Gohman309d3d52007-06-22 14:59:07 +00002513 // Otherwise, we treat this like a SUB.
2514 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002515
Dan Gohman309d3d52007-06-22 14:59:07 +00002516 // Sub can have at most one carry bit. Thus we know that the output
2517 // is, at worst, one more bit than the inputs.
2518 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2519 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002520 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002521 case ISD::TRUNCATE:
2522 // FIXME: it's tricky to do anything useful for this, but it is an important
2523 // case for targets like X86.
2524 break;
2525 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002526
Nadav Rotem4536d582013-03-20 22:53:44 +00002527 // If we are looking at the loaded value of the SDNode.
2528 if (Op.getResNo() == 0) {
2529 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2530 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2531 unsigned ExtType = LD->getExtensionType();
2532 switch (ExtType) {
2533 default: break;
2534 case ISD::SEXTLOAD: // '17' bits known
2535 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2536 return VTBits-Tmp+1;
2537 case ISD::ZEXTLOAD: // '16' bits known
2538 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2539 return VTBits-Tmp;
2540 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002541 }
2542 }
2543
2544 // Allow the target to implement this method for its nodes.
2545 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002546 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002547 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2548 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002549 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002550 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002551 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002552
Dan Gohman309d3d52007-06-22 14:59:07 +00002553 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2554 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002555 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002556 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002557
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002558 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002559 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002560 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002561 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002562 Mask = KnownOne;
2563 } else {
2564 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002565 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002566 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002567
Dan Gohman309d3d52007-06-22 14:59:07 +00002568 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2569 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002570 Mask = ~Mask;
2571 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002572 // Return # leading zeros. We use 'min' here in case Val was zero before
2573 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002574 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002575}
2576
Chris Lattner46c01a32011-02-13 22:25:43 +00002577/// isBaseWithConstantOffset - Return true if the specified operand is an
2578/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2579/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2580/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002581/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002582bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2583 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2584 !isa<ConstantSDNode>(Op.getOperand(1)))
2585 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002586
2587 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002588 !MaskedValueIsZero(Op.getOperand(0),
2589 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2590 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002591
Chris Lattner46c01a32011-02-13 22:25:43 +00002592 return true;
2593}
2594
2595
Dan Gohmand0d5e682009-09-03 20:34:31 +00002596bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2597 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002598 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002599 return true;
2600
2601 // If the value is a constant, we can obviously see if it is a NaN or not.
2602 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2603 return !C->getValueAPF().isNaN();
2604
2605 // TODO: Recognize more cases here.
2606
2607 return false;
2608}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002609
Dan Gohman38605212010-02-24 06:52:40 +00002610bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2611 // If the value is a constant, we can obviously see if it is a zero or not.
2612 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2613 return !C->isZero();
2614
2615 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002616 switch (Op.getOpcode()) {
2617 default: break;
2618 case ISD::OR:
2619 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2620 return !C->isNullValue();
2621 break;
2622 }
Dan Gohman38605212010-02-24 06:52:40 +00002623
2624 return false;
2625}
2626
2627bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2628 // Check the obvious case.
2629 if (A == B) return true;
2630
2631 // For for negative and positive zero.
2632 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2633 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2634 if (CA->isZero() && CB->isZero()) return true;
2635
2636 // Otherwise they may not be equal.
2637 return false;
2638}
2639
Chris Lattner061a1ea2005-01-07 07:46:32 +00002640/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002641///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002643 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002644 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002645 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002646 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002647 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002648
Jack Carter170a5f22013-09-09 22:02:08 +00002649 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2650 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002651 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002652
Chandler Carruth41b20e72014-07-22 04:07:55 +00002653 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002654 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002655}
2656
Andrew Trickef9de2a2013-05-25 02:42:55 +00002657SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002658 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002659 // Constant fold unary operations with an integer constant operand. Even
2660 // opaque constant will be folded, because the folding of unary operations
2661 // doesn't create new constants with different values. Nevertheless, the
2662 // opaque flag is preserved during folding to prevent future folding with
2663 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002664 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002665 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002666 switch (Opcode) {
2667 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002668 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002669 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2670 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002671 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002672 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002673 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002674 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2675 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002676 case ISD::UINT_TO_FP:
2677 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002678 APFloat apf(EVTToAPFloatSemantics(VT),
2679 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002680 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002681 Opcode==ISD::SINT_TO_FP,
2682 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002683 return getConstantFP(apf, VT);
2684 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002685 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002686 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002687 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002688 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002689 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002690 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002691 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002692 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2693 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002694 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002695 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2696 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002697 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002698 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002699 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2700 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002701 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002702 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002703 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2704 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002705 }
2706 }
2707
Dale Johannesen446b9002007-08-31 23:34:27 +00002708 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002709 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002710 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002711 switch (Opcode) {
2712 case ISD::FNEG:
2713 V.changeSign();
2714 return getConstantFP(V, VT);
2715 case ISD::FABS:
2716 V.clearSign();
2717 return getConstantFP(V, VT);
2718 case ISD::FCEIL: {
2719 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2720 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002721 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002722 break;
2723 }
2724 case ISD::FTRUNC: {
2725 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2726 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002727 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002728 break;
2729 }
2730 case ISD::FFLOOR: {
2731 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2732 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002733 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002734 break;
2735 }
2736 case ISD::FP_EXTEND: {
2737 bool ignored;
2738 // This can return overflow, underflow, or inexact; we don't care.
2739 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002740 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002741 APFloat::rmNearestTiesToEven, &ignored);
2742 return getConstantFP(V, VT);
2743 }
2744 case ISD::FP_TO_SINT:
2745 case ISD::FP_TO_UINT: {
2746 integerPart x[2];
2747 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002748 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002749 // FIXME need to be more flexible about rounding mode.
2750 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2751 Opcode==ISD::FP_TO_SINT,
2752 APFloat::rmTowardZero, &ignored);
2753 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002754 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002755 APInt api(VT.getSizeInBits(), x);
2756 return getConstant(api, VT);
2757 }
2758 case ISD::BITCAST:
2759 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2760 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2761 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2762 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2763 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002764 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002765 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002766
Jim Grosbachf7502c42014-07-18 00:40:52 +00002767 // Constant fold unary operations with a vector integer operand.
2768 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002769 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002770 switch (Opcode) {
2771 default:
2772 // FIXME: Entirely reasonable to perform folding of other unary
2773 // operations here as the need arises.
2774 break;
2775 case ISD::UINT_TO_FP:
2776 case ISD::SINT_TO_FP: {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002777 SmallVector<SDValue, 8> Ops;
2778 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2779 SDValue OpN = BV->getOperand(i);
2780 // Let the above scalar folding handle the conversion of each
2781 // element.
2782 OpN = getNode(ISD::SINT_TO_FP, DL, VT.getVectorElementType(),
2783 OpN);
2784 Ops.push_back(OpN);
2785 }
2786 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Jim Grosbachf7502c42014-07-18 00:40:52 +00002787 }
2788 }
2789 }
2790 }
2791
Gabor Greiff304a7a2008-08-28 21:40:38 +00002792 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002793 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002794 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002795 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002796 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002797 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002798 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002799 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002800 assert(VT.isFloatingPoint() &&
2801 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002802 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002803 assert((!VT.isVector() ||
2804 VT.getVectorNumElements() ==
2805 Operand.getValueType().getVectorNumElements()) &&
2806 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002807 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002808 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002809 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002810 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002811 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002812 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002813 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002814 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2815 "Invalid sext node, dst < src!");
2816 assert((!VT.isVector() ||
2817 VT.getVectorNumElements() ==
2818 Operand.getValueType().getVectorNumElements()) &&
2819 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002820 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2821 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2822 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002823 // sext(undef) = 0, because the top bits will all be the same.
2824 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002825 break;
2826 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002827 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002828 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002829 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002830 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2831 "Invalid zext node, dst < src!");
2832 assert((!VT.isVector() ||
2833 VT.getVectorNumElements() ==
2834 Operand.getValueType().getVectorNumElements()) &&
2835 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002836 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002837 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002838 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002839 else if (OpOpcode == ISD::UNDEF)
2840 // zext(undef) = 0, because the top bits will be zero.
2841 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002842 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002843 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002844 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002845 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002846 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002847 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2848 "Invalid anyext node, dst < src!");
2849 assert((!VT.isVector() ||
2850 VT.getVectorNumElements() ==
2851 Operand.getValueType().getVectorNumElements()) &&
2852 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002853
Dan Gohman08837892010-06-18 00:08:30 +00002854 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2855 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002856 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002857 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002858 else if (OpOpcode == ISD::UNDEF)
2859 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002860
2861 // (ext (trunx x)) -> x
2862 if (OpOpcode == ISD::TRUNCATE) {
2863 SDValue OpOp = Operand.getNode()->getOperand(0);
2864 if (OpOp.getValueType() == VT)
2865 return OpOp;
2866 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002867 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002868 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002869 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002870 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002871 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002872 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2873 "Invalid truncate node, src < dst!");
2874 assert((!VT.isVector() ||
2875 VT.getVectorNumElements() ==
2876 Operand.getValueType().getVectorNumElements()) &&
2877 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002878 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002879 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002880 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2881 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002882 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002883 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2884 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002885 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002886 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002887 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002888 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002889 }
Craig Topper201c1a32012-01-15 01:05:11 +00002890 if (OpOpcode == ISD::UNDEF)
2891 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002892 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002893 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002894 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002895 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002896 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002897 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002898 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2899 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002900 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002901 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002902 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002903 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002904 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002905 (VT.getVectorElementType() == Operand.getValueType() ||
2906 (VT.getVectorElementType().isInteger() &&
2907 Operand.getValueType().isInteger() &&
2908 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002909 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002910 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002911 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002912 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2913 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2914 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2915 Operand.getConstantOperandVal(1) == 0 &&
2916 Operand.getOperand(0).getValueType() == VT)
2917 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002918 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002919 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002920 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002921 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002922 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002923 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002924 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002925 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002926 break;
2927 case ISD::FABS:
2928 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002929 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002930 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002931 }
2932
Chris Lattnerf9c19152005-08-25 19:12:10 +00002933 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002934 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002935 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002936 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002937 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002938 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002939 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002940 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002941 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002942
Jack Carter170a5f22013-09-09 22:02:08 +00002943 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2944 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002945 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002946 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002947 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2948 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002949 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002950
Chandler Carruth41b20e72014-07-22 04:07:55 +00002951 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002952 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002953}
2954
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002955SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2956 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002957 // If the opcode is a target-specific ISD node, there's nothing we can
2958 // do here and the operand rules may not line up with the below, so
2959 // bail early.
2960 if (Opcode >= ISD::BUILTIN_OP_END)
2961 return SDValue();
2962
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002963 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2964 SmallVector<SDValue, 4> Outputs;
2965 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002966
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002967 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2968 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002969 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2970 return SDValue();
2971
2972 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002973 // Scalar instruction.
2974 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002975 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002976 // For vectors extract each constant element into Inputs so we can constant
2977 // fold them individually.
2978 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2979 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2980 if (!BV1 || !BV2)
2981 return SDValue();
2982
2983 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2984
2985 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2986 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2987 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2988 if (!V1 || !V2) // Not a constant, bail.
2989 return SDValue();
2990
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002991 if (V1->isOpaque() || V2->isOpaque())
2992 return SDValue();
2993
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002994 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2995 // FIXME: This is valid and could be handled by truncating the APInts.
2996 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2997 return SDValue();
2998
2999 Inputs.push_back(std::make_pair(V1, V2));
3000 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003001 }
3002
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003003 // We have a number of constant values, constant fold them element by element.
3004 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3005 const APInt &C1 = Inputs[I].first->getAPIntValue();
3006 const APInt &C2 = Inputs[I].second->getAPIntValue();
3007
3008 switch (Opcode) {
3009 case ISD::ADD:
3010 Outputs.push_back(getConstant(C1 + C2, SVT));
3011 break;
3012 case ISD::SUB:
3013 Outputs.push_back(getConstant(C1 - C2, SVT));
3014 break;
3015 case ISD::MUL:
3016 Outputs.push_back(getConstant(C1 * C2, SVT));
3017 break;
3018 case ISD::UDIV:
3019 if (!C2.getBoolValue())
3020 return SDValue();
3021 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3022 break;
3023 case ISD::UREM:
3024 if (!C2.getBoolValue())
3025 return SDValue();
3026 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3027 break;
3028 case ISD::SDIV:
3029 if (!C2.getBoolValue())
3030 return SDValue();
3031 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3032 break;
3033 case ISD::SREM:
3034 if (!C2.getBoolValue())
3035 return SDValue();
3036 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3037 break;
3038 case ISD::AND:
3039 Outputs.push_back(getConstant(C1 & C2, SVT));
3040 break;
3041 case ISD::OR:
3042 Outputs.push_back(getConstant(C1 | C2, SVT));
3043 break;
3044 case ISD::XOR:
3045 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3046 break;
3047 case ISD::SHL:
3048 Outputs.push_back(getConstant(C1 << C2, SVT));
3049 break;
3050 case ISD::SRL:
3051 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3052 break;
3053 case ISD::SRA:
3054 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3055 break;
3056 case ISD::ROTL:
3057 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3058 break;
3059 case ISD::ROTR:
3060 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3061 break;
3062 default:
3063 return SDValue();
3064 }
3065 }
3066
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003067 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3068 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003069
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003070 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003071 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003072 return Outputs.back();
3073
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003074 // We may have a vector type but a scalar result. Create a splat.
3075 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3076
3077 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003078 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003079}
3080
Andrew Trickef9de2a2013-05-25 02:42:55 +00003081SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003082 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003083 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3084 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003085 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003086 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003087 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003088 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3089 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003090 // Fold trivial token factors.
3091 if (N1.getOpcode() == ISD::EntryToken) return N2;
3092 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003093 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003094 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003095 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003096 // Concat of UNDEFs is UNDEF.
3097 if (N1.getOpcode() == ISD::UNDEF &&
3098 N2.getOpcode() == ISD::UNDEF)
3099 return getUNDEF(VT);
3100
Dan Gohman550c9af2008-08-14 20:04:46 +00003101 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3102 // one big BUILD_VECTOR.
3103 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3104 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003105 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3106 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003107 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003108 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003109 }
3110 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003111 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003112 assert(VT.isInteger() && "This operator does not apply to FP types!");
3113 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003114 N1.getValueType() == VT && "Binary operator types must match!");
3115 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3116 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003117 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003118 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003119 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3120 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003121 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003122 case ISD::OR:
3123 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003124 case ISD::ADD:
3125 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003126 assert(VT.isInteger() && "This operator does not apply to FP types!");
3127 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003128 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003129 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3130 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003131 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003132 return N1;
3133 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003134 case ISD::UDIV:
3135 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003136 case ISD::MULHU:
3137 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003138 case ISD::MUL:
3139 case ISD::SDIV:
3140 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003141 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003142 assert(N1.getValueType() == N2.getValueType() &&
3143 N1.getValueType() == VT && "Binary operator types must match!");
3144 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003145 case ISD::FADD:
3146 case ISD::FSUB:
3147 case ISD::FMUL:
3148 case ISD::FDIV:
3149 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003150 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003151 if (Opcode == ISD::FADD) {
3152 // 0+x --> x
3153 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3154 if (CFP->getValueAPF().isZero())
3155 return N2;
3156 // x+0 --> x
3157 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3158 if (CFP->getValueAPF().isZero())
3159 return N1;
3160 } else if (Opcode == ISD::FSUB) {
3161 // x-0 --> x
3162 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3163 if (CFP->getValueAPF().isZero())
3164 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003165 } else if (Opcode == ISD::FMUL) {
3166 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3167 SDValue V = N2;
3168
3169 // If the first operand isn't the constant, try the second
3170 if (!CFP) {
3171 CFP = dyn_cast<ConstantFPSDNode>(N2);
3172 V = N1;
3173 }
3174
3175 if (CFP) {
3176 // 0*x --> 0
3177 if (CFP->isZero())
3178 return SDValue(CFP,0);
3179 // 1*x --> x
3180 if (CFP->isExactlyValue(1.0))
3181 return V;
3182 }
Dan Gohman1275e282009-01-23 19:10:37 +00003183 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003184 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003185 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003186 assert(N1.getValueType() == N2.getValueType() &&
3187 N1.getValueType() == VT && "Binary operator types must match!");
3188 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003189 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3190 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003191 N1.getValueType().isFloatingPoint() &&
3192 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003193 "Invalid FCOPYSIGN!");
3194 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003195 case ISD::SHL:
3196 case ISD::SRA:
3197 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003198 case ISD::ROTL:
3199 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003200 assert(VT == N1.getValueType() &&
3201 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003202 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003203 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003204 assert((!VT.isVector() || VT == N2.getValueType()) &&
3205 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003206 // Verify that the shift amount VT is bit enough to hold valid shift
3207 // amounts. This catches things like trying to shift an i1024 value by an
3208 // i8, which is easy to fall into in generic code that uses
3209 // TLI.getShiftAmount().
3210 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003211 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003212 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003213
3214 // Always fold shifts of i1 values so the code generator doesn't need to
3215 // handle them. Since we know the size of the shift has to be less than the
3216 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003217 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003218 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003219 if (N2C && N2C->isNullValue())
3220 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003221 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003222 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003223 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003224 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003225 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003226 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003227 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003228 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003229 "type is vector!");
3230 assert((!EVT.isVector() ||
3231 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3232 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003233 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003234 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003235 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003236 break;
3237 }
Chris Lattner72733e52008-01-17 07:00:52 +00003238 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003239 assert(VT.isFloatingPoint() &&
3240 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003241 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003242 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003243 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003244 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003245 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003246 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003247 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003248 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003249 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003250 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003251 assert(!EVT.isVector() &&
3252 "AssertSExt/AssertZExt type should be the vector element type "
3253 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003254 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003255 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003256 break;
3257 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003258 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003259 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003260 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003261 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003262 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003263 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003264 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003265 "type is vector!");
3266 assert((!EVT.isVector() ||
3267 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3268 "Vector element counts must match in SIGN_EXTEND_INREG");
3269 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003270 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003271
Chris Lattner16713612008-01-22 19:09:33 +00003272 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003273 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003274 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003275 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003276 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003277 return getConstant(Val, VT);
3278 }
Chris Lattner16713612008-01-22 19:09:33 +00003279 break;
3280 }
3281 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003282 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3283 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003284 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003285
Chris Lattner16713612008-01-22 19:09:33 +00003286 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3287 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003288 if (N2C &&
3289 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003290 N1.getNumOperands() > 0) {
3291 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003292 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003293 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003294 N1.getOperand(N2C->getZExtValue() / Factor),
3295 getConstant(N2C->getZExtValue() % Factor,
3296 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003297 }
3298
3299 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3300 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003301 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3302 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003303
3304 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003305 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003306 // are promoted and implicitly truncated, and the result implicitly
3307 // extended. Make that explicit here.
3308 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003309
Bob Wilson59dbbb22009-04-13 22:05:19 +00003310 return Elt;
3311 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003312
Chris Lattner16713612008-01-22 19:09:33 +00003313 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3314 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003315 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003316 // If the indices are the same, return the inserted element else
3317 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003318 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003319 SDValue N1Op2 = N1.getOperand(2);
3320 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3321
3322 if (N1Op2C && N2C) {
3323 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3324 if (VT == N1.getOperand(1).getValueType())
3325 return N1.getOperand(1);
3326 else
3327 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3328 }
3329
Dale Johannesendb393622009-02-03 01:55:44 +00003330 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003331 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003332 }
Chris Lattner16713612008-01-22 19:09:33 +00003333 break;
3334 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003335 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003336 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3337 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003338 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003339 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003340
Chris Lattner16713612008-01-22 19:09:33 +00003341 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3342 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003343 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003344 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003345 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003346
Chris Lattner16713612008-01-22 19:09:33 +00003347 // EXTRACT_ELEMENT of a constant int is also very common.
3348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003349 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003350 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003351 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3352 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003353 }
3354 break;
David Greeneb6f16112011-01-26 15:38:49 +00003355 case ISD::EXTRACT_SUBVECTOR: {
3356 SDValue Index = N2;
3357 if (VT.isSimple() && N1.getValueType().isSimple()) {
3358 assert(VT.isVector() && N1.getValueType().isVector() &&
3359 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003360 assert(VT.getVectorElementType() ==
3361 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003362 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003363 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003364 "Extract subvector must be from larger vector to smaller vector!");
3365
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003366 if (isa<ConstantSDNode>(Index.getNode())) {
3367 assert((VT.getVectorNumElements() +
3368 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003369 <= N1.getValueType().getVectorNumElements())
3370 && "Extract subvector overflow!");
3371 }
3372
3373 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003374 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003375 return N1;
3376 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003377 break;
Chris Lattner16713612008-01-22 19:09:33 +00003378 }
David Greeneb6f16112011-01-26 15:38:49 +00003379 }
Chris Lattner16713612008-01-22 19:09:33 +00003380
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003381 // Perform trivial constant folding.
3382 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3383 if (SV.getNode()) return SV;
3384
3385 // Canonicalize constant to RHS if commutative.
3386 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3387 std::swap(N1C, N2C);
3388 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003389 }
3390
Chris Lattner16713612008-01-22 19:09:33 +00003391 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003392 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003393 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3394 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003395 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003396 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003397 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003398 std::swap(N1CFP, N2CFP);
3399 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003400 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003401 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3402 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003403 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003404 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003405 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003406 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003407 return getConstantFP(V1, VT);
3408 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003409 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003410 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003411 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003412 return getConstantFP(V1, VT);
3413 break;
3414 case ISD::FMUL:
3415 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003416 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003417 return getConstantFP(V1, VT);
3418 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003419 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003420 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003421 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3422 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003423 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003424 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003425 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003426 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003427 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003428 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3429 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003430 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003431 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003432 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003433 case ISD::FCOPYSIGN:
3434 V1.copySign(V2);
3435 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003436 default: break;
3437 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003438 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003439
3440 if (Opcode == ISD::FP_ROUND) {
3441 APFloat V = N1CFP->getValueAPF(); // make copy
3442 bool ignored;
3443 // This can return overflow, underflow, or inexact; we don't care.
3444 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003445 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003446 APFloat::rmNearestTiesToEven, &ignored);
3447 return getConstantFP(V, VT);
3448 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003449 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003450
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003451 // Canonicalize an UNDEF to the RHS, even over a constant.
3452 if (N1.getOpcode() == ISD::UNDEF) {
3453 if (isCommutativeBinOp(Opcode)) {
3454 std::swap(N1, N2);
3455 } else {
3456 switch (Opcode) {
3457 case ISD::FP_ROUND_INREG:
3458 case ISD::SIGN_EXTEND_INREG:
3459 case ISD::SUB:
3460 case ISD::FSUB:
3461 case ISD::FDIV:
3462 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003463 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003464 return N1; // fold op(undef, arg2) -> undef
3465 case ISD::UDIV:
3466 case ISD::SDIV:
3467 case ISD::UREM:
3468 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003469 case ISD::SRL:
3470 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003471 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003472 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3473 // For vectors, we can't easily build an all zero vector, just return
3474 // the LHS.
3475 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003476 }
3477 }
3478 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003479
3480 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003481 if (N2.getOpcode() == ISD::UNDEF) {
3482 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003483 case ISD::XOR:
3484 if (N1.getOpcode() == ISD::UNDEF)
3485 // Handle undef ^ undef -> 0 special case. This is a common
3486 // idiom (misuse).
3487 return getConstant(0, VT);
3488 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003489 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003490 case ISD::ADDC:
3491 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003492 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003493 case ISD::UDIV:
3494 case ISD::SDIV:
3495 case ISD::UREM:
3496 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003497 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003498 case ISD::FADD:
3499 case ISD::FSUB:
3500 case ISD::FMUL:
3501 case ISD::FDIV:
3502 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003503 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003504 return N2;
3505 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003506 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003507 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003508 case ISD::SRL:
3509 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003510 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003511 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3512 // For vectors, we can't easily build an all zero vector, just return
3513 // the LHS.
3514 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003515 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003516 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003517 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003518 // For vectors, we can't easily build an all one vector, just return
3519 // the LHS.
3520 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003521 case ISD::SRA:
3522 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003523 }
3524 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003525
Chris Lattner724f7ee2005-05-11 18:57:39 +00003526 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003527 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003528 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003529 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003530 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003531 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003532 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003533 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003534 if (BinOpHasFlags)
3535 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003536 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003538 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003539
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003540 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3541
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003542 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003543 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003544
3545 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003546 }
3547
Chandler Carruth41b20e72014-07-22 04:07:55 +00003548 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003549 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003550}
3551
Andrew Trickef9de2a2013-05-25 02:42:55 +00003552SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003553 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003554 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003555 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003556 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003557 case ISD::FMA: {
3558 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3559 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3560 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3561 if (N1CFP && N2CFP && N3CFP) {
3562 APFloat V1 = N1CFP->getValueAPF();
3563 const APFloat &V2 = N2CFP->getValueAPF();
3564 const APFloat &V3 = N3CFP->getValueAPF();
3565 APFloat::opStatus s =
3566 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3567 if (s != APFloat::opInvalidOp)
3568 return getConstantFP(V1, VT);
3569 }
3570 break;
3571 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003572 case ISD::CONCAT_VECTORS:
3573 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3574 // one big BUILD_VECTOR.
3575 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3576 N2.getOpcode() == ISD::BUILD_VECTOR &&
3577 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003578 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3579 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003580 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3581 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003582 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003583 }
3584 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003585 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003586 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003587 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003588 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003589 break;
3590 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003591 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003592 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003593 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003594 return N2; // select true, X, Y -> X
3595 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003596 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003597
3598 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003599 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003600 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003601 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003602 case ISD::INSERT_SUBVECTOR: {
3603 SDValue Index = N3;
3604 if (VT.isSimple() && N1.getValueType().isSimple()
3605 && N2.getValueType().isSimple()) {
3606 assert(VT.isVector() && N1.getValueType().isVector() &&
3607 N2.getValueType().isVector() &&
3608 "Insert subvector VTs must be a vectors");
3609 assert(VT == N1.getValueType() &&
3610 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003611 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003612 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003613 if (isa<ConstantSDNode>(Index.getNode())) {
3614 assert((N2.getValueType().getVectorNumElements() +
3615 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003616 <= VT.getVectorNumElements())
3617 && "Insert subvector overflow!");
3618 }
3619
3620 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003621 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003622 return N2;
3623 }
3624 break;
3625 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003626 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003627 // Fold bit_convert nodes from a type to themselves.
3628 if (N1.getValueType() == VT)
3629 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003630 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003631 }
3632
Chris Lattnerf9c19152005-08-25 19:12:10 +00003633 // Memoize node if it doesn't produce a flag.
3634 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003635 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003636 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003637 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003638 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003639 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003640 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003641 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003642 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003643
Jack Carter170a5f22013-09-09 22:02:08 +00003644 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3645 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003646 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003647 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003648 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3649 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003650 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003651
Chandler Carruth41b20e72014-07-22 04:07:55 +00003652 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003653 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003654}
3655
Andrew Trickef9de2a2013-05-25 02:42:55 +00003656SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003657 SDValue N1, SDValue N2, SDValue N3,
3658 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003659 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003660 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003661}
3662
Andrew Trickef9de2a2013-05-25 02:42:55 +00003663SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003664 SDValue N1, SDValue N2, SDValue N3,
3665 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003666 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003667 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003668}
3669
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003670/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3671/// the incoming stack arguments to be loaded from the stack.
3672SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3673 SmallVector<SDValue, 8> ArgChains;
3674
3675 // Include the original chain at the beginning of the list. When this is
3676 // used by target LowerCall hooks, this helps legalize find the
3677 // CALLSEQ_BEGIN node.
3678 ArgChains.push_back(Chain);
3679
3680 // Add a chain value for each stack argument.
3681 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3682 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3683 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3684 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3685 if (FI->getIndex() < 0)
3686 ArgChains.push_back(SDValue(L, 1));
3687
3688 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003689 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003690}
3691
Dan Gohman544ab2c2008-04-12 04:36:06 +00003692/// getMemsetValue - Vectorized representation of the memset value
3693/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003694static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003695 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003696 assert(Value.getOpcode() != ISD::UNDEF);
3697
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003698 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003699 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003700 assert(C->getAPIntValue().getBitWidth() == 8);
3701 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003702 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003703 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003704 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003705 }
Evan Chengef377ad2008-05-15 08:39:06 +00003706
Dale Johannesenabf66b82009-02-03 22:26:09 +00003707 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003708 if (NumBits > 8) {
3709 // Use a multiplication with 0x010101... to extend the input to the
3710 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003711 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003712 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003713 }
3714
3715 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003716}
3717
Dan Gohman544ab2c2008-04-12 04:36:06 +00003718/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3719/// used when a memcpy is turned into a memset when the source is a constant
3720/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003721static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003722 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003723 // Handle vector with all elements zero.
3724 if (Str.empty()) {
3725 if (VT.isInteger())
3726 return DAG.getConstant(0, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003727 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003728 return DAG.getConstantFP(0.0, VT);
3729 else if (VT.isVector()) {
3730 unsigned NumElts = VT.getVectorNumElements();
3731 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003732 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003733 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3734 EltVT, NumElts)));
3735 } else
3736 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003737 }
3738
Duncan Sands13237ac2008-06-06 12:08:01 +00003739 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003740 unsigned NumVTBits = VT.getSizeInBits();
3741 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003742 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3743
Evan Chengc8444b12013-01-10 22:13:27 +00003744 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003745 if (TLI.isLittleEndian()) {
3746 for (unsigned i = 0; i != NumBytes; ++i)
3747 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3748 } else {
3749 for (unsigned i = 0; i != NumBytes; ++i)
3750 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003751 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003752
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003753 // If the "cost" of materializing the integer immediate is less than the cost
3754 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003755 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3756 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003757 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003758 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003759}
3760
Scott Michelcf0da6c2009-02-17 22:15:04 +00003761/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003762///
Andrew Tricke2431c62013-05-25 03:08:10 +00003763static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003764 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003765 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003766 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003767 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003768}
3769
Evan Chengef377ad2008-05-15 08:39:06 +00003770/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3771///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003772static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003773 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003774 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003775 if (Src.getOpcode() == ISD::GlobalAddress)
3776 G = cast<GlobalAddressSDNode>(Src);
3777 else if (Src.getOpcode() == ISD::ADD &&
3778 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3779 Src.getOperand(1).getOpcode() == ISD::Constant) {
3780 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003781 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003782 }
3783 if (!G)
3784 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003785
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003786 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003787}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003788
Evan Cheng43cd9e32010-04-01 06:04:33 +00003789/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3790/// to replace the memset / memcpy. Return true if the number of memory ops
3791/// is below the threshold. It returns the types of the sequence of
3792/// memory ops to perform memset / memcpy by reference.
3793static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003794 unsigned Limit, uint64_t Size,
3795 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003796 bool IsMemset,
3797 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003798 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003799 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003800 SelectionDAG &DAG,
3801 const TargetLowering &TLI) {
3802 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3803 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003804 // If 'SrcAlign' is zero, that means the memory operation does not need to
3805 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3806 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3807 // is the specified alignment of the memory operation. If it is zero, that
3808 // means it's possible to change the alignment of the destination.
3809 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3810 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003811 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003812 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003813 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003814
Chris Lattner28dc6c12010-03-07 07:45:08 +00003815 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003816 unsigned AS = 0;
3817 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003818 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003819 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003820 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003821 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003822 case 0: VT = MVT::i64; break;
3823 case 4: VT = MVT::i32; break;
3824 case 2: VT = MVT::i16; break;
3825 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003826 }
3827 }
3828
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003829 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003830 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003831 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003832 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003833
Duncan Sands11dd4242008-06-08 20:54:56 +00003834 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003835 VT = LVT;
3836 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003837
Dan Gohman544ab2c2008-04-12 04:36:06 +00003838 unsigned NumMemOps = 0;
3839 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003840 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003841 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003842 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003843 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003844 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003845
3846 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003847 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003848 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003849 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003850 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003851 Found = true;
3852 else if (NewVT == MVT::i64 &&
3853 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003854 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003855 // i64 is usually not legal on 32-bit targets, but f64 may be.
3856 NewVT = MVT::f64;
3857 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003858 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003859 }
3860
Evan Cheng04e55182012-12-12 00:42:09 +00003861 if (!Found) {
3862 do {
3863 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3864 if (NewVT == MVT::i8)
3865 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003866 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003867 }
3868 NewVTSize = NewVT.getSizeInBits() / 8;
3869
Evan Cheng79e2ca92012-12-10 23:21:26 +00003870 // If the new VT cannot cover all of the remaining bits, then consider
3871 // issuing a (or a pair of) unaligned and overlapping load / store.
3872 // FIXME: Only does this for 64-bit or more since we don't have proper
3873 // cost model for unaligned load / store.
3874 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003875 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003876 if (NumMemOps && AllowOverlap &&
3877 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003878 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003879 VTSize = Size;
3880 else {
3881 VT = NewVT;
3882 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003883 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003884 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003885
Evan Chengb7d3d032012-12-12 20:43:23 +00003886 if (++NumMemOps > Limit)
3887 return false;
3888
Dan Gohman544ab2c2008-04-12 04:36:06 +00003889 MemOps.push_back(VT);
3890 Size -= VTSize;
3891 }
3892
3893 return true;
3894}
3895
Andrew Trickef9de2a2013-05-25 02:42:55 +00003896static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003897 SDValue Chain, SDValue Dst,
3898 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003899 unsigned Align, bool isVol,
3900 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003901 MachinePointerInfo DstPtrInfo,
3902 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003903 // Turn a memcpy of undef to nop.
3904 if (Src.getOpcode() == ISD::UNDEF)
3905 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003906
Dan Gohman714663a2008-05-29 19:42:22 +00003907 // Expand memcpy to a series of load and store ops if the size operand falls
3908 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003909 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3910 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003911 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003912 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003913 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003914 MachineFunction &MF = DAG.getMachineFunction();
3915 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003916 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003917 MF.getFunction()->getAttributes().
3918 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003919 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3920 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3921 DstAlignCanChange = true;
3922 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3923 if (Align > SrcAlign)
3924 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003925 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003926 bool CopyFromStr = isMemSrcFromString(Src, Str);
3927 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003928 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003929
Evan Cheng4c014c82010-04-01 18:19:11 +00003930 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003931 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003932 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003933 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003934 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003935
Evan Cheng43cd9e32010-04-01 06:04:33 +00003936 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003937 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003938 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003939
3940 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003941 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00003942 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00003943 if (!TRI->needsStackRealignment(MF))
3944 while (NewAlign > Align &&
3945 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3946 NewAlign /= 2;
3947
Evan Cheng43cd9e32010-04-01 06:04:33 +00003948 if (NewAlign > Align) {
3949 // Give the stack frame object a larger alignment if needed.
3950 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3951 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3952 Align = NewAlign;
3953 }
3954 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003955
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003956 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003957 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003958 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003959 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003960 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003961 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003962 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003963
Evan Cheng79e2ca92012-12-10 23:21:26 +00003964 if (VTSize > Size) {
3965 // Issuing an unaligned load / store pair that overlaps with the previous
3966 // pair. Adjust the offset accordingly.
3967 assert(i == NumMemOps-1 && i != 0);
3968 SrcOff -= VTSize - Size;
3969 DstOff -= VTSize - Size;
3970 }
3971
Evan Cheng43cd9e32010-04-01 06:04:33 +00003972 if (CopyFromStr &&
3973 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003974 // It's unlikely a store of a vector immediate can be done in a single
3975 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003976 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003977 // FIXME: Handle other cases where store of vector immediate is done in
3978 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003979 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003980 if (Value.getNode())
3981 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003982 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003983 DstPtrInfo.getWithOffset(DstOff), isVol,
3984 false, Align);
3985 }
3986
3987 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003988 // The type might not be legal for the target. This should only happen
3989 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003990 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3991 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003992 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003993 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003994 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003995 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003996 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003997 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00003998 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003999 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004000 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004001 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4002 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004003 }
4004 OutChains.push_back(Store);
4005 SrcOff += VTSize;
4006 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004007 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004008 }
4009
Craig Topper48d114b2014-04-26 18:35:24 +00004010 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004011}
4012
Andrew Trickef9de2a2013-05-25 02:42:55 +00004013static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004014 SDValue Chain, SDValue Dst,
4015 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004016 unsigned Align, bool isVol,
4017 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004018 MachinePointerInfo DstPtrInfo,
4019 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004020 // Turn a memmove of undef to nop.
4021 if (Src.getOpcode() == ISD::UNDEF)
4022 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004023
4024 // Expand memmove to a series of load and store ops if the size operand falls
4025 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004026 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004027 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004028 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004029 MachineFunction &MF = DAG.getMachineFunction();
4030 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004031 bool OptSize = MF.getFunction()->getAttributes().
4032 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004033 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4034 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4035 DstAlignCanChange = true;
4036 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4037 if (Align > SrcAlign)
4038 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004039 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004040
Evan Cheng4c014c82010-04-01 18:19:11 +00004041 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004042 (DstAlignCanChange ? 0 : Align), SrcAlign,
4043 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004044 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004045
Evan Cheng43cd9e32010-04-01 06:04:33 +00004046 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004047 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004048 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004049 if (NewAlign > Align) {
4050 // Give the stack frame object a larger alignment if needed.
4051 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4052 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4053 Align = NewAlign;
4054 }
4055 }
Dan Gohman714663a2008-05-29 19:42:22 +00004056
Evan Cheng43cd9e32010-04-01 06:04:33 +00004057 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004058 SmallVector<SDValue, 8> LoadValues;
4059 SmallVector<SDValue, 8> LoadChains;
4060 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004061 unsigned NumMemOps = MemOps.size();
4062 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004063 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004064 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004065 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004066
Dale Johannesenabf66b82009-02-03 22:26:09 +00004067 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004068 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004069 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004070 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004071 LoadValues.push_back(Value);
4072 LoadChains.push_back(Value.getValue(1));
4073 SrcOff += VTSize;
4074 }
Craig Topper48d114b2014-04-26 18:35:24 +00004075 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004076 OutChains.clear();
4077 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004078 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004079 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004080 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004081
Dale Johannesenabf66b82009-02-03 22:26:09 +00004082 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004083 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004084 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004085 OutChains.push_back(Store);
4086 DstOff += VTSize;
4087 }
4088
Craig Topper48d114b2014-04-26 18:35:24 +00004089 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004090}
4091
Serge Pavlov8ec39992013-09-17 16:24:42 +00004092/// \brief Lower the call to 'memset' intrinsic function into a series of store
4093/// operations.
4094///
4095/// \param DAG Selection DAG where lowered code is placed.
4096/// \param dl Link to corresponding IR location.
4097/// \param Chain Control flow dependency.
4098/// \param Dst Pointer to destination memory location.
4099/// \param Src Value of byte to write into the memory.
4100/// \param Size Number of bytes to write.
4101/// \param Align Alignment of the destination in bytes.
4102/// \param isVol True if destination is volatile.
4103/// \param DstPtrInfo IR information on the memory pointer.
4104/// \returns New head in the control flow, if lowering was successful, empty
4105/// SDValue otherwise.
4106///
4107/// The function tries to replace 'llvm.memset' intrinsic with several store
4108/// operations and value calculation code. This is usually profitable for small
4109/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004110static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004111 SDValue Chain, SDValue Dst,
4112 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004113 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004114 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004115 // Turn a memset of undef to nop.
4116 if (Src.getOpcode() == ISD::UNDEF)
4117 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004118
4119 // Expand memset to a series of load/store ops if the size operand
4120 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004121 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004122 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004123 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004124 MachineFunction &MF = DAG.getMachineFunction();
4125 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004126 bool OptSize = MF.getFunction()->getAttributes().
4127 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004128 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4129 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4130 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004131 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004132 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004133 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004134 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004135 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004136 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004137
Evan Cheng43cd9e32010-04-01 06:04:33 +00004138 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004139 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004140 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004141 if (NewAlign > Align) {
4142 // Give the stack frame object a larger alignment if needed.
4143 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4144 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4145 Align = NewAlign;
4146 }
4147 }
4148
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004149 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004150 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004151 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004152
4153 // Find the largest store and generate the bit pattern for it.
4154 EVT LargestVT = MemOps[0];
4155 for (unsigned i = 1; i < NumMemOps; i++)
4156 if (MemOps[i].bitsGT(LargestVT))
4157 LargestVT = MemOps[i];
4158 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4159
Dan Gohman544ab2c2008-04-12 04:36:06 +00004160 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004161 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004162 unsigned VTSize = VT.getSizeInBits() / 8;
4163 if (VTSize > Size) {
4164 // Issuing an unaligned load / store pair that overlaps with the previous
4165 // pair. Adjust the offset accordingly.
4166 assert(i == NumMemOps-1 && i != 0);
4167 DstOff -= VTSize - Size;
4168 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004169
4170 // If this store is smaller than the largest store see whether we can get
4171 // the smaller value for free with a truncate.
4172 SDValue Value = MemSetValue;
4173 if (VT.bitsLT(LargestVT)) {
4174 if (!LargestVT.isVector() && !VT.isVector() &&
4175 TLI.isTruncateFree(LargestVT, VT))
4176 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4177 else
4178 Value = getMemsetValue(Src, VT, DAG, dl);
4179 }
4180 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004181 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004182 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004183 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004184 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004185 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004186 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004187 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004188 }
4189
Craig Topper48d114b2014-04-26 18:35:24 +00004190 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004191}
4192
Andrew Trickef9de2a2013-05-25 02:42:55 +00004193SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004194 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004195 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004196 MachinePointerInfo DstPtrInfo,
4197 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004198 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004199
4200 // Check to see if we should lower the memcpy to loads and stores first.
4201 // For cases within the target-specified limits, this is the best choice.
4202 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4203 if (ConstantSize) {
4204 // Memcpy with size zero? Just return the original chain.
4205 if (ConstantSize->isNullValue())
4206 return Chain;
4207
Evan Cheng43cd9e32010-04-01 06:04:33 +00004208 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4209 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004210 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004211 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004212 return Result;
4213 }
4214
4215 // Then check to see if we should lower the memcpy with target-specific
4216 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004217 SDValue Result =
Alexey Samsonove229ec52014-08-20 21:40:15 +00004218 TSI->EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
4219 isVol, AlwaysInline, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004220 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004221 return Result;
4222
4223 // If we really need inline code and the target declined to provide it,
4224 // use a (potentially long) sequence of loads and stores.
4225 if (AlwaysInline) {
4226 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004227 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004228 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004229 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004230 }
4231
Dan Gohmanf38547c2010-04-05 20:24:08 +00004232 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4233 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4234 // respect volatile, so they may do things like read or write memory
4235 // beyond the given memory regions. But fixing this isn't easy, and most
4236 // people don't care.
4237
Dan Gohman544ab2c2008-04-12 04:36:06 +00004238 // Emit a library call.
4239 TargetLowering::ArgListTy Args;
4240 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004241 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004242 Entry.Node = Dst; Args.push_back(Entry);
4243 Entry.Node = Src; Args.push_back(Entry);
4244 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004245 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004246 TargetLowering::CallLoweringInfo CLI(*this);
4247 CLI.setDebugLoc(dl).setChain(Chain)
4248 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4249 Type::getVoidTy(*getContext()),
4250 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004251 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004252 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004253 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004254
Dan Gohman544ab2c2008-04-12 04:36:06 +00004255 return CallResult.second;
4256}
4257
Andrew Trickef9de2a2013-05-25 02:42:55 +00004258SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004259 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004260 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004261 MachinePointerInfo DstPtrInfo,
4262 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004263 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004264
Dan Gohman714663a2008-05-29 19:42:22 +00004265 // Check to see if we should lower the memmove to loads and stores first.
4266 // For cases within the target-specified limits, this is the best choice.
4267 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4268 if (ConstantSize) {
4269 // Memmove with size zero? Just return the original chain.
4270 if (ConstantSize->isNullValue())
4271 return Chain;
4272
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004273 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004274 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004275 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004276 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004277 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004278 return Result;
4279 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004280
4281 // Then check to see if we should lower the memmove with target-specific
4282 // code. If the target chooses to do this, this is the next best.
Alexey Samsonove229ec52014-08-20 21:40:15 +00004283 SDValue Result = TSI->EmitTargetCodeForMemmove(
4284 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004285 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004286 return Result;
4287
Mon P Wangbf862242010-04-06 08:27:51 +00004288 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4289 // not be safe. See memcpy above for more details.
4290
Dan Gohman544ab2c2008-04-12 04:36:06 +00004291 // Emit a library call.
4292 TargetLowering::ArgListTy Args;
4293 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004294 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004295 Entry.Node = Dst; Args.push_back(Entry);
4296 Entry.Node = Src; Args.push_back(Entry);
4297 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004298 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004299 TargetLowering::CallLoweringInfo CLI(*this);
4300 CLI.setDebugLoc(dl).setChain(Chain)
4301 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4302 Type::getVoidTy(*getContext()),
4303 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004304 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004305 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004306 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004307
Dan Gohman544ab2c2008-04-12 04:36:06 +00004308 return CallResult.second;
4309}
4310
Andrew Trickef9de2a2013-05-25 02:42:55 +00004311SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004312 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004313 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004314 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004315 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004316
4317 // Check to see if we should lower the memset to stores first.
4318 // For cases within the target-specified limits, this is the best choice.
4319 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4320 if (ConstantSize) {
4321 // Memset with size zero? Just return the original chain.
4322 if (ConstantSize->isNullValue())
4323 return Chain;
4324
Mon P Wangc576ee92010-04-04 03:10:48 +00004325 SDValue Result =
4326 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004327 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004328
Gabor Greiff304a7a2008-08-28 21:40:38 +00004329 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004330 return Result;
4331 }
4332
4333 // Then check to see if we should lower the memset with target-specific
4334 // code. If the target chooses to do this, this is the next best.
Alexey Samsonove229ec52014-08-20 21:40:15 +00004335 SDValue Result = TSI->EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src,
4336 Size, Align, isVol, DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004337 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004338 return Result;
4339
Wesley Peck527da1b2010-11-23 03:31:01 +00004340 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004341 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004342 TargetLowering::ArgListTy Args;
4343 TargetLowering::ArgListEntry Entry;
4344 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4345 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004346 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004347 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004348 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004349 Entry.Node = Size;
4350 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004351 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004352
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004353 // FIXME: pass in SDLoc
4354 TargetLowering::CallLoweringInfo CLI(*this);
4355 CLI.setDebugLoc(dl).setChain(Chain)
4356 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4357 Type::getVoidTy(*getContext()),
4358 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004359 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004360 .setDiscardResult();
4361
4362 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004363 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004364}
4365
Andrew Trickef9de2a2013-05-25 02:42:55 +00004366SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004367 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004368 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004369 AtomicOrdering SuccessOrdering,
4370 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004371 SynchronizationScope SynchScope) {
4372 FoldingSetNodeID ID;
4373 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004374 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004375 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004376 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004377 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4378 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4379 return SDValue(E, 0);
4380 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004381
4382 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4383 // SDNode doesn't have access to it. This memory will be "leaked" when
4384 // the node is deallocated, but recovered when the allocator is released.
4385 // If the number of operands is less than 5 we use AtomicSDNode's internal
4386 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004387 unsigned NumOps = Ops.size();
4388 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4389 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004390
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004391 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4392 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004393 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004394 SuccessOrdering, FailureOrdering,
4395 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004396 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004397 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004398 return SDValue(N, 0);
4399}
4400
4401SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004402 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004403 MachineMemOperand *MMO,
4404 AtomicOrdering Ordering,
4405 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004406 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004407 Ordering, SynchScope);
4408}
4409
Tim Northover420a2162014-06-13 14:24:07 +00004410SDValue SelectionDAG::getAtomicCmpSwap(
4411 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4412 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4413 unsigned Alignment, AtomicOrdering SuccessOrdering,
4414 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4415 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4416 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4417 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4418
Dan Gohman48b185d2009-09-25 20:36:54 +00004419 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4420 Alignment = getEVTAlignment(MemVT);
4421
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004422 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004423
Eli Friedmane978d2f2011-09-07 02:23:42 +00004424 // FIXME: Volatile isn't really correct; we should keep track of atomic
4425 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004426 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004427 Flags |= MachineMemOperand::MOLoad;
4428 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004429
4430 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004431 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004432
Tim Northover420a2162014-06-13 14:24:07 +00004433 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4434 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004435}
4436
Tim Northover420a2162014-06-13 14:24:07 +00004437SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4438 SDVTList VTs, SDValue Chain, SDValue Ptr,
4439 SDValue Cmp, SDValue Swp,
4440 MachineMemOperand *MMO,
4441 AtomicOrdering SuccessOrdering,
4442 AtomicOrdering FailureOrdering,
4443 SynchronizationScope SynchScope) {
4444 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4445 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004446 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4447
Dale Johannesen839acbb2009-01-29 00:47:48 +00004448 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004449 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4450 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004451}
4452
Andrew Trickef9de2a2013-05-25 02:42:55 +00004453SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004454 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004455 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004456 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004457 unsigned Alignment,
4458 AtomicOrdering Ordering,
4459 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004460 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4461 Alignment = getEVTAlignment(MemVT);
4462
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004463 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004464 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004465 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004466 // For now, atomics are considered to be volatile always, and they are
4467 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004468 // FIXME: Volatile isn't really correct; we should keep track of atomic
4469 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004470 unsigned Flags = MachineMemOperand::MOVolatile;
4471 if (Opcode != ISD::ATOMIC_STORE)
4472 Flags |= MachineMemOperand::MOLoad;
4473 if (Opcode != ISD::ATOMIC_LOAD)
4474 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004475
4476 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004477 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004478 MemVT.getStoreSize(), Alignment);
4479
Eli Friedmanadec5872011-07-29 03:05:32 +00004480 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4481 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004482}
4483
Andrew Trickef9de2a2013-05-25 02:42:55 +00004484SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004485 SDValue Chain,
4486 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004487 MachineMemOperand *MMO,
4488 AtomicOrdering Ordering,
4489 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004490 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4491 Opcode == ISD::ATOMIC_LOAD_SUB ||
4492 Opcode == ISD::ATOMIC_LOAD_AND ||
4493 Opcode == ISD::ATOMIC_LOAD_OR ||
4494 Opcode == ISD::ATOMIC_LOAD_XOR ||
4495 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004496 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004497 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004498 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004499 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004500 Opcode == ISD::ATOMIC_SWAP ||
4501 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004502 "Invalid Atomic Op");
4503
Owen Anderson53aa7a92009-08-10 22:56:29 +00004504 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004505
Eli Friedman342e8df2011-08-24 20:50:09 +00004506 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4507 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004508 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004509 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004510}
4511
Andrew Trickef9de2a2013-05-25 02:42:55 +00004512SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004513 EVT VT, SDValue Chain,
4514 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004515 MachineMemOperand *MMO,
4516 AtomicOrdering Ordering,
4517 SynchronizationScope SynchScope) {
4518 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4519
4520 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004521 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004522 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004523}
4524
Duncan Sands739a0542008-07-02 17:40:58 +00004525/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004526SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4527 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004528 return Ops[0];
4529
Owen Anderson53aa7a92009-08-10 22:56:29 +00004530 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004531 VTs.reserve(Ops.size());
4532 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004533 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004534 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004535}
4536
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004537SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004538SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004539 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004540 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004541 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004542 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004543 if (Align == 0) // Ensure that codegen never sees alignment 0
4544 Align = getEVTAlignment(MemVT);
4545
4546 MachineFunction &MF = getMachineFunction();
4547 unsigned Flags = 0;
4548 if (WriteMem)
4549 Flags |= MachineMemOperand::MOStore;
4550 if (ReadMem)
4551 Flags |= MachineMemOperand::MOLoad;
4552 if (Vol)
4553 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004554 if (!Size)
4555 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004556 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004557 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004558
Craig Topper206fcd42014-04-26 19:29:41 +00004559 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004560}
4561
4562SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004563SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004564 ArrayRef<SDValue> Ops, EVT MemVT,
4565 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004566 assert((Opcode == ISD::INTRINSIC_VOID ||
4567 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004568 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004569 Opcode == ISD::LIFETIME_START ||
4570 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004571 (Opcode <= INT_MAX &&
4572 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4573 "Opcode is not a memory-accessing opcode!");
4574
Dale Johannesen839acbb2009-01-29 00:47:48 +00004575 // Memoize the node unless it returns a flag.
4576 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004577 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004578 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004579 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004580 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004581 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004582 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4583 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004584 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004585 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004586
Jack Carter170a5f22013-09-09 22:02:08 +00004587 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4588 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004589 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004590 CSEMap.InsertNode(N, IP);
4591 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004592 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4593 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004594 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004595 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004596 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004597 return SDValue(N, 0);
4598}
4599
Chris Lattnerea952f02010-09-21 17:24:05 +00004600/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4601/// MachinePointerInfo record from it. This is particularly useful because the
4602/// code generator has many cases where it doesn't bother passing in a
4603/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4604static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4605 // If this is FI+Offset, we can model it.
4606 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4607 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4608
4609 // If this is (FI+Offset1)+Offset2, we can model it.
4610 if (Ptr.getOpcode() != ISD::ADD ||
4611 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4612 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4613 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004614
Chris Lattnerea952f02010-09-21 17:24:05 +00004615 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4616 return MachinePointerInfo::getFixedStack(FI, Offset+
4617 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4618}
4619
4620/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4621/// MachinePointerInfo record from it. This is particularly useful because the
4622/// code generator has many cases where it doesn't bother passing in a
4623/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4624static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4625 // If the 'Offset' value isn't a constant, we can't handle this.
4626 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4627 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4628 if (OffsetOp.getOpcode() == ISD::UNDEF)
4629 return InferPointerInfo(Ptr);
4630 return MachinePointerInfo();
4631}
Wesley Peck527da1b2010-11-23 03:31:01 +00004632
Chris Lattnerea952f02010-09-21 17:24:05 +00004633
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004634SDValue
4635SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004636 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004637 SDValue Ptr, SDValue Offset,
4638 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004639 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004640 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004641 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004642 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004643 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004644 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4645 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004646
Dan Gohman48b185d2009-09-25 20:36:54 +00004647 unsigned Flags = MachineMemOperand::MOLoad;
4648 if (isVolatile)
4649 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004650 if (isNonTemporal)
4651 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004652 if (isInvariant)
4653 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004654
Chris Lattnerea952f02010-09-21 17:24:05 +00004655 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4656 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004657 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004658 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004659
Chris Lattnerea952f02010-09-21 17:24:05 +00004660 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004661 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004662 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004663 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004664 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004665}
4666
4667SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004668SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004669 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004670 SDValue Ptr, SDValue Offset, EVT MemVT,
4671 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004672 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004673 ExtType = ISD::NON_EXTLOAD;
4674 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004675 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004676 } else {
4677 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004678 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4679 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004680 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004681 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004682 assert(VT.isVector() == MemVT.isVector() &&
4683 "Cannot use trunc store to convert to or from a vector!");
4684 assert((!VT.isVector() ||
4685 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4686 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004687 }
4688
4689 bool Indexed = AM != ISD::UNINDEXED;
4690 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4691 "Unindexed load with an offset!");
4692
4693 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004694 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004695 SDValue Ops[] = { Chain, Ptr, Offset };
4696 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004697 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004698 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004699 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004700 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004701 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004702 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004703 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004704 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4705 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004706 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004707 }
Jack Carter170a5f22013-09-09 22:02:08 +00004708 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4709 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004710 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004711 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004712 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004713 return SDValue(N, 0);
4714}
4715
Andrew Trickef9de2a2013-05-25 02:42:55 +00004716SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004717 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004718 MachinePointerInfo PtrInfo,
4719 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004720 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004721 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004722 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004723 SDValue Undef = getUNDEF(Ptr.getValueType());
4724 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004725 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004726 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004727}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004728
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004729SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4730 SDValue Chain, SDValue Ptr,
4731 MachineMemOperand *MMO) {
4732 SDValue Undef = getUNDEF(Ptr.getValueType());
4733 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4734 VT, MMO);
4735}
4736
Andrew Trickef9de2a2013-05-25 02:42:55 +00004737SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004738 SDValue Chain, SDValue Ptr,
4739 MachinePointerInfo PtrInfo, EVT MemVT,
4740 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004741 bool isInvariant, unsigned Alignment,
4742 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004743 SDValue Undef = getUNDEF(Ptr.getValueType());
4744 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004745 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4746 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004747}
4748
4749
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004750SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4751 SDValue Chain, SDValue Ptr, EVT MemVT,
4752 MachineMemOperand *MMO) {
4753 SDValue Undef = getUNDEF(Ptr.getValueType());
4754 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4755 MemVT, MMO);
4756}
4757
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004758SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004759SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004760 SDValue Offset, ISD::MemIndexedMode AM) {
4761 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4762 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4763 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004764 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004765 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004766 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004767 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004768}
4769
Andrew Trickef9de2a2013-05-25 02:42:55 +00004770SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004771 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004772 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004773 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004774 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004775 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004776 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004777 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004778
Dan Gohman48b185d2009-09-25 20:36:54 +00004779 unsigned Flags = MachineMemOperand::MOStore;
4780 if (isVolatile)
4781 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004782 if (isNonTemporal)
4783 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004784
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004785 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004786 PtrInfo = InferPointerInfo(Ptr);
4787
4788 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004789 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004790 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004791 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004792 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004793
4794 return getStore(Chain, dl, Val, Ptr, MMO);
4795}
4796
Andrew Trickef9de2a2013-05-25 02:42:55 +00004797SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004798 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004799 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004800 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004801 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004802 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004803 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004804 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4805 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004806 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004807 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004808 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004809 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004810 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004811 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004812 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4813 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004814 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004815 }
Jack Carter170a5f22013-09-09 22:02:08 +00004816 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4817 dl.getDebugLoc(), VTs,
4818 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004819 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004820 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004821 return SDValue(N, 0);
4822}
4823
Andrew Trickef9de2a2013-05-25 02:42:55 +00004824SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004825 SDValue Ptr, MachinePointerInfo PtrInfo,
4826 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004827 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004828 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004829 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004830 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004831 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4832 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004833
Dan Gohman48b185d2009-09-25 20:36:54 +00004834 unsigned Flags = MachineMemOperand::MOStore;
4835 if (isVolatile)
4836 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004837 if (isNonTemporal)
4838 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004839
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004840 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004841 PtrInfo = InferPointerInfo(Ptr);
4842
4843 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004844 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004845 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004846 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004847
4848 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4849}
4850
Andrew Trickef9de2a2013-05-25 02:42:55 +00004851SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004852 SDValue Ptr, EVT SVT,
4853 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004854 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004855
Michael Ilsemand5f91512012-09-10 16:56:31 +00004856 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004857 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004858 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004859 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004860
Dan Gohmancecad352009-12-14 23:40:38 +00004861 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4862 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004863 assert(VT.isInteger() == SVT.isInteger() &&
4864 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004865 assert(VT.isVector() == SVT.isVector() &&
4866 "Cannot use trunc store to convert to or from a vector!");
4867 assert((!VT.isVector() ||
4868 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4869 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004870
Owen Anderson9f944592009-08-11 20:47:22 +00004871 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004872 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004873 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4874 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004875 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004876 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004877 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004878 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004879 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004880 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004881 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4882 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004883 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004884 }
Jack Carter170a5f22013-09-09 22:02:08 +00004885 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4886 dl.getDebugLoc(), VTs,
4887 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004888 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004889 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004890 return SDValue(N, 0);
4891}
4892
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004893SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004894SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004895 SDValue Offset, ISD::MemIndexedMode AM) {
4896 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4897 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4898 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004899 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004900 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4901 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004902 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004903 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004904 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004905 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004906 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004907 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004908 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004909
Jack Carter170a5f22013-09-09 22:02:08 +00004910 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4911 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004912 ST->isTruncatingStore(),
4913 ST->getMemoryVT(),
4914 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004915 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004916 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004917 return SDValue(N, 0);
4918}
4919
Andrew Trickef9de2a2013-05-25 02:42:55 +00004920SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004921 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004922 SDValue SV,
4923 unsigned Align) {
4924 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00004925 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004926}
4927
Andrew Trickef9de2a2013-05-25 02:42:55 +00004928SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00004929 ArrayRef<SDUse> Ops) {
4930 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004931 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00004932 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004933 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4934 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004935 default: break;
4936 }
4937
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004938 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004939 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00004940 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00004941 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004942}
4943
Andrew Trickef9de2a2013-05-25 02:42:55 +00004944SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00004945 ArrayRef<SDValue> Ops) {
4946 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00004947 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004948 case 0: return getNode(Opcode, DL, VT);
4949 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4950 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4951 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004952 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004953 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004954
Chris Lattnerb0713c72005-04-09 03:27:28 +00004955 switch (Opcode) {
4956 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004957 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004958 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004959 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4960 "LHS and RHS of condition must have same type!");
4961 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4962 "True and False arms of SelectCC must have same type!");
4963 assert(Ops[2].getValueType() == VT &&
4964 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004965 break;
4966 }
4967 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004968 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004969 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4970 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004971 break;
4972 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004973 }
4974
Chris Lattner566307f2005-05-14 07:42:29 +00004975 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004976 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004977 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004978
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004979 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004980 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004981 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004982 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004983
Bill Wendling022d18f2009-12-18 23:32:53 +00004984 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004985 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004986
Jack Carter170a5f22013-09-09 22:02:08 +00004987 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004988 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004989 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004990 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004991 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004992 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004993 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004994
Chandler Carruth41b20e72014-07-22 04:07:55 +00004995 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004996 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004997}
4998
Andrew Trickef9de2a2013-05-25 02:42:55 +00004999SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005000 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5001 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005002}
5003
Andrew Trickef9de2a2013-05-25 02:42:55 +00005004SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005005 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005006 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005007 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005008
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005009#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005010 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005011 // FIXME: figure out how to safely handle things like
5012 // int foo(int x) { return 1 << (x & 255); }
5013 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005014 case ISD::SRA_PARTS:
5015 case ISD::SRL_PARTS:
5016 case ISD::SHL_PARTS:
5017 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005018 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005019 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005020 else if (N3.getOpcode() == ISD::AND)
5021 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5022 // If the and is only masking out bits that cannot effect the shift,
5023 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005024 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005025 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005026 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005027 }
5028 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005029 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005030#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005031
Chris Lattnerf9c19152005-08-25 19:12:10 +00005032 // Memoize the node unless it returns a flag.
5033 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005034 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005035 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005036 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005037 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005038 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005039 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005040 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005041
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005042 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005043 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5044 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005045 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005046 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5047 DL.getDebugLoc(), VTList, Ops[0],
5048 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005049 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005050 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5051 DL.getDebugLoc(), VTList, Ops[0],
5052 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005053 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005054 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005055 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005056 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005057 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005058 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005059 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005060 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5061 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005062 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005063 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5064 DL.getDebugLoc(), VTList, Ops[0],
5065 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005066 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005067 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5068 DL.getDebugLoc(), VTList, Ops[0],
5069 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005070 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005071 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005072 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005073 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005074 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005075 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005076 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005077}
5078
Andrew Trickef9de2a2013-05-25 02:42:55 +00005079SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005080 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005081}
5082
Andrew Trickef9de2a2013-05-25 02:42:55 +00005083SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005084 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005085 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005086 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005087}
5088
Andrew Trickef9de2a2013-05-25 02:42:55 +00005089SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005090 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005091 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005092 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005093}
5094
Andrew Trickef9de2a2013-05-25 02:42:55 +00005095SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005096 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005097 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005098 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005099}
5100
Andrew Trickef9de2a2013-05-25 02:42:55 +00005101SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005102 SDValue N1, SDValue N2, SDValue N3,
5103 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005104 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005105 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005106}
5107
Andrew Trickef9de2a2013-05-25 02:42:55 +00005108SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005109 SDValue N1, SDValue N2, SDValue N3,
5110 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005111 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005112 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005113}
5114
Owen Anderson53aa7a92009-08-10 22:56:29 +00005115SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005116 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005117}
5118
Owen Anderson53aa7a92009-08-10 22:56:29 +00005119SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005120 FoldingSetNodeID ID;
5121 ID.AddInteger(2U);
5122 ID.AddInteger(VT1.getRawBits());
5123 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005124
Craig Topperc0196b12014-04-14 00:51:57 +00005125 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005126 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005127 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005128 EVT *Array = Allocator.Allocate<EVT>(2);
5129 Array[0] = VT1;
5130 Array[1] = VT2;
5131 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5132 VTListMap.InsertNode(Result, IP);
5133 }
5134 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005135}
Dan Gohman17059682008-07-17 19:10:17 +00005136
Owen Anderson53aa7a92009-08-10 22:56:29 +00005137SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005138 FoldingSetNodeID ID;
5139 ID.AddInteger(3U);
5140 ID.AddInteger(VT1.getRawBits());
5141 ID.AddInteger(VT2.getRawBits());
5142 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005143
Craig Topperc0196b12014-04-14 00:51:57 +00005144 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005145 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005146 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005147 EVT *Array = Allocator.Allocate<EVT>(3);
5148 Array[0] = VT1;
5149 Array[1] = VT2;
5150 Array[2] = VT3;
5151 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5152 VTListMap.InsertNode(Result, IP);
5153 }
5154 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005155}
5156
Owen Anderson53aa7a92009-08-10 22:56:29 +00005157SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005158 FoldingSetNodeID ID;
5159 ID.AddInteger(4U);
5160 ID.AddInteger(VT1.getRawBits());
5161 ID.AddInteger(VT2.getRawBits());
5162 ID.AddInteger(VT3.getRawBits());
5163 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005164
Craig Topperc0196b12014-04-14 00:51:57 +00005165 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005166 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005167 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005168 EVT *Array = Allocator.Allocate<EVT>(4);
5169 Array[0] = VT1;
5170 Array[1] = VT2;
5171 Array[2] = VT3;
5172 Array[3] = VT4;
5173 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5174 VTListMap.InsertNode(Result, IP);
5175 }
5176 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005177}
5178
Craig Topperabb4ac72014-04-16 06:10:51 +00005179SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5180 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005181 FoldingSetNodeID ID;
5182 ID.AddInteger(NumVTs);
5183 for (unsigned index = 0; index < NumVTs; index++) {
5184 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005185 }
5186
Craig Topperc0196b12014-04-14 00:51:57 +00005187 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005188 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005189 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005190 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005191 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005192 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5193 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005194 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005195 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005196}
5197
5198
Chris Lattnerf34156e2006-01-28 09:32:45 +00005199/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5200/// specified operands. If the resultant node already exists in the DAG,
5201/// this does not modify the specified node, instead it returns the node that
5202/// already exists. If the resultant node does not exist in the DAG, the
5203/// input node is returned. As a degenerate case, if you specify the same
5204/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005205SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005206 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005207
Chris Lattnerf34156e2006-01-28 09:32:45 +00005208 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005209 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005210
Chris Lattnerf34156e2006-01-28 09:32:45 +00005211 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005212 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005213 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005214 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005215
Dan Gohmanebeccb42008-07-21 22:38:59 +00005216 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005217 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005218 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005219 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005220
Chris Lattnerf34156e2006-01-28 09:32:45 +00005221 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005222 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005223
Chris Lattnerf34156e2006-01-28 09:32:45 +00005224 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005225 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005226 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005227}
5228
Dan Gohman92c11ac2010-06-18 15:30:29 +00005229SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005230 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005231
Chris Lattnerf34156e2006-01-28 09:32:45 +00005232 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005233 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005234 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005235
Chris Lattnerf34156e2006-01-28 09:32:45 +00005236 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005237 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005238 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005239 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005240
Dan Gohmanebeccb42008-07-21 22:38:59 +00005241 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005242 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005243 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005244 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005245
Chris Lattnerf34156e2006-01-28 09:32:45 +00005246 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005247 if (N->OperandList[0] != Op1)
5248 N->OperandList[0].set(Op1);
5249 if (N->OperandList[1] != Op2)
5250 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005251
Chris Lattnerf34156e2006-01-28 09:32:45 +00005252 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005253 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005254 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005255}
5256
Dan Gohman92c11ac2010-06-18 15:30:29 +00005257SDNode *SelectionDAG::
5258UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005259 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005260 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005261}
5262
Dan Gohman92c11ac2010-06-18 15:30:29 +00005263SDNode *SelectionDAG::
5264UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005265 SDValue Op3, SDValue Op4) {
5266 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005267 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005268}
5269
Dan Gohman92c11ac2010-06-18 15:30:29 +00005270SDNode *SelectionDAG::
5271UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005272 SDValue Op3, SDValue Op4, SDValue Op5) {
5273 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005274 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005275}
5276
Dan Gohman92c11ac2010-06-18 15:30:29 +00005277SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005278UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5279 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005280 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005281 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005282
Chris Lattnerf34156e2006-01-28 09:32:45 +00005283 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005284 bool AnyChange = false;
5285 for (unsigned i = 0; i != NumOps; ++i) {
5286 if (Ops[i] != N->getOperand(i)) {
5287 AnyChange = true;
5288 break;
5289 }
5290 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005291
Chris Lattnerf34156e2006-01-28 09:32:45 +00005292 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005293 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005294
Chris Lattnerf34156e2006-01-28 09:32:45 +00005295 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005296 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005297 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005298 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005299
Dan Gohman2f83b472008-05-02 00:05:03 +00005300 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005301 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005302 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005303 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005304
Chris Lattnerf34156e2006-01-28 09:32:45 +00005305 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005306 for (unsigned i = 0; i != NumOps; ++i)
5307 if (N->OperandList[i] != Ops[i])
5308 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005309
5310 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005311 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005312 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005313}
5314
Dan Gohman91697632008-07-07 20:57:48 +00005315/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005316/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005317void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005318 // Unlike the code in MorphNodeTo that does this, we don't need to
5319 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005320 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5321 SDUse &Use = *I++;
5322 Use.set(SDValue());
5323 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005324}
Chris Lattner19732782005-08-16 18:17:10 +00005325
Dan Gohman17059682008-07-17 19:10:17 +00005326/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5327/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005328///
Dan Gohman17059682008-07-17 19:10:17 +00005329SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005330 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005331 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005332 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005333}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005334
Dan Gohman17059682008-07-17 19:10:17 +00005335SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005336 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005337 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005338 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005339 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005340}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005341
Dan Gohman17059682008-07-17 19:10:17 +00005342SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005343 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005344 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005345 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005346 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005347 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005348}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005349
Dan Gohman17059682008-07-17 19:10:17 +00005350SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005351 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005352 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005353 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005354 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005355 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005356}
Chris Lattner466fece2005-08-21 22:30:30 +00005357
Dan Gohman17059682008-07-17 19:10:17 +00005358SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005359 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005360 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005361 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005362}
5363
Dan Gohman17059682008-07-17 19:10:17 +00005364SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005365 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005366 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005367 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005368}
5369
Dan Gohman17059682008-07-17 19:10:17 +00005370SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005371 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005372 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005373 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005374}
5375
Dan Gohman17059682008-07-17 19:10:17 +00005376SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005377 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005378 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005379 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005380 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005381}
5382
Bill Wendling2d598632008-12-01 23:28:22 +00005383SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005384 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005385 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005386 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005387 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005388}
5389
Scott Michelcf0da6c2009-02-17 22:15:04 +00005390SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005391 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005392 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005393 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005394 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005395 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005396}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005397
Scott Michelcf0da6c2009-02-17 22:15:04 +00005398SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005399 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005400 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005401 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005402 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005403 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005404}
5405
Dan Gohman17059682008-07-17 19:10:17 +00005406SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005407 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005408 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005409 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005410 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005411 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005412 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005413}
5414
Dan Gohman17059682008-07-17 19:10:17 +00005415SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005416 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005417 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005418 SDValue Op3) {
5419 SDVTList VTs = getVTList(VT1, VT2, VT3);
5420 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005421 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005422}
5423
5424SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005425 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005426 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005427 // Reset the NodeID to -1.
5428 N->setNodeId(-1);
5429 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005430}
5431
Andrew Trickef9de2a2013-05-25 02:42:55 +00005432/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005433/// the line number information on the merged node since it is not possible to
5434/// preserve the information that operation is associated with multiple lines.
5435/// This will make the debugger working better at -O0, were there is a higher
5436/// probability having other instructions associated with that line.
5437///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005438/// For IROrder, we keep the smaller of the two
5439SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005440 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005441 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5442 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005443 N->setDebugLoc(DebugLoc());
5444 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005445 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5446 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005447 return N;
5448}
5449
Chris Lattneraf197502010-02-28 21:36:14 +00005450/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005451/// return type, opcode, and operands.
5452///
5453/// Note that MorphNodeTo returns the resultant node. If there is already a
5454/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005455/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005456///
5457/// Using MorphNodeTo is faster than creating a new node and swapping it in
5458/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005459/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005460/// the node's users.
5461///
Chandler Carruth356665a2014-08-01 22:09:43 +00005462/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5463/// As a consequence it isn't appropriate to use from within the DAG combiner or
5464/// the legalizer which maintain worklists that would need to be updated when
5465/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005466SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005467 SDVTList VTs, ArrayRef<SDValue> Ops) {
5468 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005469 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005470 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005471 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005472 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005473 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005474 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005475 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005476 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005477
Dan Gohmand3fe1742008-09-13 01:54:27 +00005478 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005479 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005480
Dan Gohman17059682008-07-17 19:10:17 +00005481 // Start the morphing.
5482 N->NodeType = Opc;
5483 N->ValueList = VTs.VTs;
5484 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005485
Dan Gohman17059682008-07-17 19:10:17 +00005486 // Clear the operands list, updating used nodes to remove this from their
5487 // use list. Keep track of any operands that become dead as a result.
5488 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005489 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5490 SDUse &Use = *I++;
5491 SDNode *Used = Use.getNode();
5492 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005493 if (Used->use_empty())
5494 DeadNodeSet.insert(Used);
5495 }
5496
Dan Gohman48b185d2009-09-25 20:36:54 +00005497 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5498 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005499 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005500 // If NumOps is larger than the # of operands we can have in a
5501 // MachineSDNode, reallocate the operand list.
5502 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5503 if (MN->OperandsNeedDelete)
5504 delete[] MN->OperandList;
5505 if (NumOps > array_lengthof(MN->LocalOperands))
5506 // We're creating a final node that will live unmorphed for the
5507 // remainder of the current SelectionDAG iteration, so we can allocate
5508 // the operands directly out of a pool with no recycling metadata.
5509 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005510 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005511 else
Craig Topper131de822014-04-27 19:21:16 +00005512 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005513 MN->OperandsNeedDelete = false;
5514 } else
Craig Topper131de822014-04-27 19:21:16 +00005515 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005516 } else {
5517 // If NumOps is larger than the # of operands we currently have, reallocate
5518 // the operand list.
5519 if (NumOps > N->NumOperands) {
5520 if (N->OperandsNeedDelete)
5521 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005522 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005523 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005524 } else
Craig Topper131de822014-04-27 19:21:16 +00005525 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005526 }
5527
5528 // Delete any nodes that are still dead after adding the uses for the
5529 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005530 if (!DeadNodeSet.empty()) {
5531 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005532 for (SDNode *N : DeadNodeSet)
5533 if (N->use_empty())
5534 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005535 RemoveDeadNodes(DeadNodes);
5536 }
Dan Gohman91697632008-07-07 20:57:48 +00005537
Dan Gohman17059682008-07-17 19:10:17 +00005538 if (IP)
5539 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005540 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005541}
5542
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005543
Dan Gohman32f71d72009-09-25 18:54:59 +00005544/// getMachineNode - These are used for target selectors to create a new node
5545/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005546///
Dan Gohman32f71d72009-09-25 18:54:59 +00005547/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005548/// node of the specified opcode and operands, it returns that node instead of
5549/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005550MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005551SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005552 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005553 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005554}
Bill Wendlinga434d932009-01-29 09:01:55 +00005555
Dan Gohmana22f2d82009-10-10 01:29:16 +00005556MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005557SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005558 SDVTList VTs = getVTList(VT);
5559 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005560 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005561}
Bill Wendlinga434d932009-01-29 09:01:55 +00005562
Dan Gohmana22f2d82009-10-10 01:29:16 +00005563MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005564SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005565 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005566 SDVTList VTs = getVTList(VT);
5567 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005568 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005569}
Bill Wendlinga434d932009-01-29 09:01:55 +00005570
Dan Gohmana22f2d82009-10-10 01:29:16 +00005571MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005572SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005573 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005574 SDVTList VTs = getVTList(VT);
5575 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005576 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005577}
Bill Wendlinga434d932009-01-29 09:01:55 +00005578
Dan Gohmana22f2d82009-10-10 01:29:16 +00005579MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005580SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005581 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005582 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005583 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005584}
Bill Wendlinga434d932009-01-29 09:01:55 +00005585
Dan Gohmana22f2d82009-10-10 01:29:16 +00005586MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005587SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005588 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005589 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005590}
Bill Wendlinga434d932009-01-29 09:01:55 +00005591
Dan Gohmana22f2d82009-10-10 01:29:16 +00005592MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005593SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005594 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005595 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005596 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005597 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005598}
Bill Wendlinga434d932009-01-29 09:01:55 +00005599
Dan Gohmana22f2d82009-10-10 01:29:16 +00005600MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005601SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005602 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005603 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005604 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005605 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005606}
5607
Dan Gohmana22f2d82009-10-10 01:29:16 +00005608MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005609SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005610 EVT VT1, EVT VT2, SDValue Op1,
5611 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005612 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005613 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005614 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005615}
5616
Dan Gohmana22f2d82009-10-10 01:29:16 +00005617MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005618SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005619 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005620 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005621 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005622 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005623}
Bill Wendlinga434d932009-01-29 09:01:55 +00005624
Dan Gohmana22f2d82009-10-10 01:29:16 +00005625MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005626SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005627 EVT VT1, EVT VT2, EVT VT3,
5628 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005629 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005630 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005631 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005632}
Bill Wendlinga434d932009-01-29 09:01:55 +00005633
Dan Gohmana22f2d82009-10-10 01:29:16 +00005634MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005635SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005636 EVT VT1, EVT VT2, EVT VT3,
5637 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005638 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005639 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005640 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005641}
Bill Wendlinga434d932009-01-29 09:01:55 +00005642
Dan Gohmana22f2d82009-10-10 01:29:16 +00005643MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005644SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005645 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005646 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005647 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005648 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005649}
Bill Wendlinga434d932009-01-29 09:01:55 +00005650
Dan Gohmana22f2d82009-10-10 01:29:16 +00005651MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005652SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005653 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005654 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005655 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005656 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005657}
5658
Dan Gohmana22f2d82009-10-10 01:29:16 +00005659MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005660SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005661 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005662 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005663 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005664 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005665}
5666
Dan Gohmana22f2d82009-10-10 01:29:16 +00005667MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005668SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005669 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005670 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005671 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005672 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005673 const SDValue *Ops = OpsArray.data();
5674 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005675
5676 if (DoCSE) {
5677 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005678 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005679 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005680 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005681 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005682 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005683 }
5684
5685 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005686 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5687 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005688
5689 // Initialize the operands list.
5690 if (NumOps > array_lengthof(N->LocalOperands))
5691 // We're creating a final node that will live unmorphed for the
5692 // remainder of the current SelectionDAG iteration, so we can allocate
5693 // the operands directly out of a pool with no recycling metadata.
5694 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5695 Ops, NumOps);
5696 else
5697 N->InitOperands(N->LocalOperands, Ops, NumOps);
5698 N->OperandsNeedDelete = false;
5699
5700 if (DoCSE)
5701 CSEMap.InsertNode(N, IP);
5702
Chandler Carruth41b20e72014-07-22 04:07:55 +00005703 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005704 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005705}
Evan Chengd3f1db92006-02-09 07:15:23 +00005706
Dan Gohmanac33a902009-08-19 18:16:17 +00005707/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005708/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005709SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005710SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005711 SDValue Operand) {
5712 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005713 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005714 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005715 return SDValue(Subreg, 0);
5716}
5717
Bob Wilson2a45a652009-10-08 18:49:46 +00005718/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005719/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005720SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005721SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005722 SDValue Operand, SDValue Subreg) {
5723 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005724 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005725 VT, Operand, Subreg, SRIdxVal);
5726 return SDValue(Result, 0);
5727}
5728
Evan Cheng31604a62008-03-22 01:55:50 +00005729/// getNodeIfExists - Get the specified node if it's already available, or
5730/// else return NULL.
5731SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005732 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5733 bool exact) {
5734 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005735 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005736 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005737 if (isBinOpWithFlags(Opcode))
5738 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005739 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005740 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005741 return E;
5742 }
Craig Topperc0196b12014-04-14 00:51:57 +00005743 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005744}
5745
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005746/// getDbgValue - Creates a SDDbgValue node.
5747///
Adrian Prantl32da8892014-04-25 20:49:25 +00005748/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005749SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5750 unsigned R, bool IsIndirect, uint64_t Off,
5751 DebugLoc DL, unsigned O) {
5752 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005753}
5754
Adrian Prantl32da8892014-04-25 20:49:25 +00005755/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005756SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5757 const Value *C, uint64_t Off,
5758 DebugLoc DL, unsigned O) {
5759 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005760}
5761
Adrian Prantl32da8892014-04-25 20:49:25 +00005762/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005763SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
5764 unsigned FI, uint64_t Off,
5765 DebugLoc DL, unsigned O) {
5766 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005767}
5768
Dan Gohman7d099f72010-03-03 21:33:37 +00005769namespace {
5770
Dan Gohman9cc886b2010-03-04 19:11:28 +00005771/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005772/// pointed to by a use iterator is deleted, increment the use iterator
5773/// so that it doesn't dangle.
5774///
Dan Gohman7d099f72010-03-03 21:33:37 +00005775class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005776 SDNode::use_iterator &UI;
5777 SDNode::use_iterator &UE;
5778
Craig Topper7b883b32014-03-08 06:31:39 +00005779 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005780 // Increment the iterator as needed.
5781 while (UI != UE && N == *UI)
5782 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005783 }
5784
5785public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005786 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005787 SDNode::use_iterator &ui,
5788 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005789 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005790};
5791
5792}
5793
Evan Cheng445b91a2006-08-07 22:13:29 +00005794/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005795/// This can cause recursive merging of nodes in the DAG.
5796///
Chris Lattner76858912008-02-03 03:35:22 +00005797/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005798///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005799void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005800 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005801 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005802 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005803 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005804
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005805 // Iterate over all the existing uses of From. New uses will be added
5806 // to the beginning of the use list, which we avoid visiting.
5807 // This specifically avoids visiting uses of From that arise while the
5808 // replacement is happening, because any such uses would be the result
5809 // of CSE: If an existing node looks like From after one of its operands
5810 // is replaced by To, we don't want to replace of all its users with To
5811 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005812 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005813 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005814 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005815 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005816
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005817 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005818 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005819
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005820 // A user can appear in a use list multiple times, and when this
5821 // happens the uses are usually next to each other in the list.
5822 // To help reduce the number of CSE recomputations, process all
5823 // the uses of this user that we can find this way.
5824 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005825 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005826 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005827 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005828 } while (UI != UE && *UI == User);
5829
5830 // Now that we have modified User, add it back to the CSE maps. If it
5831 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005832 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005833 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005834
5835 // If we just RAUW'd the root, take note.
5836 if (FromN == getRoot())
5837 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005838}
5839
5840/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5841/// This can cause recursive merging of nodes in the DAG.
5842///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005843/// This version assumes that for each value of From, there is a
5844/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005845///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005846void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005847#ifndef NDEBUG
5848 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5849 assert((!From->hasAnyUseOfValue(i) ||
5850 From->getValueType(i) == To->getValueType(i)) &&
5851 "Cannot use this version of ReplaceAllUsesWith!");
5852#endif
Dan Gohman17059682008-07-17 19:10:17 +00005853
5854 // Handle the trivial case.
5855 if (From == To)
5856 return;
5857
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005858 // Iterate over just the existing users of From. See the comments in
5859 // the ReplaceAllUsesWith above.
5860 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005861 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005862 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005863 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005864
Chris Lattner373f0482005-08-26 18:36:28 +00005865 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005866 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005867
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005868 // A user can appear in a use list multiple times, and when this
5869 // happens the uses are usually next to each other in the list.
5870 // To help reduce the number of CSE recomputations, process all
5871 // the uses of this user that we can find this way.
5872 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005873 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005874 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005875 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005876 } while (UI != UE && *UI == User);
5877
5878 // Now that we have modified User, add it back to the CSE maps. If it
5879 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005880 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005881 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005882
5883 // If we just RAUW'd the root, take note.
5884 if (From == getRoot().getNode())
5885 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005886}
5887
Chris Lattner373f0482005-08-26 18:36:28 +00005888/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5889/// This can cause recursive merging of nodes in the DAG.
5890///
5891/// This version can replace From with any result values. To must match the
5892/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005893void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005894 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005895 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005896
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005897 // Iterate over just the existing users of From. See the comments in
5898 // the ReplaceAllUsesWith above.
5899 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005900 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005901 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005902 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005903
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005904 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005905 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005906
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005907 // A user can appear in a use list multiple times, and when this
5908 // happens the uses are usually next to each other in the list.
5909 // To help reduce the number of CSE recomputations, process all
5910 // the uses of this user that we can find this way.
5911 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005912 SDUse &Use = UI.getUse();
5913 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005914 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005915 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005916 } while (UI != UE && *UI == User);
5917
5918 // Now that we have modified User, add it back to the CSE maps. If it
5919 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005920 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005921 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005922
5923 // If we just RAUW'd the root, take note.
5924 if (From == getRoot().getNode())
5925 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005926}
5927
Chris Lattner375e1a72006-02-17 21:58:01 +00005928/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005929/// uses of other values produced by From.getNode() alone. The Deleted
5930/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005931void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005932 // Handle the really simple, really trivial case efficiently.
5933 if (From == To) return;
5934
Chris Lattner375e1a72006-02-17 21:58:01 +00005935 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005936 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005937 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005938 return;
5939 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005940
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005941 // Iterate over just the existing users of From. See the comments in
5942 // the ReplaceAllUsesWith above.
5943 SDNode::use_iterator UI = From.getNode()->use_begin(),
5944 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005945 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005946 while (UI != UE) {
5947 SDNode *User = *UI;
5948 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005949
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005950 // A user can appear in a use list multiple times, and when this
5951 // happens the uses are usually next to each other in the list.
5952 // To help reduce the number of CSE recomputations, process all
5953 // the uses of this user that we can find this way.
5954 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005955 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005956
5957 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005958 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005959 ++UI;
5960 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005961 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005962
5963 // If this node hasn't been modified yet, it's still in the CSE maps,
5964 // so remove its old self from the CSE maps.
5965 if (!UserRemovedFromCSEMaps) {
5966 RemoveNodeFromCSEMaps(User);
5967 UserRemovedFromCSEMaps = true;
5968 }
5969
5970 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005971 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005972 } while (UI != UE && *UI == User);
5973
5974 // We are iterating over all uses of the From node, so if a use
5975 // doesn't use the specific value, no changes are made.
5976 if (!UserRemovedFromCSEMaps)
5977 continue;
5978
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005979 // Now that we have modified User, add it back to the CSE maps. If it
5980 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005981 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005982 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005983
5984 // If we just RAUW'd the root, take note.
5985 if (From == getRoot())
5986 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005987}
5988
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005989namespace {
5990 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5991 /// to record information about a use.
5992 struct UseMemo {
5993 SDNode *User;
5994 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005995 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005996 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005997
5998 /// operator< - Sort Memos by User.
5999 bool operator<(const UseMemo &L, const UseMemo &R) {
6000 return (intptr_t)L.User < (intptr_t)R.User;
6001 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006002}
6003
Dan Gohman17059682008-07-17 19:10:17 +00006004/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006005/// uses of other values produced by From.getNode() alone. The same value
6006/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006007/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006008void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6009 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006010 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006011 // Handle the simple, trivial case efficiently.
6012 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006013 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006014
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006015 // Read up all the uses and make records of them. This helps
6016 // processing new uses that are introduced during the
6017 // replacement process.
6018 SmallVector<UseMemo, 4> Uses;
6019 for (unsigned i = 0; i != Num; ++i) {
6020 unsigned FromResNo = From[i].getResNo();
6021 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006022 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006023 E = FromNode->use_end(); UI != E; ++UI) {
6024 SDUse &Use = UI.getUse();
6025 if (Use.getResNo() == FromResNo) {
6026 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006027 Uses.push_back(Memo);
6028 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006029 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006030 }
Dan Gohman17059682008-07-17 19:10:17 +00006031
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006032 // Sort the uses, so that all the uses from a given User are together.
6033 std::sort(Uses.begin(), Uses.end());
6034
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006035 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6036 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006037 // We know that this user uses some value of From. If it is the right
6038 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006039 SDNode *User = Uses[UseIndex].User;
6040
6041 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006042 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006043
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006044 // The Uses array is sorted, so all the uses for a given User
6045 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006046 // To help reduce the number of CSE recomputations, process all
6047 // the uses of this user that we can find this way.
6048 do {
6049 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006050 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006051 ++UseIndex;
6052
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006053 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006054 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6055
Dan Gohman17059682008-07-17 19:10:17 +00006056 // Now that we have modified User, add it back to the CSE maps. If it
6057 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006058 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006059 }
6060}
6061
Evan Cheng9631a602006-08-01 08:20:41 +00006062/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006063/// based on their topological order. It returns the maximum id and a vector
6064/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006065unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006066
Dan Gohman86aa16a2008-09-30 18:30:35 +00006067 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006068
Dan Gohman86aa16a2008-09-30 18:30:35 +00006069 // SortedPos tracks the progress of the algorithm. Nodes before it are
6070 // sorted, nodes after it are unsorted. When the algorithm completes
6071 // it is at the end of the list.
6072 allnodes_iterator SortedPos = allnodes_begin();
6073
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006074 // Visit all the nodes. Move nodes with no operands to the front of
6075 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006076 // operand count. Before we do this, the Node Id fields of the nodes
6077 // may contain arbitrary values. After, the Node Id fields for nodes
6078 // before SortedPos will contain the topological sort index, and the
6079 // Node Id fields for nodes At SortedPos and after will contain the
6080 // count of outstanding operands.
6081 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6082 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006083 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006084 unsigned Degree = N->getNumOperands();
6085 if (Degree == 0) {
6086 // A node with no uses, add it to the result array immediately.
6087 N->setNodeId(DAGSize++);
6088 allnodes_iterator Q = N;
6089 if (Q != SortedPos)
6090 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006091 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006092 ++SortedPos;
6093 } else {
6094 // Temporarily use the Node Id as scratch space for the degree count.
6095 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006096 }
6097 }
6098
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006099 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006100 // such that by the time the end is reached all nodes will be sorted.
6101 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6102 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006103 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006104 // N is in sorted position, so all its uses have one less operand
6105 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006106 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6107 UI != UE; ++UI) {
6108 SDNode *P = *UI;
6109 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006110 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006111 --Degree;
6112 if (Degree == 0) {
6113 // All of P's operands are sorted, so P may sorted now.
6114 P->setNodeId(DAGSize++);
6115 if (P != SortedPos)
6116 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006117 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006118 ++SortedPos;
6119 } else {
6120 // Update P's outstanding operand count.
6121 P->setNodeId(Degree);
6122 }
6123 }
David Greene3b2a68c2010-01-20 00:59:23 +00006124 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006125#ifndef NDEBUG
6126 SDNode *S = ++I;
6127 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006128 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006129 dbgs() << "Checking if this is due to cycles\n";
6130 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006131#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006132 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006133 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006134 }
6135
6136 assert(SortedPos == AllNodes.end() &&
6137 "Topological sort incomplete!");
6138 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6139 "First node in topological sort is not the entry token!");
6140 assert(AllNodes.front().getNodeId() == 0 &&
6141 "First node in topological sort has non-zero id!");
6142 assert(AllNodes.front().getNumOperands() == 0 &&
6143 "First node in topological sort has operands!");
6144 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6145 "Last node in topologic sort has unexpected id!");
6146 assert(AllNodes.back().use_empty() &&
6147 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006148 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006149 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006150}
6151
Evan Cheng563fe3c2010-03-25 01:38:16 +00006152/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6153/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006154void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6155 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006156 if (SD)
6157 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006158}
Evan Cheng29eefc12006-07-27 06:39:06 +00006159
Devang Patelefc6b162011-01-25 23:27:42 +00006160/// TransferDbgValues - Transfer SDDbgValues.
6161void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6162 if (From == To || !From.getNode()->getHasDebugValue())
6163 return;
6164 SDNode *FromNode = From.getNode();
6165 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006166 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006167 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006168 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006169 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006170 SDDbgValue *Dbg = *I;
6171 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006172 SDDbgValue *Clone =
6173 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6174 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6175 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006176 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006177 }
6178 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006179 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006180 E = ClonedDVs.end(); I != E; ++I)
6181 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006182}
6183
Jim Laskeyd66e6162005-08-17 20:08:02 +00006184//===----------------------------------------------------------------------===//
6185// SDNode Class
6186//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006187
Chris Lattner3bf17b62007-02-04 02:41:42 +00006188HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006189 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006190}
6191
Andrew Trickef9de2a2013-05-25 02:42:55 +00006192GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6193 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006194 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006195 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006196 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006197}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006198
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006199AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6200 SDValue X, unsigned SrcAS,
6201 unsigned DestAS)
6202 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6203 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6204
Andrew Trickef9de2a2013-05-25 02:42:55 +00006205MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6206 EVT memvt, MachineMemOperand *mmo)
6207 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006208 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006209 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006210 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006211 assert(isNonTemporal() == MMO->isNonTemporal() &&
6212 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006213 // We check here that the size of the memory operand fits within the size of
6214 // the MMO. This is because the MMO might indicate only a possible address
6215 // range instead of specifying the affected memory addresses precisely.
6216 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006217}
6218
Andrew Trickef9de2a2013-05-25 02:42:55 +00006219MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006220 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6221 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006222 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006223 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006224 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006225 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006226 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006227}
6228
Jim Laskeyf576b422006-10-27 23:46:08 +00006229/// Profile - Gather unique data for the node.
6230///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006231void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006232 AddNodeIDNode(ID, this);
6233}
6234
Owen Anderson3b1665e2009-08-25 22:27:22 +00006235namespace {
6236 struct EVTArray {
6237 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006238
Owen Anderson3b1665e2009-08-25 22:27:22 +00006239 EVTArray() {
6240 VTs.reserve(MVT::LAST_VALUETYPE);
6241 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6242 VTs.push_back(MVT((MVT::SimpleValueType)i));
6243 }
6244 };
6245}
6246
Owen Anderson53aa7a92009-08-10 22:56:29 +00006247static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006248static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006249static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006250
Chris Lattner88fa11c2005-11-08 23:30:28 +00006251/// getValueTypeList - Return a pointer to the specified value type.
6252///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006253const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006254 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006255 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006256 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006257 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006258 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006259 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006260 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006261 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006262}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006263
Chris Lattner40e79822005-01-12 18:37:47 +00006264/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6265/// indicated value. This method ignores uses of other values defined by this
6266/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006267bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006268 assert(Value < getNumValues() && "Bad value!");
6269
Roman Levenstein51f532f2008-04-07 10:06:32 +00006270 // TODO: Only iterate over uses of a given value of the node
6271 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006272 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006273 if (NUses == 0)
6274 return false;
6275 --NUses;
6276 }
Chris Lattner40e79822005-01-12 18:37:47 +00006277 }
6278
6279 // Found exactly the right number of uses?
6280 return NUses == 0;
6281}
6282
6283
Evan Cheng358c3d12007-08-02 05:29:38 +00006284/// hasAnyUseOfValue - Return true if there are any use of the indicated
6285/// value. This method ignores uses of other values defined by this operation.
6286bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6287 assert(Value < getNumValues() && "Bad value!");
6288
Dan Gohman7a510c22008-07-09 22:39:01 +00006289 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006290 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006291 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006292
6293 return false;
6294}
6295
6296
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006297/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006298///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006299bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006300 bool Seen = false;
6301 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006302 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006303 if (User == this)
6304 Seen = true;
6305 else
6306 return false;
6307 }
6308
6309 return Seen;
6310}
6311
Evan Cheng9456dd82006-11-03 07:31:32 +00006312/// isOperand - Return true if this node is an operand of N.
6313///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006314bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006315 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6316 if (*this == N->getOperand(i))
6317 return true;
6318 return false;
6319}
6320
Evan Cheng567d2e52008-03-04 00:41:45 +00006321bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006322 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006323 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006324 return true;
6325 return false;
6326}
Evan Chengd37645c2006-02-05 06:29:23 +00006327
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006328/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006329/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006330/// side-effecting instructions on any chain path. In practice, this looks
6331/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006332/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006333bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006334 unsigned Depth) const {
6335 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006336
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006337 // Don't search too deeply, we just want to be able to see through
6338 // TokenFactor's etc.
6339 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006340
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006341 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006342 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006343 if (getOpcode() == ISD::TokenFactor) {
6344 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006345 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6346 return false;
6347 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006348 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006349
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006350 // Loads don't have side effects, look through them.
6351 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6352 if (!Ld->isVolatile())
6353 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6354 }
6355 return false;
6356}
6357
Lang Hames5a004992011-07-07 04:31:51 +00006358/// hasPredecessor - Return true if N is a predecessor of this node.
6359/// N is either an operand of this node, or can be reached by recursively
6360/// traversing up the operands.
6361/// NOTE: This is an expensive method. Use it carefully.
6362bool SDNode::hasPredecessor(const SDNode *N) const {
6363 SmallPtrSet<const SDNode *, 32> Visited;
6364 SmallVector<const SDNode *, 16> Worklist;
6365 return hasPredecessorHelper(N, Visited, Worklist);
6366}
Dan Gohmancd139c02009-10-28 03:44:30 +00006367
Craig Topperb94011f2013-07-14 04:42:23 +00006368bool
6369SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006370 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006371 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006372 if (Visited.empty()) {
6373 Worklist.push_back(this);
6374 } else {
6375 // Take a look in the visited set. If we've already encountered this node
6376 // we needn't search further.
6377 if (Visited.count(N))
6378 return true;
6379 }
6380
6381 // Haven't visited N yet. Continue the search.
6382 while (!Worklist.empty()) {
6383 const SDNode *M = Worklist.pop_back_val();
6384 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6385 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006386 if (Visited.insert(Op))
6387 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006388 if (Op == N)
6389 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006390 }
Lang Hames5a004992011-07-07 04:31:51 +00006391 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006392
6393 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006394}
6395
Evan Cheng5d9fd972006-10-04 00:56:09 +00006396uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6397 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006398 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006399}
6400
Mon P Wang32f8bb92009-11-30 02:42:02 +00006401SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6402 assert(N->getNumValues() == 1 &&
6403 "Can't unroll a vector with multiple results!");
6404
6405 EVT VT = N->getValueType(0);
6406 unsigned NE = VT.getVectorNumElements();
6407 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006408 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006409
6410 SmallVector<SDValue, 8> Scalars;
6411 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6412
6413 // If ResNE is 0, fully unroll the vector op.
6414 if (ResNE == 0)
6415 ResNE = NE;
6416 else if (NE > ResNE)
6417 NE = ResNE;
6418
6419 unsigned i;
6420 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006421 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006422 SDValue Operand = N->getOperand(j);
6423 EVT OperandVT = Operand.getValueType();
6424 if (OperandVT.isVector()) {
6425 // A vector operand; extract a single element.
6426 EVT OperandEltVT = OperandVT.getVectorElementType();
6427 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6428 OperandEltVT,
6429 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006430 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006431 } else {
6432 // A scalar operand; just use it as is.
6433 Operands[j] = Operand;
6434 }
6435 }
6436
6437 switch (N->getOpcode()) {
6438 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006439 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006440 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006441 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006442 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006443 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006444 case ISD::SHL:
6445 case ISD::SRA:
6446 case ISD::SRL:
6447 case ISD::ROTL:
6448 case ISD::ROTR:
6449 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006450 getShiftAmountOperand(Operands[0].getValueType(),
6451 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006452 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006453 case ISD::SIGN_EXTEND_INREG:
6454 case ISD::FP_ROUND_INREG: {
6455 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6456 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6457 Operands[0],
6458 getValueType(ExtVT)));
6459 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006460 }
6461 }
6462
6463 for (; i < ResNE; ++i)
6464 Scalars.push_back(getUNDEF(EltVT));
6465
6466 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006467 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006468}
6469
Evan Chengf5938d52009-12-09 01:36:00 +00006470
Wesley Peck527da1b2010-11-23 03:31:01 +00006471/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6472/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006473/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006474bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006475 unsigned Bytes, int Dist) const {
6476 if (LD->getChain() != Base->getChain())
6477 return false;
6478 EVT VT = LD->getValueType(0);
6479 if (VT.getSizeInBits() / 8 != Bytes)
6480 return false;
6481
6482 SDValue Loc = LD->getOperand(1);
6483 SDValue BaseLoc = Base->getOperand(1);
6484 if (Loc.getOpcode() == ISD::FrameIndex) {
6485 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6486 return false;
6487 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6488 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6489 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6490 int FS = MFI->getObjectSize(FI);
6491 int BFS = MFI->getObjectSize(BFI);
6492 if (FS != BFS || FS != (int)Bytes) return false;
6493 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6494 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006495
6496 // Handle X+C
6497 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6498 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6499 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006500
Craig Topperc0196b12014-04-14 00:51:57 +00006501 const GlobalValue *GV1 = nullptr;
6502 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006503 int64_t Offset1 = 0;
6504 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006505 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6506 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006507 if (isGA1 && isGA2 && GV1 == GV2)
6508 return Offset1 == (Offset2 + Dist*Bytes);
6509 return false;
6510}
6511
6512
Evan Cheng34a23ea2009-12-09 01:04:59 +00006513/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6514/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006515unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006516 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006517 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006518 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006519 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006520 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006521 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +00006522 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6523 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006524 unsigned AlignBits = KnownZero.countTrailingOnes();
6525 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6526 if (Align)
6527 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006528 }
Evan Chengd938faf2009-12-09 01:53:58 +00006529
Evan Cheng34a23ea2009-12-09 01:04:59 +00006530 // If this is a direct reference to a stack slot, use information about the
6531 // stack slot's alignment.
6532 int FrameIdx = 1 << 31;
6533 int64_t FrameOffset = 0;
6534 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6535 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006536 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006537 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006538 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006539 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6540 FrameOffset = Ptr.getConstantOperandVal(1);
6541 }
6542
6543 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006544 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006545 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6546 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006547 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006548 }
6549
6550 return 0;
6551}
6552
Juergen Ributzkab3487102013-11-19 21:20:17 +00006553/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6554/// which is split (or expanded) into two not necessarily identical pieces.
6555std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6556 // Currently all types are split in half.
6557 EVT LoVT, HiVT;
6558 if (!VT.isVector()) {
6559 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6560 } else {
6561 unsigned NumElements = VT.getVectorNumElements();
6562 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6563 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6564 NumElements/2);
6565 }
6566 return std::make_pair(LoVT, HiVT);
6567}
6568
6569/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6570/// low/high part.
6571std::pair<SDValue, SDValue>
6572SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6573 const EVT &HiVT) {
6574 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6575 N.getValueType().getVectorNumElements() &&
6576 "More vector elements requested than available!");
6577 SDValue Lo, Hi;
6578 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6579 getConstant(0, TLI->getVectorIdxTy()));
6580 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6581 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6582 return std::make_pair(Lo, Hi);
6583}
6584
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006585void SelectionDAG::ExtractVectorElements(SDValue Op,
6586 SmallVectorImpl<SDValue> &Args,
6587 unsigned Start, unsigned Count) {
6588 EVT VT = Op.getValueType();
6589 if (Count == 0)
6590 Count = VT.getVectorNumElements();
6591
6592 EVT EltVT = VT.getVectorElementType();
6593 EVT IdxTy = TLI->getVectorIdxTy();
6594 SDLoc SL(Op);
6595 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6596 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6597 Op, getConstant(i, IdxTy)));
6598 }
6599}
6600
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006601// getAddressSpace - Return the address space this GlobalAddress belongs to.
6602unsigned GlobalAddressSDNode::getAddressSpace() const {
6603 return getGlobal()->getType()->getAddressSpace();
6604}
6605
6606
Chris Lattner229907c2011-07-18 04:54:35 +00006607Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006608 if (isMachineConstantPoolEntry())
6609 return Val.MachineCPVal->getType();
6610 return Val.ConstVal->getType();
6611}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006612
Bob Wilson85cefe82009-03-02 23:24:16 +00006613bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6614 APInt &SplatUndef,
6615 unsigned &SplatBitSize,
6616 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006617 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006618 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006619 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006620 assert(VT.isVector() && "Expected a vector type");
6621 unsigned sz = VT.getSizeInBits();
6622 if (MinSplatBits > sz)
6623 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006624
Bob Wilson85cefe82009-03-02 23:24:16 +00006625 SplatValue = APInt(sz, 0);
6626 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006627
Bob Wilson85cefe82009-03-02 23:24:16 +00006628 // Get the bits. Bits with undefined values (when the corresponding element
6629 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6630 // in SplatValue. If any of the values are not constant, give up and return
6631 // false.
6632 unsigned int nOps = getNumOperands();
6633 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6634 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006635
6636 for (unsigned j = 0; j < nOps; ++j) {
6637 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006638 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006639 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006640
Bob Wilson85cefe82009-03-02 23:24:16 +00006641 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006642 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006643 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006644 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006645 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006646 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006647 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006648 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006649 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006650 }
6651
Bob Wilson85cefe82009-03-02 23:24:16 +00006652 // The build_vector is all constants or undefs. Find the smallest element
6653 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006654
Bob Wilson85cefe82009-03-02 23:24:16 +00006655 HasAnyUndefs = (SplatUndef != 0);
6656 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006657
Bob Wilson85cefe82009-03-02 23:24:16 +00006658 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006659 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6660 APInt LowValue = SplatValue.trunc(HalfSize);
6661 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6662 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006663
Bob Wilson85cefe82009-03-02 23:24:16 +00006664 // If the two halves do not match (ignoring undef bits), stop here.
6665 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6666 MinSplatBits > HalfSize)
6667 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006668
Bob Wilson85cefe82009-03-02 23:24:16 +00006669 SplatValue = HighValue | LowValue;
6670 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006671
Bob Wilson85cefe82009-03-02 23:24:16 +00006672 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006673 }
6674
Bob Wilson85cefe82009-03-02 23:24:16 +00006675 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006676 return true;
6677}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006678
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006679SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6680 if (UndefElements) {
6681 UndefElements->clear();
6682 UndefElements->resize(getNumOperands());
6683 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006684 SDValue Splatted;
6685 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6686 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006687 if (Op.getOpcode() == ISD::UNDEF) {
6688 if (UndefElements)
6689 (*UndefElements)[i] = true;
6690 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006691 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006692 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006693 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006694 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006695 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006696
Chandler Carruthb844e722014-07-08 07:19:55 +00006697 if (!Splatted) {
6698 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6699 "Can only have a splat without a constant for all undefs.");
6700 return getOperand(0);
6701 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006702
Chandler Carruthb844e722014-07-08 07:19:55 +00006703 return Splatted;
6704}
6705
6706ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006707BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006708 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006709 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006710}
6711
6712ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006713BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006714 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006715 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006716}
6717
Juergen Ributzka73844052014-01-13 20:51:35 +00006718bool BuildVectorSDNode::isConstant() const {
6719 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6720 unsigned Opc = getOperand(i).getOpcode();
6721 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6722 return false;
6723 }
6724 return true;
6725}
6726
Owen Anderson53aa7a92009-08-10 22:56:29 +00006727bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006728 // Find the first non-undef value in the shuffle mask.
6729 unsigned i, e;
6730 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6731 /* search */;
6732
Nate Begeman39b59db2009-04-29 18:13:31 +00006733 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006734
Nate Begeman5f829d82009-04-29 05:20:52 +00006735 // Make sure all remaining elements are either undef or the same as the first
6736 // non-undef value.
6737 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006738 if (Mask[i] >= 0 && Mask[i] != Idx)
6739 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006740 return true;
6741}
David Greene09851602010-01-20 20:13:31 +00006742
Adam Nemetb4690e32014-05-31 16:23:20 +00006743#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00006744static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006745 SmallPtrSetImpl<const SDNode*> &Visited,
6746 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00006747 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006748 // If this node has already been checked, don't check it again.
6749 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006750 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006751
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006752 // If a node has already been visited on this depth-first walk, reject it as
6753 // a cycle.
6754 if (!Visited.insert(N)) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006755 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006756 dbgs() << "Offending node:\n";
6757 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006758 abort();
David Greene09851602010-01-20 20:13:31 +00006759 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006760
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006761 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00006762 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00006763
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006764 Checked.insert(N);
6765 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006766}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006767#endif
David Greene09851602010-01-20 20:13:31 +00006768
Adam Nemet7d394302014-05-31 16:23:17 +00006769void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00006770 const llvm::SelectionDAG *DAG,
6771 bool force) {
6772#ifndef NDEBUG
6773 bool check = force;
David Greene09851602010-01-20 20:13:31 +00006774#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00006775 check = true;
6776#endif // XDEBUG
6777 if (check) {
6778 assert(N && "Checking nonexistent SDNode");
6779 SmallPtrSet<const SDNode*, 32> visited;
6780 SmallPtrSet<const SDNode*, 32> checked;
6781 checkForCyclesHelper(N, visited, checked, DAG);
6782 }
6783#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00006784}
6785
Adam Nemetb4690e32014-05-31 16:23:20 +00006786void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6787 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00006788}