blob: 408a25b9098f3ecece382f270d41dc5ff9ec4fb2 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendling6de55a02009-12-21 19:34:59 +000013
Chris Lattner600d3082003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng00fd0b62010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
Bill Wendlingbd092622008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetIntrinsicInfo.h"
44#include "llvm/Target/TargetLowering.h"
45#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetOptions.h"
47#include "llvm/Target/TargetRegisterInfo.h"
48#include "llvm/Target/TargetSelectionDAGInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000049#include "llvm/Target/TargetSubtargetInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000050#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000051#include <cmath>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000052
Chris Lattner560b5e42004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000054
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000065
Jim Laskeyd66e6162005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000076}
77
Owen Anderson53aa7a92009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Dale Johannesend246b2c2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000089}
90
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000094
Evan Chengc70e33c2006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000098 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +000099 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Evan Chengc70e33c2006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000105
Chris Lattnerc2d28112006-03-25 22:57:01 +0000106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000109
Chris Lattnerc2d28112006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000112
Chris Lattnerc2d28112006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000114 // elements. We have to be a bit careful here, as the type of the constant
115 // may not be the same as the type of the vector elements due to type
116 // legalization (the elements are promoted to a legal type for the target and
117 // a vector of a type may be legal when the base element type is not).
118 // We only want to check enough bits to cover the vector elements, because
119 // we care if the resultant vector is all ones, not whether the individual
120 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000125 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000128 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000129 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Chris Lattnerc2d28112006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000133 // undefs. Even with the above element type twiddling, this should be OK, as
134 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Chenga6789912006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000146 // Look through a bit convert.
Simon Pilgrim3ac3b252014-11-19 10:06:49 +0000147 while (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chenga6789912006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Kevin Qin93d45ec2014-06-24 05:37:27 +0000152 bool IsAllUndef = true;
153 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
154 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
155 continue;
156 IsAllUndef = false;
157 // Do not accept build_vectors that aren't all constants or which have non-0
158 // elements. We have to be a bit careful here, as the type of the constant
159 // may not be the same as the type of the vector elements due to type
160 // legalization (the elements are promoted to a legal type for the target
161 // and a vector of a type may be legal when the base element type is not).
162 // We only want to check enough bits to cover the vector elements, because
163 // we care if the resultant vector is all zeros, not whether the individual
164 // constants are.
165 SDValue Zero = N->getOperand(i);
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
168 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
169 return false;
170 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
171 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
172 return false;
173 } else
174 return false;
175 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000176
Evan Chengc70e33c2006-03-27 06:58:47 +0000177 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000178 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000179 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000180 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000181}
182
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000183/// \brief Return true if the specified node is a BUILD_VECTOR node of
184/// all ConstantSDNode or undef.
185bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
186 if (N->getOpcode() != ISD::BUILD_VECTOR)
187 return false;
188
189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
190 SDValue Op = N->getOperand(i);
191 if (Op.getOpcode() == ISD::UNDEF)
192 continue;
193 if (!isa<ConstantSDNode>(Op))
194 return false;
195 }
196 return true;
197}
198
Evan Cheng6200c222008-02-18 23:04:32 +0000199/// isScalarToVector - Return true if the specified node is a
200/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
201/// element is not an undef.
202bool ISD::isScalarToVector(const SDNode *N) {
203 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
204 return true;
205
206 if (N->getOpcode() != ISD::BUILD_VECTOR)
207 return false;
208 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
209 return false;
210 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000211 if (NumElems == 1)
212 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000213 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000214 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000215 if (V.getOpcode() != ISD::UNDEF)
216 return false;
217 }
218 return true;
219}
220
Nadav Rotema62368c2012-07-15 08:38:23 +0000221/// allOperandsUndef - Return true if the node has at least one operand
222/// and all operands of the specified node are ISD::UNDEF.
223bool ISD::allOperandsUndef(const SDNode *N) {
224 // Return false if the node has no operands.
225 // This is "logically inconsistent" with the definition of "all" but
226 // is probably the desired behavior.
227 if (N->getNumOperands() == 0)
228 return false;
229
230 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
231 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
232 return false;
233
234 return true;
235}
236
Matt Arsenaultbd223422015-01-14 01:35:17 +0000237ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000238 switch (ExtType) {
239 case ISD::EXTLOAD:
Matt Arsenaultbd223422015-01-14 01:35:17 +0000240 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000241 case ISD::SEXTLOAD:
242 return ISD::SIGN_EXTEND;
243 case ISD::ZEXTLOAD:
244 return ISD::ZERO_EXTEND;
245 default:
246 break;
247 }
248
249 llvm_unreachable("Invalid LoadExtType");
250}
251
Chris Lattner061a1ea2005-01-07 07:46:32 +0000252/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
253/// when given the operation for (X op Y).
254ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
255 // To perform this operation, we just need to swap the L and G bits of the
256 // operation.
257 unsigned OldL = (Operation >> 2) & 1;
258 unsigned OldG = (Operation >> 1) & 1;
259 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
260 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000261 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000262}
263
264/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
265/// 'op' is a valid SetCC operation.
266ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
267 unsigned Operation = Op;
268 if (isInteger)
269 Operation ^= 7; // Flip L, G, E bits, but not U.
270 else
271 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000272
Chris Lattner061a1ea2005-01-07 07:46:32 +0000273 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000274 Operation &= ~8; // Don't let N and U bits get set.
275
Chris Lattner061a1ea2005-01-07 07:46:32 +0000276 return ISD::CondCode(Operation);
277}
278
279
280/// isSignedOp - For an integer comparison, return 1 if the comparison is a
281/// signed operation and 2 if the result is an unsigned comparison. Return zero
282/// if the operation does not depend on the sign of the input (setne and seteq).
283static int isSignedOp(ISD::CondCode Opcode) {
284 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000285 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000286 case ISD::SETEQ:
287 case ISD::SETNE: return 0;
288 case ISD::SETLT:
289 case ISD::SETLE:
290 case ISD::SETGT:
291 case ISD::SETGE: return 1;
292 case ISD::SETULT:
293 case ISD::SETULE:
294 case ISD::SETUGT:
295 case ISD::SETUGE: return 2;
296 }
297}
298
299/// getSetCCOrOperation - Return the result of a logical OR between different
300/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
301/// returns SETCC_INVALID if it is not possible to represent the resultant
302/// comparison.
303ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
304 bool isInteger) {
305 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
306 // Cannot fold a signed integer setcc with an unsigned integer setcc.
307 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000308
Chris Lattner061a1ea2005-01-07 07:46:32 +0000309 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000310
Chris Lattner061a1ea2005-01-07 07:46:32 +0000311 // If the N and U bits get set then the resultant comparison DOES suddenly
312 // care about orderedness, and is true when ordered.
313 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000314 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000315
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000316 // Canonicalize illegal integer setcc's.
317 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
318 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Chris Lattner061a1ea2005-01-07 07:46:32 +0000320 return ISD::CondCode(Op);
321}
322
323/// getSetCCAndOperation - Return the result of a logical AND between different
324/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
325/// function returns zero if it is not possible to represent the resultant
326/// comparison.
327ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
328 bool isInteger) {
329 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
330 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000331 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000332
333 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000334 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Chris Lattner393d96a2006-04-27 05:01:07 +0000336 // Canonicalize illegal integer setcc's.
337 if (isInteger) {
338 switch (Result) {
339 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000340 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000341 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000342 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
343 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
344 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000345 }
346 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000347
Chris Lattner393d96a2006-04-27 05:01:07 +0000348 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000349}
350
Jim Laskeyd66e6162005-08-17 20:08:02 +0000351//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000352// SDNode Profile Support
353//===----------------------------------------------------------------------===//
354
Jim Laskeybd0f0882006-10-27 23:52:51 +0000355/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
356///
Jim Laskeyf576b422006-10-27 23:46:08 +0000357static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
358 ID.AddInteger(OpC);
359}
360
361/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
362/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000363static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000364 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000365}
366
Jim Laskeybd0f0882006-10-27 23:52:51 +0000367/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
368///
Jim Laskeyf576b422006-10-27 23:46:08 +0000369static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000370 ArrayRef<SDValue> Ops) {
371 for (auto& Op : Ops) {
372 ID.AddPointer(Op.getNode());
373 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000374 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000375}
376
Dan Gohman768f2c92008-07-07 18:26:29 +0000377/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
378///
379static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000380 ArrayRef<SDUse> Ops) {
381 for (auto& Op : Ops) {
382 ID.AddPointer(Op.getNode());
383 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000384 }
385}
386
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000387static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
388 bool exact) {
389 ID.AddBoolean(nuw);
390 ID.AddBoolean(nsw);
391 ID.AddBoolean(exact);
392}
393
394/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
395static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
396 bool nuw, bool nsw, bool exact) {
397 if (isBinOpWithFlags(Opcode))
398 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
399}
400
Craig Topper633d99b2014-04-27 23:22:43 +0000401static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
402 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000403 AddNodeIDOpcode(ID, OpC);
404 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000405 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000406}
407
Duncan Sands835bdca2008-10-27 15:30:53 +0000408/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
409/// the NodeID data.
410static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000411 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000412 case ISD::TargetExternalSymbol:
413 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000414 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000415 default: break; // Normal nodes don't need extra info.
416 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000417 case ISD::Constant: {
418 const ConstantSDNode *C = cast<ConstantSDNode>(N);
419 ID.AddPointer(C->getConstantIntValue());
420 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000421 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000422 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000423 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000424 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000425 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000426 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000427 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000428 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000429 case ISD::GlobalAddress:
430 case ISD::TargetGlobalTLSAddress:
431 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000432 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000433 ID.AddPointer(GA->getGlobal());
434 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000435 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000436 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 break;
438 }
439 case ISD::BasicBlock:
440 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
441 break;
442 case ISD::Register:
443 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
444 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000445 case ISD::RegisterMask:
446 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
447 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000448 case ISD::SRCVALUE:
449 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
450 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000451 case ISD::FrameIndex:
452 case ISD::TargetFrameIndex:
453 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
454 break;
455 case ISD::JumpTable:
456 case ISD::TargetJumpTable:
457 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000458 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000459 break;
460 case ISD::ConstantPool:
461 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000462 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000463 ID.AddInteger(CP->getAlignment());
464 ID.AddInteger(CP->getOffset());
465 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000466 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000467 else
468 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000469 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000470 break;
471 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000472 case ISD::TargetIndex: {
473 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
474 ID.AddInteger(TI->getIndex());
475 ID.AddInteger(TI->getOffset());
476 ID.AddInteger(TI->getTargetFlags());
477 break;
478 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000479 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000480 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000481 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000482 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000483 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000484 break;
485 }
486 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000487 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000488 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000489 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000490 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000491 break;
492 }
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000493 case ISD::SDIV:
494 case ISD::UDIV:
495 case ISD::SRA:
496 case ISD::SRL:
497 case ISD::MUL:
498 case ISD::ADD:
499 case ISD::SUB:
500 case ISD::SHL: {
501 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
502 AddBinaryNodeIDCustom(ID, N->getOpcode(), BinNode->hasNoUnsignedWrap(),
503 BinNode->hasNoSignedWrap(), BinNode->isExact());
504 break;
505 }
Dan Gohman12f24902008-12-23 21:37:04 +0000506 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000507 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000508 case ISD::ATOMIC_SWAP:
509 case ISD::ATOMIC_LOAD_ADD:
510 case ISD::ATOMIC_LOAD_SUB:
511 case ISD::ATOMIC_LOAD_AND:
512 case ISD::ATOMIC_LOAD_OR:
513 case ISD::ATOMIC_LOAD_XOR:
514 case ISD::ATOMIC_LOAD_NAND:
515 case ISD::ATOMIC_LOAD_MIN:
516 case ISD::ATOMIC_LOAD_MAX:
517 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000518 case ISD::ATOMIC_LOAD_UMAX:
519 case ISD::ATOMIC_LOAD:
520 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000521 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000522 ID.AddInteger(AT->getMemoryVT().getRawBits());
523 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000524 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
525 break;
526 }
527 case ISD::PREFETCH: {
528 const MemSDNode *PF = cast<MemSDNode>(N);
529 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000530 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000531 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000532 case ISD::VECTOR_SHUFFLE: {
533 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000534 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000535 i != e; ++i)
536 ID.AddInteger(SVN->getMaskElt(i));
537 break;
538 }
Dan Gohman6c938802009-10-30 01:27:03 +0000539 case ISD::TargetBlockAddress:
540 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000541 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
542 ID.AddPointer(BA->getBlockAddress());
543 ID.AddInteger(BA->getOffset());
544 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000545 break;
546 }
Mon P Wang6a490372008-06-25 08:15:39 +0000547 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000548
549 // Target specific memory nodes could also have address spaces to check.
550 if (N->isTargetMemoryOpcode())
551 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000552}
553
Duncan Sands835bdca2008-10-27 15:30:53 +0000554/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
555/// data.
556static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
557 AddNodeIDOpcode(ID, N->getOpcode());
558 // Add the return value info.
559 AddNodeIDValueTypes(ID, N->getVTList());
560 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000561 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000562
563 // Handle SDNode leafs with special info.
564 AddNodeIDCustom(ID, N);
565}
566
Dan Gohman2da2bed2008-08-20 15:58:01 +0000567/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000568/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000569/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000570///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000571static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000572encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000573 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000574 assert((ConvType & 3) == ConvType &&
575 "ConvType may not require more than 2 bits!");
576 assert((AM & 7) == AM &&
577 "AM may not require more than 3 bits!");
578 return ConvType |
579 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000580 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000581 (isNonTemporal << 6) |
582 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000583}
584
Jim Laskeyf576b422006-10-27 23:46:08 +0000585//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000586// SelectionDAG Class
587//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000588
Duncan Sands835bdca2008-10-27 15:30:53 +0000589/// doNotCSE - Return true if CSE should not be performed for this node.
590static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000591 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000592 return true; // Never CSE anything that produces a flag.
593
594 switch (N->getOpcode()) {
595 default: break;
596 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000597 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000598 return true; // Never CSE these nodes.
599 }
600
601 // Check that remaining values produced are not flags.
602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000603 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000604 return true; // Never CSE anything that produces a flag.
605
606 return false;
607}
608
Chris Lattner9c667932005-01-07 21:09:16 +0000609/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000610/// SelectionDAG.
611void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000612 // Create a dummy node (which is not added to allnodes), that adds a reference
613 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000614 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000615
Chris Lattner8927c872006-08-04 17:45:20 +0000616 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000617
Chris Lattner8927c872006-08-04 17:45:20 +0000618 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000619 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000620 if (I->use_empty())
621 DeadNodes.push_back(I);
622
Dan Gohman91697632008-07-07 20:57:48 +0000623 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000624
Dan Gohman91697632008-07-07 20:57:48 +0000625 // If the root changed (e.g. it was a dead load, update the root).
626 setRoot(Dummy.getValue());
627}
628
629/// RemoveDeadNodes - This method deletes the unreachable nodes in the
630/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000631void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000632
Chris Lattner8927c872006-08-04 17:45:20 +0000633 // Process the worklist, deleting the nodes and adding their uses to the
634 // worklist.
635 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000636 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000637
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000638 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000639 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000640
Chris Lattner8927c872006-08-04 17:45:20 +0000641 // Take the node out of the appropriate CSE map.
642 RemoveNodeFromCSEMaps(N);
643
644 // Next, brutally remove the operand list. This is safe to do, as there are
645 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000646 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
647 SDUse &Use = *I++;
648 SDNode *Operand = Use.getNode();
649 Use.set(SDValue());
650
Chris Lattner8927c872006-08-04 17:45:20 +0000651 // Now that we removed this operand, see if there are no uses of it left.
652 if (Operand->use_empty())
653 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000654 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000655
Dan Gohman534c8a22009-01-19 22:39:36 +0000656 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000657 }
Chris Lattner9c667932005-01-07 21:09:16 +0000658}
659
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000660void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000661 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000662
663 // Create a dummy node that adds a reference to the root node, preventing
664 // it from being deleted. (This matters if the root is an operand of the
665 // dead node.)
666 HandleSDNode Dummy(getRoot());
667
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000668 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000669}
670
Chris Lattnerc738d002005-08-29 21:59:31 +0000671void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000672 // First take this out of the appropriate CSE map.
673 RemoveNodeFromCSEMaps(N);
674
Scott Michelcf0da6c2009-02-17 22:15:04 +0000675 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000676 // AllNodes list, and delete the node.
677 DeleteNodeNotInCSEMaps(N);
678}
679
680void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000681 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
682 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000683
Dan Gohman86aa16a2008-09-30 18:30:35 +0000684 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000685 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000686
Dan Gohman534c8a22009-01-19 22:39:36 +0000687 DeallocateNode(N);
688}
689
Frederic Riss8ad4f492014-11-11 21:21:08 +0000690void SDDbgInfo::erase(const SDNode *Node) {
691 DbgValMapType::iterator I = DbgValMap.find(Node);
692 if (I == DbgValMap.end())
693 return;
694 for (auto &Val: I->second)
695 Val->setIsInvalidated();
696 DbgValMap.erase(I);
697}
698
Dan Gohman534c8a22009-01-19 22:39:36 +0000699void SelectionDAG::DeallocateNode(SDNode *N) {
700 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000701 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000702
Dan Gohman534c8a22009-01-19 22:39:36 +0000703 // Set the opcode to DELETED_NODE to help catch bugs when node
704 // memory is reallocated.
705 N->NodeType = ISD::DELETED_NODE;
706
Dan Gohman2e834902008-08-26 01:44:34 +0000707 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000708
Frederic Riss8ad4f492014-11-11 21:21:08 +0000709 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
710 // them and forget about that node.
711 DbgInfo->erase(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000712}
713
Chandler Carruth41b20e72014-07-22 04:07:55 +0000714#ifndef NDEBUG
715/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
716static void VerifySDNode(SDNode *N) {
717 switch (N->getOpcode()) {
718 default:
719 break;
720 case ISD::BUILD_PAIR: {
721 EVT VT = N->getValueType(0);
722 assert(N->getNumValues() == 1 && "Too many results!");
723 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
724 "Wrong return type!");
725 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
726 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
727 "Mismatched operand types!");
728 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
729 "Wrong operand type!");
730 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
731 "Wrong return type size");
732 break;
733 }
734 case ISD::BUILD_VECTOR: {
735 assert(N->getNumValues() == 1 && "Too many results!");
736 assert(N->getValueType(0).isVector() && "Wrong return type!");
737 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
738 "Wrong number of operands!");
739 EVT EltVT = N->getValueType(0).getVectorElementType();
740 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
741 assert((I->getValueType() == EltVT ||
742 (EltVT.isInteger() && I->getValueType().isInteger() &&
743 EltVT.bitsLE(I->getValueType()))) &&
744 "Wrong operand type!");
745 assert(I->getValueType() == N->getOperand(0).getValueType() &&
746 "Operands must all have the same type");
747 }
748 break;
749 }
750 }
751}
752#endif // NDEBUG
753
754/// \brief Insert a newly allocated node into the DAG.
755///
756/// Handles insertion into the all nodes list and CSE map, as well as
757/// verification and other common operations when a new node is allocated.
758void SelectionDAG::InsertNode(SDNode *N) {
759 AllNodes.push_back(N);
760#ifndef NDEBUG
761 VerifySDNode(N);
762#endif
763}
764
Chris Lattner19732782005-08-16 18:17:10 +0000765/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
766/// correspond to it. This is useful when we're about to delete or repurpose
767/// the node. We don't want future request for structurally identical nodes
768/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000769bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000770 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000771 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000772 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000773 case ISD::CONDCODE:
774 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
775 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000776 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
777 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000778 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000779 case ISD::ExternalSymbol:
780 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000781 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000782 case ISD::TargetExternalSymbol: {
783 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
784 Erased = TargetExternalSymbols.erase(
785 std::pair<std::string,unsigned char>(ESN->getSymbol(),
786 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000787 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000788 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000789 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000790 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000791 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000792 Erased = ExtendedValueTypeNodes.erase(VT);
793 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000794 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
795 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000796 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000797 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000798 }
Chris Lattner9c667932005-01-07 21:09:16 +0000799 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000800 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000801 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
802 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000803 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000804 break;
805 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000806#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000807 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000808 // flag result (which cannot be CSE'd) or is one of the special cases that are
809 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000810 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000811 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000812 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000813 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000814 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000815 }
816#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000817 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000818}
819
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000820/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
821/// maps and modified in place. Add it back to the CSE maps, unless an identical
822/// node already exists, in which case transfer all its users to the existing
823/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000824///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000825void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000826SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000827 // For node types that aren't CSE'd, just act as if no identical node
828 // already exists.
829 if (!doNotCSE(N)) {
830 SDNode *Existing = CSEMap.GetOrInsertNode(N);
831 if (Existing != N) {
832 // If there was already an existing matching node, use ReplaceAllUsesWith
833 // to replace the dead one with the existing one. This can cause
834 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000835 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000836
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000837 // N is now dead. Inform the listeners and delete it.
838 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
839 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000840 DeleteNodeNotInCSEMaps(N);
841 return;
842 }
843 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000844
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000845 // If the node doesn't already exist, we updated it. Inform listeners.
846 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
847 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000848}
849
Chris Lattnerf34156e2006-01-28 09:32:45 +0000850/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000851/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000852/// return null, otherwise return a pointer to the slot it would take. If a
853/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000854SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000855 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000856 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000857 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000858
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000859 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000860 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000861 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000862 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000863 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000864 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000865}
866
867/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000868/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000869/// return null, otherwise return a pointer to the slot it would take. If a
870/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000871SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000872 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000873 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000874 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000875 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000876
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000877 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000878 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000879 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000880 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000881 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000882 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000883}
884
885
886/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000887/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000888/// return null, otherwise return a pointer to the slot it would take. If a
889/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000890SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000891 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000892 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000893 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000894
Jim Laskeyf576b422006-10-27 23:46:08 +0000895 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000896 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000897 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000898 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000899 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000900}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000901
Owen Anderson53aa7a92009-08-10 22:56:29 +0000902/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000903/// given type.
904///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000905unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000906 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000907 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000908 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000909
Eric Christopher1e845f22014-10-08 21:08:32 +0000910 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000911}
Chris Lattner9c667932005-01-07 21:09:16 +0000912
Dale Johannesen8ba713212009-02-07 02:15:05 +0000913// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000914SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000915 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000916 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
917 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
918 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000919 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000920 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000921}
922
Eric Christopher8d07f442014-10-08 01:57:58 +0000923void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000924 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000925 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000926 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000927 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000928}
929
Chris Lattner600d3082003-08-11 14:57:33 +0000930SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000931 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000932 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000933 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000934}
935
936void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000937 assert(&*AllNodes.begin() == &EntryNode);
938 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000939 while (!AllNodes.empty())
940 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000941}
942
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000943BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
944 SDVTList VTs, SDValue N1,
945 SDValue N2, bool nuw, bool nsw,
946 bool exact) {
947 if (isBinOpWithFlags(Opcode)) {
948 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
949 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
950 FN->setHasNoUnsignedWrap(nuw);
951 FN->setHasNoSignedWrap(nsw);
952 FN->setIsExact(exact);
953
954 return FN;
955 }
956
957 BinarySDNode *N = new (NodeAllocator)
958 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
959 return N;
960}
961
Dan Gohmane1a9a782008-08-27 23:52:12 +0000962void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000963 allnodes_clear();
964 OperandAllocator.Reset();
965 CSEMap.clear();
966
967 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000968 ExternalSymbols.clear();
969 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000970 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000971 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000972 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000973 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000974
Craig Topperc0196b12014-04-14 00:51:57 +0000975 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000976 AllNodes.push_back(&EntryNode);
977 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000978 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000979}
980
Andrew Trickef9de2a2013-05-25 02:42:55 +0000981SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000982 return VT.bitsGT(Op.getValueType()) ?
983 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
984 getNode(ISD::TRUNCATE, DL, VT, Op);
985}
986
Andrew Trickef9de2a2013-05-25 02:42:55 +0000987SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000988 return VT.bitsGT(Op.getValueType()) ?
989 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
990 getNode(ISD::TRUNCATE, DL, VT, Op);
991}
992
Andrew Trickef9de2a2013-05-25 02:42:55 +0000993SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000994 return VT.bitsGT(Op.getValueType()) ?
995 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
996 getNode(ISD::TRUNCATE, DL, VT, Op);
997}
998
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000999SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1000 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001001 if (VT.bitsLE(Op.getValueType()))
1002 return getNode(ISD::TRUNCATE, SL, VT, Op);
1003
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001004 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001005 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1006}
1007
Andrew Trickef9de2a2013-05-25 02:42:55 +00001008SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001009 assert(!VT.isVector() &&
1010 "getZeroExtendInReg should use the vector element type instead of "
1011 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001012 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001013 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1014 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001015 VT.getSizeInBits());
1016 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1017 getConstant(Imm, Op.getValueType()));
1018}
1019
Chandler Carruth0b666e02014-07-10 12:32:32 +00001020SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1021 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1022 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1023 "The sizes of the input and result must match in order to perform the "
1024 "extend in-register.");
1025 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1026 "The destination vector type must have fewer lanes than the input.");
1027 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1028}
1029
1030SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1031 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1032 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1033 "The sizes of the input and result must match in order to perform the "
1034 "extend in-register.");
1035 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1036 "The destination vector type must have fewer lanes than the input.");
1037 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1038}
1039
Chandler Carruthafe4b252014-07-09 10:58:18 +00001040SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1041 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001042 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1043 "The sizes of the input and result must match in order to perform the "
1044 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001045 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1046 "The destination vector type must have fewer lanes than the input.");
1047 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1048}
1049
Bob Wilsonc5890052009-01-22 17:39:32 +00001050/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1051///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001052SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001053 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001054 SDValue NegOne =
1055 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001056 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1057}
1058
Pete Cooper7fd1d722014-05-12 23:26:58 +00001059SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1060 EVT EltVT = VT.getScalarType();
1061 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001062 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001063 case TargetLowering::ZeroOrOneBooleanContent:
1064 case TargetLowering::UndefinedBooleanContent:
1065 TrueValue = getConstant(1, VT);
1066 break;
1067 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1068 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1069 VT);
1070 break;
1071 }
1072 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1073}
1074
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001075SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001076 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001077 assert((EltVT.getSizeInBits() >= 64 ||
1078 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1079 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001080 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001081}
1082
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001083SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1084{
1085 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001086}
1087
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001088SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1089 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001090 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001091
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001092 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001093 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001094
Duncan Sandsf2641e12011-09-06 19:07:46 +00001095 // In some cases the vector type is legal but the element type is illegal and
1096 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1097 // inserted value (the type does not need to match the vector element type).
1098 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001099 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001100 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001101 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001102 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1103 Elt = ConstantInt::get(*getContext(), NewVal);
1104 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001105 // In other cases the element type is illegal and needs to be expanded, for
1106 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1107 // the value into n parts and use a vector type with n-times the elements.
1108 // Then bitcast to the type requested.
1109 // Legalizing constants too early makes the DAGCombiner's job harder so we
1110 // only legalize if the DAG tells us we must produce legal types.
1111 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1112 TLI->getTypeAction(*getContext(), EltVT) ==
1113 TargetLowering::TypeExpandInteger) {
1114 APInt NewVal = Elt->getValue();
1115 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1116 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1117 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1118 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1119
1120 // Check the temporary vector is the correct size. If this fails then
1121 // getTypeToTransformTo() probably returned a type whose size (in bits)
1122 // isn't a power-of-2 factor of the requested type size.
1123 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1124
1125 SmallVector<SDValue, 2> EltParts;
1126 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1127 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1128 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001129 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001130 }
1131
1132 // EltParts is currently in little endian order. If we actually want
1133 // big-endian order then reverse it now.
1134 if (TLI->isBigEndian())
1135 std::reverse(EltParts.begin(), EltParts.end());
1136
1137 // The elements must be reversed when the element order is different
1138 // to the endianness of the elements (because the BITCAST is itself a
1139 // vector shuffle in this situation). However, we do not need any code to
1140 // perform this reversal because getConstant() is producing a vector
1141 // splat.
1142 // This situation occurs in MIPS MSA.
1143
1144 SmallVector<SDValue, 8> Ops;
1145 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1146 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1147
1148 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1149 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001150 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001151 return Result;
1152 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001153
1154 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1155 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001156 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001157 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001158 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001159 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001160 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001161 void *IP = nullptr;
1162 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001163 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001164 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001165 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001166
Dan Gohman7a7742c2007-12-12 22:21:26 +00001167 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001168 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001169 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001170 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001171 }
1172
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001173 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001174 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001175 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001176 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001177 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001178 }
1179 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001180}
1181
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001182SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Eric Christopher1e845f22014-10-08 21:08:32 +00001183 return getConstant(Val, TLI->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001184}
1185
1186
Owen Anderson53aa7a92009-08-10 22:56:29 +00001187SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001188 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001189}
1190
Owen Anderson53aa7a92009-08-10 22:56:29 +00001191SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001192 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001193
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001194 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001195
Chris Lattner381dddc2005-02-17 20:17:32 +00001196 // Do the map lookup using the actual bit pattern for the floating point
1197 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1198 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001199 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001200 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001201 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001202 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001203 void *IP = nullptr;
1204 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001205 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001206 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001207 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001208
Evan Cheng9458e6a2007-06-29 21:36:04 +00001209 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001210 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001211 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001212 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001213 }
1214
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001215 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001216 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001217 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001218 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001219 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001220 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001221 }
1222 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001223}
1224
Owen Anderson53aa7a92009-08-10 22:56:29 +00001225SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001226 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001227 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001228 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001229 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001230 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001231 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1232 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001233 bool ignored;
1234 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001235 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001236 &ignored);
1237 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001238 } else
1239 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001240}
1241
Andrew Trickef9de2a2013-05-25 02:42:55 +00001242SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001243 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001244 bool isTargetGA,
1245 unsigned char TargetFlags) {
1246 assert((TargetFlags == 0 || isTargetGA) &&
1247 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001248
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001249 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001250 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001251 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001252 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001253
Chris Lattner8e34f982009-06-25 21:21:14 +00001254 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001255 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001256 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1257 else
1258 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001259
Jim Laskeyf576b422006-10-27 23:46:08 +00001260 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001261 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001262 ID.AddPointer(GV);
1263 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001264 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001265 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001266 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001267 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001268 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001269
Andrew Trickef9de2a2013-05-25 02:42:55 +00001270 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1271 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001272 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001273 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001274 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001275 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001276}
1277
Owen Anderson53aa7a92009-08-10 22:56:29 +00001278SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001279 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001280 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001281 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001282 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001283 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001284 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001285 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001286
Dan Gohman01c65a22010-03-18 18:49:47 +00001287 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001288 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001289 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001290 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001291}
1292
Owen Anderson53aa7a92009-08-10 22:56:29 +00001293SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001294 unsigned char TargetFlags) {
1295 assert((TargetFlags == 0 || isTarget) &&
1296 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001297 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001298 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001299 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001300 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001301 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001302 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001304 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001305
Dan Gohman01c65a22010-03-18 18:49:47 +00001306 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1307 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001308 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001309 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001310 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001311}
1312
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001313SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001314 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001315 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001316 unsigned char TargetFlags) {
1317 assert((TargetFlags == 0 || isTarget) &&
1318 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001319 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001320 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001321 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001322 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001323 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001324 ID.AddInteger(Alignment);
1325 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001326 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001327 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001328 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001329 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001330 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001331
Dan Gohman01c65a22010-03-18 18:49:47 +00001332 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1333 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001334 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001335 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001336 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001337}
1338
Chris Lattner061a1ea2005-01-07 07:46:32 +00001339
Owen Anderson53aa7a92009-08-10 22:56:29 +00001340SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001341 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001342 bool isTarget,
1343 unsigned char TargetFlags) {
1344 assert((TargetFlags == 0 || isTarget) &&
1345 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001346 if (Alignment == 0)
Eric Christopher1e845f22014-10-08 21:08:32 +00001347 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001348 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001349 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001350 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001351 ID.AddInteger(Alignment);
1352 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001353 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001354 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001355 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001356 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001358
Dan Gohman01c65a22010-03-18 18:49:47 +00001359 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1360 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001361 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001362 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001363 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001364}
1365
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001366SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1367 unsigned char TargetFlags) {
1368 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001369 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001370 ID.AddInteger(Index);
1371 ID.AddInteger(Offset);
1372 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001373 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001374 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1375 return SDValue(E, 0);
1376
1377 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1378 TargetFlags);
1379 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001380 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001381 return SDValue(N, 0);
1382}
1383
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001384SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001385 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001386 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001387 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001388 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001390 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001391
Dan Gohman01c65a22010-03-18 18:49:47 +00001392 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001393 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001394 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001395 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001396}
1397
Owen Anderson53aa7a92009-08-10 22:56:29 +00001398SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001399 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1400 ValueTypeNodes.size())
1401 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001402
Duncan Sands13237ac2008-06-06 12:08:01 +00001403 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001404 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001405
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001406 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001407 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001408 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001409 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001410}
1411
Owen Anderson53aa7a92009-08-10 22:56:29 +00001412SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001413 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001414 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001415 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001416 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001417 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001418}
1419
Owen Anderson53aa7a92009-08-10 22:56:29 +00001420SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001421 unsigned char TargetFlags) {
1422 SDNode *&N =
1423 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1424 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001425 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001426 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001427 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001428 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001429}
1430
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001431SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001432 if ((unsigned)Cond >= CondCodeNodes.size())
1433 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001434
Craig Topperc0196b12014-04-14 00:51:57 +00001435 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001436 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001437 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001438 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001439 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001440
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001441 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001442}
1443
Nate Begeman5f829d82009-04-29 05:20:52 +00001444// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1445// the shuffle mask M that point at N1 to point at N2, and indices that point
1446// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001447static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1448 std::swap(N1, N2);
1449 int NElts = M.size();
1450 for (int i = 0; i != NElts; ++i) {
1451 if (M[i] >= NElts)
1452 M[i] -= NElts;
1453 else if (M[i] >= 0)
1454 M[i] += NElts;
1455 }
1456}
1457
Andrew Trickef9de2a2013-05-25 02:42:55 +00001458SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001459 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001460 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1461 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001462
1463 // Canonicalize shuffle undef, undef -> undef
1464 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001465 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001466
Eric Christopherdfda92b2009-08-22 00:40:45 +00001467 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001468 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001469 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001470 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001471 for (unsigned i = 0; i != NElts; ++i) {
1472 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001473 MaskVec.push_back(Mask[i]);
1474 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001475
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001476 // Canonicalize shuffle v, v -> v, undef
1477 if (N1 == N2) {
1478 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001479 for (unsigned i = 0; i != NElts; ++i)
1480 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001481 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001482
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001483 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1484 if (N1.getOpcode() == ISD::UNDEF)
1485 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001486
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001487 // Canonicalize all index into lhs, -> shuffle lhs, undef
1488 // Canonicalize all index into rhs, -> shuffle rhs, undef
1489 bool AllLHS = true, AllRHS = true;
1490 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001491 for (unsigned i = 0; i != NElts; ++i) {
1492 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001493 if (N2Undef)
1494 MaskVec[i] = -1;
1495 else
1496 AllLHS = false;
1497 } else if (MaskVec[i] >= 0) {
1498 AllRHS = false;
1499 }
1500 }
1501 if (AllLHS && AllRHS)
1502 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001503 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001504 N2 = getUNDEF(VT);
1505 if (AllRHS) {
1506 N1 = getUNDEF(VT);
1507 commuteShuffle(N1, N2, MaskVec);
1508 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001509 // Reset our undef status after accounting for the mask.
1510 N2Undef = N2.getOpcode() == ISD::UNDEF;
1511 // Re-check whether both sides ended up undef.
1512 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1513 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001514
Craig Topper9a39b072013-08-08 08:03:12 +00001515 // If Identity shuffle return that node.
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001516 bool Identity = true, AllSame = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001517 for (unsigned i = 0; i != NElts; ++i) {
1518 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001519 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001520 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001521 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001522 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001523
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001524 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001525 if (N2Undef) {
1526 SDValue V = N1;
1527
1528 // Look through any bitcasts. We check that these don't change the number
1529 // (and size) of elements and just changes their types.
1530 while (V.getOpcode() == ISD::BITCAST)
1531 V = V->getOperand(0);
1532
1533 // A splat should always show up as a build vector node.
1534 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001535 BitVector UndefElements;
1536 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001537 // If this is a splat of an undef, shuffling it is also undef.
1538 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1539 return getUNDEF(VT);
1540
Andrea Di Biagio83814752015-01-24 11:54:29 +00001541 bool SameNumElts =
1542 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1543
Chandler Carruth142e9662014-07-08 08:45:38 +00001544 // We only have a splat which can skip shuffles if there is a splatted
1545 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001546 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001547 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1548 // number of elements match or the value splatted is a zero constant.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001549 if (SameNumElts)
Chandler Carruth142e9662014-07-08 08:45:38 +00001550 return N1;
1551 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1552 if (C->isNullValue())
1553 return N1;
1554 }
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001555
1556 // If the shuffle itself creates a constant splat, build the vector
1557 // directly.
Andrea Di Biagio83814752015-01-24 11:54:29 +00001558 if (AllSame && SameNumElts) {
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001559 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1560 if (isa<ConstantSDNode>(Splatted) || isa<ConstantFPSDNode>(Splatted)) {
1561 SmallVector<SDValue, 8> Ops;
Andrea Di Biagio83814752015-01-24 11:54:29 +00001562 for (unsigned i = 0; i != NElts; ++i)
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001563 Ops.push_back(Splatted);
Andrea Di Biagio83814752015-01-24 11:54:29 +00001564
1565 SDValue NewBV =
1566 getNode(ISD::BUILD_VECTOR, dl, BV->getValueType(0), Ops);
Michael Kuperstein25e34d12015-01-22 13:07:28 +00001567
1568 // We may have jumped through bitcasts, so the type of the
1569 // BUILD_VECTOR may not match the type of the shuffle.
1570 if (BV->getValueType(0) != VT)
1571 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1572 return NewBV;
1573 }
1574 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001575 }
1576 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001577
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001578 FoldingSetNodeID ID;
1579 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001580 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001581 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001582 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001583
Craig Topperc0196b12014-04-14 00:51:57 +00001584 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001585 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001586 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001587
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001588 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1589 // SDNode doesn't have access to it. This memory will be "leaked" when
1590 // the node is deallocated, but recovered when the NodeAllocator is released.
1591 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1592 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001593
Dan Gohman01c65a22010-03-18 18:49:47 +00001594 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001595 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1596 dl.getDebugLoc(), N1, N2,
1597 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001598 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001599 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001600 return SDValue(N, 0);
1601}
1602
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001603SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1604 MVT VT = SV.getSimpleValueType(0);
1605 unsigned NumElems = VT.getVectorNumElements();
1606 SmallVector<int, 8> MaskVec;
1607
1608 for (unsigned i = 0; i != NumElems; ++i) {
1609 int Idx = SV.getMaskElt(i);
1610 if (Idx >= 0) {
1611 if (Idx < (int)NumElems)
1612 Idx += NumElems;
1613 else
1614 Idx -= NumElems;
1615 }
1616 MaskVec.push_back(Idx);
1617 }
1618
1619 SDValue Op0 = SV.getOperand(0);
1620 SDValue Op1 = SV.getOperand(1);
1621 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1622}
1623
Andrew Trickef9de2a2013-05-25 02:42:55 +00001624SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001625 SDValue Val, SDValue DTy,
1626 SDValue STy, SDValue Rnd, SDValue Sat,
1627 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001628 // If the src and dest types are the same and the conversion is between
1629 // integer types of the same sign or two floats, no conversion is necessary.
1630 if (DTy == STy &&
1631 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001632 return Val;
1633
1634 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001635 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001636 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001637 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001638 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001639 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001640
Jack Carter170a5f22013-09-09 22:02:08 +00001641 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1642 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001643 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001644 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001645 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001646 return SDValue(N, 0);
1647}
1648
Owen Anderson53aa7a92009-08-10 22:56:29 +00001649SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001650 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001651 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001652 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001653 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001654 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001655 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001656
Dan Gohman01c65a22010-03-18 18:49:47 +00001657 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001658 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001659 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001660 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001661}
1662
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001663SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1664 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001665 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001666 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001667 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001668 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1669 return SDValue(E, 0);
1670
1671 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1672 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001673 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001674 return SDValue(N, 0);
1675}
1676
Andrew Trickef9de2a2013-05-25 02:42:55 +00001677SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001678 FoldingSetNodeID ID;
1679 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001680 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001681 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001682 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001683 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001684 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001685
Jack Carter170a5f22013-09-09 22:02:08 +00001686 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1687 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001688 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001689 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001690 return SDValue(N, 0);
1691}
1692
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001693
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001694SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001695 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001696 bool isTarget,
1697 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001698 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1699
1700 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001701 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001702 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001703 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001704 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001705 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001706 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001707 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001708
Michael Liaoabb87d42012-09-12 21:43:09 +00001709 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1710 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001711 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001712 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001713 return SDValue(N, 0);
1714}
1715
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001716SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001717 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001718 "SrcValue is not a pointer?");
1719
Jim Laskeyf576b422006-10-27 23:46:08 +00001720 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001721 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001722 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001723
Craig Topperc0196b12014-04-14 00:51:57 +00001724 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001725 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001726 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001727
Dan Gohman01c65a22010-03-18 18:49:47 +00001728 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001729 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001730 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001731 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001732}
1733
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001734/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1735SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1736 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001737 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001738 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001739
Craig Topperc0196b12014-04-14 00:51:57 +00001740 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001741 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1742 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001743
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001744 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1745 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001746 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001747 return SDValue(N, 0);
1748}
1749
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001750/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1751SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1752 unsigned SrcAS, unsigned DestAS) {
1753 SDValue Ops[] = {Ptr};
1754 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001755 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001756 ID.AddInteger(SrcAS);
1757 ID.AddInteger(DestAS);
1758
Craig Topperc0196b12014-04-14 00:51:57 +00001759 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001760 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1761 return SDValue(E, 0);
1762
1763 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1764 dl.getDebugLoc(),
1765 VT, Ptr, SrcAS, DestAS);
1766 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001767 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001768 return SDValue(N, 0);
1769}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001770
Duncan Sands41826032009-01-31 15:50:11 +00001771/// getShiftAmountOperand - Return the specified value casted to
1772/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001773SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001774 EVT OpTy = Op.getValueType();
Eric Christopher1e845f22014-10-08 21:08:32 +00001775 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001776 if (OpTy == ShTy || OpTy.isVector()) return Op;
1777
1778 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001779 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001780}
1781
Chris Lattner9eb7a822007-10-15 17:47:20 +00001782/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1783/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001784SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001785 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001786 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001787 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang5c755ff2008-07-05 20:40:31 +00001788 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001789 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001790
David Greene1fbe0542009-11-12 20:49:22 +00001791 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001792 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001793}
1794
Duncan Sands445071c2008-12-09 21:33:20 +00001795/// CreateStackTemporary - Create a stack temporary suitable for holding
1796/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001797SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001798 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1799 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001800 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1801 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001802 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001803 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1804 TD->getPrefTypeAlignment(Ty2));
1805
1806 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001807 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001808 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001809}
1810
Owen Anderson53aa7a92009-08-10 22:56:29 +00001811SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001812 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001813 // These setcc operations always fold.
1814 switch (Cond) {
1815 default: break;
1816 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001817 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001818 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001819 case ISD::SETTRUE2: {
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001820 TargetLowering::BooleanContent Cnt =
1821 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001822 return getConstant(
1823 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1824 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001825
Chris Lattner393d96a2006-04-27 05:01:07 +00001826 case ISD::SETOEQ:
1827 case ISD::SETOGT:
1828 case ISD::SETOGE:
1829 case ISD::SETOLT:
1830 case ISD::SETOLE:
1831 case ISD::SETONE:
1832 case ISD::SETO:
1833 case ISD::SETUO:
1834 case ISD::SETUEQ:
1835 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001836 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001837 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001838 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001839
Gabor Greiff304a7a2008-08-28 21:40:38 +00001840 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001841 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001842 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001843 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001844
Chris Lattner061a1ea2005-01-07 07:46:32 +00001845 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001846 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001847 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1848 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001849 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1850 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1851 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1852 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1853 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1854 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1855 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1856 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001857 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001858 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001859 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001860 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1861 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001862 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001863 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001864 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001865 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001866 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001867 // fall through
1868 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001869 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001870 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001871 // fall through
1872 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001873 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001874 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001875 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001876 // fall through
1877 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001878 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001879 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001880 // fall through
1881 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001882 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001883 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001884 // fall through
1885 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001886 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001887 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001888 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001889 // fall through
1890 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001891 R==APFloat::cmpEqual, VT);
1892 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1893 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1894 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1895 R==APFloat::cmpEqual, VT);
1896 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1897 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1898 R==APFloat::cmpLessThan, VT);
1899 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1900 R==APFloat::cmpUnordered, VT);
1901 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1902 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001903 }
1904 } else {
1905 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001906 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1907 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopher1e845f22014-10-08 21:08:32 +00001908 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001909 return SDValue();
1910
1911 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001912 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001913 }
1914
Chris Lattnerd47675e2005-08-09 20:20:18 +00001915 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001916 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001917}
1918
Dan Gohman1f372ed2008-02-25 21:11:39 +00001919/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1920/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001921bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001922 // This predicate is not safe for vector operations.
1923 if (Op.getValueType().isVector())
1924 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001925
Dan Gohman1d459e42009-12-11 21:31:27 +00001926 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001927 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1928}
1929
Dan Gohman309d3d52007-06-22 14:59:07 +00001930/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1931/// this predicate to simplify operations downstream. Mask is known to be zero
1932/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001933bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001934 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001935 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001936 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001937 return (KnownZero & Mask) == Mask;
1938}
1939
Jay Foada0653a32014-05-14 21:14:37 +00001940/// Determine which bits of Op are known to be either zero or one and return
1941/// them in the KnownZero/KnownOne bitsets.
1942void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1943 APInt &KnownOne, unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001944 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001945
Dan Gohmanf990faf2008-02-13 00:35:47 +00001946 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001947 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001948 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001949
Dan Gohmanf990faf2008-02-13 00:35:47 +00001950 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001951
1952 switch (Op.getOpcode()) {
1953 case ISD::Constant:
1954 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001955 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1956 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001957 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001958 case ISD::AND:
1959 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001960 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1961 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001962
1963 // Output known-1 bits are only known if set in both the LHS & RHS.
1964 KnownOne &= KnownOne2;
1965 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1966 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001967 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001968 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001969 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1970 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001971
Dan Gohman309d3d52007-06-22 14:59:07 +00001972 // Output known-0 bits are only known if clear in both the LHS & RHS.
1973 KnownZero &= KnownZero2;
1974 // Output known-1 are known to be set if set in either the LHS | RHS.
1975 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001976 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001977 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001978 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1979 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001980
Dan Gohman309d3d52007-06-22 14:59:07 +00001981 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001982 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001983 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1984 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1985 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001986 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001987 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001988 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00001989 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1990 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001991
1992 // If low bits are zero in either operand, output low known-0 bits.
1993 // Also compute a conserative estimate for high known-0 bits.
1994 // More trickiness is possible, but this is sufficient for the
1995 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001996 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001997 unsigned TrailZ = KnownZero.countTrailingOnes() +
1998 KnownZero2.countTrailingOnes();
1999 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00002000 KnownZero2.countLeadingOnes(),
2001 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002002
2003 TrailZ = std::min(TrailZ, BitWidth);
2004 LeadZ = std::min(LeadZ, BitWidth);
2005 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2006 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002007 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002008 }
2009 case ISD::UDIV: {
2010 // For the purposes of computing leading zeros we can conservatively
2011 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002012 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002013 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002014 unsigned LeadZ = KnownZero2.countLeadingOnes();
2015
Jay Foad25a5e4c2010-12-01 08:53:58 +00002016 KnownOne2.clearAllBits();
2017 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002018 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002019 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2020 if (RHSUnknownLeadingOnes != BitWidth)
2021 LeadZ = std::min(BitWidth,
2022 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002023
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002024 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002025 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002026 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002027 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002028 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2029 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002030
Dan Gohman309d3d52007-06-22 14:59:07 +00002031 // Only known if known in both the LHS and RHS.
2032 KnownOne &= KnownOne2;
2033 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002034 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002035 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002036 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2037 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002038
Dan Gohman309d3d52007-06-22 14:59:07 +00002039 // Only known if known in both the LHS and RHS.
2040 KnownOne &= KnownOne2;
2041 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002042 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002043 case ISD::SADDO:
2044 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002045 case ISD::SSUBO:
2046 case ISD::USUBO:
2047 case ISD::SMULO:
2048 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002049 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002050 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002051 // The boolean result conforms to getBooleanContents.
2052 // If we know the result of a setcc has the top bits zero, use this info.
2053 // We know that we have an integer-based boolean since these operations
2054 // are only available for integer.
2055 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2056 TargetLowering::ZeroOrOneBooleanContent &&
2057 BitWidth > 1)
2058 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2059 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002060 case ISD::SETCC:
2061 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002062 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2063 TargetLowering::ZeroOrOneBooleanContent &&
2064 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002065 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002066 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002067 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002068 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002069 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002070 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002071
2072 // If the shift count is an invalid immediate, don't do anything.
2073 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002074 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002075
Jay Foada0653a32014-05-14 21:14:37 +00002076 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002077 KnownZero <<= ShAmt;
2078 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002079 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002080 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002081 }
Jay Foad5a29c362014-05-15 12:12:55 +00002082 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002083 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002084 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002085 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002086 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002087
Dan Gohman9db0aa82008-02-26 18:50:50 +00002088 // If the shift count is an invalid immediate, don't do anything.
2089 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002090 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002091
Jay Foada0653a32014-05-14 21:14:37 +00002092 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002093 KnownZero = KnownZero.lshr(ShAmt);
2094 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002095
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002096 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002097 KnownZero |= HighBits; // High bits known zero.
2098 }
Jay Foad5a29c362014-05-15 12:12:55 +00002099 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002100 case ISD::SRA:
2101 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002102 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002103
Dan Gohman9db0aa82008-02-26 18:50:50 +00002104 // If the shift count is an invalid immediate, don't do anything.
2105 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002106 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002107
Dan Gohman309d3d52007-06-22 14:59:07 +00002108 // If any of the demanded bits are produced by the sign extension, we also
2109 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002110 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002111
Jay Foada0653a32014-05-14 21:14:37 +00002112 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002113 KnownZero = KnownZero.lshr(ShAmt);
2114 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002115
Dan Gohman309d3d52007-06-22 14:59:07 +00002116 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002117 APInt SignBit = APInt::getSignBit(BitWidth);
2118 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002119
Dan Gohmanb717fda2008-02-20 16:30:17 +00002120 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002121 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002122 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002123 KnownOne |= HighBits; // New bits are known one.
2124 }
2125 }
Jay Foad5a29c362014-05-15 12:12:55 +00002126 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002127 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002128 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002129 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002130
2131 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002132 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002133 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002134
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002135 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002136 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002137
Dan Gohman309d3d52007-06-22 14:59:07 +00002138 // If the sign extended bits are demanded, we know that the sign
2139 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002140 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002141 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002142 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002143
Jay Foada0653a32014-05-14 21:14:37 +00002144 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002145 KnownOne &= InputDemandedBits;
2146 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002147
Dan Gohman309d3d52007-06-22 14:59:07 +00002148 // If the sign bit of the input is known set or clear, then we know the
2149 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002150 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002151 KnownZero |= NewBits;
2152 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002153 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002154 KnownOne |= NewBits;
2155 KnownZero &= ~NewBits;
2156 } else { // Input sign bit unknown
2157 KnownZero &= ~NewBits;
2158 KnownOne &= ~NewBits;
2159 }
Jay Foad5a29c362014-05-15 12:12:55 +00002160 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002161 }
2162 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002163 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002164 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002165 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002166 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002167 unsigned LowBits = Log2_32(BitWidth)+1;
2168 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002169 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002170 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002171 }
2172 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002173 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002174 // If this is a ZEXTLoad and we are looking at the loaded value.
2175 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002176 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002177 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002178 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002179 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002180 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002181 }
Jay Foad5a29c362014-05-15 12:12:55 +00002182 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002183 }
2184 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002185 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002186 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002187 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002188 KnownZero = KnownZero.trunc(InBits);
2189 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002190 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002191 KnownZero = KnownZero.zext(BitWidth);
2192 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002193 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002194 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002195 }
2196 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002197 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002198 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002199 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002200
Jay Foad583abbc2010-12-07 08:25:19 +00002201 KnownZero = KnownZero.trunc(InBits);
2202 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002203 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002204
2205 // Note if the sign bit is known to be zero or one.
2206 bool SignBitKnownZero = KnownZero.isNegative();
2207 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002208
Jay Foad583abbc2010-12-07 08:25:19 +00002209 KnownZero = KnownZero.zext(BitWidth);
2210 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002211
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002212 // If the sign bit is known zero or one, the top bits match.
2213 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002214 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002215 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002216 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002217 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002218 }
2219 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002220 EVT InVT = Op.getOperand(0).getValueType();
2221 unsigned InBits = InVT.getScalarType().getSizeInBits();
2222 KnownZero = KnownZero.trunc(InBits);
2223 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002224 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002225 KnownZero = KnownZero.zext(BitWidth);
2226 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002227 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002228 }
2229 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002230 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002231 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002232 KnownZero = KnownZero.zext(InBits);
2233 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002234 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002235 KnownZero = KnownZero.trunc(BitWidth);
2236 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002237 break;
2238 }
2239 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002240 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002241 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002242 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002243 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002244 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002245 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002246 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002247 case ISD::FGETSIGN:
2248 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002249 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002250 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002251
Dan Gohman72ec3f42008-04-28 17:02:21 +00002252 case ISD::SUB: {
2253 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2254 // We know that the top bits of C-X are clear if X contains less bits
2255 // than C (i.e. no wrap-around can happen). For example, 20-X is
2256 // positive if we can prove that X is >= 0 and < 16.
2257 if (CLHS->getAPIntValue().isNonNegative()) {
2258 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2259 // NLZ can't be BitWidth with no sign bit
2260 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002261 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002262
2263 // If all of the MaskV bits are known to be zero, then we know the
2264 // output top bits are zero, because we now know that the output is
2265 // from [0-C].
2266 if ((KnownZero2 & MaskV) == MaskV) {
2267 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2268 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002269 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002270 }
2271 }
2272 }
2273 }
2274 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002275 case ISD::ADD:
2276 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002277 // Output known-0 bits are known if clear or set in both the low clear bits
2278 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2279 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002280 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002281 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2282
Jay Foada0653a32014-05-14 21:14:37 +00002283 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002284 KnownZeroOut = std::min(KnownZeroOut,
2285 KnownZero2.countTrailingOnes());
2286
Chris Lattner440b2802010-12-19 20:38:28 +00002287 if (Op.getOpcode() == ISD::ADD) {
2288 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002289 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002290 }
2291
2292 // With ADDE, a carry bit may be added in, so we can only use this
2293 // information if we know (at least) that the low two bits are clear. We
2294 // then return to the caller that the low bit is unknown but that other bits
2295 // are known zero.
2296 if (KnownZeroOut >= 2) // ADDE
2297 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002298 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002299 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002300 case ISD::SREM:
2301 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002302 const APInt &RA = Rem->getAPIntValue().abs();
2303 if (RA.isPowerOf2()) {
2304 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002305 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002306
Duncan Sands33274982010-01-29 09:45:26 +00002307 // The low bits of the first operand are unchanged by the srem.
2308 KnownZero = KnownZero2 & LowBits;
2309 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002310
Duncan Sands33274982010-01-29 09:45:26 +00002311 // If the first operand is non-negative or has all low bits zero, then
2312 // the upper bits are all zero.
2313 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2314 KnownZero |= ~LowBits;
2315
2316 // If the first operand is negative and not all low bits are zero, then
2317 // the upper bits are all one.
2318 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2319 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002320 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002321 }
2322 }
Jay Foad5a29c362014-05-15 12:12:55 +00002323 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002324 case ISD::UREM: {
2325 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002326 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002327 if (RA.isPowerOf2()) {
2328 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002329 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2330
2331 // The upper bits are all zero, the lower ones are unchanged.
2332 KnownZero = KnownZero2 | ~LowBits;
2333 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002334 break;
2335 }
2336 }
2337
2338 // Since the result is less than or equal to either operand, any leading
2339 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002340 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2341 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002342
2343 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2344 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002345 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002346 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002347 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002348 }
Jan Vesely6269e3c2015-01-22 23:42:41 +00002349 case ISD::EXTRACT_ELEMENT: {
2350 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2351 const unsigned Index =
2352 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2353 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2354
2355 // Remove low part of known bits mask
2356 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2357 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2358
2359 // Remove high part of known bit mask
2360 KnownZero = KnownZero.trunc(BitWidth);
2361 KnownOne = KnownOne.trunc(BitWidth);
2362 break;
2363 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002364 case ISD::FrameIndex:
2365 case ISD::TargetFrameIndex:
2366 if (unsigned Align = InferPtrAlignment(Op)) {
2367 // The low bits are known zero if the pointer is aligned.
2368 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002369 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002370 }
2371 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002372
Dan Gohman309d3d52007-06-22 14:59:07 +00002373 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002374 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2375 break;
2376 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 case ISD::INTRINSIC_WO_CHAIN:
2378 case ISD::INTRINSIC_W_CHAIN:
2379 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002380 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002381 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002382 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002383 }
Jay Foad5a29c362014-05-15 12:12:55 +00002384
2385 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002386}
2387
2388/// ComputeNumSignBits - Return the number of times the sign bit of the
2389/// register is replicated into the other bits. We know that at least 1 bit
2390/// is always equal to the sign bit (itself), but other cases can give us
2391/// information. For example, immediately after an "SRA X, 2", we know that
2392/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002393unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Anderson53aa7a92009-08-10 22:56:29 +00002394 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002395 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002396 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002397 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002398 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002399
Dan Gohman309d3d52007-06-22 14:59:07 +00002400 if (Depth == 6)
2401 return 1; // Limit search depth.
2402
2403 switch (Op.getOpcode()) {
2404 default: break;
2405 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002406 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002407 return VTBits-Tmp+1;
2408 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002409 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002410 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002411
Dan Gohman309d3d52007-06-22 14:59:07 +00002412 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002413 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002414 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002415 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002416
Dan Gohman309d3d52007-06-22 14:59:07 +00002417 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002418 Tmp =
2419 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002420 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002421
Dan Gohman309d3d52007-06-22 14:59:07 +00002422 case ISD::SIGN_EXTEND_INREG:
2423 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002424 Tmp =
2425 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002427
Dan Gohman309d3d52007-06-22 14:59:07 +00002428 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2429 return std::max(Tmp, Tmp2);
2430
2431 case ISD::SRA:
2432 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2433 // SRA X, C -> adds C sign bits.
2434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002435 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002436 if (Tmp > VTBits) Tmp = VTBits;
2437 }
2438 return Tmp;
2439 case ISD::SHL:
2440 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2441 // shl destroys sign bits.
2442 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002443 if (C->getZExtValue() >= VTBits || // Bad shift.
2444 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2445 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 }
2447 break;
2448 case ISD::AND:
2449 case ISD::OR:
2450 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002451 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002452 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002453 if (Tmp != 1) {
2454 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2455 FirstAnswer = std::min(Tmp, Tmp2);
2456 // We computed what we know about the sign bits as our first
2457 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002458 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002459 }
2460 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002461
2462 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002463 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002464 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002465 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002466 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002467
2468 case ISD::SADDO:
2469 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002470 case ISD::SSUBO:
2471 case ISD::USUBO:
2472 case ISD::SMULO:
2473 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002474 if (Op.getResNo() != 1)
2475 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002476 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002477 // If setcc returns 0/-1, all bits are sign bits.
2478 // We know that we have an integer-based boolean since these operations
2479 // are only available for integer.
2480 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2481 TargetLowering::ZeroOrNegativeOneBooleanContent)
2482 return VTBits;
2483 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002484 case ISD::SETCC:
2485 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002486 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002487 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002488 return VTBits;
2489 break;
2490 case ISD::ROTL:
2491 case ISD::ROTR:
2492 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002493 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002494
Dan Gohman309d3d52007-06-22 14:59:07 +00002495 // Handle rotate right by N like a rotate left by 32-N.
2496 if (Op.getOpcode() == ISD::ROTR)
2497 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2498
2499 // If we aren't rotating out all of the known-in sign bits, return the
2500 // number that are left. This handles rotl(sext(x), 1) for example.
2501 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2502 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2503 }
2504 break;
2505 case ISD::ADD:
2506 // Add can have at most one carry bit. Thus we know that the output
2507 // is, at worst, one more bit than the inputs.
2508 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2509 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002510
Dan Gohman309d3d52007-06-22 14:59:07 +00002511 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002512 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002513 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002514 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002515 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002516
Dan Gohman309d3d52007-06-22 14:59:07 +00002517 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2518 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002519 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002520 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002521
Dan Gohman309d3d52007-06-22 14:59:07 +00002522 // If we are subtracting one from a positive number, there is no carry
2523 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002524 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002525 return Tmp;
2526 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002527
Dan Gohman309d3d52007-06-22 14:59:07 +00002528 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2529 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002530 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002531
Dan Gohman309d3d52007-06-22 14:59:07 +00002532 case ISD::SUB:
2533 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2534 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002535
Dan Gohman309d3d52007-06-22 14:59:07 +00002536 // Handle NEG.
2537 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002538 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002539 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002540 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002541 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2542 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002543 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002544 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002545
Dan Gohman309d3d52007-06-22 14:59:07 +00002546 // If the input is known to be positive (the sign bit is known clear),
2547 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002548 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002549 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002550
Dan Gohman309d3d52007-06-22 14:59:07 +00002551 // Otherwise, we treat this like a SUB.
2552 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002553
Dan Gohman309d3d52007-06-22 14:59:07 +00002554 // Sub can have at most one carry bit. Thus we know that the output
2555 // is, at worst, one more bit than the inputs.
2556 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2557 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002558 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002559 case ISD::TRUNCATE:
2560 // FIXME: it's tricky to do anything useful for this, but it is an important
2561 // case for targets like X86.
2562 break;
Jan Vesely6269e3c2015-01-22 23:42:41 +00002563 case ISD::EXTRACT_ELEMENT: {
2564 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2565 const int BitWidth = Op.getValueType().getSizeInBits();
2566 const int Items =
2567 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2568
2569 // Get reverse index (starting from 1), Op1 value indexes elements from
2570 // little end. Sign starts at big end.
2571 const int rIndex = Items - 1 -
2572 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2573
2574 // If the sign portion ends in our element the substraction gives correct
2575 // result. Otherwise it gives either negative or > bitwidth result
2576 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2577 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002578 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002579
Nadav Rotem4536d582013-03-20 22:53:44 +00002580 // If we are looking at the loaded value of the SDNode.
2581 if (Op.getResNo() == 0) {
2582 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2583 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2584 unsigned ExtType = LD->getExtensionType();
2585 switch (ExtType) {
2586 default: break;
2587 case ISD::SEXTLOAD: // '17' bits known
2588 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2589 return VTBits-Tmp+1;
2590 case ISD::ZEXTLOAD: // '16' bits known
2591 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2592 return VTBits-Tmp;
2593 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002594 }
2595 }
2596
2597 // Allow the target to implement this method for its nodes.
2598 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002599 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002600 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2601 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002602 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002603 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002604 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002605
Dan Gohman309d3d52007-06-22 14:59:07 +00002606 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2607 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002608 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002609 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002610
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002611 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002612 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002613 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002614 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002615 Mask = KnownOne;
2616 } else {
2617 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002618 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002619 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002620
Dan Gohman309d3d52007-06-22 14:59:07 +00002621 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2622 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002623 Mask = ~Mask;
2624 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002625 // Return # leading zeros. We use 'min' here in case Val was zero before
2626 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002627 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002628}
2629
Chris Lattner46c01a32011-02-13 22:25:43 +00002630/// isBaseWithConstantOffset - Return true if the specified operand is an
2631/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2632/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2633/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002634/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002635bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2636 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2637 !isa<ConstantSDNode>(Op.getOperand(1)))
2638 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002639
2640 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002641 !MaskedValueIsZero(Op.getOperand(0),
2642 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2643 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002644
Chris Lattner46c01a32011-02-13 22:25:43 +00002645 return true;
2646}
2647
2648
Dan Gohmand0d5e682009-09-03 20:34:31 +00002649bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2650 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002651 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002652 return true;
2653
2654 // If the value is a constant, we can obviously see if it is a NaN or not.
2655 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2656 return !C->getValueAPF().isNaN();
2657
2658 // TODO: Recognize more cases here.
2659
2660 return false;
2661}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002662
Dan Gohman38605212010-02-24 06:52:40 +00002663bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2664 // If the value is a constant, we can obviously see if it is a zero or not.
2665 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2666 return !C->isZero();
2667
2668 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002669 switch (Op.getOpcode()) {
2670 default: break;
2671 case ISD::OR:
2672 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2673 return !C->isNullValue();
2674 break;
2675 }
Dan Gohman38605212010-02-24 06:52:40 +00002676
2677 return false;
2678}
2679
2680bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2681 // Check the obvious case.
2682 if (A == B) return true;
2683
2684 // For for negative and positive zero.
2685 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2686 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2687 if (CA->isZero() && CB->isZero()) return true;
2688
2689 // Otherwise they may not be equal.
2690 return false;
2691}
2692
Chris Lattner061a1ea2005-01-07 07:46:32 +00002693/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002694///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002695SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002696 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002697 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002698 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002699 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002700 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002701
Jack Carter170a5f22013-09-09 22:02:08 +00002702 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2703 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002704 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002705
Chandler Carruth41b20e72014-07-22 04:07:55 +00002706 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002707 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002708}
2709
Andrew Trickef9de2a2013-05-25 02:42:55 +00002710SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002711 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002712 // Constant fold unary operations with an integer constant operand. Even
2713 // opaque constant will be folded, because the folding of unary operations
2714 // doesn't create new constants with different values. Nevertheless, the
2715 // opaque flag is preserved during folding to prevent future folding with
2716 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002717 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002718 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002719 switch (Opcode) {
2720 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002721 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002722 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2723 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002724 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002725 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002726 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002727 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2728 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002729 case ISD::UINT_TO_FP:
2730 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002731 APFloat apf(EVTToAPFloatSemantics(VT),
2732 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002733 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002734 Opcode==ISD::SINT_TO_FP,
2735 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002736 return getConstantFP(apf, VT);
2737 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002738 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002739 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
2740 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002741 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002742 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002743 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002744 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002745 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002746 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002747 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2748 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002749 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002750 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2751 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002752 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002753 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002754 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2755 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002756 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002757 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002758 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2759 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002760 }
2761 }
2762
Dale Johannesen446b9002007-08-31 23:34:27 +00002763 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002764 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002765 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002766 switch (Opcode) {
2767 case ISD::FNEG:
2768 V.changeSign();
2769 return getConstantFP(V, VT);
2770 case ISD::FABS:
2771 V.clearSign();
2772 return getConstantFP(V, VT);
2773 case ISD::FCEIL: {
2774 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2775 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002776 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002777 break;
2778 }
2779 case ISD::FTRUNC: {
2780 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2781 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002782 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002783 break;
2784 }
2785 case ISD::FFLOOR: {
2786 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2787 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002788 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002789 break;
2790 }
2791 case ISD::FP_EXTEND: {
2792 bool ignored;
2793 // This can return overflow, underflow, or inexact; we don't care.
2794 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002795 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002796 APFloat::rmNearestTiesToEven, &ignored);
2797 return getConstantFP(V, VT);
2798 }
2799 case ISD::FP_TO_SINT:
2800 case ISD::FP_TO_UINT: {
2801 integerPart x[2];
2802 bool ignored;
Benjamin Kramer7000ca32014-10-12 17:56:40 +00002803 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002804 // FIXME need to be more flexible about rounding mode.
2805 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2806 Opcode==ISD::FP_TO_SINT,
2807 APFloat::rmTowardZero, &ignored);
2808 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002809 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002810 APInt api(VT.getSizeInBits(), x);
2811 return getConstant(api, VT);
2812 }
2813 case ISD::BITCAST:
Owen Anderson558012a2014-12-09 06:50:39 +00002814 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
2815 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), VT);
2816 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002817 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2818 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2819 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2820 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002821 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002822 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002823
Jim Grosbachf7502c42014-07-18 00:40:52 +00002824 // Constant fold unary operations with a vector integer operand.
2825 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002826 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002827 switch (Opcode) {
2828 default:
2829 // FIXME: Entirely reasonable to perform folding of other unary
2830 // operations here as the need arises.
2831 break;
2832 case ISD::UINT_TO_FP:
2833 case ISD::SINT_TO_FP: {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002834 SmallVector<SDValue, 8> Ops;
2835 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2836 SDValue OpN = BV->getOperand(i);
2837 // Let the above scalar folding handle the conversion of each
2838 // element.
2839 OpN = getNode(ISD::SINT_TO_FP, DL, VT.getVectorElementType(),
2840 OpN);
2841 Ops.push_back(OpN);
2842 }
2843 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Jim Grosbachf7502c42014-07-18 00:40:52 +00002844 }
2845 }
2846 }
2847 }
2848
Gabor Greiff304a7a2008-08-28 21:40:38 +00002849 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002850 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002851 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002852 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002853 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002854 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002855 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002856 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002857 assert(VT.isFloatingPoint() &&
2858 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002859 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002860 assert((!VT.isVector() ||
2861 VT.getVectorNumElements() ==
2862 Operand.getValueType().getVectorNumElements()) &&
2863 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002864 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002865 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002866 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002867 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002868 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002869 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002870 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002871 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2872 "Invalid sext node, dst < src!");
2873 assert((!VT.isVector() ||
2874 VT.getVectorNumElements() ==
2875 Operand.getValueType().getVectorNumElements()) &&
2876 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002877 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2878 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2879 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002880 // sext(undef) = 0, because the top bits will all be the same.
2881 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002882 break;
2883 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002884 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002885 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002886 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002887 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2888 "Invalid zext node, dst < src!");
2889 assert((!VT.isVector() ||
2890 VT.getVectorNumElements() ==
2891 Operand.getValueType().getVectorNumElements()) &&
2892 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002893 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002894 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002895 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002896 else if (OpOpcode == ISD::UNDEF)
2897 // zext(undef) = 0, because the top bits will be zero.
2898 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002899 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002900 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002901 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002902 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002903 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002904 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2905 "Invalid anyext node, dst < src!");
2906 assert((!VT.isVector() ||
2907 VT.getVectorNumElements() ==
2908 Operand.getValueType().getVectorNumElements()) &&
2909 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002910
Dan Gohman08837892010-06-18 00:08:30 +00002911 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2912 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002913 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002914 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002915 else if (OpOpcode == ISD::UNDEF)
2916 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002917
2918 // (ext (trunx x)) -> x
2919 if (OpOpcode == ISD::TRUNCATE) {
2920 SDValue OpOp = Operand.getNode()->getOperand(0);
2921 if (OpOp.getValueType() == VT)
2922 return OpOp;
2923 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002924 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002925 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002926 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002927 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002928 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002929 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2930 "Invalid truncate node, src < dst!");
2931 assert((!VT.isVector() ||
2932 VT.getVectorNumElements() ==
2933 Operand.getValueType().getVectorNumElements()) &&
2934 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002935 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002936 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002937 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2938 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002939 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002940 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2941 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002942 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002943 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002944 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002945 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002946 }
Craig Topper201c1a32012-01-15 01:05:11 +00002947 if (OpOpcode == ISD::UNDEF)
2948 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002949 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002950 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002951 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002952 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002953 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002954 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002955 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2956 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002957 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002958 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002959 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002960 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002961 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002962 (VT.getVectorElementType() == Operand.getValueType() ||
2963 (VT.getVectorElementType().isInteger() &&
2964 Operand.getValueType().isInteger() &&
2965 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002966 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002967 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002968 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002969 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2970 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2971 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2972 Operand.getConstantOperandVal(1) == 0 &&
2973 Operand.getOperand(0).getValueType() == VT)
2974 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002975 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002976 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002977 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002978 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002979 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002980 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002981 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002982 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002983 break;
2984 case ISD::FABS:
2985 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002986 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002987 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002988 }
2989
Chris Lattnerf9c19152005-08-25 19:12:10 +00002990 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002991 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002992 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002993 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002994 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002995 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002996 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002997 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002998 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002999
Jack Carter170a5f22013-09-09 22:02:08 +00003000 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3001 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003002 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003003 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003004 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3005 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003006 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00003007
Chandler Carruth41b20e72014-07-22 04:07:55 +00003008 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003009 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00003010}
3011
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003012SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
3013 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00003014 // If the opcode is a target-specific ISD node, there's nothing we can
3015 // do here and the operand rules may not line up with the below, so
3016 // bail early.
3017 if (Opcode >= ISD::BUILTIN_OP_END)
3018 return SDValue();
3019
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003020 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3021 SmallVector<SDValue, 4> Outputs;
3022 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00003023
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003024 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3025 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003026 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3027 return SDValue();
3028
3029 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003030 // Scalar instruction.
3031 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003032 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003033 // For vectors extract each constant element into Inputs so we can constant
3034 // fold them individually.
3035 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3036 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3037 if (!BV1 || !BV2)
3038 return SDValue();
3039
3040 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3041
3042 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3043 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3044 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3045 if (!V1 || !V2) // Not a constant, bail.
3046 return SDValue();
3047
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003048 if (V1->isOpaque() || V2->isOpaque())
3049 return SDValue();
3050
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003051 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3052 // FIXME: This is valid and could be handled by truncating the APInts.
3053 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3054 return SDValue();
3055
3056 Inputs.push_back(std::make_pair(V1, V2));
3057 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003058 }
3059
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003060 // We have a number of constant values, constant fold them element by element.
3061 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3062 const APInt &C1 = Inputs[I].first->getAPIntValue();
3063 const APInt &C2 = Inputs[I].second->getAPIntValue();
3064
3065 switch (Opcode) {
3066 case ISD::ADD:
3067 Outputs.push_back(getConstant(C1 + C2, SVT));
3068 break;
3069 case ISD::SUB:
3070 Outputs.push_back(getConstant(C1 - C2, SVT));
3071 break;
3072 case ISD::MUL:
3073 Outputs.push_back(getConstant(C1 * C2, SVT));
3074 break;
3075 case ISD::UDIV:
3076 if (!C2.getBoolValue())
3077 return SDValue();
3078 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3079 break;
3080 case ISD::UREM:
3081 if (!C2.getBoolValue())
3082 return SDValue();
3083 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3084 break;
3085 case ISD::SDIV:
3086 if (!C2.getBoolValue())
3087 return SDValue();
3088 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3089 break;
3090 case ISD::SREM:
3091 if (!C2.getBoolValue())
3092 return SDValue();
3093 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3094 break;
3095 case ISD::AND:
3096 Outputs.push_back(getConstant(C1 & C2, SVT));
3097 break;
3098 case ISD::OR:
3099 Outputs.push_back(getConstant(C1 | C2, SVT));
3100 break;
3101 case ISD::XOR:
3102 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3103 break;
3104 case ISD::SHL:
3105 Outputs.push_back(getConstant(C1 << C2, SVT));
3106 break;
3107 case ISD::SRL:
3108 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3109 break;
3110 case ISD::SRA:
3111 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3112 break;
3113 case ISD::ROTL:
3114 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3115 break;
3116 case ISD::ROTR:
3117 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3118 break;
3119 default:
3120 return SDValue();
3121 }
3122 }
3123
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003124 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3125 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003126
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003127 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003128 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003129 return Outputs.back();
3130
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003131 // We may have a vector type but a scalar result. Create a splat.
3132 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3133
3134 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003135 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003136}
3137
Andrew Trickef9de2a2013-05-25 02:42:55 +00003138SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003139 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003140 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3141 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003142 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003143 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003144 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003145 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3146 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003147 // Fold trivial token factors.
3148 if (N1.getOpcode() == ISD::EntryToken) return N2;
3149 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003150 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003151 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003152 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003153 // Concat of UNDEFs is UNDEF.
3154 if (N1.getOpcode() == ISD::UNDEF &&
3155 N2.getOpcode() == ISD::UNDEF)
3156 return getUNDEF(VT);
3157
Dan Gohman550c9af2008-08-14 20:04:46 +00003158 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3159 // one big BUILD_VECTOR.
3160 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3161 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003162 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3163 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003164 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003165 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003166 }
3167 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003168 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003169 assert(VT.isInteger() && "This operator does not apply to FP types!");
3170 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003171 N1.getValueType() == VT && "Binary operator types must match!");
3172 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3173 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003174 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003175 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003176 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3177 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003178 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003179 case ISD::OR:
3180 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003181 case ISD::ADD:
3182 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003183 assert(VT.isInteger() && "This operator does not apply to FP types!");
3184 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003185 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003186 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3187 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003188 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003189 return N1;
3190 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003191 case ISD::UDIV:
3192 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003193 case ISD::MULHU:
3194 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003195 case ISD::MUL:
3196 case ISD::SDIV:
3197 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003198 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003199 assert(N1.getValueType() == N2.getValueType() &&
3200 N1.getValueType() == VT && "Binary operator types must match!");
3201 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003202 case ISD::FADD:
3203 case ISD::FSUB:
3204 case ISD::FMUL:
3205 case ISD::FDIV:
3206 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003207 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003208 if (Opcode == ISD::FADD) {
3209 // 0+x --> x
3210 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3211 if (CFP->getValueAPF().isZero())
3212 return N2;
3213 // x+0 --> x
3214 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3215 if (CFP->getValueAPF().isZero())
3216 return N1;
3217 } else if (Opcode == ISD::FSUB) {
3218 // x-0 --> x
3219 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3220 if (CFP->getValueAPF().isZero())
3221 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003222 } else if (Opcode == ISD::FMUL) {
3223 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3224 SDValue V = N2;
3225
3226 // If the first operand isn't the constant, try the second
3227 if (!CFP) {
3228 CFP = dyn_cast<ConstantFPSDNode>(N2);
3229 V = N1;
3230 }
3231
3232 if (CFP) {
3233 // 0*x --> 0
3234 if (CFP->isZero())
3235 return SDValue(CFP,0);
3236 // 1*x --> x
3237 if (CFP->isExactlyValue(1.0))
3238 return V;
3239 }
Dan Gohman1275e282009-01-23 19:10:37 +00003240 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003241 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003242 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003243 assert(N1.getValueType() == N2.getValueType() &&
3244 N1.getValueType() == VT && "Binary operator types must match!");
3245 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003246 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3247 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003248 N1.getValueType().isFloatingPoint() &&
3249 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003250 "Invalid FCOPYSIGN!");
3251 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003252 case ISD::SHL:
3253 case ISD::SRA:
3254 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003255 case ISD::ROTL:
3256 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003257 assert(VT == N1.getValueType() &&
3258 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003259 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003260 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003261 assert((!VT.isVector() || VT == N2.getValueType()) &&
3262 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003263 // Verify that the shift amount VT is bit enough to hold valid shift
3264 // amounts. This catches things like trying to shift an i1024 value by an
3265 // i8, which is easy to fall into in generic code that uses
3266 // TLI.getShiftAmount().
3267 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003268 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003269 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003270
3271 // Always fold shifts of i1 values so the code generator doesn't need to
3272 // handle them. Since we know the size of the shift has to be less than the
3273 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003274 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003275 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003276 if (N2C && N2C->isNullValue())
3277 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003278 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003279 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003280 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003281 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003282 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003283 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003284 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003285 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003286 "type is vector!");
3287 assert((!EVT.isVector() ||
3288 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3289 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003290 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003291 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003292 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003293 break;
3294 }
Chris Lattner72733e52008-01-17 07:00:52 +00003295 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003296 assert(VT.isFloatingPoint() &&
3297 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003298 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003299 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003300 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003301 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003302 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003303 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003304 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003305 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003306 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003307 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003308 assert(!EVT.isVector() &&
3309 "AssertSExt/AssertZExt type should be the vector element type "
3310 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003311 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003312 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003313 break;
3314 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003315 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003316 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003317 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003318 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003319 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003320 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003321 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003322 "type is vector!");
3323 assert((!EVT.isVector() ||
3324 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3325 "Vector element counts must match in SIGN_EXTEND_INREG");
3326 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003327 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003328
Chris Lattner16713612008-01-22 19:09:33 +00003329 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003330 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003331 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003332 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003333 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003334 return getConstant(Val, VT);
3335 }
Chris Lattner16713612008-01-22 19:09:33 +00003336 break;
3337 }
3338 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003339 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3340 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003341 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003342
Chris Lattner16713612008-01-22 19:09:33 +00003343 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3344 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003345 if (N2C &&
3346 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003347 N1.getNumOperands() > 0) {
3348 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003349 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003350 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003351 N1.getOperand(N2C->getZExtValue() / Factor),
3352 getConstant(N2C->getZExtValue() % Factor,
3353 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003354 }
3355
3356 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3357 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003358 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3359 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003360
3361 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003362 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003363 // are promoted and implicitly truncated, and the result implicitly
3364 // extended. Make that explicit here.
3365 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003366
Bob Wilson59dbbb22009-04-13 22:05:19 +00003367 return Elt;
3368 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003369
Chris Lattner16713612008-01-22 19:09:33 +00003370 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3371 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003372 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003373 // If the indices are the same, return the inserted element else
3374 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003375 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003376 SDValue N1Op2 = N1.getOperand(2);
3377 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3378
3379 if (N1Op2C && N2C) {
3380 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3381 if (VT == N1.getOperand(1).getValueType())
3382 return N1.getOperand(1);
3383 else
3384 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3385 }
3386
Dale Johannesendb393622009-02-03 01:55:44 +00003387 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003388 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003389 }
Chris Lattner16713612008-01-22 19:09:33 +00003390 break;
3391 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003392 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003393 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3394 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003395 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003396 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003397
Chris Lattner16713612008-01-22 19:09:33 +00003398 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3399 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003400 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003401 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003402 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003403
Chris Lattner16713612008-01-22 19:09:33 +00003404 // EXTRACT_ELEMENT of a constant int is also very common.
3405 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003406 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003407 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003408 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3409 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003410 }
3411 break;
David Greeneb6f16112011-01-26 15:38:49 +00003412 case ISD::EXTRACT_SUBVECTOR: {
3413 SDValue Index = N2;
3414 if (VT.isSimple() && N1.getValueType().isSimple()) {
3415 assert(VT.isVector() && N1.getValueType().isVector() &&
3416 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003417 assert(VT.getVectorElementType() ==
3418 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003419 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003420 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003421 "Extract subvector must be from larger vector to smaller vector!");
3422
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003423 if (isa<ConstantSDNode>(Index.getNode())) {
3424 assert((VT.getVectorNumElements() +
3425 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003426 <= N1.getValueType().getVectorNumElements())
3427 && "Extract subvector overflow!");
3428 }
3429
3430 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003431 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003432 return N1;
3433 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003434 break;
Chris Lattner16713612008-01-22 19:09:33 +00003435 }
David Greeneb6f16112011-01-26 15:38:49 +00003436 }
Chris Lattner16713612008-01-22 19:09:33 +00003437
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003438 // Perform trivial constant folding.
Matthias Braunf50ab432015-01-13 22:17:46 +00003439 if (SDValue SV =
3440 FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode()))
3441 return SV;
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003442
3443 // Canonicalize constant to RHS if commutative.
3444 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3445 std::swap(N1C, N2C);
3446 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003447 }
3448
Chris Lattner16713612008-01-22 19:09:33 +00003449 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003450 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003451 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3452 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003453 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003454 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003455 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003456 std::swap(N1CFP, N2CFP);
3457 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003458 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003459 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3460 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003461 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003462 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003463 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003464 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003465 return getConstantFP(V1, VT);
3466 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003467 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003468 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003469 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003470 return getConstantFP(V1, VT);
3471 break;
3472 case ISD::FMUL:
3473 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003474 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003475 return getConstantFP(V1, VT);
3476 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003477 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003478 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003479 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3480 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003481 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003482 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003483 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003484 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003485 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003486 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3487 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003488 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003489 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003490 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003491 case ISD::FCOPYSIGN:
3492 V1.copySign(V2);
3493 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003494 default: break;
3495 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003496 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003497
3498 if (Opcode == ISD::FP_ROUND) {
3499 APFloat V = N1CFP->getValueAPF(); // make copy
3500 bool ignored;
3501 // This can return overflow, underflow, or inexact; we don't care.
3502 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003503 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003504 APFloat::rmNearestTiesToEven, &ignored);
3505 return getConstantFP(V, VT);
3506 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003507 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003508
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003509 // Canonicalize an UNDEF to the RHS, even over a constant.
3510 if (N1.getOpcode() == ISD::UNDEF) {
3511 if (isCommutativeBinOp(Opcode)) {
3512 std::swap(N1, N2);
3513 } else {
3514 switch (Opcode) {
3515 case ISD::FP_ROUND_INREG:
3516 case ISD::SIGN_EXTEND_INREG:
3517 case ISD::SUB:
3518 case ISD::FSUB:
3519 case ISD::FDIV:
3520 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003521 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003522 return N1; // fold op(undef, arg2) -> undef
3523 case ISD::UDIV:
3524 case ISD::SDIV:
3525 case ISD::UREM:
3526 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003527 case ISD::SRL:
3528 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003529 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003530 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3531 // For vectors, we can't easily build an all zero vector, just return
3532 // the LHS.
3533 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003534 }
3535 }
3536 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003537
3538 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003539 if (N2.getOpcode() == ISD::UNDEF) {
3540 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003541 case ISD::XOR:
3542 if (N1.getOpcode() == ISD::UNDEF)
3543 // Handle undef ^ undef -> 0 special case. This is a common
3544 // idiom (misuse).
3545 return getConstant(0, VT);
3546 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003547 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003548 case ISD::ADDC:
3549 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003550 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003551 case ISD::UDIV:
3552 case ISD::SDIV:
3553 case ISD::UREM:
3554 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003555 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003556 case ISD::FADD:
3557 case ISD::FSUB:
3558 case ISD::FMUL:
3559 case ISD::FDIV:
3560 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003561 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003562 return N2;
3563 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003564 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003565 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003566 case ISD::SRL:
3567 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003568 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003569 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3570 // For vectors, we can't easily build an all zero vector, just return
3571 // the LHS.
3572 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003573 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003574 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003575 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003576 // For vectors, we can't easily build an all one vector, just return
3577 // the LHS.
3578 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003579 case ISD::SRA:
3580 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003581 }
3582 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003583
Chris Lattner724f7ee2005-05-11 18:57:39 +00003584 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003585 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003586 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003587 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003588 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003589 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003590 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003591 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003592 if (BinOpHasFlags)
3593 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003594 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003595 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003596 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003597
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003598 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3599
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003600 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003601 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003602
3603 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003604 }
3605
Chandler Carruth41b20e72014-07-22 04:07:55 +00003606 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003607 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003608}
3609
Andrew Trickef9de2a2013-05-25 02:42:55 +00003610SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003611 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003612 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003613 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003614 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003615 case ISD::FMA: {
3616 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3617 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3618 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3619 if (N1CFP && N2CFP && N3CFP) {
3620 APFloat V1 = N1CFP->getValueAPF();
3621 const APFloat &V2 = N2CFP->getValueAPF();
3622 const APFloat &V3 = N3CFP->getValueAPF();
3623 APFloat::opStatus s =
3624 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Mehdi Amini50598132015-01-23 07:07:20 +00003625 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Owen Anderson32baf992013-05-09 22:27:13 +00003626 return getConstantFP(V1, VT);
3627 }
3628 break;
3629 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003630 case ISD::CONCAT_VECTORS:
3631 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3632 // one big BUILD_VECTOR.
3633 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3634 N2.getOpcode() == ISD::BUILD_VECTOR &&
3635 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003636 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3637 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003638 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3639 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003640 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003641 }
3642 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003643 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003644 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003645 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003646 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003647 break;
3648 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003649 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003650 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003651 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003652 return N2; // select true, X, Y -> X
3653 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003654 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003655
3656 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003657 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003658 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003659 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003660 case ISD::INSERT_SUBVECTOR: {
3661 SDValue Index = N3;
3662 if (VT.isSimple() && N1.getValueType().isSimple()
3663 && N2.getValueType().isSimple()) {
3664 assert(VT.isVector() && N1.getValueType().isVector() &&
3665 N2.getValueType().isVector() &&
3666 "Insert subvector VTs must be a vectors");
3667 assert(VT == N1.getValueType() &&
3668 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003669 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003670 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003671 if (isa<ConstantSDNode>(Index.getNode())) {
3672 assert((N2.getValueType().getVectorNumElements() +
3673 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003674 <= VT.getVectorNumElements())
3675 && "Insert subvector overflow!");
3676 }
3677
3678 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003679 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003680 return N2;
3681 }
3682 break;
3683 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003684 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003685 // Fold bit_convert nodes from a type to themselves.
3686 if (N1.getValueType() == VT)
3687 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003688 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003689 }
3690
Chris Lattnerf9c19152005-08-25 19:12:10 +00003691 // Memoize node if it doesn't produce a flag.
3692 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003693 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003694 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003695 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003696 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003697 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003698 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003699 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003700 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003701
Jack Carter170a5f22013-09-09 22:02:08 +00003702 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3703 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003704 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003705 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003706 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3707 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003708 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003709
Chandler Carruth41b20e72014-07-22 04:07:55 +00003710 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003711 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003712}
3713
Andrew Trickef9de2a2013-05-25 02:42:55 +00003714SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003715 SDValue N1, SDValue N2, SDValue N3,
3716 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003717 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003718 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003719}
3720
Andrew Trickef9de2a2013-05-25 02:42:55 +00003721SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003722 SDValue N1, SDValue N2, SDValue N3,
3723 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003724 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003725 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003726}
3727
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003728/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3729/// the incoming stack arguments to be loaded from the stack.
3730SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3731 SmallVector<SDValue, 8> ArgChains;
3732
3733 // Include the original chain at the beginning of the list. When this is
3734 // used by target LowerCall hooks, this helps legalize find the
3735 // CALLSEQ_BEGIN node.
3736 ArgChains.push_back(Chain);
3737
3738 // Add a chain value for each stack argument.
3739 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3740 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3741 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3742 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3743 if (FI->getIndex() < 0)
3744 ArgChains.push_back(SDValue(L, 1));
3745
3746 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003747 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003748}
3749
Dan Gohman544ab2c2008-04-12 04:36:06 +00003750/// getMemsetValue - Vectorized representation of the memset value
3751/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003752static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003753 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003754 assert(Value.getOpcode() != ISD::UNDEF);
3755
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003756 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003758 assert(C->getAPIntValue().getBitWidth() == 8);
3759 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003760 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003761 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003762 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003763 }
Evan Chengef377ad2008-05-15 08:39:06 +00003764
Dale Johannesenabf66b82009-02-03 22:26:09 +00003765 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003766 if (NumBits > 8) {
3767 // Use a multiplication with 0x010101... to extend the input to the
3768 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003769 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003770 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003771 }
3772
3773 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003774}
3775
Dan Gohman544ab2c2008-04-12 04:36:06 +00003776/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3777/// used when a memcpy is turned into a memset when the source is a constant
3778/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003779static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003780 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003781 // Handle vector with all elements zero.
3782 if (Str.empty()) {
3783 if (VT.isInteger())
3784 return DAG.getConstant(0, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003785 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003786 return DAG.getConstantFP(0.0, VT);
3787 else if (VT.isVector()) {
3788 unsigned NumElts = VT.getVectorNumElements();
3789 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003790 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003791 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3792 EltVT, NumElts)));
3793 } else
3794 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003795 }
3796
Duncan Sands13237ac2008-06-06 12:08:01 +00003797 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003798 unsigned NumVTBits = VT.getSizeInBits();
3799 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003800 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3801
Evan Chengc8444b12013-01-10 22:13:27 +00003802 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003803 if (TLI.isLittleEndian()) {
3804 for (unsigned i = 0; i != NumBytes; ++i)
3805 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3806 } else {
3807 for (unsigned i = 0; i != NumBytes; ++i)
3808 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003809 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003810
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003811 // If the "cost" of materializing the integer immediate is less than the cost
3812 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003813 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3814 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003815 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003816 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003817}
3818
Scott Michelcf0da6c2009-02-17 22:15:04 +00003819/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003820///
Andrew Tricke2431c62013-05-25 03:08:10 +00003821static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003822 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003823 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003824 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003825 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003826}
3827
Evan Chengef377ad2008-05-15 08:39:06 +00003828/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3829///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003830static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003831 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003832 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003833 if (Src.getOpcode() == ISD::GlobalAddress)
3834 G = cast<GlobalAddressSDNode>(Src);
3835 else if (Src.getOpcode() == ISD::ADD &&
3836 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3837 Src.getOperand(1).getOpcode() == ISD::Constant) {
3838 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003839 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003840 }
3841 if (!G)
3842 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003843
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003844 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003845}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003846
Evan Cheng43cd9e32010-04-01 06:04:33 +00003847/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3848/// to replace the memset / memcpy. Return true if the number of memory ops
3849/// is below the threshold. It returns the types of the sequence of
3850/// memory ops to perform memset / memcpy by reference.
3851static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003852 unsigned Limit, uint64_t Size,
3853 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003854 bool IsMemset,
3855 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003856 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003857 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003858 SelectionDAG &DAG,
3859 const TargetLowering &TLI) {
3860 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3861 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003862 // If 'SrcAlign' is zero, that means the memory operation does not need to
3863 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3864 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3865 // is the specified alignment of the memory operation. If it is zero, that
3866 // means it's possible to change the alignment of the destination.
3867 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3868 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003869 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003870 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003871 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003872
Chris Lattner28dc6c12010-03-07 07:45:08 +00003873 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003874 unsigned AS = 0;
3875 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003876 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003877 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003878 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003879 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003880 case 0: VT = MVT::i64; break;
3881 case 4: VT = MVT::i32; break;
3882 case 2: VT = MVT::i16; break;
3883 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003884 }
3885 }
3886
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003887 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003888 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003889 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003890 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003891
Duncan Sands11dd4242008-06-08 20:54:56 +00003892 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003893 VT = LVT;
3894 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003895
Dan Gohman544ab2c2008-04-12 04:36:06 +00003896 unsigned NumMemOps = 0;
3897 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003898 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003899 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003900 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003901 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003902 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003903
3904 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003905 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003906 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003907 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003908 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003909 Found = true;
3910 else if (NewVT == MVT::i64 &&
3911 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003912 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003913 // i64 is usually not legal on 32-bit targets, but f64 may be.
3914 NewVT = MVT::f64;
3915 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003916 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003917 }
3918
Evan Cheng04e55182012-12-12 00:42:09 +00003919 if (!Found) {
3920 do {
3921 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3922 if (NewVT == MVT::i8)
3923 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003924 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003925 }
3926 NewVTSize = NewVT.getSizeInBits() / 8;
3927
Evan Cheng79e2ca92012-12-10 23:21:26 +00003928 // If the new VT cannot cover all of the remaining bits, then consider
3929 // issuing a (or a pair of) unaligned and overlapping load / store.
3930 // FIXME: Only does this for 64-bit or more since we don't have proper
3931 // cost model for unaligned load / store.
3932 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003933 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003934 if (NumMemOps && AllowOverlap &&
3935 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003936 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003937 VTSize = Size;
3938 else {
3939 VT = NewVT;
3940 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003941 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003942 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003943
Evan Chengb7d3d032012-12-12 20:43:23 +00003944 if (++NumMemOps > Limit)
3945 return false;
3946
Dan Gohman544ab2c2008-04-12 04:36:06 +00003947 MemOps.push_back(VT);
3948 Size -= VTSize;
3949 }
3950
3951 return true;
3952}
3953
Andrew Trickef9de2a2013-05-25 02:42:55 +00003954static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003955 SDValue Chain, SDValue Dst,
3956 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003957 unsigned Align, bool isVol,
3958 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003959 MachinePointerInfo DstPtrInfo,
3960 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003961 // Turn a memcpy of undef to nop.
3962 if (Src.getOpcode() == ISD::UNDEF)
3963 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003964
Dan Gohman714663a2008-05-29 19:42:22 +00003965 // Expand memcpy to a series of load and store ops if the size operand falls
3966 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003967 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3968 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003970 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003971 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003972 MachineFunction &MF = DAG.getMachineFunction();
3973 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003974 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003975 MF.getFunction()->getAttributes().
3976 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003977 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3978 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3979 DstAlignCanChange = true;
3980 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3981 if (Align > SrcAlign)
3982 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003983 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003984 bool CopyFromStr = isMemSrcFromString(Src, Str);
3985 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003986 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003987
Evan Cheng4c014c82010-04-01 18:19:11 +00003988 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003989 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003990 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003991 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003992 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003993
Evan Cheng43cd9e32010-04-01 06:04:33 +00003994 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003995 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003996 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003997
3998 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003999 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00004000 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00004001 if (!TRI->needsStackRealignment(MF))
4002 while (NewAlign > Align &&
4003 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4004 NewAlign /= 2;
4005
Evan Cheng43cd9e32010-04-01 06:04:33 +00004006 if (NewAlign > Align) {
4007 // Give the stack frame object a larger alignment if needed.
4008 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4009 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4010 Align = NewAlign;
4011 }
4012 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004013
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004014 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00004015 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00004016 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00004017 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004018 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004019 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004020 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004021
Evan Cheng79e2ca92012-12-10 23:21:26 +00004022 if (VTSize > Size) {
4023 // Issuing an unaligned load / store pair that overlaps with the previous
4024 // pair. Adjust the offset accordingly.
4025 assert(i == NumMemOps-1 && i != 0);
4026 SrcOff -= VTSize - Size;
4027 DstOff -= VTSize - Size;
4028 }
4029
Evan Cheng43cd9e32010-04-01 06:04:33 +00004030 if (CopyFromStr &&
4031 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00004032 // It's unlikely a store of a vector immediate can be done in a single
4033 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00004034 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00004035 // FIXME: Handle other cases where store of vector immediate is done in
4036 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00004037 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00004038 if (Value.getNode())
4039 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004040 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00004041 DstPtrInfo.getWithOffset(DstOff), isVol,
4042 false, Align);
4043 }
4044
4045 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004046 // The type might not be legal for the target. This should only happen
4047 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004048 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4049 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004050 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004051 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004052 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004053 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004054 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004055 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004056 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004057 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004058 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004059 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4060 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004061 }
4062 OutChains.push_back(Store);
4063 SrcOff += VTSize;
4064 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004065 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004066 }
4067
Craig Topper48d114b2014-04-26 18:35:24 +00004068 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004069}
4070
Andrew Trickef9de2a2013-05-25 02:42:55 +00004071static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004072 SDValue Chain, SDValue Dst,
4073 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004074 unsigned Align, bool isVol,
4075 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004076 MachinePointerInfo DstPtrInfo,
4077 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004078 // Turn a memmove of undef to nop.
4079 if (Src.getOpcode() == ISD::UNDEF)
4080 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004081
4082 // Expand memmove to a series of load and store ops if the size operand falls
4083 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004084 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004085 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004086 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004087 MachineFunction &MF = DAG.getMachineFunction();
4088 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004089 bool OptSize = MF.getFunction()->getAttributes().
4090 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004091 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4092 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4093 DstAlignCanChange = true;
4094 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4095 if (Align > SrcAlign)
4096 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004097 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004098
Evan Cheng4c014c82010-04-01 18:19:11 +00004099 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004100 (DstAlignCanChange ? 0 : Align), SrcAlign,
4101 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004102 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004103
Evan Cheng43cd9e32010-04-01 06:04:33 +00004104 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004105 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004106 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004107 if (NewAlign > Align) {
4108 // Give the stack frame object a larger alignment if needed.
4109 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4110 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4111 Align = NewAlign;
4112 }
4113 }
Dan Gohman714663a2008-05-29 19:42:22 +00004114
Evan Cheng43cd9e32010-04-01 06:04:33 +00004115 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004116 SmallVector<SDValue, 8> LoadValues;
4117 SmallVector<SDValue, 8> LoadChains;
4118 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004119 unsigned NumMemOps = MemOps.size();
4120 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004121 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004122 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004123 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004124
Dale Johannesenabf66b82009-02-03 22:26:09 +00004125 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004126 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004127 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004128 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004129 LoadValues.push_back(Value);
4130 LoadChains.push_back(Value.getValue(1));
4131 SrcOff += VTSize;
4132 }
Craig Topper48d114b2014-04-26 18:35:24 +00004133 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004134 OutChains.clear();
4135 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004136 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004137 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004138 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004139
Dale Johannesenabf66b82009-02-03 22:26:09 +00004140 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004141 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004142 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004143 OutChains.push_back(Store);
4144 DstOff += VTSize;
4145 }
4146
Craig Topper48d114b2014-04-26 18:35:24 +00004147 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004148}
4149
Serge Pavlov8ec39992013-09-17 16:24:42 +00004150/// \brief Lower the call to 'memset' intrinsic function into a series of store
4151/// operations.
4152///
4153/// \param DAG Selection DAG where lowered code is placed.
4154/// \param dl Link to corresponding IR location.
4155/// \param Chain Control flow dependency.
4156/// \param Dst Pointer to destination memory location.
4157/// \param Src Value of byte to write into the memory.
4158/// \param Size Number of bytes to write.
4159/// \param Align Alignment of the destination in bytes.
4160/// \param isVol True if destination is volatile.
4161/// \param DstPtrInfo IR information on the memory pointer.
4162/// \returns New head in the control flow, if lowering was successful, empty
4163/// SDValue otherwise.
4164///
4165/// The function tries to replace 'llvm.memset' intrinsic with several store
4166/// operations and value calculation code. This is usually profitable for small
4167/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004168static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004169 SDValue Chain, SDValue Dst,
4170 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004171 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004172 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004173 // Turn a memset of undef to nop.
4174 if (Src.getOpcode() == ISD::UNDEF)
4175 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004176
4177 // Expand memset to a series of load/store ops if the size operand
4178 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004179 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004180 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004181 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004182 MachineFunction &MF = DAG.getMachineFunction();
4183 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004184 bool OptSize = MF.getFunction()->getAttributes().
4185 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004186 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4187 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4188 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004189 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004190 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004191 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004192 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004193 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004194 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004195
Evan Cheng43cd9e32010-04-01 06:04:33 +00004196 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004197 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004198 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004199 if (NewAlign > Align) {
4200 // Give the stack frame object a larger alignment if needed.
4201 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4202 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4203 Align = NewAlign;
4204 }
4205 }
4206
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004207 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004208 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004209 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004210
4211 // Find the largest store and generate the bit pattern for it.
4212 EVT LargestVT = MemOps[0];
4213 for (unsigned i = 1; i < NumMemOps; i++)
4214 if (MemOps[i].bitsGT(LargestVT))
4215 LargestVT = MemOps[i];
4216 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4217
Dan Gohman544ab2c2008-04-12 04:36:06 +00004218 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004219 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004220 unsigned VTSize = VT.getSizeInBits() / 8;
4221 if (VTSize > Size) {
4222 // Issuing an unaligned load / store pair that overlaps with the previous
4223 // pair. Adjust the offset accordingly.
4224 assert(i == NumMemOps-1 && i != 0);
4225 DstOff -= VTSize - Size;
4226 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004227
4228 // If this store is smaller than the largest store see whether we can get
4229 // the smaller value for free with a truncate.
4230 SDValue Value = MemSetValue;
4231 if (VT.bitsLT(LargestVT)) {
4232 if (!LargestVT.isVector() && !VT.isVector() &&
4233 TLI.isTruncateFree(LargestVT, VT))
4234 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4235 else
4236 Value = getMemsetValue(Src, VT, DAG, dl);
4237 }
4238 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004239 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004240 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004241 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004242 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004243 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004244 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004245 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004246 }
4247
Craig Topper48d114b2014-04-26 18:35:24 +00004248 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004249}
4250
Andrew Trickef9de2a2013-05-25 02:42:55 +00004251SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004252 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004253 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004254 MachinePointerInfo DstPtrInfo,
4255 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004256 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004257
4258 // Check to see if we should lower the memcpy to loads and stores first.
4259 // For cases within the target-specified limits, this is the best choice.
4260 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4261 if (ConstantSize) {
4262 // Memcpy with size zero? Just return the original chain.
4263 if (ConstantSize->isNullValue())
4264 return Chain;
4265
Evan Cheng43cd9e32010-04-01 06:04:33 +00004266 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4267 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004268 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004269 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004270 return Result;
4271 }
4272
4273 // Then check to see if we should lower the memcpy with target-specific
4274 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004275 if (TSI) {
4276 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4277 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4278 DstPtrInfo, SrcPtrInfo);
4279 if (Result.getNode())
4280 return Result;
4281 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004282
4283 // If we really need inline code and the target declined to provide it,
4284 // use a (potentially long) sequence of loads and stores.
4285 if (AlwaysInline) {
4286 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004287 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004288 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004289 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004290 }
4291
Dan Gohmanf38547c2010-04-05 20:24:08 +00004292 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4293 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4294 // respect volatile, so they may do things like read or write memory
4295 // beyond the given memory regions. But fixing this isn't easy, and most
4296 // people don't care.
4297
Dan Gohman544ab2c2008-04-12 04:36:06 +00004298 // Emit a library call.
4299 TargetLowering::ArgListTy Args;
4300 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004301 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004302 Entry.Node = Dst; Args.push_back(Entry);
4303 Entry.Node = Src; Args.push_back(Entry);
4304 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004305 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004306 TargetLowering::CallLoweringInfo CLI(*this);
4307 CLI.setDebugLoc(dl).setChain(Chain)
4308 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4309 Type::getVoidTy(*getContext()),
4310 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004311 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004312 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004313 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004314
Dan Gohman544ab2c2008-04-12 04:36:06 +00004315 return CallResult.second;
4316}
4317
Andrew Trickef9de2a2013-05-25 02:42:55 +00004318SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004319 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004320 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004321 MachinePointerInfo DstPtrInfo,
4322 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004323 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004324
Dan Gohman714663a2008-05-29 19:42:22 +00004325 // Check to see if we should lower the memmove to loads and stores first.
4326 // For cases within the target-specified limits, this is the best choice.
4327 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4328 if (ConstantSize) {
4329 // Memmove with size zero? Just return the original chain.
4330 if (ConstantSize->isNullValue())
4331 return Chain;
4332
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004333 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004334 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004335 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004336 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004337 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004338 return Result;
4339 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004340
4341 // Then check to see if we should lower the memmove with target-specific
4342 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004343 if (TSI) {
4344 SDValue Result = TSI->EmitTargetCodeForMemmove(
4345 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4346 if (Result.getNode())
4347 return Result;
4348 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004349
Mon P Wangbf862242010-04-06 08:27:51 +00004350 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4351 // not be safe. See memcpy above for more details.
4352
Dan Gohman544ab2c2008-04-12 04:36:06 +00004353 // Emit a library call.
4354 TargetLowering::ArgListTy Args;
4355 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004356 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004357 Entry.Node = Dst; Args.push_back(Entry);
4358 Entry.Node = Src; Args.push_back(Entry);
4359 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004360 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004361 TargetLowering::CallLoweringInfo CLI(*this);
4362 CLI.setDebugLoc(dl).setChain(Chain)
4363 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4364 Type::getVoidTy(*getContext()),
4365 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004366 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004367 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004368 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004369
Dan Gohman544ab2c2008-04-12 04:36:06 +00004370 return CallResult.second;
4371}
4372
Andrew Trickef9de2a2013-05-25 02:42:55 +00004373SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004374 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004375 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004376 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004377 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004378
4379 // Check to see if we should lower the memset to stores first.
4380 // For cases within the target-specified limits, this is the best choice.
4381 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4382 if (ConstantSize) {
4383 // Memset with size zero? Just return the original chain.
4384 if (ConstantSize->isNullValue())
4385 return Chain;
4386
Mon P Wangc576ee92010-04-04 03:10:48 +00004387 SDValue Result =
4388 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004389 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004390
Gabor Greiff304a7a2008-08-28 21:40:38 +00004391 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004392 return Result;
4393 }
4394
4395 // Then check to see if we should lower the memset with target-specific
4396 // code. If the target chooses to do this, this is the next best.
Manuel Jacob6f508c52015-01-28 23:50:40 +00004397 if (TSI) {
4398 SDValue Result = TSI->EmitTargetCodeForMemset(
4399 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4400 if (Result.getNode())
4401 return Result;
4402 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004403
Wesley Peck527da1b2010-11-23 03:31:01 +00004404 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004405 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004406 TargetLowering::ArgListTy Args;
4407 TargetLowering::ArgListEntry Entry;
4408 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4409 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004410 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004411 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004412 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004413 Entry.Node = Size;
4414 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004415 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004416
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004417 // FIXME: pass in SDLoc
4418 TargetLowering::CallLoweringInfo CLI(*this);
4419 CLI.setDebugLoc(dl).setChain(Chain)
4420 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4421 Type::getVoidTy(*getContext()),
4422 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004423 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004424 .setDiscardResult();
4425
4426 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004427 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004428}
4429
Andrew Trickef9de2a2013-05-25 02:42:55 +00004430SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004431 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004432 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004433 AtomicOrdering SuccessOrdering,
4434 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004435 SynchronizationScope SynchScope) {
4436 FoldingSetNodeID ID;
4437 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004438 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004439 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004440 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004441 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4442 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4443 return SDValue(E, 0);
4444 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004445
4446 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4447 // SDNode doesn't have access to it. This memory will be "leaked" when
4448 // the node is deallocated, but recovered when the allocator is released.
4449 // If the number of operands is less than 5 we use AtomicSDNode's internal
4450 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004451 unsigned NumOps = Ops.size();
4452 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4453 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004454
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004455 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4456 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004457 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004458 SuccessOrdering, FailureOrdering,
4459 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004460 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004461 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004462 return SDValue(N, 0);
4463}
4464
4465SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004466 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004467 MachineMemOperand *MMO,
4468 AtomicOrdering Ordering,
4469 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004470 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004471 Ordering, SynchScope);
4472}
4473
Tim Northover420a2162014-06-13 14:24:07 +00004474SDValue SelectionDAG::getAtomicCmpSwap(
4475 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4476 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4477 unsigned Alignment, AtomicOrdering SuccessOrdering,
4478 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4479 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4480 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4481 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4482
Dan Gohman48b185d2009-09-25 20:36:54 +00004483 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4484 Alignment = getEVTAlignment(MemVT);
4485
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004486 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004487
Eli Friedmane978d2f2011-09-07 02:23:42 +00004488 // FIXME: Volatile isn't really correct; we should keep track of atomic
4489 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004490 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004491 Flags |= MachineMemOperand::MOLoad;
4492 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004493
4494 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004495 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004496
Tim Northover420a2162014-06-13 14:24:07 +00004497 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4498 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004499}
4500
Tim Northover420a2162014-06-13 14:24:07 +00004501SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4502 SDVTList VTs, SDValue Chain, SDValue Ptr,
4503 SDValue Cmp, SDValue Swp,
4504 MachineMemOperand *MMO,
4505 AtomicOrdering SuccessOrdering,
4506 AtomicOrdering FailureOrdering,
4507 SynchronizationScope SynchScope) {
4508 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4509 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004510 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4511
Dale Johannesen839acbb2009-01-29 00:47:48 +00004512 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004513 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4514 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004515}
4516
Andrew Trickef9de2a2013-05-25 02:42:55 +00004517SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004518 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004519 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004520 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004521 unsigned Alignment,
4522 AtomicOrdering Ordering,
4523 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004524 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4525 Alignment = getEVTAlignment(MemVT);
4526
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004527 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004528 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004529 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004530 // For now, atomics are considered to be volatile always, and they are
4531 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004532 // FIXME: Volatile isn't really correct; we should keep track of atomic
4533 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004534 unsigned Flags = MachineMemOperand::MOVolatile;
4535 if (Opcode != ISD::ATOMIC_STORE)
4536 Flags |= MachineMemOperand::MOLoad;
4537 if (Opcode != ISD::ATOMIC_LOAD)
4538 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004539
4540 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004541 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004542 MemVT.getStoreSize(), Alignment);
4543
Eli Friedmanadec5872011-07-29 03:05:32 +00004544 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4545 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004546}
4547
Andrew Trickef9de2a2013-05-25 02:42:55 +00004548SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004549 SDValue Chain,
4550 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004551 MachineMemOperand *MMO,
4552 AtomicOrdering Ordering,
4553 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004554 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4555 Opcode == ISD::ATOMIC_LOAD_SUB ||
4556 Opcode == ISD::ATOMIC_LOAD_AND ||
4557 Opcode == ISD::ATOMIC_LOAD_OR ||
4558 Opcode == ISD::ATOMIC_LOAD_XOR ||
4559 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004560 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004561 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004562 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004563 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004564 Opcode == ISD::ATOMIC_SWAP ||
4565 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004566 "Invalid Atomic Op");
4567
Owen Anderson53aa7a92009-08-10 22:56:29 +00004568 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004569
Eli Friedman342e8df2011-08-24 20:50:09 +00004570 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4571 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004572 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004573 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004574}
4575
Andrew Trickef9de2a2013-05-25 02:42:55 +00004576SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004577 EVT VT, SDValue Chain,
4578 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004579 MachineMemOperand *MMO,
4580 AtomicOrdering Ordering,
4581 SynchronizationScope SynchScope) {
4582 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4583
4584 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004585 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004586 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004587}
4588
Duncan Sands739a0542008-07-02 17:40:58 +00004589/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004590SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4591 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004592 return Ops[0];
4593
Owen Anderson53aa7a92009-08-10 22:56:29 +00004594 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004595 VTs.reserve(Ops.size());
4596 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004597 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004598 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004599}
4600
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004601SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004602SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004603 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004604 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004605 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004606 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004607 if (Align == 0) // Ensure that codegen never sees alignment 0
4608 Align = getEVTAlignment(MemVT);
4609
4610 MachineFunction &MF = getMachineFunction();
4611 unsigned Flags = 0;
4612 if (WriteMem)
4613 Flags |= MachineMemOperand::MOStore;
4614 if (ReadMem)
4615 Flags |= MachineMemOperand::MOLoad;
4616 if (Vol)
4617 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004618 if (!Size)
4619 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004620 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004621 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004622
Craig Topper206fcd42014-04-26 19:29:41 +00004623 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004624}
4625
4626SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004627SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004628 ArrayRef<SDValue> Ops, EVT MemVT,
4629 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004630 assert((Opcode == ISD::INTRINSIC_VOID ||
4631 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004632 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004633 Opcode == ISD::LIFETIME_START ||
4634 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004635 (Opcode <= INT_MAX &&
4636 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4637 "Opcode is not a memory-accessing opcode!");
4638
Dale Johannesen839acbb2009-01-29 00:47:48 +00004639 // Memoize the node unless it returns a flag.
4640 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004641 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004642 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004643 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004644 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004645 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004646 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4647 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004648 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004649 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004650
Jack Carter170a5f22013-09-09 22:02:08 +00004651 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4652 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004653 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004654 CSEMap.InsertNode(N, IP);
4655 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004656 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4657 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004658 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004659 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004660 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004661 return SDValue(N, 0);
4662}
4663
Chris Lattnerea952f02010-09-21 17:24:05 +00004664/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4665/// MachinePointerInfo record from it. This is particularly useful because the
4666/// code generator has many cases where it doesn't bother passing in a
4667/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4668static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4669 // If this is FI+Offset, we can model it.
4670 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4671 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4672
4673 // If this is (FI+Offset1)+Offset2, we can model it.
4674 if (Ptr.getOpcode() != ISD::ADD ||
4675 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4676 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4677 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004678
Chris Lattnerea952f02010-09-21 17:24:05 +00004679 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4680 return MachinePointerInfo::getFixedStack(FI, Offset+
4681 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4682}
4683
4684/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4685/// MachinePointerInfo record from it. This is particularly useful because the
4686/// code generator has many cases where it doesn't bother passing in a
4687/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4688static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4689 // If the 'Offset' value isn't a constant, we can't handle this.
4690 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4691 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4692 if (OffsetOp.getOpcode() == ISD::UNDEF)
4693 return InferPointerInfo(Ptr);
4694 return MachinePointerInfo();
4695}
Wesley Peck527da1b2010-11-23 03:31:01 +00004696
Chris Lattnerea952f02010-09-21 17:24:05 +00004697
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004698SDValue
4699SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004700 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004701 SDValue Ptr, SDValue Offset,
4702 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004703 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004704 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004705 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004706 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004707 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004708 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4709 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004710
Dan Gohman48b185d2009-09-25 20:36:54 +00004711 unsigned Flags = MachineMemOperand::MOLoad;
4712 if (isVolatile)
4713 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004714 if (isNonTemporal)
4715 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004716 if (isInvariant)
4717 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004718
Chris Lattnerea952f02010-09-21 17:24:05 +00004719 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4720 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004721 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004722 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004723
Chris Lattnerea952f02010-09-21 17:24:05 +00004724 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004725 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004726 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004727 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004728 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004729}
4730
4731SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004732SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004733 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004734 SDValue Ptr, SDValue Offset, EVT MemVT,
4735 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004736 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004737 ExtType = ISD::NON_EXTLOAD;
4738 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004739 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004740 } else {
4741 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004742 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4743 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004744 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004745 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004746 assert(VT.isVector() == MemVT.isVector() &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004747 "Cannot use an ext load to convert to or from a vector!");
Dan Gohmancecad352009-12-14 23:40:38 +00004748 assert((!VT.isVector() ||
4749 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Ahmed Bougacha9a9e1a52015-01-26 19:31:42 +00004750 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004751 }
4752
4753 bool Indexed = AM != ISD::UNINDEXED;
4754 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4755 "Unindexed load with an offset!");
4756
4757 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004758 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004759 SDValue Ops[] = { Chain, Ptr, Offset };
4760 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004761 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004762 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004763 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004764 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004765 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004766 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004767 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004768 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4769 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004770 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004771 }
Jack Carter170a5f22013-09-09 22:02:08 +00004772 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4773 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004774 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004775 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004776 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004777 return SDValue(N, 0);
4778}
4779
Andrew Trickef9de2a2013-05-25 02:42:55 +00004780SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004781 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004782 MachinePointerInfo PtrInfo,
4783 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004784 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004785 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004786 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004787 SDValue Undef = getUNDEF(Ptr.getValueType());
4788 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004789 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004790 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004791}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004792
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004793SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4794 SDValue Chain, SDValue Ptr,
4795 MachineMemOperand *MMO) {
4796 SDValue Undef = getUNDEF(Ptr.getValueType());
4797 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4798 VT, MMO);
4799}
4800
Andrew Trickef9de2a2013-05-25 02:42:55 +00004801SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004802 SDValue Chain, SDValue Ptr,
4803 MachinePointerInfo PtrInfo, EVT MemVT,
4804 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004805 bool isInvariant, unsigned Alignment,
4806 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004807 SDValue Undef = getUNDEF(Ptr.getValueType());
4808 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004809 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4810 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004811}
4812
4813
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004814SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4815 SDValue Chain, SDValue Ptr, EVT MemVT,
4816 MachineMemOperand *MMO) {
4817 SDValue Undef = getUNDEF(Ptr.getValueType());
4818 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4819 MemVT, MMO);
4820}
4821
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004822SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004823SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004824 SDValue Offset, ISD::MemIndexedMode AM) {
4825 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4826 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4827 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004828 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004829 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004830 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004831 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004832}
4833
Andrew Trickef9de2a2013-05-25 02:42:55 +00004834SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004835 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004836 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004837 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004838 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004839 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004840 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004841 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004842
Dan Gohman48b185d2009-09-25 20:36:54 +00004843 unsigned Flags = MachineMemOperand::MOStore;
4844 if (isVolatile)
4845 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004846 if (isNonTemporal)
4847 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004848
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004849 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004850 PtrInfo = InferPointerInfo(Ptr);
4851
4852 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004853 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004854 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004855 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004856 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004857
4858 return getStore(Chain, dl, Val, Ptr, MMO);
4859}
4860
Andrew Trickef9de2a2013-05-25 02:42:55 +00004861SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004862 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004863 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004864 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004865 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004866 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004867 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004868 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4869 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004870 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004871 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004872 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004873 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004874 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004875 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004876 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4877 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004878 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004879 }
Jack Carter170a5f22013-09-09 22:02:08 +00004880 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4881 dl.getDebugLoc(), VTs,
4882 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004883 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004884 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004885 return SDValue(N, 0);
4886}
4887
Andrew Trickef9de2a2013-05-25 02:42:55 +00004888SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004889 SDValue Ptr, MachinePointerInfo PtrInfo,
4890 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004891 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004892 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004893 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004894 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004895 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4896 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004897
Dan Gohman48b185d2009-09-25 20:36:54 +00004898 unsigned Flags = MachineMemOperand::MOStore;
4899 if (isVolatile)
4900 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004901 if (isNonTemporal)
4902 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004903
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004904 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004905 PtrInfo = InferPointerInfo(Ptr);
4906
4907 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004908 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004909 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004910 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004911
4912 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4913}
4914
Andrew Trickef9de2a2013-05-25 02:42:55 +00004915SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004916 SDValue Ptr, EVT SVT,
4917 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004918 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004919
Michael Ilsemand5f91512012-09-10 16:56:31 +00004920 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004921 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004922 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004923 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004924
Dan Gohmancecad352009-12-14 23:40:38 +00004925 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4926 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004927 assert(VT.isInteger() == SVT.isInteger() &&
4928 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004929 assert(VT.isVector() == SVT.isVector() &&
4930 "Cannot use trunc store to convert to or from a vector!");
4931 assert((!VT.isVector() ||
4932 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4933 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004934
Owen Anderson9f944592009-08-11 20:47:22 +00004935 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004936 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004937 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4938 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004939 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004940 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004941 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004942 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004943 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004944 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4946 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004947 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004948 }
Jack Carter170a5f22013-09-09 22:02:08 +00004949 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4950 dl.getDebugLoc(), VTs,
4951 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004952 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004953 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004954 return SDValue(N, 0);
4955}
4956
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004957SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004958SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004959 SDValue Offset, ISD::MemIndexedMode AM) {
4960 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4961 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4962 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004963 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004964 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4965 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004966 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004967 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004968 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004969 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004970 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004971 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004972 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004973
Jack Carter170a5f22013-09-09 22:02:08 +00004974 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4975 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004976 ST->isTruncatingStore(),
4977 ST->getMemoryVT(),
4978 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004979 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004980 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004981 return SDValue(N, 0);
4982}
4983
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00004984SDValue
4985SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00004986 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
4987 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00004988
4989 SDVTList VTs = getVTList(VT, MVT::Other);
4990 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
4991 FoldingSetNodeID ID;
4992 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
4993 ID.AddInteger(VT.getRawBits());
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00004994 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00004995 MMO->isVolatile(),
4996 MMO->isNonTemporal(),
4997 MMO->isInvariant()));
4998 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4999 void *IP = nullptr;
5000 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5001 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5002 return SDValue(E, 0);
5003 }
5004 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5005 dl.getDebugLoc(), Ops, 4, VTs,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005006 ExtTy, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005007 CSEMap.InsertNode(N, IP);
5008 InsertNode(N);
5009 return SDValue(N, 0);
5010}
5011
5012SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005013 SDValue Ptr, SDValue Mask, EVT MemVT,
5014 MachineMemOperand *MMO, bool isTrunc) {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005015 assert(Chain.getValueType() == MVT::Other &&
5016 "Invalid chain type");
5017 EVT VT = Val.getValueType();
5018 SDVTList VTs = getVTList(MVT::Other);
5019 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5020 FoldingSetNodeID ID;
5021 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5022 ID.AddInteger(VT.getRawBits());
5023 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5024 MMO->isNonTemporal(), MMO->isInvariant()));
5025 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5026 void *IP = nullptr;
5027 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5028 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5029 return SDValue(E, 0);
5030 }
5031 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5032 dl.getDebugLoc(), Ops, 4,
Elena Demikhovsky150d9f32015-01-22 12:07:59 +00005033 VTs, isTrunc, MemVT, MMO);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00005034 CSEMap.InsertNode(N, IP);
5035 InsertNode(N);
5036 return SDValue(N, 0);
5037}
5038
Andrew Trickef9de2a2013-05-25 02:42:55 +00005039SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00005040 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00005041 SDValue SV,
5042 unsigned Align) {
5043 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00005044 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00005045}
5046
Andrew Trickef9de2a2013-05-25 02:42:55 +00005047SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00005048 ArrayRef<SDUse> Ops) {
5049 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005050 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00005051 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005052 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5053 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00005054 default: break;
5055 }
5056
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005057 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00005058 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00005059 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00005060 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00005061}
5062
Andrew Trickef9de2a2013-05-25 02:42:55 +00005063SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00005064 ArrayRef<SDValue> Ops) {
5065 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005066 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005067 case 0: return getNode(Opcode, DL, VT);
5068 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5069 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5070 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00005071 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00005072 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005073
Chris Lattnerb0713c72005-04-09 03:27:28 +00005074 switch (Opcode) {
5075 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005076 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005077 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005078 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5079 "LHS and RHS of condition must have same type!");
5080 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5081 "True and False arms of SelectCC must have same type!");
5082 assert(Ops[2].getValueType() == VT &&
5083 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005084 break;
5085 }
5086 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00005087 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005088 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5089 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005090 break;
5091 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00005092 }
5093
Chris Lattner566307f2005-05-14 07:42:29 +00005094 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00005095 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005096 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005097
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005098 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005099 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005100 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005101 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005102
Bill Wendling022d18f2009-12-18 23:32:53 +00005103 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005104 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005105
Jack Carter170a5f22013-09-09 22:02:08 +00005106 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005107 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005108 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005109 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005110 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005111 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005112 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005113
Chandler Carruth41b20e72014-07-22 04:07:55 +00005114 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005115 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005116}
5117
Andrew Trickef9de2a2013-05-25 02:42:55 +00005118SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005119 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5120 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005121}
5122
Andrew Trickef9de2a2013-05-25 02:42:55 +00005123SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005124 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005125 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005126 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005127
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005128#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005129 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005130 // FIXME: figure out how to safely handle things like
5131 // int foo(int x) { return 1 << (x & 255); }
5132 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005133 case ISD::SRA_PARTS:
5134 case ISD::SRL_PARTS:
5135 case ISD::SHL_PARTS:
5136 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005137 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005138 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005139 else if (N3.getOpcode() == ISD::AND)
5140 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5141 // If the and is only masking out bits that cannot effect the shift,
5142 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005143 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005144 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005145 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005146 }
5147 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005148 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005149#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005150
Chris Lattnerf9c19152005-08-25 19:12:10 +00005151 // Memoize the node unless it returns a flag.
5152 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005153 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005154 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005155 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005156 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005157 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005158 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005159 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005160
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005161 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005162 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5163 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005164 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005165 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5166 DL.getDebugLoc(), VTList, Ops[0],
5167 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005168 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005169 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5170 DL.getDebugLoc(), VTList, Ops[0],
5171 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005172 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005173 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005174 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005175 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005176 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005177 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005178 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005179 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5180 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005181 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005182 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5183 DL.getDebugLoc(), VTList, Ops[0],
5184 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005185 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005186 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5187 DL.getDebugLoc(), VTList, Ops[0],
5188 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005189 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005190 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005191 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005192 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005193 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005194 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005195 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005196}
5197
Andrew Trickef9de2a2013-05-25 02:42:55 +00005198SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005199 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005200}
5201
Andrew Trickef9de2a2013-05-25 02:42:55 +00005202SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005203 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005204 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005205 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005206}
5207
Andrew Trickef9de2a2013-05-25 02:42:55 +00005208SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005209 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005210 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005211 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005212}
5213
Andrew Trickef9de2a2013-05-25 02:42:55 +00005214SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005215 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005216 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005217 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005218}
5219
Andrew Trickef9de2a2013-05-25 02:42:55 +00005220SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005221 SDValue N1, SDValue N2, SDValue N3,
5222 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005223 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005224 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005225}
5226
Andrew Trickef9de2a2013-05-25 02:42:55 +00005227SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005228 SDValue N1, SDValue N2, SDValue N3,
5229 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005230 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005231 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005232}
5233
Owen Anderson53aa7a92009-08-10 22:56:29 +00005234SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005235 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005236}
5237
Owen Anderson53aa7a92009-08-10 22:56:29 +00005238SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005239 FoldingSetNodeID ID;
5240 ID.AddInteger(2U);
5241 ID.AddInteger(VT1.getRawBits());
5242 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005243
Craig Topperc0196b12014-04-14 00:51:57 +00005244 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005245 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005246 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005247 EVT *Array = Allocator.Allocate<EVT>(2);
5248 Array[0] = VT1;
5249 Array[1] = VT2;
5250 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5251 VTListMap.InsertNode(Result, IP);
5252 }
5253 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005254}
Dan Gohman17059682008-07-17 19:10:17 +00005255
Owen Anderson53aa7a92009-08-10 22:56:29 +00005256SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005257 FoldingSetNodeID ID;
5258 ID.AddInteger(3U);
5259 ID.AddInteger(VT1.getRawBits());
5260 ID.AddInteger(VT2.getRawBits());
5261 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005262
Craig Topperc0196b12014-04-14 00:51:57 +00005263 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005264 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005265 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005266 EVT *Array = Allocator.Allocate<EVT>(3);
5267 Array[0] = VT1;
5268 Array[1] = VT2;
5269 Array[2] = VT3;
5270 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5271 VTListMap.InsertNode(Result, IP);
5272 }
5273 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005274}
5275
Owen Anderson53aa7a92009-08-10 22:56:29 +00005276SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005277 FoldingSetNodeID ID;
5278 ID.AddInteger(4U);
5279 ID.AddInteger(VT1.getRawBits());
5280 ID.AddInteger(VT2.getRawBits());
5281 ID.AddInteger(VT3.getRawBits());
5282 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005283
Craig Topperc0196b12014-04-14 00:51:57 +00005284 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005285 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005286 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005287 EVT *Array = Allocator.Allocate<EVT>(4);
5288 Array[0] = VT1;
5289 Array[1] = VT2;
5290 Array[2] = VT3;
5291 Array[3] = VT4;
5292 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5293 VTListMap.InsertNode(Result, IP);
5294 }
5295 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005296}
5297
Craig Topperabb4ac72014-04-16 06:10:51 +00005298SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5299 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005300 FoldingSetNodeID ID;
5301 ID.AddInteger(NumVTs);
5302 for (unsigned index = 0; index < NumVTs; index++) {
5303 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005304 }
5305
Craig Topperc0196b12014-04-14 00:51:57 +00005306 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005307 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005308 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005309 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005310 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005311 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5312 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005313 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005314 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005315}
5316
5317
Chris Lattnerf34156e2006-01-28 09:32:45 +00005318/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5319/// specified operands. If the resultant node already exists in the DAG,
5320/// this does not modify the specified node, instead it returns the node that
5321/// already exists. If the resultant node does not exist in the DAG, the
5322/// input node is returned. As a degenerate case, if you specify the same
5323/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005324SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005325 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005326
Chris Lattnerf34156e2006-01-28 09:32:45 +00005327 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005328 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005329
Chris Lattnerf34156e2006-01-28 09:32:45 +00005330 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005331 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005332 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005333 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005334
Dan Gohmanebeccb42008-07-21 22:38:59 +00005335 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005336 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005337 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005338 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005339
Chris Lattnerf34156e2006-01-28 09:32:45 +00005340 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005341 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005342
Chris Lattnerf34156e2006-01-28 09:32:45 +00005343 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005344 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005345 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005346}
5347
Dan Gohman92c11ac2010-06-18 15:30:29 +00005348SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005349 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005350
Chris Lattnerf34156e2006-01-28 09:32:45 +00005351 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005352 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005353 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005354
Chris Lattnerf34156e2006-01-28 09:32:45 +00005355 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005356 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005357 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005358 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005359
Dan Gohmanebeccb42008-07-21 22:38:59 +00005360 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005361 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005362 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005363 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005364
Chris Lattnerf34156e2006-01-28 09:32:45 +00005365 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005366 if (N->OperandList[0] != Op1)
5367 N->OperandList[0].set(Op1);
5368 if (N->OperandList[1] != Op2)
5369 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005370
Chris Lattnerf34156e2006-01-28 09:32:45 +00005371 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005372 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005373 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005374}
5375
Dan Gohman92c11ac2010-06-18 15:30:29 +00005376SDNode *SelectionDAG::
5377UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005378 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005379 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005380}
5381
Dan Gohman92c11ac2010-06-18 15:30:29 +00005382SDNode *SelectionDAG::
5383UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005384 SDValue Op3, SDValue Op4) {
5385 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005386 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005387}
5388
Dan Gohman92c11ac2010-06-18 15:30:29 +00005389SDNode *SelectionDAG::
5390UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005391 SDValue Op3, SDValue Op4, SDValue Op5) {
5392 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005393 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005394}
5395
Dan Gohman92c11ac2010-06-18 15:30:29 +00005396SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005397UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5398 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005399 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005400 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005401
Chris Lattnerf34156e2006-01-28 09:32:45 +00005402 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005403 bool AnyChange = false;
5404 for (unsigned i = 0; i != NumOps; ++i) {
5405 if (Ops[i] != N->getOperand(i)) {
5406 AnyChange = true;
5407 break;
5408 }
5409 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005410
Chris Lattnerf34156e2006-01-28 09:32:45 +00005411 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005412 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005413
Chris Lattnerf34156e2006-01-28 09:32:45 +00005414 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005415 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005416 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005417 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005418
Dan Gohman2f83b472008-05-02 00:05:03 +00005419 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005420 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005421 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005422 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005423
Chris Lattnerf34156e2006-01-28 09:32:45 +00005424 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005425 for (unsigned i = 0; i != NumOps; ++i)
5426 if (N->OperandList[i] != Ops[i])
5427 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005428
5429 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005430 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005431 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005432}
5433
Dan Gohman91697632008-07-07 20:57:48 +00005434/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005435/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005436void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005437 // Unlike the code in MorphNodeTo that does this, we don't need to
5438 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005439 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5440 SDUse &Use = *I++;
5441 Use.set(SDValue());
5442 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005443}
Chris Lattner19732782005-08-16 18:17:10 +00005444
Dan Gohman17059682008-07-17 19:10:17 +00005445/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5446/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005447///
Dan Gohman17059682008-07-17 19:10:17 +00005448SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005449 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005450 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005451 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005452}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005453
Dan Gohman17059682008-07-17 19:10:17 +00005454SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005455 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005456 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005457 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005458 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005459}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005460
Dan Gohman17059682008-07-17 19:10:17 +00005461SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005462 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005463 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005464 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005465 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005466 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005467}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005468
Dan Gohman17059682008-07-17 19:10:17 +00005469SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005470 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005471 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005472 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005473 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005474 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005475}
Chris Lattner466fece2005-08-21 22:30:30 +00005476
Dan Gohman17059682008-07-17 19:10:17 +00005477SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005478 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005479 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005480 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005481}
5482
Dan Gohman17059682008-07-17 19:10:17 +00005483SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005484 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005485 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005486 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005487}
5488
Dan Gohman17059682008-07-17 19:10:17 +00005489SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005490 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005491 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005492 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005493}
5494
Dan Gohman17059682008-07-17 19:10:17 +00005495SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005496 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005497 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005498 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005499 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005500}
5501
Bill Wendling2d598632008-12-01 23:28:22 +00005502SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005503 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005504 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005505 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005506 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005507}
5508
Scott Michelcf0da6c2009-02-17 22:15:04 +00005509SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005510 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005511 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005512 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005513 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005514 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005515}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005516
Scott Michelcf0da6c2009-02-17 22:15:04 +00005517SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005518 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005519 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005520 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005521 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005522 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005523}
5524
Dan Gohman17059682008-07-17 19:10:17 +00005525SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005526 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005527 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005528 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005529 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005530 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005531 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005532}
5533
Dan Gohman17059682008-07-17 19:10:17 +00005534SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005535 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005536 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005537 SDValue Op3) {
5538 SDVTList VTs = getVTList(VT1, VT2, VT3);
5539 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005540 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005541}
5542
5543SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005544 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005545 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005546 // Reset the NodeID to -1.
5547 N->setNodeId(-1);
5548 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005549}
5550
Andrew Trickef9de2a2013-05-25 02:42:55 +00005551/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005552/// the line number information on the merged node since it is not possible to
5553/// preserve the information that operation is associated with multiple lines.
5554/// This will make the debugger working better at -O0, were there is a higher
5555/// probability having other instructions associated with that line.
5556///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005557/// For IROrder, we keep the smaller of the two
5558SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005559 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005560 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5561 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005562 N->setDebugLoc(DebugLoc());
5563 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005564 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5565 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005566 return N;
5567}
5568
Chris Lattneraf197502010-02-28 21:36:14 +00005569/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005570/// return type, opcode, and operands.
5571///
5572/// Note that MorphNodeTo returns the resultant node. If there is already a
5573/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005574/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005575///
5576/// Using MorphNodeTo is faster than creating a new node and swapping it in
5577/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005578/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005579/// the node's users.
5580///
Chandler Carruth356665a2014-08-01 22:09:43 +00005581/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5582/// As a consequence it isn't appropriate to use from within the DAG combiner or
5583/// the legalizer which maintain worklists that would need to be updated when
5584/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005585SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005586 SDVTList VTs, ArrayRef<SDValue> Ops) {
5587 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005588 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005589 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005590 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005591 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005592 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005593 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005594 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005595 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005596
Dan Gohmand3fe1742008-09-13 01:54:27 +00005597 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005598 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005599
Dan Gohman17059682008-07-17 19:10:17 +00005600 // Start the morphing.
5601 N->NodeType = Opc;
5602 N->ValueList = VTs.VTs;
5603 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005604
Dan Gohman17059682008-07-17 19:10:17 +00005605 // Clear the operands list, updating used nodes to remove this from their
5606 // use list. Keep track of any operands that become dead as a result.
5607 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005608 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5609 SDUse &Use = *I++;
5610 SDNode *Used = Use.getNode();
5611 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005612 if (Used->use_empty())
5613 DeadNodeSet.insert(Used);
5614 }
5615
Dan Gohman48b185d2009-09-25 20:36:54 +00005616 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5617 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005618 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005619 // If NumOps is larger than the # of operands we can have in a
5620 // MachineSDNode, reallocate the operand list.
5621 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5622 if (MN->OperandsNeedDelete)
5623 delete[] MN->OperandList;
5624 if (NumOps > array_lengthof(MN->LocalOperands))
5625 // We're creating a final node that will live unmorphed for the
5626 // remainder of the current SelectionDAG iteration, so we can allocate
5627 // the operands directly out of a pool with no recycling metadata.
5628 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005629 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005630 else
Craig Topper131de822014-04-27 19:21:16 +00005631 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005632 MN->OperandsNeedDelete = false;
5633 } else
Craig Topper131de822014-04-27 19:21:16 +00005634 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005635 } else {
5636 // If NumOps is larger than the # of operands we currently have, reallocate
5637 // the operand list.
5638 if (NumOps > N->NumOperands) {
5639 if (N->OperandsNeedDelete)
5640 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005641 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005642 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005643 } else
Craig Topper131de822014-04-27 19:21:16 +00005644 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005645 }
5646
5647 // Delete any nodes that are still dead after adding the uses for the
5648 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005649 if (!DeadNodeSet.empty()) {
5650 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005651 for (SDNode *N : DeadNodeSet)
5652 if (N->use_empty())
5653 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005654 RemoveDeadNodes(DeadNodes);
5655 }
Dan Gohman91697632008-07-07 20:57:48 +00005656
Dan Gohman17059682008-07-17 19:10:17 +00005657 if (IP)
5658 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005659 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005660}
5661
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005662
Dan Gohman32f71d72009-09-25 18:54:59 +00005663/// getMachineNode - These are used for target selectors to create a new node
5664/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005665///
Dan Gohman32f71d72009-09-25 18:54:59 +00005666/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005667/// node of the specified opcode and operands, it returns that node instead of
5668/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005669MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005670SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005671 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005672 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005673}
Bill Wendlinga434d932009-01-29 09:01:55 +00005674
Dan Gohmana22f2d82009-10-10 01:29:16 +00005675MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005676SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005677 SDVTList VTs = getVTList(VT);
5678 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005679 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005680}
Bill Wendlinga434d932009-01-29 09:01:55 +00005681
Dan Gohmana22f2d82009-10-10 01:29:16 +00005682MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005683SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005684 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005685 SDVTList VTs = getVTList(VT);
5686 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005687 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005688}
Bill Wendlinga434d932009-01-29 09:01:55 +00005689
Dan Gohmana22f2d82009-10-10 01:29:16 +00005690MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005691SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005692 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005693 SDVTList VTs = getVTList(VT);
5694 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005695 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005696}
Bill Wendlinga434d932009-01-29 09:01:55 +00005697
Dan Gohmana22f2d82009-10-10 01:29:16 +00005698MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005699SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005700 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005701 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005702 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005703}
Bill Wendlinga434d932009-01-29 09:01:55 +00005704
Dan Gohmana22f2d82009-10-10 01:29:16 +00005705MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005706SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005707 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005708 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005709}
Bill Wendlinga434d932009-01-29 09:01:55 +00005710
Dan Gohmana22f2d82009-10-10 01:29:16 +00005711MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005712SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005713 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005714 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005715 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005716 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005717}
Bill Wendlinga434d932009-01-29 09:01:55 +00005718
Dan Gohmana22f2d82009-10-10 01:29:16 +00005719MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005720SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005721 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005722 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005723 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005724 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005725}
5726
Dan Gohmana22f2d82009-10-10 01:29:16 +00005727MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005728SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005729 EVT VT1, EVT VT2, SDValue Op1,
5730 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005731 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005732 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005733 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005734}
5735
Dan Gohmana22f2d82009-10-10 01:29:16 +00005736MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005737SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005738 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005739 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005740 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005741 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005742}
Bill Wendlinga434d932009-01-29 09:01:55 +00005743
Dan Gohmana22f2d82009-10-10 01:29:16 +00005744MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005745SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005746 EVT VT1, EVT VT2, EVT VT3,
5747 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005748 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005749 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005750 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005751}
Bill Wendlinga434d932009-01-29 09:01:55 +00005752
Dan Gohmana22f2d82009-10-10 01:29:16 +00005753MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005754SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005755 EVT VT1, EVT VT2, EVT VT3,
5756 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005757 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005758 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005759 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005760}
Bill Wendlinga434d932009-01-29 09:01:55 +00005761
Dan Gohmana22f2d82009-10-10 01:29:16 +00005762MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005763SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005764 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005765 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005766 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005767 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005768}
Bill Wendlinga434d932009-01-29 09:01:55 +00005769
Dan Gohmana22f2d82009-10-10 01:29:16 +00005770MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005771SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005772 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005773 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005774 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005775 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005776}
5777
Dan Gohmana22f2d82009-10-10 01:29:16 +00005778MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005779SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005780 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005781 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005782 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005783 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005784}
5785
Dan Gohmana22f2d82009-10-10 01:29:16 +00005786MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005787SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005788 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005789 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005790 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005791 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005792 const SDValue *Ops = OpsArray.data();
5793 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005794
5795 if (DoCSE) {
5796 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005797 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005798 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005799 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005800 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005801 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005802 }
5803
5804 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005805 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5806 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005807
5808 // Initialize the operands list.
5809 if (NumOps > array_lengthof(N->LocalOperands))
5810 // We're creating a final node that will live unmorphed for the
5811 // remainder of the current SelectionDAG iteration, so we can allocate
5812 // the operands directly out of a pool with no recycling metadata.
5813 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5814 Ops, NumOps);
5815 else
5816 N->InitOperands(N->LocalOperands, Ops, NumOps);
5817 N->OperandsNeedDelete = false;
5818
5819 if (DoCSE)
5820 CSEMap.InsertNode(N, IP);
5821
Chandler Carruth41b20e72014-07-22 04:07:55 +00005822 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005823 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005824}
Evan Chengd3f1db92006-02-09 07:15:23 +00005825
Dan Gohmanac33a902009-08-19 18:16:17 +00005826/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005827/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005828SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005829SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005830 SDValue Operand) {
5831 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005832 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005833 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005834 return SDValue(Subreg, 0);
5835}
5836
Bob Wilson2a45a652009-10-08 18:49:46 +00005837/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005838/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005839SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005840SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005841 SDValue Operand, SDValue Subreg) {
5842 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005843 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005844 VT, Operand, Subreg, SRIdxVal);
5845 return SDValue(Result, 0);
5846}
5847
Evan Cheng31604a62008-03-22 01:55:50 +00005848/// getNodeIfExists - Get the specified node if it's already available, or
5849/// else return NULL.
5850SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005851 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5852 bool exact) {
5853 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005854 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005855 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005856 if (isBinOpWithFlags(Opcode))
5857 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005858 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005859 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005860 return E;
5861 }
Craig Topperc0196b12014-04-14 00:51:57 +00005862 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005863}
5864
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005865/// getDbgValue - Creates a SDDbgValue node.
5866///
Adrian Prantl32da8892014-04-25 20:49:25 +00005867/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005868SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5869 unsigned R, bool IsIndirect, uint64_t Off,
5870 DebugLoc DL, unsigned O) {
5871 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005872}
5873
Adrian Prantl32da8892014-04-25 20:49:25 +00005874/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005875SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5876 const Value *C, uint64_t Off,
5877 DebugLoc DL, unsigned O) {
5878 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005879}
5880
Adrian Prantl32da8892014-04-25 20:49:25 +00005881/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005882SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
5883 unsigned FI, uint64_t Off,
5884 DebugLoc DL, unsigned O) {
5885 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005886}
5887
Dan Gohman7d099f72010-03-03 21:33:37 +00005888namespace {
5889
Dan Gohman9cc886b2010-03-04 19:11:28 +00005890/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005891/// pointed to by a use iterator is deleted, increment the use iterator
5892/// so that it doesn't dangle.
5893///
Dan Gohman7d099f72010-03-03 21:33:37 +00005894class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005895 SDNode::use_iterator &UI;
5896 SDNode::use_iterator &UE;
5897
Craig Topper7b883b32014-03-08 06:31:39 +00005898 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005899 // Increment the iterator as needed.
5900 while (UI != UE && N == *UI)
5901 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005902 }
5903
5904public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005905 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005906 SDNode::use_iterator &ui,
5907 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005908 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005909};
5910
5911}
5912
Evan Cheng445b91a2006-08-07 22:13:29 +00005913/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005914/// This can cause recursive merging of nodes in the DAG.
5915///
Chris Lattner76858912008-02-03 03:35:22 +00005916/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005917///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005918void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005919 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005920 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005921 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005922 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005923
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005924 // Iterate over all the existing uses of From. New uses will be added
5925 // to the beginning of the use list, which we avoid visiting.
5926 // This specifically avoids visiting uses of From that arise while the
5927 // replacement is happening, because any such uses would be the result
5928 // of CSE: If an existing node looks like From after one of its operands
5929 // is replaced by To, we don't want to replace of all its users with To
5930 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005931 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005932 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005933 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005934 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005935
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005936 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005937 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005938
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005939 // A user can appear in a use list multiple times, and when this
5940 // happens the uses are usually next to each other in the list.
5941 // To help reduce the number of CSE recomputations, process all
5942 // the uses of this user that we can find this way.
5943 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005944 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005945 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005946 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005947 } while (UI != UE && *UI == User);
5948
5949 // Now that we have modified User, add it back to the CSE maps. If it
5950 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005951 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005952 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005953
5954 // If we just RAUW'd the root, take note.
5955 if (FromN == getRoot())
5956 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005957}
5958
5959/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5960/// This can cause recursive merging of nodes in the DAG.
5961///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005962/// This version assumes that for each value of From, there is a
5963/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005964///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005965void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005966#ifndef NDEBUG
5967 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5968 assert((!From->hasAnyUseOfValue(i) ||
5969 From->getValueType(i) == To->getValueType(i)) &&
5970 "Cannot use this version of ReplaceAllUsesWith!");
5971#endif
Dan Gohman17059682008-07-17 19:10:17 +00005972
5973 // Handle the trivial case.
5974 if (From == To)
5975 return;
5976
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005977 // Iterate over just the existing users of From. See the comments in
5978 // the ReplaceAllUsesWith above.
5979 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005980 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005981 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005982 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005983
Chris Lattner373f0482005-08-26 18:36:28 +00005984 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005985 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005986
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005987 // A user can appear in a use list multiple times, and when this
5988 // happens the uses are usually next to each other in the list.
5989 // To help reduce the number of CSE recomputations, process all
5990 // the uses of this user that we can find this way.
5991 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005992 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005993 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005994 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005995 } while (UI != UE && *UI == User);
5996
5997 // Now that we have modified User, add it back to the CSE maps. If it
5998 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005999 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006000 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006001
6002 // If we just RAUW'd the root, take note.
6003 if (From == getRoot().getNode())
6004 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00006005}
6006
Chris Lattner373f0482005-08-26 18:36:28 +00006007/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6008/// This can cause recursive merging of nodes in the DAG.
6009///
6010/// This version can replace From with any result values. To must match the
6011/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006012void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00006013 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006014 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006015
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006016 // Iterate over just the existing users of From. See the comments in
6017 // the ReplaceAllUsesWith above.
6018 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006019 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00006020 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006021 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00006022
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006023 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006024 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00006025
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006026 // A user can appear in a use list multiple times, and when this
6027 // happens the uses are usually next to each other in the list.
6028 // To help reduce the number of CSE recomputations, process all
6029 // the uses of this user that we can find this way.
6030 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006031 SDUse &Use = UI.getUse();
6032 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006033 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006034 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006035 } while (UI != UE && *UI == User);
6036
6037 // Now that we have modified User, add it back to the CSE maps. If it
6038 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006039 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006040 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006041
6042 // If we just RAUW'd the root, take note.
6043 if (From == getRoot().getNode())
6044 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00006045}
6046
Chris Lattner375e1a72006-02-17 21:58:01 +00006047/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006048/// uses of other values produced by From.getNode() alone. The Deleted
6049/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006050void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00006051 // Handle the really simple, really trivial case efficiently.
6052 if (From == To) return;
6053
Chris Lattner375e1a72006-02-17 21:58:01 +00006054 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006055 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006056 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00006057 return;
6058 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00006059
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006060 // Iterate over just the existing users of From. See the comments in
6061 // the ReplaceAllUsesWith above.
6062 SDNode::use_iterator UI = From.getNode()->use_begin(),
6063 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006064 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006065 while (UI != UE) {
6066 SDNode *User = *UI;
6067 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00006068
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006069 // A user can appear in a use list multiple times, and when this
6070 // happens the uses are usually next to each other in the list.
6071 // To help reduce the number of CSE recomputations, process all
6072 // the uses of this user that we can find this way.
6073 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006074 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006075
6076 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006077 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006078 ++UI;
6079 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00006080 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006081
6082 // If this node hasn't been modified yet, it's still in the CSE maps,
6083 // so remove its old self from the CSE maps.
6084 if (!UserRemovedFromCSEMaps) {
6085 RemoveNodeFromCSEMaps(User);
6086 UserRemovedFromCSEMaps = true;
6087 }
6088
6089 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006090 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006091 } while (UI != UE && *UI == User);
6092
6093 // We are iterating over all uses of the From node, so if a use
6094 // doesn't use the specific value, no changes are made.
6095 if (!UserRemovedFromCSEMaps)
6096 continue;
6097
Chris Lattner3cfb56d2007-10-15 06:10:22 +00006098 // Now that we have modified User, add it back to the CSE maps. If it
6099 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006100 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006101 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006102
6103 // If we just RAUW'd the root, take note.
6104 if (From == getRoot())
6105 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006106}
6107
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006108namespace {
6109 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6110 /// to record information about a use.
6111 struct UseMemo {
6112 SDNode *User;
6113 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006114 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006115 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006116
6117 /// operator< - Sort Memos by User.
6118 bool operator<(const UseMemo &L, const UseMemo &R) {
6119 return (intptr_t)L.User < (intptr_t)R.User;
6120 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006121}
6122
Dan Gohman17059682008-07-17 19:10:17 +00006123/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006124/// uses of other values produced by From.getNode() alone. The same value
6125/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006126/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006127void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6128 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006129 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006130 // Handle the simple, trivial case efficiently.
6131 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006132 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006133
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006134 // Read up all the uses and make records of them. This helps
6135 // processing new uses that are introduced during the
6136 // replacement process.
6137 SmallVector<UseMemo, 4> Uses;
6138 for (unsigned i = 0; i != Num; ++i) {
6139 unsigned FromResNo = From[i].getResNo();
6140 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006141 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006142 E = FromNode->use_end(); UI != E; ++UI) {
6143 SDUse &Use = UI.getUse();
6144 if (Use.getResNo() == FromResNo) {
6145 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006146 Uses.push_back(Memo);
6147 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006148 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006149 }
Dan Gohman17059682008-07-17 19:10:17 +00006150
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006151 // Sort the uses, so that all the uses from a given User are together.
6152 std::sort(Uses.begin(), Uses.end());
6153
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006154 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6155 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006156 // We know that this user uses some value of From. If it is the right
6157 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006158 SDNode *User = Uses[UseIndex].User;
6159
6160 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006161 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006162
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006163 // The Uses array is sorted, so all the uses for a given User
6164 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006165 // To help reduce the number of CSE recomputations, process all
6166 // the uses of this user that we can find this way.
6167 do {
6168 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006169 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006170 ++UseIndex;
6171
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006172 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006173 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6174
Dan Gohman17059682008-07-17 19:10:17 +00006175 // Now that we have modified User, add it back to the CSE maps. If it
6176 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006177 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006178 }
6179}
6180
Evan Cheng9631a602006-08-01 08:20:41 +00006181/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006182/// based on their topological order. It returns the maximum id and a vector
6183/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006184unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006185
Dan Gohman86aa16a2008-09-30 18:30:35 +00006186 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006187
Dan Gohman86aa16a2008-09-30 18:30:35 +00006188 // SortedPos tracks the progress of the algorithm. Nodes before it are
6189 // sorted, nodes after it are unsorted. When the algorithm completes
6190 // it is at the end of the list.
6191 allnodes_iterator SortedPos = allnodes_begin();
6192
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006193 // Visit all the nodes. Move nodes with no operands to the front of
6194 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006195 // operand count. Before we do this, the Node Id fields of the nodes
6196 // may contain arbitrary values. After, the Node Id fields for nodes
6197 // before SortedPos will contain the topological sort index, and the
6198 // Node Id fields for nodes At SortedPos and after will contain the
6199 // count of outstanding operands.
6200 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6201 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006202 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006203 unsigned Degree = N->getNumOperands();
6204 if (Degree == 0) {
6205 // A node with no uses, add it to the result array immediately.
6206 N->setNodeId(DAGSize++);
6207 allnodes_iterator Q = N;
6208 if (Q != SortedPos)
6209 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006210 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006211 ++SortedPos;
6212 } else {
6213 // Temporarily use the Node Id as scratch space for the degree count.
6214 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006215 }
6216 }
6217
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006218 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006219 // such that by the time the end is reached all nodes will be sorted.
6220 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6221 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006222 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006223 // N is in sorted position, so all its uses have one less operand
6224 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006225 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6226 UI != UE; ++UI) {
6227 SDNode *P = *UI;
6228 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006229 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006230 --Degree;
6231 if (Degree == 0) {
6232 // All of P's operands are sorted, so P may sorted now.
6233 P->setNodeId(DAGSize++);
6234 if (P != SortedPos)
6235 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006236 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006237 ++SortedPos;
6238 } else {
6239 // Update P's outstanding operand count.
6240 P->setNodeId(Degree);
6241 }
6242 }
David Greene3b2a68c2010-01-20 00:59:23 +00006243 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006244#ifndef NDEBUG
6245 SDNode *S = ++I;
6246 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006247 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006248 dbgs() << "Checking if this is due to cycles\n";
6249 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006250#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006251 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006252 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006253 }
6254
6255 assert(SortedPos == AllNodes.end() &&
6256 "Topological sort incomplete!");
6257 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6258 "First node in topological sort is not the entry token!");
6259 assert(AllNodes.front().getNodeId() == 0 &&
6260 "First node in topological sort has non-zero id!");
6261 assert(AllNodes.front().getNumOperands() == 0 &&
6262 "First node in topological sort has operands!");
6263 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6264 "Last node in topologic sort has unexpected id!");
6265 assert(AllNodes.back().use_empty() &&
6266 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006267 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006268 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006269}
6270
Evan Cheng563fe3c2010-03-25 01:38:16 +00006271/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6272/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006273void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Frederic Riss0f7abef2014-11-13 03:20:23 +00006274 if (SD) {
6275 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Cheng563fe3c2010-03-25 01:38:16 +00006276 SD->setHasDebugValue(true);
Frederic Riss0f7abef2014-11-13 03:20:23 +00006277 }
6278 DbgInfo->add(DB, SD, isParameter);
Dale Johannesen49de0602010-03-10 22:13:47 +00006279}
Evan Cheng29eefc12006-07-27 06:39:06 +00006280
Devang Patelefc6b162011-01-25 23:27:42 +00006281/// TransferDbgValues - Transfer SDDbgValues.
6282void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6283 if (From == To || !From.getNode()->getHasDebugValue())
6284 return;
6285 SDNode *FromNode = From.getNode();
6286 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006287 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006288 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006289 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006290 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006291 SDDbgValue *Dbg = *I;
6292 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006293 SDDbgValue *Clone =
6294 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6295 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6296 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006297 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006298 }
6299 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006300 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006301 E = ClonedDVs.end(); I != E; ++I)
6302 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006303}
6304
Jim Laskeyd66e6162005-08-17 20:08:02 +00006305//===----------------------------------------------------------------------===//
6306// SDNode Class
6307//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006308
Chris Lattner3bf17b62007-02-04 02:41:42 +00006309HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006310 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006311}
6312
Andrew Trickef9de2a2013-05-25 02:42:55 +00006313GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6314 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006315 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006316 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006317 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006318}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006319
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006320AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6321 SDValue X, unsigned SrcAS,
6322 unsigned DestAS)
6323 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6324 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6325
Andrew Trickef9de2a2013-05-25 02:42:55 +00006326MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6327 EVT memvt, MachineMemOperand *mmo)
6328 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006329 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006330 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006331 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006332 assert(isNonTemporal() == MMO->isNonTemporal() &&
6333 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006334 // We check here that the size of the memory operand fits within the size of
6335 // the MMO. This is because the MMO might indicate only a possible address
6336 // range instead of specifying the affected memory addresses precisely.
6337 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006338}
6339
Andrew Trickef9de2a2013-05-25 02:42:55 +00006340MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006341 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6342 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006343 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006344 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006345 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006346 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006347 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006348}
6349
Jim Laskeyf576b422006-10-27 23:46:08 +00006350/// Profile - Gather unique data for the node.
6351///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006352void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006353 AddNodeIDNode(ID, this);
6354}
6355
Owen Anderson3b1665e2009-08-25 22:27:22 +00006356namespace {
6357 struct EVTArray {
6358 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006359
Owen Anderson3b1665e2009-08-25 22:27:22 +00006360 EVTArray() {
6361 VTs.reserve(MVT::LAST_VALUETYPE);
6362 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6363 VTs.push_back(MVT((MVT::SimpleValueType)i));
6364 }
6365 };
6366}
6367
Owen Anderson53aa7a92009-08-10 22:56:29 +00006368static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006369static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006370static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006371
Chris Lattner88fa11c2005-11-08 23:30:28 +00006372/// getValueTypeList - Return a pointer to the specified value type.
6373///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006374const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006375 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006376 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006377 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006378 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006379 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006380 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006381 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006382 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006383}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006384
Chris Lattner40e79822005-01-12 18:37:47 +00006385/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6386/// indicated value. This method ignores uses of other values defined by this
6387/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006388bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006389 assert(Value < getNumValues() && "Bad value!");
6390
Roman Levenstein51f532f2008-04-07 10:06:32 +00006391 // TODO: Only iterate over uses of a given value of the node
6392 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006393 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006394 if (NUses == 0)
6395 return false;
6396 --NUses;
6397 }
Chris Lattner40e79822005-01-12 18:37:47 +00006398 }
6399
6400 // Found exactly the right number of uses?
6401 return NUses == 0;
6402}
6403
6404
Evan Cheng358c3d12007-08-02 05:29:38 +00006405/// hasAnyUseOfValue - Return true if there are any use of the indicated
6406/// value. This method ignores uses of other values defined by this operation.
6407bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6408 assert(Value < getNumValues() && "Bad value!");
6409
Dan Gohman7a510c22008-07-09 22:39:01 +00006410 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006411 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006412 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006413
6414 return false;
6415}
6416
6417
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006418/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006419///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006420bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006421 bool Seen = false;
6422 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006423 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006424 if (User == this)
6425 Seen = true;
6426 else
6427 return false;
6428 }
6429
6430 return Seen;
6431}
6432
Evan Cheng9456dd82006-11-03 07:31:32 +00006433/// isOperand - Return true if this node is an operand of N.
6434///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006435bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6437 if (*this == N->getOperand(i))
6438 return true;
6439 return false;
6440}
6441
Evan Cheng567d2e52008-03-04 00:41:45 +00006442bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006443 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006444 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006445 return true;
6446 return false;
6447}
Evan Chengd37645c2006-02-05 06:29:23 +00006448
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006449/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006450/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006451/// side-effecting instructions on any chain path. In practice, this looks
6452/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006453/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006454bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006455 unsigned Depth) const {
6456 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006457
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006458 // Don't search too deeply, we just want to be able to see through
6459 // TokenFactor's etc.
6460 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006461
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006462 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006463 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006464 if (getOpcode() == ISD::TokenFactor) {
6465 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006466 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6467 return false;
6468 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006469 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006470
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006471 // Loads don't have side effects, look through them.
6472 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6473 if (!Ld->isVolatile())
6474 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6475 }
6476 return false;
6477}
6478
Lang Hames5a004992011-07-07 04:31:51 +00006479/// hasPredecessor - Return true if N is a predecessor of this node.
6480/// N is either an operand of this node, or can be reached by recursively
6481/// traversing up the operands.
6482/// NOTE: This is an expensive method. Use it carefully.
6483bool SDNode::hasPredecessor(const SDNode *N) const {
6484 SmallPtrSet<const SDNode *, 32> Visited;
6485 SmallVector<const SDNode *, 16> Worklist;
6486 return hasPredecessorHelper(N, Visited, Worklist);
6487}
Dan Gohmancd139c02009-10-28 03:44:30 +00006488
Craig Topperb94011f2013-07-14 04:42:23 +00006489bool
6490SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006491 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006492 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006493 if (Visited.empty()) {
6494 Worklist.push_back(this);
6495 } else {
6496 // Take a look in the visited set. If we've already encountered this node
6497 // we needn't search further.
6498 if (Visited.count(N))
6499 return true;
6500 }
6501
6502 // Haven't visited N yet. Continue the search.
6503 while (!Worklist.empty()) {
6504 const SDNode *M = Worklist.pop_back_val();
6505 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6506 SDNode *Op = M->getOperand(i).getNode();
David Blaikie70573dc2014-11-19 07:49:26 +00006507 if (Visited.insert(Op).second)
Dan Gohmancd139c02009-10-28 03:44:30 +00006508 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006509 if (Op == N)
6510 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006511 }
Lang Hames5a004992011-07-07 04:31:51 +00006512 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006513
6514 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006515}
6516
Evan Cheng5d9fd972006-10-04 00:56:09 +00006517uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6518 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006519 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006520}
6521
Mon P Wang32f8bb92009-11-30 02:42:02 +00006522SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6523 assert(N->getNumValues() == 1 &&
6524 "Can't unroll a vector with multiple results!");
6525
6526 EVT VT = N->getValueType(0);
6527 unsigned NE = VT.getVectorNumElements();
6528 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006529 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006530
6531 SmallVector<SDValue, 8> Scalars;
6532 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6533
6534 // If ResNE is 0, fully unroll the vector op.
6535 if (ResNE == 0)
6536 ResNE = NE;
6537 else if (NE > ResNE)
6538 NE = ResNE;
6539
6540 unsigned i;
6541 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006542 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006543 SDValue Operand = N->getOperand(j);
6544 EVT OperandVT = Operand.getValueType();
6545 if (OperandVT.isVector()) {
6546 // A vector operand; extract a single element.
6547 EVT OperandEltVT = OperandVT.getVectorElementType();
6548 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6549 OperandEltVT,
6550 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006551 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006552 } else {
6553 // A scalar operand; just use it as is.
6554 Operands[j] = Operand;
6555 }
6556 }
6557
6558 switch (N->getOpcode()) {
6559 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006560 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006561 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006562 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006563 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006564 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006565 case ISD::SHL:
6566 case ISD::SRA:
6567 case ISD::SRL:
6568 case ISD::ROTL:
6569 case ISD::ROTR:
6570 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006571 getShiftAmountOperand(Operands[0].getValueType(),
6572 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006573 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006574 case ISD::SIGN_EXTEND_INREG:
6575 case ISD::FP_ROUND_INREG: {
6576 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6577 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6578 Operands[0],
6579 getValueType(ExtVT)));
6580 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006581 }
6582 }
6583
6584 for (; i < ResNE; ++i)
6585 Scalars.push_back(getUNDEF(EltVT));
6586
6587 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006588 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006589}
6590
Evan Chengf5938d52009-12-09 01:36:00 +00006591
Wesley Peck527da1b2010-11-23 03:31:01 +00006592/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6593/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006594/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006595bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006596 unsigned Bytes, int Dist) const {
6597 if (LD->getChain() != Base->getChain())
6598 return false;
6599 EVT VT = LD->getValueType(0);
6600 if (VT.getSizeInBits() / 8 != Bytes)
6601 return false;
6602
6603 SDValue Loc = LD->getOperand(1);
6604 SDValue BaseLoc = Base->getOperand(1);
6605 if (Loc.getOpcode() == ISD::FrameIndex) {
6606 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6607 return false;
6608 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6609 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6610 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6611 int FS = MFI->getObjectSize(FI);
6612 int BFS = MFI->getObjectSize(BFI);
6613 if (FS != BFS || FS != (int)Bytes) return false;
6614 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6615 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006616
Sanjay Patel7129c102014-12-16 21:57:18 +00006617 // Handle X + C.
6618 if (isBaseWithConstantOffset(Loc)) {
6619 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6620 if (Loc.getOperand(0) == BaseLoc) {
6621 // If the base location is a simple address with no offset itself, then
6622 // the second load's first add operand should be the base address.
6623 if (LocOffset == Dist * (int)Bytes)
6624 return true;
6625 } else if (isBaseWithConstantOffset(BaseLoc)) {
6626 // The base location itself has an offset, so subtract that value from the
6627 // second load's offset before comparing to distance * size.
6628 int64_t BOffset =
6629 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6630 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6631 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6632 return true;
6633 }
6634 }
6635 }
Craig Topperc0196b12014-04-14 00:51:57 +00006636 const GlobalValue *GV1 = nullptr;
6637 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006638 int64_t Offset1 = 0;
6639 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006640 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6641 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006642 if (isGA1 && isGA2 && GV1 == GV2)
6643 return Offset1 == (Offset2 + Dist*Bytes);
6644 return false;
6645}
6646
6647
Evan Cheng34a23ea2009-12-09 01:04:59 +00006648/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6649/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006650unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006651 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006652 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006653 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006654 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006655 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006656 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +00006657 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6658 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006659 unsigned AlignBits = KnownZero.countTrailingOnes();
6660 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6661 if (Align)
6662 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006663 }
Evan Chengd938faf2009-12-09 01:53:58 +00006664
Evan Cheng34a23ea2009-12-09 01:04:59 +00006665 // If this is a direct reference to a stack slot, use information about the
6666 // stack slot's alignment.
6667 int FrameIdx = 1 << 31;
6668 int64_t FrameOffset = 0;
6669 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6670 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006671 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006672 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006673 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006674 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6675 FrameOffset = Ptr.getConstantOperandVal(1);
6676 }
6677
6678 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006679 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006680 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6681 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006682 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006683 }
6684
6685 return 0;
6686}
6687
Juergen Ributzkab3487102013-11-19 21:20:17 +00006688/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6689/// which is split (or expanded) into two not necessarily identical pieces.
6690std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6691 // Currently all types are split in half.
6692 EVT LoVT, HiVT;
6693 if (!VT.isVector()) {
6694 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6695 } else {
6696 unsigned NumElements = VT.getVectorNumElements();
6697 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6698 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6699 NumElements/2);
6700 }
6701 return std::make_pair(LoVT, HiVT);
6702}
6703
6704/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6705/// low/high part.
6706std::pair<SDValue, SDValue>
6707SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6708 const EVT &HiVT) {
6709 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6710 N.getValueType().getVectorNumElements() &&
6711 "More vector elements requested than available!");
6712 SDValue Lo, Hi;
6713 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6714 getConstant(0, TLI->getVectorIdxTy()));
6715 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6716 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6717 return std::make_pair(Lo, Hi);
6718}
6719
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006720void SelectionDAG::ExtractVectorElements(SDValue Op,
6721 SmallVectorImpl<SDValue> &Args,
6722 unsigned Start, unsigned Count) {
6723 EVT VT = Op.getValueType();
6724 if (Count == 0)
6725 Count = VT.getVectorNumElements();
6726
6727 EVT EltVT = VT.getVectorElementType();
6728 EVT IdxTy = TLI->getVectorIdxTy();
6729 SDLoc SL(Op);
6730 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6731 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6732 Op, getConstant(i, IdxTy)));
6733 }
6734}
6735
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006736// getAddressSpace - Return the address space this GlobalAddress belongs to.
6737unsigned GlobalAddressSDNode::getAddressSpace() const {
6738 return getGlobal()->getType()->getAddressSpace();
6739}
6740
6741
Chris Lattner229907c2011-07-18 04:54:35 +00006742Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006743 if (isMachineConstantPoolEntry())
6744 return Val.MachineCPVal->getType();
6745 return Val.ConstVal->getType();
6746}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006747
Bob Wilson85cefe82009-03-02 23:24:16 +00006748bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6749 APInt &SplatUndef,
6750 unsigned &SplatBitSize,
6751 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006752 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006753 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006754 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006755 assert(VT.isVector() && "Expected a vector type");
6756 unsigned sz = VT.getSizeInBits();
6757 if (MinSplatBits > sz)
6758 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006759
Bob Wilson85cefe82009-03-02 23:24:16 +00006760 SplatValue = APInt(sz, 0);
6761 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006762
Bob Wilson85cefe82009-03-02 23:24:16 +00006763 // Get the bits. Bits with undefined values (when the corresponding element
6764 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6765 // in SplatValue. If any of the values are not constant, give up and return
6766 // false.
6767 unsigned int nOps = getNumOperands();
6768 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6769 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006770
6771 for (unsigned j = 0; j < nOps; ++j) {
6772 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006773 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006774 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006775
Bob Wilson85cefe82009-03-02 23:24:16 +00006776 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006777 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006778 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006779 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006780 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006781 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006782 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006783 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006784 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006785 }
6786
Bob Wilson85cefe82009-03-02 23:24:16 +00006787 // The build_vector is all constants or undefs. Find the smallest element
6788 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006789
Bob Wilson85cefe82009-03-02 23:24:16 +00006790 HasAnyUndefs = (SplatUndef != 0);
6791 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006792
Bob Wilson85cefe82009-03-02 23:24:16 +00006793 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006794 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6795 APInt LowValue = SplatValue.trunc(HalfSize);
6796 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6797 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006798
Bob Wilson85cefe82009-03-02 23:24:16 +00006799 // If the two halves do not match (ignoring undef bits), stop here.
6800 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6801 MinSplatBits > HalfSize)
6802 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006803
Bob Wilson85cefe82009-03-02 23:24:16 +00006804 SplatValue = HighValue | LowValue;
6805 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006806
Bob Wilson85cefe82009-03-02 23:24:16 +00006807 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006808 }
6809
Bob Wilson85cefe82009-03-02 23:24:16 +00006810 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006811 return true;
6812}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006813
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006814SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6815 if (UndefElements) {
6816 UndefElements->clear();
6817 UndefElements->resize(getNumOperands());
6818 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006819 SDValue Splatted;
6820 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6821 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006822 if (Op.getOpcode() == ISD::UNDEF) {
6823 if (UndefElements)
6824 (*UndefElements)[i] = true;
6825 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006826 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006827 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006828 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006829 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006830 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006831
Chandler Carruthb844e722014-07-08 07:19:55 +00006832 if (!Splatted) {
6833 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6834 "Can only have a splat without a constant for all undefs.");
6835 return getOperand(0);
6836 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006837
Chandler Carruthb844e722014-07-08 07:19:55 +00006838 return Splatted;
6839}
6840
6841ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006842BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006843 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006844 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006845}
6846
6847ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006848BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006849 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006850 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006851}
6852
Juergen Ributzka73844052014-01-13 20:51:35 +00006853bool BuildVectorSDNode::isConstant() const {
6854 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6855 unsigned Opc = getOperand(i).getOpcode();
6856 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6857 return false;
6858 }
6859 return true;
6860}
6861
Owen Anderson53aa7a92009-08-10 22:56:29 +00006862bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006863 // Find the first non-undef value in the shuffle mask.
6864 unsigned i, e;
6865 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6866 /* search */;
6867
Nate Begeman39b59db2009-04-29 18:13:31 +00006868 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006869
Nate Begeman5f829d82009-04-29 05:20:52 +00006870 // Make sure all remaining elements are either undef or the same as the first
6871 // non-undef value.
6872 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006873 if (Mask[i] >= 0 && Mask[i] != Idx)
6874 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006875 return true;
6876}
David Greene09851602010-01-20 20:13:31 +00006877
Adam Nemetb4690e32014-05-31 16:23:20 +00006878#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00006879static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006880 SmallPtrSetImpl<const SDNode*> &Visited,
6881 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00006882 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006883 // If this node has already been checked, don't check it again.
6884 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006885 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006886
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006887 // If a node has already been visited on this depth-first walk, reject it as
6888 // a cycle.
David Blaikie70573dc2014-11-19 07:49:26 +00006889 if (!Visited.insert(N).second) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006890 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006891 dbgs() << "Offending node:\n";
6892 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006893 abort();
David Greene09851602010-01-20 20:13:31 +00006894 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006895
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006896 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00006897 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00006898
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006899 Checked.insert(N);
6900 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006901}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006902#endif
David Greene09851602010-01-20 20:13:31 +00006903
Adam Nemet7d394302014-05-31 16:23:17 +00006904void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00006905 const llvm::SelectionDAG *DAG,
6906 bool force) {
6907#ifndef NDEBUG
6908 bool check = force;
David Greene09851602010-01-20 20:13:31 +00006909#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00006910 check = true;
6911#endif // XDEBUG
6912 if (check) {
6913 assert(N && "Checking nonexistent SDNode");
6914 SmallPtrSet<const SDNode*, 32> visited;
6915 SmallPtrSet<const SDNode*, 32> checked;
6916 checkForCyclesHelper(N, visited, checked, DAG);
6917 }
6918#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00006919}
6920
Adam Nemetb4690e32014-05-31 16:23:20 +00006921void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6922 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00006923}