blob: 6cd640a3bfd93630720f11471ba99e8d2a778180 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendling6de55a02009-12-21 19:34:59 +000013
Chris Lattner600d3082003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng00fd0b62010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
Bill Wendlingbd092622008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetIntrinsicInfo.h"
44#include "llvm/Target/TargetLowering.h"
45#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetOptions.h"
47#include "llvm/Target/TargetRegisterInfo.h"
48#include "llvm/Target/TargetSelectionDAGInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000049#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000050#include <cmath>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000051
Chris Lattner560b5e42004-06-02 04:28:06 +000052using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000053
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000054/// makeVTList - Return an instance of the SDVTList struct initialized with the
55/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000056static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000057 SDVTList Res = {VTs, NumVTs};
58 return Res;
59}
60
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000061// Default null implementations of the callbacks.
62void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
63void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000064
Jim Laskeyd66e6162005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
66// ConstantFPSDNode Class
67//===----------------------------------------------------------------------===//
68
69/// isExactlyValue - We don't rely on operator== working on double values, as
70/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
71/// As such, this method can be used to do an exact bit-for-bit comparison of
72/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000073bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000074 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000075}
76
Owen Anderson53aa7a92009-08-10 22:56:29 +000077bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000078 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000079 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000080
Dale Johannesend246b2c2007-08-30 00:23:21 +000081 // convert modifies in place, so make a copy.
82 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000083 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000084 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
85 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000086 &losesInfo);
87 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000088}
89
Jim Laskeyd66e6162005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000093
Evan Chengc70e33c2006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000097 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +000098 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +000099 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000100
Evan Chengc70e33c2006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000102
Chris Lattnerc2d28112006-03-25 22:57:01 +0000103 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000104
Chris Lattnerc2d28112006-03-25 22:57:01 +0000105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000108
Chris Lattnerc2d28112006-03-25 22:57:01 +0000109 // Do not accept an all-undef vector.
110 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000111
Chris Lattnerc2d28112006-03-25 22:57:01 +0000112 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000113 // elements. We have to be a bit careful here, as the type of the constant
114 // may not be the same as the type of the vector elements due to type
115 // legalization (the elements are promoted to a legal type for the target and
116 // a vector of a type may be legal when the base element type is not).
117 // We only want to check enough bits to cover the vector elements, because
118 // we care if the resultant vector is all ones, not whether the individual
119 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000120 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000121 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000122 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
123 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000124 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000125 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
126 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000127 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000128 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000129 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000130
Chris Lattnerc2d28112006-03-25 22:57:01 +0000131 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000132 // undefs. Even with the above element type twiddling, this should be OK, as
133 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000134 for (++i; i != e; ++i)
135 if (N->getOperand(i) != NotZero &&
136 N->getOperand(i).getOpcode() != ISD::UNDEF)
137 return false;
138 return true;
139}
140
141
Evan Chenga6789912006-03-26 09:50:58 +0000142/// isBuildVectorAllZeros - Return true if the specified node is a
143/// BUILD_VECTOR where all of the elements are 0 or undef.
144bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000145 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +0000146 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000147 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000148
Evan Chenga6789912006-03-26 09:50:58 +0000149 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000150
Kevin Qin93d45ec2014-06-24 05:37:27 +0000151 bool IsAllUndef = true;
152 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
153 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
154 continue;
155 IsAllUndef = false;
156 // Do not accept build_vectors that aren't all constants or which have non-0
157 // elements. We have to be a bit careful here, as the type of the constant
158 // may not be the same as the type of the vector elements due to type
159 // legalization (the elements are promoted to a legal type for the target
160 // and a vector of a type may be legal when the base element type is not).
161 // We only want to check enough bits to cover the vector elements, because
162 // we care if the resultant vector is all zeros, not whether the individual
163 // constants are.
164 SDValue Zero = N->getOperand(i);
165 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
166 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
167 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
168 return false;
169 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
170 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
171 return false;
172 } else
173 return false;
174 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000175
Evan Chengc70e33c2006-03-27 06:58:47 +0000176 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000177 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000178 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000179 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000180}
181
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000182/// \brief Return true if the specified node is a BUILD_VECTOR node of
183/// all ConstantSDNode or undef.
184bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
185 if (N->getOpcode() != ISD::BUILD_VECTOR)
186 return false;
187
188 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
189 SDValue Op = N->getOperand(i);
190 if (Op.getOpcode() == ISD::UNDEF)
191 continue;
192 if (!isa<ConstantSDNode>(Op))
193 return false;
194 }
195 return true;
196}
197
Evan Cheng6200c222008-02-18 23:04:32 +0000198/// isScalarToVector - Return true if the specified node is a
199/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
200/// element is not an undef.
201bool ISD::isScalarToVector(const SDNode *N) {
202 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
203 return true;
204
205 if (N->getOpcode() != ISD::BUILD_VECTOR)
206 return false;
207 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
208 return false;
209 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000210 if (NumElems == 1)
211 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000212 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000213 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000214 if (V.getOpcode() != ISD::UNDEF)
215 return false;
216 }
217 return true;
218}
219
Nadav Rotema62368c2012-07-15 08:38:23 +0000220/// allOperandsUndef - Return true if the node has at least one operand
221/// and all operands of the specified node are ISD::UNDEF.
222bool ISD::allOperandsUndef(const SDNode *N) {
223 // Return false if the node has no operands.
224 // This is "logically inconsistent" with the definition of "all" but
225 // is probably the desired behavior.
226 if (N->getNumOperands() == 0)
227 return false;
228
229 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
230 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
231 return false;
232
233 return true;
234}
235
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000236ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) {
237 switch (ExtType) {
238 case ISD::EXTLOAD:
239 return ISD::ANY_EXTEND;
240 case ISD::SEXTLOAD:
241 return ISD::SIGN_EXTEND;
242 case ISD::ZEXTLOAD:
243 return ISD::ZERO_EXTEND;
244 default:
245 break;
246 }
247
248 llvm_unreachable("Invalid LoadExtType");
249}
250
Chris Lattner061a1ea2005-01-07 07:46:32 +0000251/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
252/// when given the operation for (X op Y).
253ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
254 // To perform this operation, we just need to swap the L and G bits of the
255 // operation.
256 unsigned OldL = (Operation >> 2) & 1;
257 unsigned OldG = (Operation >> 1) & 1;
258 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
259 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000260 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000261}
262
263/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
264/// 'op' is a valid SetCC operation.
265ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
266 unsigned Operation = Op;
267 if (isInteger)
268 Operation ^= 7; // Flip L, G, E bits, but not U.
269 else
270 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000271
Chris Lattner061a1ea2005-01-07 07:46:32 +0000272 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000273 Operation &= ~8; // Don't let N and U bits get set.
274
Chris Lattner061a1ea2005-01-07 07:46:32 +0000275 return ISD::CondCode(Operation);
276}
277
278
279/// isSignedOp - For an integer comparison, return 1 if the comparison is a
280/// signed operation and 2 if the result is an unsigned comparison. Return zero
281/// if the operation does not depend on the sign of the input (setne and seteq).
282static int isSignedOp(ISD::CondCode Opcode) {
283 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000284 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000285 case ISD::SETEQ:
286 case ISD::SETNE: return 0;
287 case ISD::SETLT:
288 case ISD::SETLE:
289 case ISD::SETGT:
290 case ISD::SETGE: return 1;
291 case ISD::SETULT:
292 case ISD::SETULE:
293 case ISD::SETUGT:
294 case ISD::SETUGE: return 2;
295 }
296}
297
298/// getSetCCOrOperation - Return the result of a logical OR between different
299/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
300/// returns SETCC_INVALID if it is not possible to represent the resultant
301/// comparison.
302ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
303 bool isInteger) {
304 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
305 // Cannot fold a signed integer setcc with an unsigned integer setcc.
306 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000307
Chris Lattner061a1ea2005-01-07 07:46:32 +0000308 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000309
Chris Lattner061a1ea2005-01-07 07:46:32 +0000310 // If the N and U bits get set then the resultant comparison DOES suddenly
311 // care about orderedness, and is true when ordered.
312 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000313 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000314
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000315 // Canonicalize illegal integer setcc's.
316 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
317 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000318
Chris Lattner061a1ea2005-01-07 07:46:32 +0000319 return ISD::CondCode(Op);
320}
321
322/// getSetCCAndOperation - Return the result of a logical AND between different
323/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
324/// function returns zero if it is not possible to represent the resultant
325/// comparison.
326ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
327 bool isInteger) {
328 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
329 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000330 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000331
332 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000333 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000334
Chris Lattner393d96a2006-04-27 05:01:07 +0000335 // Canonicalize illegal integer setcc's.
336 if (isInteger) {
337 switch (Result) {
338 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000339 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000340 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000341 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
342 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
343 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000344 }
345 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000346
Chris Lattner393d96a2006-04-27 05:01:07 +0000347 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000348}
349
Jim Laskeyd66e6162005-08-17 20:08:02 +0000350//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000351// SDNode Profile Support
352//===----------------------------------------------------------------------===//
353
Jim Laskeybd0f0882006-10-27 23:52:51 +0000354/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
355///
Jim Laskeyf576b422006-10-27 23:46:08 +0000356static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
357 ID.AddInteger(OpC);
358}
359
360/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
361/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000362static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000363 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000364}
365
Jim Laskeybd0f0882006-10-27 23:52:51 +0000366/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
367///
Jim Laskeyf576b422006-10-27 23:46:08 +0000368static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000369 ArrayRef<SDValue> Ops) {
370 for (auto& Op : Ops) {
371 ID.AddPointer(Op.getNode());
372 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000373 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000374}
375
Dan Gohman768f2c92008-07-07 18:26:29 +0000376/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
377///
378static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000379 ArrayRef<SDUse> Ops) {
380 for (auto& Op : Ops) {
381 ID.AddPointer(Op.getNode());
382 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000383 }
384}
385
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000386static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
387 bool exact) {
388 ID.AddBoolean(nuw);
389 ID.AddBoolean(nsw);
390 ID.AddBoolean(exact);
391}
392
393/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
394static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
395 bool nuw, bool nsw, bool exact) {
396 if (isBinOpWithFlags(Opcode))
397 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
398}
399
Craig Topper633d99b2014-04-27 23:22:43 +0000400static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
401 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000402 AddNodeIDOpcode(ID, OpC);
403 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000404 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000405}
406
Duncan Sands835bdca2008-10-27 15:30:53 +0000407/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
408/// the NodeID data.
409static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000410 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000411 case ISD::TargetExternalSymbol:
412 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000413 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000414 default: break; // Normal nodes don't need extra info.
415 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000416 case ISD::Constant: {
417 const ConstantSDNode *C = cast<ConstantSDNode>(N);
418 ID.AddPointer(C->getConstantIntValue());
419 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000420 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000421 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000422 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000423 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000424 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000425 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000426 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000427 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000428 case ISD::GlobalAddress:
429 case ISD::TargetGlobalTLSAddress:
430 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000431 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000432 ID.AddPointer(GA->getGlobal());
433 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000434 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000435 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000436 break;
437 }
438 case ISD::BasicBlock:
439 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
440 break;
441 case ISD::Register:
442 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
443 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000444 case ISD::RegisterMask:
445 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
446 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000447 case ISD::SRCVALUE:
448 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
449 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000450 case ISD::FrameIndex:
451 case ISD::TargetFrameIndex:
452 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
453 break;
454 case ISD::JumpTable:
455 case ISD::TargetJumpTable:
456 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000457 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000458 break;
459 case ISD::ConstantPool:
460 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000461 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000462 ID.AddInteger(CP->getAlignment());
463 ID.AddInteger(CP->getOffset());
464 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000465 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000466 else
467 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000468 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000469 break;
470 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000471 case ISD::TargetIndex: {
472 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
473 ID.AddInteger(TI->getIndex());
474 ID.AddInteger(TI->getOffset());
475 ID.AddInteger(TI->getTargetFlags());
476 break;
477 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000478 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000479 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000480 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000481 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000482 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000483 break;
484 }
485 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000486 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000487 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000488 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000489 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000490 break;
491 }
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000492 case ISD::SDIV:
493 case ISD::UDIV:
494 case ISD::SRA:
495 case ISD::SRL:
496 case ISD::MUL:
497 case ISD::ADD:
498 case ISD::SUB:
499 case ISD::SHL: {
500 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
501 AddBinaryNodeIDCustom(ID, N->getOpcode(), BinNode->hasNoUnsignedWrap(),
502 BinNode->hasNoSignedWrap(), BinNode->isExact());
503 break;
504 }
Dan Gohman12f24902008-12-23 21:37:04 +0000505 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000506 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000507 case ISD::ATOMIC_SWAP:
508 case ISD::ATOMIC_LOAD_ADD:
509 case ISD::ATOMIC_LOAD_SUB:
510 case ISD::ATOMIC_LOAD_AND:
511 case ISD::ATOMIC_LOAD_OR:
512 case ISD::ATOMIC_LOAD_XOR:
513 case ISD::ATOMIC_LOAD_NAND:
514 case ISD::ATOMIC_LOAD_MIN:
515 case ISD::ATOMIC_LOAD_MAX:
516 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000517 case ISD::ATOMIC_LOAD_UMAX:
518 case ISD::ATOMIC_LOAD:
519 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000520 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000521 ID.AddInteger(AT->getMemoryVT().getRawBits());
522 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000523 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
524 break;
525 }
526 case ISD::PREFETCH: {
527 const MemSDNode *PF = cast<MemSDNode>(N);
528 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000529 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000530 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000531 case ISD::VECTOR_SHUFFLE: {
532 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000533 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000534 i != e; ++i)
535 ID.AddInteger(SVN->getMaskElt(i));
536 break;
537 }
Dan Gohman6c938802009-10-30 01:27:03 +0000538 case ISD::TargetBlockAddress:
539 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000540 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
541 ID.AddPointer(BA->getBlockAddress());
542 ID.AddInteger(BA->getOffset());
543 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000544 break;
545 }
Mon P Wang6a490372008-06-25 08:15:39 +0000546 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000547
548 // Target specific memory nodes could also have address spaces to check.
549 if (N->isTargetMemoryOpcode())
550 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000551}
552
Duncan Sands835bdca2008-10-27 15:30:53 +0000553/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
554/// data.
555static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
556 AddNodeIDOpcode(ID, N->getOpcode());
557 // Add the return value info.
558 AddNodeIDValueTypes(ID, N->getVTList());
559 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000560 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000561
562 // Handle SDNode leafs with special info.
563 AddNodeIDCustom(ID, N);
564}
565
Dan Gohman2da2bed2008-08-20 15:58:01 +0000566/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000567/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000568/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000569///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000570static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000571encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000572 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000573 assert((ConvType & 3) == ConvType &&
574 "ConvType may not require more than 2 bits!");
575 assert((AM & 7) == AM &&
576 "AM may not require more than 3 bits!");
577 return ConvType |
578 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000579 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000580 (isNonTemporal << 6) |
581 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000582}
583
Jim Laskeyf576b422006-10-27 23:46:08 +0000584//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000585// SelectionDAG Class
586//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000587
Duncan Sands835bdca2008-10-27 15:30:53 +0000588/// doNotCSE - Return true if CSE should not be performed for this node.
589static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000590 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000591 return true; // Never CSE anything that produces a flag.
592
593 switch (N->getOpcode()) {
594 default: break;
595 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000596 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000597 return true; // Never CSE these nodes.
598 }
599
600 // Check that remaining values produced are not flags.
601 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000602 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000603 return true; // Never CSE anything that produces a flag.
604
605 return false;
606}
607
Chris Lattner9c667932005-01-07 21:09:16 +0000608/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000609/// SelectionDAG.
610void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000611 // Create a dummy node (which is not added to allnodes), that adds a reference
612 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000613 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000614
Chris Lattner8927c872006-08-04 17:45:20 +0000615 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000616
Chris Lattner8927c872006-08-04 17:45:20 +0000617 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000618 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000619 if (I->use_empty())
620 DeadNodes.push_back(I);
621
Dan Gohman91697632008-07-07 20:57:48 +0000622 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000623
Dan Gohman91697632008-07-07 20:57:48 +0000624 // If the root changed (e.g. it was a dead load, update the root).
625 setRoot(Dummy.getValue());
626}
627
628/// RemoveDeadNodes - This method deletes the unreachable nodes in the
629/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000630void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000631
Chris Lattner8927c872006-08-04 17:45:20 +0000632 // Process the worklist, deleting the nodes and adding their uses to the
633 // worklist.
634 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000635 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000636
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000637 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000638 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000639
Chris Lattner8927c872006-08-04 17:45:20 +0000640 // Take the node out of the appropriate CSE map.
641 RemoveNodeFromCSEMaps(N);
642
643 // Next, brutally remove the operand list. This is safe to do, as there are
644 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000645 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
646 SDUse &Use = *I++;
647 SDNode *Operand = Use.getNode();
648 Use.set(SDValue());
649
Chris Lattner8927c872006-08-04 17:45:20 +0000650 // Now that we removed this operand, see if there are no uses of it left.
651 if (Operand->use_empty())
652 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000653 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000654
Dan Gohman534c8a22009-01-19 22:39:36 +0000655 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000656 }
Chris Lattner9c667932005-01-07 21:09:16 +0000657}
658
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000659void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000660 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000661
662 // Create a dummy node that adds a reference to the root node, preventing
663 // it from being deleted. (This matters if the root is an operand of the
664 // dead node.)
665 HandleSDNode Dummy(getRoot());
666
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000667 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000668}
669
Chris Lattnerc738d002005-08-29 21:59:31 +0000670void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000671 // First take this out of the appropriate CSE map.
672 RemoveNodeFromCSEMaps(N);
673
Scott Michelcf0da6c2009-02-17 22:15:04 +0000674 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000675 // AllNodes list, and delete the node.
676 DeleteNodeNotInCSEMaps(N);
677}
678
679void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000680 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
681 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000682
Dan Gohman86aa16a2008-09-30 18:30:35 +0000683 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000684 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000685
Dan Gohman534c8a22009-01-19 22:39:36 +0000686 DeallocateNode(N);
687}
688
689void SelectionDAG::DeallocateNode(SDNode *N) {
690 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000691 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000692
Dan Gohman534c8a22009-01-19 22:39:36 +0000693 // Set the opcode to DELETED_NODE to help catch bugs when node
694 // memory is reallocated.
695 N->NodeType = ISD::DELETED_NODE;
696
Dan Gohman2e834902008-08-26 01:44:34 +0000697 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000698
Evan Cheng563fe3c2010-03-25 01:38:16 +0000699 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000700 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000701 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
702 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000703}
704
Chris Lattner19732782005-08-16 18:17:10 +0000705/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
706/// correspond to it. This is useful when we're about to delete or repurpose
707/// the node. We don't want future request for structurally identical nodes
708/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000709bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000710 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000711 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000712 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000713 case ISD::CONDCODE:
714 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
715 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000716 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
717 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000718 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000719 case ISD::ExternalSymbol:
720 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000721 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000722 case ISD::TargetExternalSymbol: {
723 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
724 Erased = TargetExternalSymbols.erase(
725 std::pair<std::string,unsigned char>(ESN->getSymbol(),
726 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000727 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000728 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000729 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000730 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000731 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000732 Erased = ExtendedValueTypeNodes.erase(VT);
733 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000734 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
735 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000736 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000737 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000738 }
Chris Lattner9c667932005-01-07 21:09:16 +0000739 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000740 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000741 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
742 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000743 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000744 break;
745 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000746#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000747 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000748 // flag result (which cannot be CSE'd) or is one of the special cases that are
749 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000750 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000751 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000752 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000753 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000754 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000755 }
756#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000757 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000758}
759
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000760/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
761/// maps and modified in place. Add it back to the CSE maps, unless an identical
762/// node already exists, in which case transfer all its users to the existing
763/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000764///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000765void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000766SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000767 // For node types that aren't CSE'd, just act as if no identical node
768 // already exists.
769 if (!doNotCSE(N)) {
770 SDNode *Existing = CSEMap.GetOrInsertNode(N);
771 if (Existing != N) {
772 // If there was already an existing matching node, use ReplaceAllUsesWith
773 // to replace the dead one with the existing one. This can cause
774 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000775 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000776
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000777 // N is now dead. Inform the listeners and delete it.
778 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
779 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000780 DeleteNodeNotInCSEMaps(N);
781 return;
782 }
783 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000784
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000785 // If the node doesn't already exist, we updated it. Inform listeners.
786 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
787 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000788}
789
Chris Lattnerf34156e2006-01-28 09:32:45 +0000790/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000791/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000792/// return null, otherwise return a pointer to the slot it would take. If a
793/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000794SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000795 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000796 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000797 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000798
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000799 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000800 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000801 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000802 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000803 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000804 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000805}
806
807/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000808/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000809/// return null, otherwise return a pointer to the slot it would take. If a
810/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000811SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000812 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000813 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000814 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000815 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000816
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000817 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000818 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000819 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000820 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000821 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000822 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000823}
824
825
826/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000827/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000828/// return null, otherwise return a pointer to the slot it would take. If a
829/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000830SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000831 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000832 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000833 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000834
Jim Laskeyf576b422006-10-27 23:46:08 +0000835 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000836 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000837 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000838 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000839 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000840}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000841
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000842#ifndef NDEBUG
Chandler Carruth2fc9a2b2014-07-22 04:03:22 +0000843/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
844static void VerifySDNode(SDNode *N) {
Duncan Sandsb0e39382008-07-21 10:20:31 +0000845 switch (N->getOpcode()) {
846 default:
847 break;
Duncan Sands17e678b2008-10-29 14:22:20 +0000848 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000849 EVT VT = N->getValueType(0);
Duncan Sands17e678b2008-10-29 14:22:20 +0000850 assert(N->getNumValues() == 1 && "Too many results!");
851 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
852 "Wrong return type!");
853 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
854 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
855 "Mismatched operand types!");
856 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
857 "Wrong operand type!");
858 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
859 "Wrong return type size");
860 break;
861 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000862 case ISD::BUILD_VECTOR: {
Duncan Sands17e678b2008-10-29 14:22:20 +0000863 assert(N->getNumValues() == 1 && "Too many results!");
864 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsb0e39382008-07-21 10:20:31 +0000865 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands17e678b2008-10-29 14:22:20 +0000866 "Wrong number of operands!");
Owen Anderson53aa7a92009-08-10 22:56:29 +0000867 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedmanb7910b72011-09-09 21:04:06 +0000868 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindolab93db662009-04-24 12:40:33 +0000869 assert((I->getValueType() == EltVT ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000870 (EltVT.isInteger() && I->getValueType().isInteger() &&
871 EltVT.bitsLE(I->getValueType()))) &&
872 "Wrong operand type!");
Eli Friedmanb7910b72011-09-09 21:04:06 +0000873 assert(I->getValueType() == N->getOperand(0).getValueType() &&
874 "Operands must all have the same type");
875 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000876 break;
877 }
878 }
879}
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000880#endif // NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000881
Owen Anderson53aa7a92009-08-10 22:56:29 +0000882/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000883/// given type.
884///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000885unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000886 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000887 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000888 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000889
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000890 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000891}
Chris Lattner9c667932005-01-07 21:09:16 +0000892
Dale Johannesen8ba713212009-02-07 02:15:05 +0000893// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000894SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Craig Topperc0196b12014-04-14 00:51:57 +0000895 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TLI(nullptr), OptLevel(OL),
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000896 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sanders50b80412013-11-15 12:56:49 +0000897 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
Craig Topperc0196b12014-04-14 00:51:57 +0000898 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000899 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000900 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000901}
902
Juergen Ributzka659ce002014-01-28 01:20:14 +0000903void SelectionDAG::init(MachineFunction &mf, const TargetLowering *tli) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000904 MF = &mf;
Juergen Ributzkab3487102013-11-19 21:20:17 +0000905 TLI = tli;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000906 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000907}
908
Chris Lattner600d3082003-08-11 14:57:33 +0000909SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000910 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000911 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000912 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000913}
914
915void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000916 assert(&*AllNodes.begin() == &EntryNode);
917 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000918 while (!AllNodes.empty())
919 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000920}
921
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000922BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
923 SDVTList VTs, SDValue N1,
924 SDValue N2, bool nuw, bool nsw,
925 bool exact) {
926 if (isBinOpWithFlags(Opcode)) {
927 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
928 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
929 FN->setHasNoUnsignedWrap(nuw);
930 FN->setHasNoSignedWrap(nsw);
931 FN->setIsExact(exact);
932
933 return FN;
934 }
935
936 BinarySDNode *N = new (NodeAllocator)
937 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
938 return N;
939}
940
Dan Gohmane1a9a782008-08-27 23:52:12 +0000941void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000942 allnodes_clear();
943 OperandAllocator.Reset();
944 CSEMap.clear();
945
946 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000947 ExternalSymbols.clear();
948 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000949 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000950 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000951 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000952 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000953
Craig Topperc0196b12014-04-14 00:51:57 +0000954 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000955 AllNodes.push_back(&EntryNode);
956 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000957 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000958}
959
Andrew Trickef9de2a2013-05-25 02:42:55 +0000960SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000961 return VT.bitsGT(Op.getValueType()) ?
962 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
963 getNode(ISD::TRUNCATE, DL, VT, Op);
964}
965
Andrew Trickef9de2a2013-05-25 02:42:55 +0000966SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000967 return VT.bitsGT(Op.getValueType()) ?
968 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
969 getNode(ISD::TRUNCATE, DL, VT, Op);
970}
971
Andrew Trickef9de2a2013-05-25 02:42:55 +0000972SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000973 return VT.bitsGT(Op.getValueType()) ?
974 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
975 getNode(ISD::TRUNCATE, DL, VT, Op);
976}
977
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000978SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
979 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +0000980 if (VT.bitsLE(Op.getValueType()))
981 return getNode(ISD::TRUNCATE, SL, VT, Op);
982
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000983 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +0000984 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
985}
986
Andrew Trickef9de2a2013-05-25 02:42:55 +0000987SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000988 assert(!VT.isVector() &&
989 "getZeroExtendInReg should use the vector element type instead of "
990 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000991 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000992 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
993 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000994 VT.getSizeInBits());
995 return getNode(ISD::AND, DL, Op.getValueType(), Op,
996 getConstant(Imm, Op.getValueType()));
997}
998
Chandler Carruth0b666e02014-07-10 12:32:32 +0000999SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1000 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1001 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1002 "The sizes of the input and result must match in order to perform the "
1003 "extend in-register.");
1004 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1005 "The destination vector type must have fewer lanes than the input.");
1006 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1007}
1008
1009SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1010 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1011 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1012 "The sizes of the input and result must match in order to perform the "
1013 "extend in-register.");
1014 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1015 "The destination vector type must have fewer lanes than the input.");
1016 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1017}
1018
Chandler Carruthafe4b252014-07-09 10:58:18 +00001019SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1020 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001021 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1022 "The sizes of the input and result must match in order to perform the "
1023 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001024 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1025 "The destination vector type must have fewer lanes than the input.");
1026 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1027}
1028
Bob Wilsonc5890052009-01-22 17:39:32 +00001029/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1030///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001031SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001032 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001033 SDValue NegOne =
1034 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001035 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1036}
1037
Pete Cooper7fd1d722014-05-12 23:26:58 +00001038SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1039 EVT EltVT = VT.getScalarType();
1040 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001041 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001042 case TargetLowering::ZeroOrOneBooleanContent:
1043 case TargetLowering::UndefinedBooleanContent:
1044 TrueValue = getConstant(1, VT);
1045 break;
1046 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1047 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1048 VT);
1049 break;
1050 }
1051 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1052}
1053
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001054SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001055 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001056 assert((EltVT.getSizeInBits() >= 64 ||
1057 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1058 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001059 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001060}
1061
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001062SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1063{
1064 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001065}
1066
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001067SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1068 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001069 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001070
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001071 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001072 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001073
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001074 const TargetLowering *TLI = TM.getTargetLowering();
1075
Duncan Sandsf2641e12011-09-06 19:07:46 +00001076 // In some cases the vector type is legal but the element type is illegal and
1077 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1078 // inserted value (the type does not need to match the vector element type).
1079 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001080 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001081 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001082 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001083 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1084 Elt = ConstantInt::get(*getContext(), NewVal);
1085 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001086 // In other cases the element type is illegal and needs to be expanded, for
1087 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1088 // the value into n parts and use a vector type with n-times the elements.
1089 // Then bitcast to the type requested.
1090 // Legalizing constants too early makes the DAGCombiner's job harder so we
1091 // only legalize if the DAG tells us we must produce legal types.
1092 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1093 TLI->getTypeAction(*getContext(), EltVT) ==
1094 TargetLowering::TypeExpandInteger) {
1095 APInt NewVal = Elt->getValue();
1096 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1097 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1098 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1099 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1100
1101 // Check the temporary vector is the correct size. If this fails then
1102 // getTypeToTransformTo() probably returned a type whose size (in bits)
1103 // isn't a power-of-2 factor of the requested type size.
1104 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1105
1106 SmallVector<SDValue, 2> EltParts;
1107 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1108 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1109 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001110 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001111 }
1112
1113 // EltParts is currently in little endian order. If we actually want
1114 // big-endian order then reverse it now.
1115 if (TLI->isBigEndian())
1116 std::reverse(EltParts.begin(), EltParts.end());
1117
1118 // The elements must be reversed when the element order is different
1119 // to the endianness of the elements (because the BITCAST is itself a
1120 // vector shuffle in this situation). However, we do not need any code to
1121 // perform this reversal because getConstant() is producing a vector
1122 // splat.
1123 // This situation occurs in MIPS MSA.
1124
1125 SmallVector<SDValue, 8> Ops;
1126 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1127 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1128
1129 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1130 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001131 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001132 return Result;
1133 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001134
1135 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1136 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001137 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001138 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001139 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001140 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001141 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001142 void *IP = nullptr;
1143 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001144 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001145 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001146 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001147
Dan Gohman7a7742c2007-12-12 22:21:26 +00001148 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001149 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001150 CSEMap.InsertNode(N, IP);
1151 AllNodes.push_back(N);
1152 }
1153
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001154 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001155 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001156 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001157 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001158 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001159 }
1160 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001161}
1162
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001163SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001164 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001165}
1166
1167
Owen Anderson53aa7a92009-08-10 22:56:29 +00001168SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001169 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001170}
1171
Owen Anderson53aa7a92009-08-10 22:56:29 +00001172SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001173 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001174
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001175 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001176
Chris Lattner381dddc2005-02-17 20:17:32 +00001177 // Do the map lookup using the actual bit pattern for the floating point
1178 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1179 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001180 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001181 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001182 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001183 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001184 void *IP = nullptr;
1185 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001186 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001187 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001188 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001189
Evan Cheng9458e6a2007-06-29 21:36:04 +00001190 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001191 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001192 CSEMap.InsertNode(N, IP);
1193 AllNodes.push_back(N);
1194 }
1195
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001196 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001197 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001198 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001199 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001200 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001201 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001202 }
1203 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001204}
1205
Owen Anderson53aa7a92009-08-10 22:56:29 +00001206SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001207 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001208 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001209 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001210 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001211 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001212 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1213 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001214 bool ignored;
1215 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001216 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001217 &ignored);
1218 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001219 } else
1220 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001221}
1222
Andrew Trickef9de2a2013-05-25 02:42:55 +00001223SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001224 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001225 bool isTargetGA,
1226 unsigned char TargetFlags) {
1227 assert((TargetFlags == 0 || isTargetGA) &&
1228 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001229 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001230
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001231 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001232 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001233 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001234 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001235
Chris Lattner8e34f982009-06-25 21:21:14 +00001236 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001237 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001238 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1239 else
1240 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001241
Jim Laskeyf576b422006-10-27 23:46:08 +00001242 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001243 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001244 ID.AddPointer(GV);
1245 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001246 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001247 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001248 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001249 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001250 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001251
Andrew Trickef9de2a2013-05-25 02:42:55 +00001252 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1253 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001254 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001255 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001256 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001257 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001258}
1259
Owen Anderson53aa7a92009-08-10 22:56:29 +00001260SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001261 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001262 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001263 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001264 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001265 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001266 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001267 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001268
Dan Gohman01c65a22010-03-18 18:49:47 +00001269 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001270 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001271 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001272 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001273}
1274
Owen Anderson53aa7a92009-08-10 22:56:29 +00001275SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001276 unsigned char TargetFlags) {
1277 assert((TargetFlags == 0 || isTarget) &&
1278 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001279 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
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(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001283 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001284 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001285 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001286 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001287
Dan Gohman01c65a22010-03-18 18:49:47 +00001288 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1289 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001290 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001291 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001292 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001293}
1294
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001295SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001296 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001297 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001298 unsigned char TargetFlags) {
1299 assert((TargetFlags == 0 || isTarget) &&
1300 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001301 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001302 Alignment =
1303 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001304 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001305 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001306 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001307 ID.AddInteger(Alignment);
1308 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001309 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001310 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001311 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001312 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001313 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001314
Dan Gohman01c65a22010-03-18 18:49:47 +00001315 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1316 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001317 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001318 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001319 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001320}
1321
Chris Lattner061a1ea2005-01-07 07:46:32 +00001322
Owen Anderson53aa7a92009-08-10 22:56:29 +00001323SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001324 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001325 bool isTarget,
1326 unsigned char TargetFlags) {
1327 assert((TargetFlags == 0 || isTarget) &&
1328 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001329 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001330 Alignment =
1331 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001332 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001333 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001334 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001335 ID.AddInteger(Alignment);
1336 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001337 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001338 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001339 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001340 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001341 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001342
Dan Gohman01c65a22010-03-18 18:49:47 +00001343 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1344 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001345 CSEMap.InsertNode(N, IP);
1346 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001347 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001348}
1349
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001350SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1351 unsigned char TargetFlags) {
1352 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001353 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001354 ID.AddInteger(Index);
1355 ID.AddInteger(Offset);
1356 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001357 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001358 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1359 return SDValue(E, 0);
1360
1361 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1362 TargetFlags);
1363 CSEMap.InsertNode(N, IP);
1364 AllNodes.push_back(N);
1365 return SDValue(N, 0);
1366}
1367
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001368SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001369 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001370 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001371 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001372 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001373 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001374 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001375
Dan Gohman01c65a22010-03-18 18:49:47 +00001376 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001377 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001378 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001379 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001380}
1381
Owen Anderson53aa7a92009-08-10 22:56:29 +00001382SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001383 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1384 ValueTypeNodes.size())
1385 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001386
Duncan Sands13237ac2008-06-06 12:08:01 +00001387 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001388 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001389
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001390 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001391 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001392 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001393 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001394}
1395
Owen Anderson53aa7a92009-08-10 22:56:29 +00001396SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001397 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001398 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001399 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001400 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001401 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001402}
1403
Owen Anderson53aa7a92009-08-10 22:56:29 +00001404SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001405 unsigned char TargetFlags) {
1406 SDNode *&N =
1407 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1408 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001409 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001410 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001411 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001412 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001413}
1414
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001415SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001416 if ((unsigned)Cond >= CondCodeNodes.size())
1417 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001418
Craig Topperc0196b12014-04-14 00:51:57 +00001419 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001420 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001421 CondCodeNodes[Cond] = N;
1422 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001423 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001424
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001425 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001426}
1427
Nate Begeman5f829d82009-04-29 05:20:52 +00001428// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1429// the shuffle mask M that point at N1 to point at N2, and indices that point
1430// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001431static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1432 std::swap(N1, N2);
1433 int NElts = M.size();
1434 for (int i = 0; i != NElts; ++i) {
1435 if (M[i] >= NElts)
1436 M[i] -= NElts;
1437 else if (M[i] >= 0)
1438 M[i] += NElts;
1439 }
1440}
1441
Andrew Trickef9de2a2013-05-25 02:42:55 +00001442SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001443 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001444 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1445 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001446
1447 // Canonicalize shuffle undef, undef -> undef
1448 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001449 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001450
Eric Christopherdfda92b2009-08-22 00:40:45 +00001451 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001452 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001453 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001454 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001455 for (unsigned i = 0; i != NElts; ++i) {
1456 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001457 MaskVec.push_back(Mask[i]);
1458 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001459
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001460 // Canonicalize shuffle v, v -> v, undef
1461 if (N1 == N2) {
1462 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001463 for (unsigned i = 0; i != NElts; ++i)
1464 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001465 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001466
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001467 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1468 if (N1.getOpcode() == ISD::UNDEF)
1469 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001470
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001471 // Canonicalize all index into lhs, -> shuffle lhs, undef
1472 // Canonicalize all index into rhs, -> shuffle rhs, undef
1473 bool AllLHS = true, AllRHS = true;
1474 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001475 for (unsigned i = 0; i != NElts; ++i) {
1476 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001477 if (N2Undef)
1478 MaskVec[i] = -1;
1479 else
1480 AllLHS = false;
1481 } else if (MaskVec[i] >= 0) {
1482 AllRHS = false;
1483 }
1484 }
1485 if (AllLHS && AllRHS)
1486 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001487 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001488 N2 = getUNDEF(VT);
1489 if (AllRHS) {
1490 N1 = getUNDEF(VT);
1491 commuteShuffle(N1, N2, MaskVec);
1492 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001493 // Reset our undef status after accounting for the mask.
1494 N2Undef = N2.getOpcode() == ISD::UNDEF;
1495 // Re-check whether both sides ended up undef.
1496 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1497 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001498
Craig Topper9a39b072013-08-08 08:03:12 +00001499 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001500 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001501 for (unsigned i = 0; i != NElts; ++i) {
1502 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001503 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001504 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001505 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001506
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001507 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001508 if (N2Undef) {
1509 SDValue V = N1;
1510
1511 // Look through any bitcasts. We check that these don't change the number
1512 // (and size) of elements and just changes their types.
1513 while (V.getOpcode() == ISD::BITCAST)
1514 V = V->getOperand(0);
1515
1516 // A splat should always show up as a build vector node.
1517 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001518 BitVector UndefElements;
1519 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001520 // If this is a splat of an undef, shuffling it is also undef.
1521 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1522 return getUNDEF(VT);
1523
1524 // We only have a splat which can skip shuffles if there is a splatted
1525 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001526 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001527 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1528 // number of elements match or the value splatted is a zero constant.
1529 if (V.getValueType().getVectorNumElements() ==
1530 VT.getVectorNumElements())
1531 return N1;
1532 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1533 if (C->isNullValue())
1534 return N1;
1535 }
1536 }
1537 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001538
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001539 FoldingSetNodeID ID;
1540 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001541 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001542 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001543 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001544
Craig Topperc0196b12014-04-14 00:51:57 +00001545 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001546 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001547 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001548
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001549 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1550 // SDNode doesn't have access to it. This memory will be "leaked" when
1551 // the node is deallocated, but recovered when the NodeAllocator is released.
1552 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1553 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001554
Dan Gohman01c65a22010-03-18 18:49:47 +00001555 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001556 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1557 dl.getDebugLoc(), N1, N2,
1558 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001559 CSEMap.InsertNode(N, IP);
1560 AllNodes.push_back(N);
1561 return SDValue(N, 0);
1562}
1563
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001564SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1565 MVT VT = SV.getSimpleValueType(0);
1566 unsigned NumElems = VT.getVectorNumElements();
1567 SmallVector<int, 8> MaskVec;
1568
1569 for (unsigned i = 0; i != NumElems; ++i) {
1570 int Idx = SV.getMaskElt(i);
1571 if (Idx >= 0) {
1572 if (Idx < (int)NumElems)
1573 Idx += NumElems;
1574 else
1575 Idx -= NumElems;
1576 }
1577 MaskVec.push_back(Idx);
1578 }
1579
1580 SDValue Op0 = SV.getOperand(0);
1581 SDValue Op1 = SV.getOperand(1);
1582 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1583}
1584
Andrew Trickef9de2a2013-05-25 02:42:55 +00001585SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001586 SDValue Val, SDValue DTy,
1587 SDValue STy, SDValue Rnd, SDValue Sat,
1588 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001589 // If the src and dest types are the same and the conversion is between
1590 // integer types of the same sign or two floats, no conversion is necessary.
1591 if (DTy == STy &&
1592 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001593 return Val;
1594
1595 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001596 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001597 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001598 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001599 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001600 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001601
Jack Carter170a5f22013-09-09 22:02:08 +00001602 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1603 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001604 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001605 CSEMap.InsertNode(N, IP);
1606 AllNodes.push_back(N);
1607 return SDValue(N, 0);
1608}
1609
Owen Anderson53aa7a92009-08-10 22:56:29 +00001610SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001611 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001612 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001613 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001614 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001616 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001617
Dan Gohman01c65a22010-03-18 18:49:47 +00001618 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001619 CSEMap.InsertNode(N, IP);
1620 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001621 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001622}
1623
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001624SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1625 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001626 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001627 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001628 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001629 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1630 return SDValue(E, 0);
1631
1632 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1633 CSEMap.InsertNode(N, IP);
1634 AllNodes.push_back(N);
1635 return SDValue(N, 0);
1636}
1637
Andrew Trickef9de2a2013-05-25 02:42:55 +00001638SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001639 FoldingSetNodeID ID;
1640 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001641 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001642 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001643 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001644 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001645 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001646
Jack Carter170a5f22013-09-09 22:02:08 +00001647 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1648 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001649 CSEMap.InsertNode(N, IP);
1650 AllNodes.push_back(N);
1651 return SDValue(N, 0);
1652}
1653
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001654
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001655SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001656 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001657 bool isTarget,
1658 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001659 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1660
1661 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001662 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001663 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001664 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001665 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001666 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001667 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001668 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001669
Michael Liaoabb87d42012-09-12 21:43:09 +00001670 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1671 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001672 CSEMap.InsertNode(N, IP);
1673 AllNodes.push_back(N);
1674 return SDValue(N, 0);
1675}
1676
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001677SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001678 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001679 "SrcValue is not a pointer?");
1680
Jim Laskeyf576b422006-10-27 23:46:08 +00001681 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001682 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001683 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001684
Craig Topperc0196b12014-04-14 00:51:57 +00001685 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001686 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001687 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001688
Dan Gohman01c65a22010-03-18 18:49:47 +00001689 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001690 CSEMap.InsertNode(N, IP);
1691 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001692 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001693}
1694
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001695/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1696SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1697 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001698 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001699 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001700
Craig Topperc0196b12014-04-14 00:51:57 +00001701 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001702 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1703 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001704
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001705 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1706 CSEMap.InsertNode(N, IP);
1707 AllNodes.push_back(N);
1708 return SDValue(N, 0);
1709}
1710
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001711/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1712SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1713 unsigned SrcAS, unsigned DestAS) {
1714 SDValue Ops[] = {Ptr};
1715 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001716 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001717 ID.AddInteger(SrcAS);
1718 ID.AddInteger(DestAS);
1719
Craig Topperc0196b12014-04-14 00:51:57 +00001720 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001721 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1722 return SDValue(E, 0);
1723
1724 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1725 dl.getDebugLoc(),
1726 VT, Ptr, SrcAS, DestAS);
1727 CSEMap.InsertNode(N, IP);
1728 AllNodes.push_back(N);
1729 return SDValue(N, 0);
1730}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001731
Duncan Sands41826032009-01-31 15:50:11 +00001732/// getShiftAmountOperand - Return the specified value casted to
1733/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001734SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001735 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001736 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001737 if (OpTy == ShTy || OpTy.isVector()) return Op;
1738
1739 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001740 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001741}
1742
Chris Lattner9eb7a822007-10-15 17:47:20 +00001743/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1744/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001745SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001746 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001747 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001748 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001749 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001750 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001751 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001752
David Greene1fbe0542009-11-12 20:49:22 +00001753 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001754 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001755}
1756
Duncan Sands445071c2008-12-09 21:33:20 +00001757/// CreateStackTemporary - Create a stack temporary suitable for holding
1758/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001759SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001760 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1761 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001762 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1763 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001764 const TargetLowering *TLI = TM.getTargetLowering();
1765 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001766 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1767 TD->getPrefTypeAlignment(Ty2));
1768
1769 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001770 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001771 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001772}
1773
Owen Anderson53aa7a92009-08-10 22:56:29 +00001774SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001775 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001776 // These setcc operations always fold.
1777 switch (Cond) {
1778 default: break;
1779 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001780 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001781 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001782 case ISD::SETTRUE2: {
1783 const TargetLowering *TLI = TM.getTargetLowering();
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001784 TargetLowering::BooleanContent Cnt =
1785 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001786 return getConstant(
1787 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1788 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001789
Chris Lattner393d96a2006-04-27 05:01:07 +00001790 case ISD::SETOEQ:
1791 case ISD::SETOGT:
1792 case ISD::SETOGE:
1793 case ISD::SETOLT:
1794 case ISD::SETOLE:
1795 case ISD::SETONE:
1796 case ISD::SETO:
1797 case ISD::SETUO:
1798 case ISD::SETUEQ:
1799 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001800 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001801 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001802 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001803
Gabor Greiff304a7a2008-08-28 21:40:38 +00001804 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001805 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001806 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001807 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001808
Chris Lattner061a1ea2005-01-07 07:46:32 +00001809 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001810 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001811 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1812 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001813 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1814 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1815 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1816 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1817 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1818 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1819 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1820 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001821 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001822 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001823 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001824 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1825 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001826 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001827 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001828 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001829 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001830 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001831 // fall through
1832 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001833 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001834 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001835 // fall through
1836 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001837 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001838 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001839 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001840 // fall through
1841 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001842 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001843 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001844 // fall through
1845 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001846 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001847 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001848 // fall through
1849 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001850 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001851 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001852 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001853 // fall through
1854 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001855 R==APFloat::cmpEqual, VT);
1856 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1857 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1858 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1859 R==APFloat::cmpEqual, VT);
1860 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1861 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1862 R==APFloat::cmpLessThan, VT);
1863 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1864 R==APFloat::cmpUnordered, VT);
1865 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1866 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001867 }
1868 } else {
1869 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001870 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1871 MVT CompVT = N1.getValueType().getSimpleVT();
1872 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1873 return SDValue();
1874
1875 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001876 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001877 }
1878
Chris Lattnerd47675e2005-08-09 20:20:18 +00001879 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001880 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001881}
1882
Dan Gohman1f372ed2008-02-25 21:11:39 +00001883/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1884/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001885bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001886 // This predicate is not safe for vector operations.
1887 if (Op.getValueType().isVector())
1888 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001889
Dan Gohman1d459e42009-12-11 21:31:27 +00001890 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001891 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1892}
1893
Dan Gohman309d3d52007-06-22 14:59:07 +00001894/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1895/// this predicate to simplify operations downstream. Mask is known to be zero
1896/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001897bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001898 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001899 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001900 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001901 return (KnownZero & Mask) == Mask;
1902}
1903
Jay Foada0653a32014-05-14 21:14:37 +00001904/// Determine which bits of Op are known to be either zero or one and return
1905/// them in the KnownZero/KnownOne bitsets.
1906void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1907 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001908 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001909 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001910
Dan Gohmanf990faf2008-02-13 00:35:47 +00001911 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001912 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001913 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001914
Dan Gohmanf990faf2008-02-13 00:35:47 +00001915 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001916
1917 switch (Op.getOpcode()) {
1918 case ISD::Constant:
1919 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001920 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1921 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001922 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001923 case ISD::AND:
1924 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001925 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1926 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001927
1928 // Output known-1 bits are only known if set in both the LHS & RHS.
1929 KnownOne &= KnownOne2;
1930 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1931 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001932 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001933 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001934 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1935 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001936
Dan Gohman309d3d52007-06-22 14:59:07 +00001937 // Output known-0 bits are only known if clear in both the LHS & RHS.
1938 KnownZero &= KnownZero2;
1939 // Output known-1 are known to be set if set in either the LHS | RHS.
1940 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001941 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001942 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001943 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1944 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001945
Dan Gohman309d3d52007-06-22 14:59:07 +00001946 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001947 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001948 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1949 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1950 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001951 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001952 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001953 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00001954 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1955 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001956
1957 // If low bits are zero in either operand, output low known-0 bits.
1958 // Also compute a conserative estimate for high known-0 bits.
1959 // More trickiness is possible, but this is sufficient for the
1960 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001961 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001962 unsigned TrailZ = KnownZero.countTrailingOnes() +
1963 KnownZero2.countTrailingOnes();
1964 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001965 KnownZero2.countLeadingOnes(),
1966 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001967
1968 TrailZ = std::min(TrailZ, BitWidth);
1969 LeadZ = std::min(LeadZ, BitWidth);
1970 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1971 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001972 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001973 }
1974 case ISD::UDIV: {
1975 // For the purposes of computing leading zeros we can conservatively
1976 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001977 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00001978 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001979 unsigned LeadZ = KnownZero2.countLeadingOnes();
1980
Jay Foad25a5e4c2010-12-01 08:53:58 +00001981 KnownOne2.clearAllBits();
1982 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00001983 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001984 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1985 if (RHSUnknownLeadingOnes != BitWidth)
1986 LeadZ = std::min(BitWidth,
1987 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001988
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001989 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001990 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001991 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001992 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00001993 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1994 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001995
Dan Gohman309d3d52007-06-22 14:59:07 +00001996 // Only known if known in both the LHS and RHS.
1997 KnownOne &= KnownOne2;
1998 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001999 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002000 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002001 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2002 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002003
Dan Gohman309d3d52007-06-22 14:59:07 +00002004 // Only known if known in both the LHS and RHS.
2005 KnownOne &= KnownOne2;
2006 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002007 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002008 case ISD::SADDO:
2009 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002010 case ISD::SSUBO:
2011 case ISD::USUBO:
2012 case ISD::SMULO:
2013 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002014 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002015 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002016 // The boolean result conforms to getBooleanContents.
2017 // If we know the result of a setcc has the top bits zero, use this info.
2018 // We know that we have an integer-based boolean since these operations
2019 // are only available for integer.
2020 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2021 TargetLowering::ZeroOrOneBooleanContent &&
2022 BitWidth > 1)
2023 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2024 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002025 case ISD::SETCC:
2026 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002027 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2028 TargetLowering::ZeroOrOneBooleanContent &&
2029 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002030 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002031 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002032 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002033 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002034 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002035 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002036
2037 // If the shift count is an invalid immediate, don't do anything.
2038 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002039 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002040
Jay Foada0653a32014-05-14 21:14:37 +00002041 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002042 KnownZero <<= ShAmt;
2043 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002044 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002045 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002046 }
Jay Foad5a29c362014-05-15 12:12:55 +00002047 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002048 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002049 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002050 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002051 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002052
Dan Gohman9db0aa82008-02-26 18:50:50 +00002053 // If the shift count is an invalid immediate, don't do anything.
2054 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002055 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002056
Jay Foada0653a32014-05-14 21:14:37 +00002057 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002058 KnownZero = KnownZero.lshr(ShAmt);
2059 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002060
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002061 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002062 KnownZero |= HighBits; // High bits known zero.
2063 }
Jay Foad5a29c362014-05-15 12:12:55 +00002064 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002065 case ISD::SRA:
2066 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002067 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002068
Dan Gohman9db0aa82008-02-26 18:50:50 +00002069 // If the shift count is an invalid immediate, don't do anything.
2070 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002071 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002072
Dan Gohman309d3d52007-06-22 14:59:07 +00002073 // If any of the demanded bits are produced by the sign extension, we also
2074 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002075 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002076
Jay Foada0653a32014-05-14 21:14:37 +00002077 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002078 KnownZero = KnownZero.lshr(ShAmt);
2079 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002080
Dan Gohman309d3d52007-06-22 14:59:07 +00002081 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002082 APInt SignBit = APInt::getSignBit(BitWidth);
2083 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002084
Dan Gohmanb717fda2008-02-20 16:30:17 +00002085 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002086 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002087 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002088 KnownOne |= HighBits; // New bits are known one.
2089 }
2090 }
Jay Foad5a29c362014-05-15 12:12:55 +00002091 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002092 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002093 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002094 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002095
2096 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002097 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002098 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002099
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002100 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002101 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002102
Dan Gohman309d3d52007-06-22 14:59:07 +00002103 // If the sign extended bits are demanded, we know that the sign
2104 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002105 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002106 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002107 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002108
Jay Foada0653a32014-05-14 21:14:37 +00002109 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002110 KnownOne &= InputDemandedBits;
2111 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002112
Dan Gohman309d3d52007-06-22 14:59:07 +00002113 // If the sign bit of the input is known set or clear, then we know the
2114 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002115 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002116 KnownZero |= NewBits;
2117 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002118 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002119 KnownOne |= NewBits;
2120 KnownZero &= ~NewBits;
2121 } else { // Input sign bit unknown
2122 KnownZero &= ~NewBits;
2123 KnownOne &= ~NewBits;
2124 }
Jay Foad5a29c362014-05-15 12:12:55 +00002125 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002126 }
2127 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002128 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002129 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002130 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002131 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002132 unsigned LowBits = Log2_32(BitWidth)+1;
2133 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002134 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002135 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002136 }
2137 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002138 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002139 // If this is a ZEXTLoad and we are looking at the loaded value.
2140 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002141 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002142 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002143 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002144 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002145 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002146 }
Jay Foad5a29c362014-05-15 12:12:55 +00002147 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002148 }
2149 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002150 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002151 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002152 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002153 KnownZero = KnownZero.trunc(InBits);
2154 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002155 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002156 KnownZero = KnownZero.zext(BitWidth);
2157 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002158 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002159 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002160 }
2161 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002162 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002163 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002164 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002165
Jay Foad583abbc2010-12-07 08:25:19 +00002166 KnownZero = KnownZero.trunc(InBits);
2167 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002168 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002169
2170 // Note if the sign bit is known to be zero or one.
2171 bool SignBitKnownZero = KnownZero.isNegative();
2172 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002173
Jay Foad583abbc2010-12-07 08:25:19 +00002174 KnownZero = KnownZero.zext(BitWidth);
2175 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002176
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002177 // If the sign bit is known zero or one, the top bits match.
2178 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002179 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002180 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002181 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002182 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002183 }
2184 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002185 EVT InVT = Op.getOperand(0).getValueType();
2186 unsigned InBits = InVT.getScalarType().getSizeInBits();
2187 KnownZero = KnownZero.trunc(InBits);
2188 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002189 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002190 KnownZero = KnownZero.zext(BitWidth);
2191 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002192 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002193 }
2194 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002195 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002196 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002197 KnownZero = KnownZero.zext(InBits);
2198 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002199 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002200 KnownZero = KnownZero.trunc(BitWidth);
2201 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002202 break;
2203 }
2204 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002205 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002206 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002207 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002208 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002209 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002210 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002211 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002212 case ISD::FGETSIGN:
2213 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002214 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002215 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002216
Dan Gohman72ec3f42008-04-28 17:02:21 +00002217 case ISD::SUB: {
2218 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2219 // We know that the top bits of C-X are clear if X contains less bits
2220 // than C (i.e. no wrap-around can happen). For example, 20-X is
2221 // positive if we can prove that X is >= 0 and < 16.
2222 if (CLHS->getAPIntValue().isNonNegative()) {
2223 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2224 // NLZ can't be BitWidth with no sign bit
2225 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002226 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002227
2228 // If all of the MaskV bits are known to be zero, then we know the
2229 // output top bits are zero, because we now know that the output is
2230 // from [0-C].
2231 if ((KnownZero2 & MaskV) == MaskV) {
2232 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2233 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002234 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002235 }
2236 }
2237 }
2238 }
2239 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002240 case ISD::ADD:
2241 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002242 // Output known-0 bits are known if clear or set in both the low clear bits
2243 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2244 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002245 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002246 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2247
Jay Foada0653a32014-05-14 21:14:37 +00002248 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002249 KnownZeroOut = std::min(KnownZeroOut,
2250 KnownZero2.countTrailingOnes());
2251
Chris Lattner440b2802010-12-19 20:38:28 +00002252 if (Op.getOpcode() == ISD::ADD) {
2253 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002254 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002255 }
2256
2257 // With ADDE, a carry bit may be added in, so we can only use this
2258 // information if we know (at least) that the low two bits are clear. We
2259 // then return to the caller that the low bit is unknown but that other bits
2260 // are known zero.
2261 if (KnownZeroOut >= 2) // ADDE
2262 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002263 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002264 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002265 case ISD::SREM:
2266 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002267 const APInt &RA = Rem->getAPIntValue().abs();
2268 if (RA.isPowerOf2()) {
2269 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002270 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002271
Duncan Sands33274982010-01-29 09:45:26 +00002272 // The low bits of the first operand are unchanged by the srem.
2273 KnownZero = KnownZero2 & LowBits;
2274 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002275
Duncan Sands33274982010-01-29 09:45:26 +00002276 // If the first operand is non-negative or has all low bits zero, then
2277 // the upper bits are all zero.
2278 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2279 KnownZero |= ~LowBits;
2280
2281 // If the first operand is negative and not all low bits are zero, then
2282 // the upper bits are all one.
2283 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2284 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002285 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002286 }
2287 }
Jay Foad5a29c362014-05-15 12:12:55 +00002288 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002289 case ISD::UREM: {
2290 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002291 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002292 if (RA.isPowerOf2()) {
2293 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002294 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2295
2296 // The upper bits are all zero, the lower ones are unchanged.
2297 KnownZero = KnownZero2 | ~LowBits;
2298 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002299 break;
2300 }
2301 }
2302
2303 // Since the result is less than or equal to either operand, any leading
2304 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002305 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2306 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002307
2308 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2309 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002310 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002311 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002312 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002313 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002314 case ISD::FrameIndex:
2315 case ISD::TargetFrameIndex:
2316 if (unsigned Align = InferPtrAlignment(Op)) {
2317 // The low bits are known zero if the pointer is aligned.
2318 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002319 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002320 }
2321 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002322
Dan Gohman309d3d52007-06-22 14:59:07 +00002323 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002324 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2325 break;
2326 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002327 case ISD::INTRINSIC_WO_CHAIN:
2328 case ISD::INTRINSIC_W_CHAIN:
2329 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002330 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002331 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002332 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002333 }
Jay Foad5a29c362014-05-15 12:12:55 +00002334
2335 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002336}
2337
2338/// ComputeNumSignBits - Return the number of times the sign bit of the
2339/// register is replicated into the other bits. We know that at least 1 bit
2340/// is always equal to the sign bit (itself), but other cases can give us
2341/// information. For example, immediately after an "SRA X, 2", we know that
2342/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002343unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002344 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002345 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002346 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002347 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002348 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002349 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002350
Dan Gohman309d3d52007-06-22 14:59:07 +00002351 if (Depth == 6)
2352 return 1; // Limit search depth.
2353
2354 switch (Op.getOpcode()) {
2355 default: break;
2356 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002357 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002358 return VTBits-Tmp+1;
2359 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002360 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002361 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002362
Dan Gohman309d3d52007-06-22 14:59:07 +00002363 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002364 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002365 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002366 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002367
Dan Gohman309d3d52007-06-22 14:59:07 +00002368 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002369 Tmp =
2370 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002371 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002372
Dan Gohman309d3d52007-06-22 14:59:07 +00002373 case ISD::SIGN_EXTEND_INREG:
2374 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002375 Tmp =
2376 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002378
Dan Gohman309d3d52007-06-22 14:59:07 +00002379 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2380 return std::max(Tmp, Tmp2);
2381
2382 case ISD::SRA:
2383 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2384 // SRA X, C -> adds C sign bits.
2385 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002386 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002387 if (Tmp > VTBits) Tmp = VTBits;
2388 }
2389 return Tmp;
2390 case ISD::SHL:
2391 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2392 // shl destroys sign bits.
2393 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002394 if (C->getZExtValue() >= VTBits || // Bad shift.
2395 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2396 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002397 }
2398 break;
2399 case ISD::AND:
2400 case ISD::OR:
2401 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002402 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002403 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002404 if (Tmp != 1) {
2405 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2406 FirstAnswer = std::min(Tmp, Tmp2);
2407 // We computed what we know about the sign bits as our first
2408 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002409 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002410 }
2411 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002412
2413 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002414 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002415 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002416 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002417 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002418
2419 case ISD::SADDO:
2420 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002421 case ISD::SSUBO:
2422 case ISD::USUBO:
2423 case ISD::SMULO:
2424 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002425 if (Op.getResNo() != 1)
2426 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002427 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002428 // If setcc returns 0/-1, all bits are sign bits.
2429 // We know that we have an integer-based boolean since these operations
2430 // are only available for integer.
2431 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2432 TargetLowering::ZeroOrNegativeOneBooleanContent)
2433 return VTBits;
2434 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002435 case ISD::SETCC:
2436 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002437 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002438 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002439 return VTBits;
2440 break;
2441 case ISD::ROTL:
2442 case ISD::ROTR:
2443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002444 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002445
Dan Gohman309d3d52007-06-22 14:59:07 +00002446 // Handle rotate right by N like a rotate left by 32-N.
2447 if (Op.getOpcode() == ISD::ROTR)
2448 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2449
2450 // If we aren't rotating out all of the known-in sign bits, return the
2451 // number that are left. This handles rotl(sext(x), 1) for example.
2452 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2453 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2454 }
2455 break;
2456 case ISD::ADD:
2457 // Add can have at most one carry bit. Thus we know that the output
2458 // is, at worst, one more bit than the inputs.
2459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2460 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002461
Dan Gohman309d3d52007-06-22 14:59:07 +00002462 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002463 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002464 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002465 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002466 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002467
Dan Gohman309d3d52007-06-22 14:59:07 +00002468 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2469 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002470 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002471 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002472
Dan Gohman309d3d52007-06-22 14:59:07 +00002473 // If we are subtracting one from a positive number, there is no carry
2474 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002475 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002476 return Tmp;
2477 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002478
Dan Gohman309d3d52007-06-22 14:59:07 +00002479 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2480 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002481 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002482
Dan Gohman309d3d52007-06-22 14:59:07 +00002483 case ISD::SUB:
2484 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2485 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002486
Dan Gohman309d3d52007-06-22 14:59:07 +00002487 // Handle NEG.
2488 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002489 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002490 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002491 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002492 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2493 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002494 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002495 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002496
Dan Gohman309d3d52007-06-22 14:59:07 +00002497 // If the input is known to be positive (the sign bit is known clear),
2498 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002499 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002500 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002501
Dan Gohman309d3d52007-06-22 14:59:07 +00002502 // Otherwise, we treat this like a SUB.
2503 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002504
Dan Gohman309d3d52007-06-22 14:59:07 +00002505 // Sub can have at most one carry bit. Thus we know that the output
2506 // is, at worst, one more bit than the inputs.
2507 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2508 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002509 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002510 case ISD::TRUNCATE:
2511 // FIXME: it's tricky to do anything useful for this, but it is an important
2512 // case for targets like X86.
2513 break;
2514 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002515
Nadav Rotem4536d582013-03-20 22:53:44 +00002516 // If we are looking at the loaded value of the SDNode.
2517 if (Op.getResNo() == 0) {
2518 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2519 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2520 unsigned ExtType = LD->getExtensionType();
2521 switch (ExtType) {
2522 default: break;
2523 case ISD::SEXTLOAD: // '17' bits known
2524 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2525 return VTBits-Tmp+1;
2526 case ISD::ZEXTLOAD: // '16' bits known
2527 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2528 return VTBits-Tmp;
2529 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002530 }
2531 }
2532
2533 // Allow the target to implement this method for its nodes.
2534 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002535 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002536 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2537 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002538 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002539 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002540 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002541
Dan Gohman309d3d52007-06-22 14:59:07 +00002542 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2543 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002544 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002545 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002546
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002547 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002548 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002549 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002550 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002551 Mask = KnownOne;
2552 } else {
2553 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002554 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002555 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002556
Dan Gohman309d3d52007-06-22 14:59:07 +00002557 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2558 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002559 Mask = ~Mask;
2560 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002561 // Return # leading zeros. We use 'min' here in case Val was zero before
2562 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002563 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002564}
2565
Chris Lattner46c01a32011-02-13 22:25:43 +00002566/// isBaseWithConstantOffset - Return true if the specified operand is an
2567/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2568/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2569/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002570/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002571bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2572 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2573 !isa<ConstantSDNode>(Op.getOperand(1)))
2574 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002575
2576 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002577 !MaskedValueIsZero(Op.getOperand(0),
2578 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2579 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002580
Chris Lattner46c01a32011-02-13 22:25:43 +00002581 return true;
2582}
2583
2584
Dan Gohmand0d5e682009-09-03 20:34:31 +00002585bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2586 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002587 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002588 return true;
2589
2590 // If the value is a constant, we can obviously see if it is a NaN or not.
2591 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2592 return !C->getValueAPF().isNaN();
2593
2594 // TODO: Recognize more cases here.
2595
2596 return false;
2597}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002598
Dan Gohman38605212010-02-24 06:52:40 +00002599bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2600 // If the value is a constant, we can obviously see if it is a zero or not.
2601 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2602 return !C->isZero();
2603
2604 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002605 switch (Op.getOpcode()) {
2606 default: break;
2607 case ISD::OR:
2608 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2609 return !C->isNullValue();
2610 break;
2611 }
Dan Gohman38605212010-02-24 06:52:40 +00002612
2613 return false;
2614}
2615
2616bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2617 // Check the obvious case.
2618 if (A == B) return true;
2619
2620 // For for negative and positive zero.
2621 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2622 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2623 if (CA->isZero() && CB->isZero()) return true;
2624
2625 // Otherwise they may not be equal.
2626 return false;
2627}
2628
Chris Lattner061a1ea2005-01-07 07:46:32 +00002629/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002630///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002631SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002632 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002633 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002634 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002635 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002636 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002637
Jack Carter170a5f22013-09-09 22:02:08 +00002638 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2639 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002640 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002641
Chris Lattnerfcb16472006-08-11 18:38:11 +00002642 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002643#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002644 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002645#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002646 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002647}
2648
Andrew Trickef9de2a2013-05-25 02:42:55 +00002649SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002650 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002651 // Constant fold unary operations with an integer constant operand. Even
2652 // opaque constant will be folded, because the folding of unary operations
2653 // doesn't create new constants with different values. Nevertheless, the
2654 // opaque flag is preserved during folding to prevent future folding with
2655 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002656 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002657 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002658 switch (Opcode) {
2659 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002660 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002661 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2662 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002663 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002664 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002665 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002666 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2667 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002668 case ISD::UINT_TO_FP:
2669 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002670 APFloat apf(EVTToAPFloatSemantics(VT),
2671 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002672 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002673 Opcode==ISD::SINT_TO_FP,
2674 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002675 return getConstantFP(apf, VT);
2676 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002677 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002678 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002679 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002680 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002681 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002682 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002683 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002684 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2685 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002686 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002687 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2688 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002689 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002690 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002691 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2692 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002693 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002694 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002695 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2696 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002697 }
2698 }
2699
Dale Johannesen446b9002007-08-31 23:34:27 +00002700 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002701 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002702 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002703 switch (Opcode) {
2704 case ISD::FNEG:
2705 V.changeSign();
2706 return getConstantFP(V, VT);
2707 case ISD::FABS:
2708 V.clearSign();
2709 return getConstantFP(V, VT);
2710 case ISD::FCEIL: {
2711 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2712 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002713 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002714 break;
2715 }
2716 case ISD::FTRUNC: {
2717 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2718 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002719 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002720 break;
2721 }
2722 case ISD::FFLOOR: {
2723 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2724 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002725 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002726 break;
2727 }
2728 case ISD::FP_EXTEND: {
2729 bool ignored;
2730 // This can return overflow, underflow, or inexact; we don't care.
2731 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002732 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002733 APFloat::rmNearestTiesToEven, &ignored);
2734 return getConstantFP(V, VT);
2735 }
2736 case ISD::FP_TO_SINT:
2737 case ISD::FP_TO_UINT: {
2738 integerPart x[2];
2739 bool ignored;
2740 assert(integerPartWidth >= 64);
2741 // FIXME need to be more flexible about rounding mode.
2742 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2743 Opcode==ISD::FP_TO_SINT,
2744 APFloat::rmTowardZero, &ignored);
2745 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002746 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002747 APInt api(VT.getSizeInBits(), x);
2748 return getConstant(api, VT);
2749 }
2750 case ISD::BITCAST:
2751 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2752 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2753 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2754 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2755 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002756 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002757 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002758
Jim Grosbachf7502c42014-07-18 00:40:52 +00002759 // Constant fold unary operations with a vector integer operand.
2760 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
2761 APInt Val;
2762 APInt DummyUndefs;
2763 unsigned SplatBitSize;
2764 bool DummyHasUndefs;
2765 if (BV->isConstantSplat(Val, DummyUndefs, SplatBitSize, DummyHasUndefs)) {
2766 switch (Opcode) {
2767 default:
2768 // FIXME: Entirely reasonable to perform folding of other unary
2769 // operations here as the need arises.
2770 break;
2771 case ISD::UINT_TO_FP:
2772 case ISD::SINT_TO_FP: {
2773 APFloat APF(
2774 EVTToAPFloatSemantics(VT.getVectorElementType()),
2775 APInt::getNullValue(VT.getVectorElementType().getSizeInBits()));
2776 (void)APF.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
2777 APFloat::rmNearestTiesToEven);
2778
2779 return getConstantFP(APF, VT);
2780 }
2781 }
2782 }
2783 }
2784
Gabor Greiff304a7a2008-08-28 21:40:38 +00002785 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002786 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002787 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002788 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002789 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002790 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002791 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002792 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002793 assert(VT.isFloatingPoint() &&
2794 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002795 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002796 assert((!VT.isVector() ||
2797 VT.getVectorNumElements() ==
2798 Operand.getValueType().getVectorNumElements()) &&
2799 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002800 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002801 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002802 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002803 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002804 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002805 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002806 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002807 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2808 "Invalid sext node, dst < src!");
2809 assert((!VT.isVector() ||
2810 VT.getVectorNumElements() ==
2811 Operand.getValueType().getVectorNumElements()) &&
2812 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002813 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2814 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2815 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002816 // sext(undef) = 0, because the top bits will all be the same.
2817 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002818 break;
2819 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002820 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002821 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002822 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002823 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2824 "Invalid zext node, dst < src!");
2825 assert((!VT.isVector() ||
2826 VT.getVectorNumElements() ==
2827 Operand.getValueType().getVectorNumElements()) &&
2828 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002829 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002830 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002831 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002832 else if (OpOpcode == ISD::UNDEF)
2833 // zext(undef) = 0, because the top bits will be zero.
2834 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002835 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002836 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002837 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002838 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002839 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002840 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2841 "Invalid anyext node, dst < src!");
2842 assert((!VT.isVector() ||
2843 VT.getVectorNumElements() ==
2844 Operand.getValueType().getVectorNumElements()) &&
2845 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002846
Dan Gohman08837892010-06-18 00:08:30 +00002847 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2848 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002849 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002850 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002851 else if (OpOpcode == ISD::UNDEF)
2852 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002853
2854 // (ext (trunx x)) -> x
2855 if (OpOpcode == ISD::TRUNCATE) {
2856 SDValue OpOp = Operand.getNode()->getOperand(0);
2857 if (OpOp.getValueType() == VT)
2858 return OpOp;
2859 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002860 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002861 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002862 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002863 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002864 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002865 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2866 "Invalid truncate node, src < dst!");
2867 assert((!VT.isVector() ||
2868 VT.getVectorNumElements() ==
2869 Operand.getValueType().getVectorNumElements()) &&
2870 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002871 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002872 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002873 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2874 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002875 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002876 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2877 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002878 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002879 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002880 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002881 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002882 }
Craig Topper201c1a32012-01-15 01:05:11 +00002883 if (OpOpcode == ISD::UNDEF)
2884 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002885 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002886 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002887 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002888 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002889 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002890 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002891 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2892 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002893 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002894 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002895 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002896 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002897 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002898 (VT.getVectorElementType() == Operand.getValueType() ||
2899 (VT.getVectorElementType().isInteger() &&
2900 Operand.getValueType().isInteger() &&
2901 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002902 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002903 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002904 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002905 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2906 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2907 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2908 Operand.getConstantOperandVal(1) == 0 &&
2909 Operand.getOperand(0).getValueType() == VT)
2910 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002911 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002912 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002913 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002914 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002915 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002916 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002917 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002918 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002919 break;
2920 case ISD::FABS:
2921 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002922 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002923 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002924 }
2925
Chris Lattnerf9c19152005-08-25 19:12:10 +00002926 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002927 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002928 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002929 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002930 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002931 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002932 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002933 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002934 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002935
Jack Carter170a5f22013-09-09 22:02:08 +00002936 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2937 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002938 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002939 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002940 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2941 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002942 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002943
Chris Lattner061a1ea2005-01-07 07:46:32 +00002944 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002945#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002946 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002947#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002948 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002949}
2950
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002951SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2952 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002953 // If the opcode is a target-specific ISD node, there's nothing we can
2954 // do here and the operand rules may not line up with the below, so
2955 // bail early.
2956 if (Opcode >= ISD::BUILTIN_OP_END)
2957 return SDValue();
2958
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002959 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2960 SmallVector<SDValue, 4> Outputs;
2961 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002962
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002963 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2964 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002965 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2966 return SDValue();
2967
2968 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002969 // Scalar instruction.
2970 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002971 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002972 // For vectors extract each constant element into Inputs so we can constant
2973 // fold them individually.
2974 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2975 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2976 if (!BV1 || !BV2)
2977 return SDValue();
2978
2979 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2980
2981 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2982 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2983 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2984 if (!V1 || !V2) // Not a constant, bail.
2985 return SDValue();
2986
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002987 if (V1->isOpaque() || V2->isOpaque())
2988 return SDValue();
2989
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002990 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2991 // FIXME: This is valid and could be handled by truncating the APInts.
2992 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2993 return SDValue();
2994
2995 Inputs.push_back(std::make_pair(V1, V2));
2996 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002997 }
2998
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002999 // We have a number of constant values, constant fold them element by element.
3000 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3001 const APInt &C1 = Inputs[I].first->getAPIntValue();
3002 const APInt &C2 = Inputs[I].second->getAPIntValue();
3003
3004 switch (Opcode) {
3005 case ISD::ADD:
3006 Outputs.push_back(getConstant(C1 + C2, SVT));
3007 break;
3008 case ISD::SUB:
3009 Outputs.push_back(getConstant(C1 - C2, SVT));
3010 break;
3011 case ISD::MUL:
3012 Outputs.push_back(getConstant(C1 * C2, SVT));
3013 break;
3014 case ISD::UDIV:
3015 if (!C2.getBoolValue())
3016 return SDValue();
3017 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3018 break;
3019 case ISD::UREM:
3020 if (!C2.getBoolValue())
3021 return SDValue();
3022 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3023 break;
3024 case ISD::SDIV:
3025 if (!C2.getBoolValue())
3026 return SDValue();
3027 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3028 break;
3029 case ISD::SREM:
3030 if (!C2.getBoolValue())
3031 return SDValue();
3032 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3033 break;
3034 case ISD::AND:
3035 Outputs.push_back(getConstant(C1 & C2, SVT));
3036 break;
3037 case ISD::OR:
3038 Outputs.push_back(getConstant(C1 | C2, SVT));
3039 break;
3040 case ISD::XOR:
3041 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3042 break;
3043 case ISD::SHL:
3044 Outputs.push_back(getConstant(C1 << C2, SVT));
3045 break;
3046 case ISD::SRL:
3047 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3048 break;
3049 case ISD::SRA:
3050 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3051 break;
3052 case ISD::ROTL:
3053 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3054 break;
3055 case ISD::ROTR:
3056 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3057 break;
3058 default:
3059 return SDValue();
3060 }
3061 }
3062
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003063 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3064 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003065
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003066 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003067 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003068 return Outputs.back();
3069
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003070 // We may have a vector type but a scalar result. Create a splat.
3071 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3072
3073 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003074 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003075}
3076
Andrew Trickef9de2a2013-05-25 02:42:55 +00003077SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003078 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003079 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3080 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003081 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003082 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003083 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003084 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3085 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003086 // Fold trivial token factors.
3087 if (N1.getOpcode() == ISD::EntryToken) return N2;
3088 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003089 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003090 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003091 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003092 // Concat of UNDEFs is UNDEF.
3093 if (N1.getOpcode() == ISD::UNDEF &&
3094 N2.getOpcode() == ISD::UNDEF)
3095 return getUNDEF(VT);
3096
Dan Gohman550c9af2008-08-14 20:04:46 +00003097 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3098 // one big BUILD_VECTOR.
3099 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3100 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003101 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3102 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003103 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003104 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003105 }
3106 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003107 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003108 assert(VT.isInteger() && "This operator does not apply to FP types!");
3109 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003110 N1.getValueType() == VT && "Binary operator types must match!");
3111 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3112 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003113 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003114 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003115 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3116 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003117 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003118 case ISD::OR:
3119 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003120 case ISD::ADD:
3121 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003122 assert(VT.isInteger() && "This operator does not apply to FP types!");
3123 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003124 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003125 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3126 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003127 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003128 return N1;
3129 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003130 case ISD::UDIV:
3131 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003132 case ISD::MULHU:
3133 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003134 case ISD::MUL:
3135 case ISD::SDIV:
3136 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003137 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003138 assert(N1.getValueType() == N2.getValueType() &&
3139 N1.getValueType() == VT && "Binary operator types must match!");
3140 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003141 case ISD::FADD:
3142 case ISD::FSUB:
3143 case ISD::FMUL:
3144 case ISD::FDIV:
3145 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003146 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003147 if (Opcode == ISD::FADD) {
3148 // 0+x --> x
3149 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3150 if (CFP->getValueAPF().isZero())
3151 return N2;
3152 // x+0 --> x
3153 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3154 if (CFP->getValueAPF().isZero())
3155 return N1;
3156 } else if (Opcode == ISD::FSUB) {
3157 // x-0 --> x
3158 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3159 if (CFP->getValueAPF().isZero())
3160 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003161 } else if (Opcode == ISD::FMUL) {
3162 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3163 SDValue V = N2;
3164
3165 // If the first operand isn't the constant, try the second
3166 if (!CFP) {
3167 CFP = dyn_cast<ConstantFPSDNode>(N2);
3168 V = N1;
3169 }
3170
3171 if (CFP) {
3172 // 0*x --> 0
3173 if (CFP->isZero())
3174 return SDValue(CFP,0);
3175 // 1*x --> x
3176 if (CFP->isExactlyValue(1.0))
3177 return V;
3178 }
Dan Gohman1275e282009-01-23 19:10:37 +00003179 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003180 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003181 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003182 assert(N1.getValueType() == N2.getValueType() &&
3183 N1.getValueType() == VT && "Binary operator types must match!");
3184 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003185 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3186 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003187 N1.getValueType().isFloatingPoint() &&
3188 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003189 "Invalid FCOPYSIGN!");
3190 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003191 case ISD::SHL:
3192 case ISD::SRA:
3193 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003194 case ISD::ROTL:
3195 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003196 assert(VT == N1.getValueType() &&
3197 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003198 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003199 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003200 assert((!VT.isVector() || VT == N2.getValueType()) &&
3201 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003202 // Verify that the shift amount VT is bit enough to hold valid shift
3203 // amounts. This catches things like trying to shift an i1024 value by an
3204 // i8, which is easy to fall into in generic code that uses
3205 // TLI.getShiftAmount().
3206 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003207 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003208 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003209
3210 // Always fold shifts of i1 values so the code generator doesn't need to
3211 // handle them. Since we know the size of the shift has to be less than the
3212 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003213 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003214 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003215 if (N2C && N2C->isNullValue())
3216 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003217 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003218 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003219 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003220 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003221 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003222 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003223 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003224 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003225 "type is vector!");
3226 assert((!EVT.isVector() ||
3227 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3228 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003229 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003230 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003231 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003232 break;
3233 }
Chris Lattner72733e52008-01-17 07:00:52 +00003234 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003235 assert(VT.isFloatingPoint() &&
3236 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003237 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003238 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003239 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003240 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003241 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003242 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003243 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003244 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003245 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003246 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003247 assert(!EVT.isVector() &&
3248 "AssertSExt/AssertZExt type should be the vector element type "
3249 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003250 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003251 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003252 break;
3253 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003254 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003255 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003256 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003257 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003258 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003259 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003260 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003261 "type is vector!");
3262 assert((!EVT.isVector() ||
3263 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3264 "Vector element counts must match in SIGN_EXTEND_INREG");
3265 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003266 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003267
Chris Lattner16713612008-01-22 19:09:33 +00003268 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003269 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003270 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003271 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003272 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003273 return getConstant(Val, VT);
3274 }
Chris Lattner16713612008-01-22 19:09:33 +00003275 break;
3276 }
3277 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003278 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3279 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003280 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003281
Chris Lattner16713612008-01-22 19:09:33 +00003282 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3283 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003284 if (N2C &&
3285 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003286 N1.getNumOperands() > 0) {
3287 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003288 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003289 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003290 N1.getOperand(N2C->getZExtValue() / Factor),
3291 getConstant(N2C->getZExtValue() % Factor,
3292 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003293 }
3294
3295 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3296 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003297 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3298 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003299
3300 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003301 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003302 // are promoted and implicitly truncated, and the result implicitly
3303 // extended. Make that explicit here.
3304 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003305
Bob Wilson59dbbb22009-04-13 22:05:19 +00003306 return Elt;
3307 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003308
Chris Lattner16713612008-01-22 19:09:33 +00003309 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3310 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003311 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003312 // If the indices are the same, return the inserted element else
3313 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003314 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003315 SDValue N1Op2 = N1.getOperand(2);
3316 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3317
3318 if (N1Op2C && N2C) {
3319 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3320 if (VT == N1.getOperand(1).getValueType())
3321 return N1.getOperand(1);
3322 else
3323 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3324 }
3325
Dale Johannesendb393622009-02-03 01:55:44 +00003326 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003327 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003328 }
Chris Lattner16713612008-01-22 19:09:33 +00003329 break;
3330 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003331 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003332 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3333 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003334 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003335 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003336
Chris Lattner16713612008-01-22 19:09:33 +00003337 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3338 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003339 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003340 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003341 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003342
Chris Lattner16713612008-01-22 19:09:33 +00003343 // EXTRACT_ELEMENT of a constant int is also very common.
3344 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003345 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003346 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003347 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3348 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003349 }
3350 break;
David Greeneb6f16112011-01-26 15:38:49 +00003351 case ISD::EXTRACT_SUBVECTOR: {
3352 SDValue Index = N2;
3353 if (VT.isSimple() && N1.getValueType().isSimple()) {
3354 assert(VT.isVector() && N1.getValueType().isVector() &&
3355 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003356 assert(VT.getVectorElementType() ==
3357 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003358 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003359 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003360 "Extract subvector must be from larger vector to smaller vector!");
3361
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003362 if (isa<ConstantSDNode>(Index.getNode())) {
3363 assert((VT.getVectorNumElements() +
3364 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003365 <= N1.getValueType().getVectorNumElements())
3366 && "Extract subvector overflow!");
3367 }
3368
3369 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003370 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003371 return N1;
3372 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003373 break;
Chris Lattner16713612008-01-22 19:09:33 +00003374 }
David Greeneb6f16112011-01-26 15:38:49 +00003375 }
Chris Lattner16713612008-01-22 19:09:33 +00003376
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003377 // Perform trivial constant folding.
3378 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3379 if (SV.getNode()) return SV;
3380
3381 // Canonicalize constant to RHS if commutative.
3382 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3383 std::swap(N1C, N2C);
3384 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003385 }
3386
Chris Lattner16713612008-01-22 19:09:33 +00003387 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003388 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3389 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003390 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003391 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003392 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003393 std::swap(N1CFP, N2CFP);
3394 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003395 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003396 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3397 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003398 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003399 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003400 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003401 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003402 return getConstantFP(V1, VT);
3403 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003404 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003405 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3406 if (s!=APFloat::opInvalidOp)
3407 return getConstantFP(V1, VT);
3408 break;
3409 case ISD::FMUL:
3410 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3411 if (s!=APFloat::opInvalidOp)
3412 return getConstantFP(V1, VT);
3413 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003414 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003415 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3416 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3417 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003418 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003419 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003420 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3421 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3422 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003423 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003424 case ISD::FCOPYSIGN:
3425 V1.copySign(V2);
3426 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003427 default: break;
3428 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003429 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003430
3431 if (Opcode == ISD::FP_ROUND) {
3432 APFloat V = N1CFP->getValueAPF(); // make copy
3433 bool ignored;
3434 // This can return overflow, underflow, or inexact; we don't care.
3435 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003436 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003437 APFloat::rmNearestTiesToEven, &ignored);
3438 return getConstantFP(V, VT);
3439 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003440 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003441
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003442 // Canonicalize an UNDEF to the RHS, even over a constant.
3443 if (N1.getOpcode() == ISD::UNDEF) {
3444 if (isCommutativeBinOp(Opcode)) {
3445 std::swap(N1, N2);
3446 } else {
3447 switch (Opcode) {
3448 case ISD::FP_ROUND_INREG:
3449 case ISD::SIGN_EXTEND_INREG:
3450 case ISD::SUB:
3451 case ISD::FSUB:
3452 case ISD::FDIV:
3453 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003454 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003455 return N1; // fold op(undef, arg2) -> undef
3456 case ISD::UDIV:
3457 case ISD::SDIV:
3458 case ISD::UREM:
3459 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003460 case ISD::SRL:
3461 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003462 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003463 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3464 // For vectors, we can't easily build an all zero vector, just return
3465 // the LHS.
3466 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003467 }
3468 }
3469 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003470
3471 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003472 if (N2.getOpcode() == ISD::UNDEF) {
3473 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003474 case ISD::XOR:
3475 if (N1.getOpcode() == ISD::UNDEF)
3476 // Handle undef ^ undef -> 0 special case. This is a common
3477 // idiom (misuse).
3478 return getConstant(0, VT);
3479 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003480 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003481 case ISD::ADDC:
3482 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003483 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003484 case ISD::UDIV:
3485 case ISD::SDIV:
3486 case ISD::UREM:
3487 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003488 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003489 case ISD::FADD:
3490 case ISD::FSUB:
3491 case ISD::FMUL:
3492 case ISD::FDIV:
3493 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003494 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003495 return N2;
3496 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003497 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003498 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003499 case ISD::SRL:
3500 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003501 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003502 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3503 // For vectors, we can't easily build an all zero vector, just return
3504 // the LHS.
3505 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003506 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003507 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003508 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003509 // For vectors, we can't easily build an all one vector, just return
3510 // the LHS.
3511 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003512 case ISD::SRA:
3513 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003514 }
3515 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003516
Chris Lattner724f7ee2005-05-11 18:57:39 +00003517 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003518 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003519 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003520 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003521 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003522 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003523 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003524 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003525 if (BinOpHasFlags)
3526 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003527 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003529 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003530
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003531 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3532
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003533 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003534 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003535
3536 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003537 }
3538
Chris Lattner061a1ea2005-01-07 07:46:32 +00003539 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003540#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003541 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003542#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003543 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003544}
3545
Andrew Trickef9de2a2013-05-25 02:42:55 +00003546SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003547 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003548 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003549 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003550 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003551 case ISD::FMA: {
3552 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3553 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3554 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3555 if (N1CFP && N2CFP && N3CFP) {
3556 APFloat V1 = N1CFP->getValueAPF();
3557 const APFloat &V2 = N2CFP->getValueAPF();
3558 const APFloat &V3 = N3CFP->getValueAPF();
3559 APFloat::opStatus s =
3560 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3561 if (s != APFloat::opInvalidOp)
3562 return getConstantFP(V1, VT);
3563 }
3564 break;
3565 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003566 case ISD::CONCAT_VECTORS:
3567 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3568 // one big BUILD_VECTOR.
3569 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3570 N2.getOpcode() == ISD::BUILD_VECTOR &&
3571 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003572 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3573 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003574 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3575 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003576 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003577 }
3578 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003579 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003580 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003581 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003582 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003583 break;
3584 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003585 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003586 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003587 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003588 return N2; // select true, X, Y -> X
3589 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003590 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003591
3592 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003593 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003594 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003595 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003596 case ISD::INSERT_SUBVECTOR: {
3597 SDValue Index = N3;
3598 if (VT.isSimple() && N1.getValueType().isSimple()
3599 && N2.getValueType().isSimple()) {
3600 assert(VT.isVector() && N1.getValueType().isVector() &&
3601 N2.getValueType().isVector() &&
3602 "Insert subvector VTs must be a vectors");
3603 assert(VT == N1.getValueType() &&
3604 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003605 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003606 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003607 if (isa<ConstantSDNode>(Index.getNode())) {
3608 assert((N2.getValueType().getVectorNumElements() +
3609 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003610 <= VT.getVectorNumElements())
3611 && "Insert subvector overflow!");
3612 }
3613
3614 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003615 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003616 return N2;
3617 }
3618 break;
3619 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003620 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003621 // Fold bit_convert nodes from a type to themselves.
3622 if (N1.getValueType() == VT)
3623 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003624 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003625 }
3626
Chris Lattnerf9c19152005-08-25 19:12:10 +00003627 // Memoize node if it doesn't produce a flag.
3628 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003629 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003630 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003631 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003632 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003633 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003634 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003635 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003636 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003637
Jack Carter170a5f22013-09-09 22:02:08 +00003638 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3639 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003640 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003641 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003642 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3643 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003644 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003645
Chris Lattner061a1ea2005-01-07 07:46:32 +00003646 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003647#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003648 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003649#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003650 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003651}
3652
Andrew Trickef9de2a2013-05-25 02:42:55 +00003653SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003654 SDValue N1, SDValue N2, SDValue N3,
3655 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003656 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003657 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003658}
3659
Andrew Trickef9de2a2013-05-25 02:42:55 +00003660SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003661 SDValue N1, SDValue N2, SDValue N3,
3662 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003663 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003664 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003665}
3666
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003667/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3668/// the incoming stack arguments to be loaded from the stack.
3669SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3670 SmallVector<SDValue, 8> ArgChains;
3671
3672 // Include the original chain at the beginning of the list. When this is
3673 // used by target LowerCall hooks, this helps legalize find the
3674 // CALLSEQ_BEGIN node.
3675 ArgChains.push_back(Chain);
3676
3677 // Add a chain value for each stack argument.
3678 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3679 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3680 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3681 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3682 if (FI->getIndex() < 0)
3683 ArgChains.push_back(SDValue(L, 1));
3684
3685 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003686 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003687}
3688
Dan Gohman544ab2c2008-04-12 04:36:06 +00003689/// getMemsetValue - Vectorized representation of the memset value
3690/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003691static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003692 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003693 assert(Value.getOpcode() != ISD::UNDEF);
3694
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003695 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003696 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003697 assert(C->getAPIntValue().getBitWidth() == 8);
3698 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003699 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003700 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003701 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003702 }
Evan Chengef377ad2008-05-15 08:39:06 +00003703
Dale Johannesenabf66b82009-02-03 22:26:09 +00003704 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003705 if (NumBits > 8) {
3706 // Use a multiplication with 0x010101... to extend the input to the
3707 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003708 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003709 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003710 }
3711
3712 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003713}
3714
Dan Gohman544ab2c2008-04-12 04:36:06 +00003715/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3716/// used when a memcpy is turned into a memset when the source is a constant
3717/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003718static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003719 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003720 // Handle vector with all elements zero.
3721 if (Str.empty()) {
3722 if (VT.isInteger())
3723 return DAG.getConstant(0, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003724 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003725 return DAG.getConstantFP(0.0, VT);
3726 else if (VT.isVector()) {
3727 unsigned NumElts = VT.getVectorNumElements();
3728 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003729 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003730 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3731 EltVT, NumElts)));
3732 } else
3733 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003734 }
3735
Duncan Sands13237ac2008-06-06 12:08:01 +00003736 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003737 unsigned NumVTBits = VT.getSizeInBits();
3738 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003739 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3740
Evan Chengc8444b12013-01-10 22:13:27 +00003741 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003742 if (TLI.isLittleEndian()) {
3743 for (unsigned i = 0; i != NumBytes; ++i)
3744 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3745 } else {
3746 for (unsigned i = 0; i != NumBytes; ++i)
3747 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003748 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003749
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003750 // If the "cost" of materializing the integer immediate is less than the cost
3751 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003752 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3753 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003754 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003755 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003756}
3757
Scott Michelcf0da6c2009-02-17 22:15:04 +00003758/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003759///
Andrew Tricke2431c62013-05-25 03:08:10 +00003760static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003761 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003762 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003763 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003764 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003765}
3766
Evan Chengef377ad2008-05-15 08:39:06 +00003767/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3768///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003769static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003770 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003771 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003772 if (Src.getOpcode() == ISD::GlobalAddress)
3773 G = cast<GlobalAddressSDNode>(Src);
3774 else if (Src.getOpcode() == ISD::ADD &&
3775 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3776 Src.getOperand(1).getOpcode() == ISD::Constant) {
3777 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003778 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003779 }
3780 if (!G)
3781 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003782
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003783 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003784}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003785
Evan Cheng43cd9e32010-04-01 06:04:33 +00003786/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3787/// to replace the memset / memcpy. Return true if the number of memory ops
3788/// is below the threshold. It returns the types of the sequence of
3789/// memory ops to perform memset / memcpy by reference.
3790static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003791 unsigned Limit, uint64_t Size,
3792 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003793 bool IsMemset,
3794 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003795 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003796 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003797 SelectionDAG &DAG,
3798 const TargetLowering &TLI) {
3799 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3800 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003801 // If 'SrcAlign' is zero, that means the memory operation does not need to
3802 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3803 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3804 // is the specified alignment of the memory operation. If it is zero, that
3805 // means it's possible to change the alignment of the destination.
3806 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3807 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003808 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003809 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003810 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003811
Chris Lattner28dc6c12010-03-07 07:45:08 +00003812 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003813 unsigned AS = 0;
3814 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
3815 TLI.allowsUnalignedMemoryAccesses(VT, AS)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003816 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003817 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003818 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003819 case 0: VT = MVT::i64; break;
3820 case 4: VT = MVT::i32; break;
3821 case 2: VT = MVT::i16; break;
3822 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003823 }
3824 }
3825
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003826 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003827 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003828 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003829 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003830
Duncan Sands11dd4242008-06-08 20:54:56 +00003831 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003832 VT = LVT;
3833 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003834
Dan Gohman544ab2c2008-04-12 04:36:06 +00003835 unsigned NumMemOps = 0;
3836 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003837 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003838 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003839 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003840 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003841 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003842
3843 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003844 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003845 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003846 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003847 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003848 Found = true;
3849 else if (NewVT == MVT::i64 &&
3850 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003851 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003852 // i64 is usually not legal on 32-bit targets, but f64 may be.
3853 NewVT = MVT::f64;
3854 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003855 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003856 }
3857
Evan Cheng04e55182012-12-12 00:42:09 +00003858 if (!Found) {
3859 do {
3860 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3861 if (NewVT == MVT::i8)
3862 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003863 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003864 }
3865 NewVTSize = NewVT.getSizeInBits() / 8;
3866
Evan Cheng79e2ca92012-12-10 23:21:26 +00003867 // If the new VT cannot cover all of the remaining bits, then consider
3868 // issuing a (or a pair of) unaligned and overlapping load / store.
3869 // FIXME: Only does this for 64-bit or more since we don't have proper
3870 // cost model for unaligned load / store.
3871 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003872 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003873 if (NumMemOps && AllowOverlap &&
3874 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003875 TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003876 VTSize = Size;
3877 else {
3878 VT = NewVT;
3879 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003880 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003881 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003882
Evan Chengb7d3d032012-12-12 20:43:23 +00003883 if (++NumMemOps > Limit)
3884 return false;
3885
Dan Gohman544ab2c2008-04-12 04:36:06 +00003886 MemOps.push_back(VT);
3887 Size -= VTSize;
3888 }
3889
3890 return true;
3891}
3892
Andrew Trickef9de2a2013-05-25 02:42:55 +00003893static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003894 SDValue Chain, SDValue Dst,
3895 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003896 unsigned Align, bool isVol,
3897 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003898 MachinePointerInfo DstPtrInfo,
3899 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003900 // Turn a memcpy of undef to nop.
3901 if (Src.getOpcode() == ISD::UNDEF)
3902 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003903
Dan Gohman714663a2008-05-29 19:42:22 +00003904 // Expand memcpy to a series of load and store ops if the size operand falls
3905 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003906 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3907 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003909 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003910 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003911 MachineFunction &MF = DAG.getMachineFunction();
3912 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003913 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003914 MF.getFunction()->getAttributes().
3915 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003916 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3917 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3918 DstAlignCanChange = true;
3919 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3920 if (Align > SrcAlign)
3921 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003922 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003923 bool CopyFromStr = isMemSrcFromString(Src, Str);
3924 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003925 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003926
Evan Cheng4c014c82010-04-01 18:19:11 +00003927 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003928 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003929 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003930 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003931 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003932
Evan Cheng43cd9e32010-04-01 06:04:33 +00003933 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003934 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003935 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003936
3937 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003938 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003939 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3940 if (!TRI->needsStackRealignment(MF))
3941 while (NewAlign > Align &&
3942 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3943 NewAlign /= 2;
3944
Evan Cheng43cd9e32010-04-01 06:04:33 +00003945 if (NewAlign > Align) {
3946 // Give the stack frame object a larger alignment if needed.
3947 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3948 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3949 Align = NewAlign;
3950 }
3951 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003952
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003953 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003954 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003955 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003956 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003957 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003958 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003959 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003960
Evan Cheng79e2ca92012-12-10 23:21:26 +00003961 if (VTSize > Size) {
3962 // Issuing an unaligned load / store pair that overlaps with the previous
3963 // pair. Adjust the offset accordingly.
3964 assert(i == NumMemOps-1 && i != 0);
3965 SrcOff -= VTSize - Size;
3966 DstOff -= VTSize - Size;
3967 }
3968
Evan Cheng43cd9e32010-04-01 06:04:33 +00003969 if (CopyFromStr &&
3970 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003971 // It's unlikely a store of a vector immediate can be done in a single
3972 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003973 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003974 // FIXME: Handle other cases where store of vector immediate is done in
3975 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003976 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003977 if (Value.getNode())
3978 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003979 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003980 DstPtrInfo.getWithOffset(DstOff), isVol,
3981 false, Align);
3982 }
3983
3984 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003985 // The type might not be legal for the target. This should only happen
3986 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003987 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3988 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003989 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003990 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003991 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003992 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003993 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003994 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003995 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003996 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003997 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003998 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3999 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004000 }
4001 OutChains.push_back(Store);
4002 SrcOff += VTSize;
4003 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004004 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004005 }
4006
Craig Topper48d114b2014-04-26 18:35:24 +00004007 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004008}
4009
Andrew Trickef9de2a2013-05-25 02:42:55 +00004010static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004011 SDValue Chain, SDValue Dst,
4012 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004013 unsigned Align, bool isVol,
4014 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004015 MachinePointerInfo DstPtrInfo,
4016 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004017 // Turn a memmove of undef to nop.
4018 if (Src.getOpcode() == ISD::UNDEF)
4019 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004020
4021 // Expand memmove to a series of load and store ops if the size operand falls
4022 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004023 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004024 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004025 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004026 MachineFunction &MF = DAG.getMachineFunction();
4027 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004028 bool OptSize = MF.getFunction()->getAttributes().
4029 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004030 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4031 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4032 DstAlignCanChange = true;
4033 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4034 if (Align > SrcAlign)
4035 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004036 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004037
Evan Cheng4c014c82010-04-01 18:19:11 +00004038 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004039 (DstAlignCanChange ? 0 : Align), SrcAlign,
4040 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004041 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004042
Evan Cheng43cd9e32010-04-01 06:04:33 +00004043 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004044 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004045 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004046 if (NewAlign > Align) {
4047 // Give the stack frame object a larger alignment if needed.
4048 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4049 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4050 Align = NewAlign;
4051 }
4052 }
Dan Gohman714663a2008-05-29 19:42:22 +00004053
Evan Cheng43cd9e32010-04-01 06:04:33 +00004054 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004055 SmallVector<SDValue, 8> LoadValues;
4056 SmallVector<SDValue, 8> LoadChains;
4057 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004058 unsigned NumMemOps = MemOps.size();
4059 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004060 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004061 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004062 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004063
Dale Johannesenabf66b82009-02-03 22:26:09 +00004064 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004065 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004066 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004067 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004068 LoadValues.push_back(Value);
4069 LoadChains.push_back(Value.getValue(1));
4070 SrcOff += VTSize;
4071 }
Craig Topper48d114b2014-04-26 18:35:24 +00004072 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004073 OutChains.clear();
4074 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004075 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004076 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004077 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004078
Dale Johannesenabf66b82009-02-03 22:26:09 +00004079 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004080 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004081 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004082 OutChains.push_back(Store);
4083 DstOff += VTSize;
4084 }
4085
Craig Topper48d114b2014-04-26 18:35:24 +00004086 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004087}
4088
Serge Pavlov8ec39992013-09-17 16:24:42 +00004089/// \brief Lower the call to 'memset' intrinsic function into a series of store
4090/// operations.
4091///
4092/// \param DAG Selection DAG where lowered code is placed.
4093/// \param dl Link to corresponding IR location.
4094/// \param Chain Control flow dependency.
4095/// \param Dst Pointer to destination memory location.
4096/// \param Src Value of byte to write into the memory.
4097/// \param Size Number of bytes to write.
4098/// \param Align Alignment of the destination in bytes.
4099/// \param isVol True if destination is volatile.
4100/// \param DstPtrInfo IR information on the memory pointer.
4101/// \returns New head in the control flow, if lowering was successful, empty
4102/// SDValue otherwise.
4103///
4104/// The function tries to replace 'llvm.memset' intrinsic with several store
4105/// operations and value calculation code. This is usually profitable for small
4106/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004107static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004108 SDValue Chain, SDValue Dst,
4109 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004110 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004111 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004112 // Turn a memset of undef to nop.
4113 if (Src.getOpcode() == ISD::UNDEF)
4114 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004115
4116 // Expand memset to a series of load/store ops if the size operand
4117 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004118 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004119 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004120 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004121 MachineFunction &MF = DAG.getMachineFunction();
4122 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004123 bool OptSize = MF.getFunction()->getAttributes().
4124 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004125 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4126 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4127 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004128 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004129 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004130 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004131 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004132 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004133 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004134
Evan Cheng43cd9e32010-04-01 06:04:33 +00004135 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004136 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004137 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004138 if (NewAlign > Align) {
4139 // Give the stack frame object a larger alignment if needed.
4140 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4141 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4142 Align = NewAlign;
4143 }
4144 }
4145
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004146 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004147 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004148 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004149
4150 // Find the largest store and generate the bit pattern for it.
4151 EVT LargestVT = MemOps[0];
4152 for (unsigned i = 1; i < NumMemOps; i++)
4153 if (MemOps[i].bitsGT(LargestVT))
4154 LargestVT = MemOps[i];
4155 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4156
Dan Gohman544ab2c2008-04-12 04:36:06 +00004157 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004158 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004159 unsigned VTSize = VT.getSizeInBits() / 8;
4160 if (VTSize > Size) {
4161 // Issuing an unaligned load / store pair that overlaps with the previous
4162 // pair. Adjust the offset accordingly.
4163 assert(i == NumMemOps-1 && i != 0);
4164 DstOff -= VTSize - Size;
4165 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004166
4167 // If this store is smaller than the largest store see whether we can get
4168 // the smaller value for free with a truncate.
4169 SDValue Value = MemSetValue;
4170 if (VT.bitsLT(LargestVT)) {
4171 if (!LargestVT.isVector() && !VT.isVector() &&
4172 TLI.isTruncateFree(LargestVT, VT))
4173 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4174 else
4175 Value = getMemsetValue(Src, VT, DAG, dl);
4176 }
4177 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004178 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004179 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004180 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004181 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004182 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004183 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004184 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004185 }
4186
Craig Topper48d114b2014-04-26 18:35:24 +00004187 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004188}
4189
Andrew Trickef9de2a2013-05-25 02:42:55 +00004190SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004191 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004192 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004193 MachinePointerInfo DstPtrInfo,
4194 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004195 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004196
4197 // Check to see if we should lower the memcpy to loads and stores first.
4198 // For cases within the target-specified limits, this is the best choice.
4199 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4200 if (ConstantSize) {
4201 // Memcpy with size zero? Just return the original chain.
4202 if (ConstantSize->isNullValue())
4203 return Chain;
4204
Evan Cheng43cd9e32010-04-01 06:04:33 +00004205 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4206 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004207 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004208 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004209 return Result;
4210 }
4211
4212 // Then check to see if we should lower the memcpy with target-specific
4213 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004214 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004215 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004216 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004217 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004218 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004219 return Result;
4220
4221 // If we really need inline code and the target declined to provide it,
4222 // use a (potentially long) sequence of loads and stores.
4223 if (AlwaysInline) {
4224 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004225 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004226 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004227 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004228 }
4229
Dan Gohmanf38547c2010-04-05 20:24:08 +00004230 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4231 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4232 // respect volatile, so they may do things like read or write memory
4233 // beyond the given memory regions. But fixing this isn't easy, and most
4234 // people don't care.
4235
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004236 const TargetLowering *TLI = TM.getTargetLowering();
4237
Dan Gohman544ab2c2008-04-12 04:36:06 +00004238 // Emit a library call.
4239 TargetLowering::ArgListTy Args;
4240 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004241 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004242 Entry.Node = Dst; Args.push_back(Entry);
4243 Entry.Node = Src; Args.push_back(Entry);
4244 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004245 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004246 TargetLowering::CallLoweringInfo CLI(*this);
4247 CLI.setDebugLoc(dl).setChain(Chain)
4248 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4249 Type::getVoidTy(*getContext()),
4250 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004251 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004252 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004253 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004254
Dan Gohman544ab2c2008-04-12 04:36:06 +00004255 return CallResult.second;
4256}
4257
Andrew Trickef9de2a2013-05-25 02:42:55 +00004258SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004259 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004260 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004261 MachinePointerInfo DstPtrInfo,
4262 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004263 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004264
Dan Gohman714663a2008-05-29 19:42:22 +00004265 // Check to see if we should lower the memmove to loads and stores first.
4266 // For cases within the target-specified limits, this is the best choice.
4267 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4268 if (ConstantSize) {
4269 // Memmove with size zero? Just return the original chain.
4270 if (ConstantSize->isNullValue())
4271 return Chain;
4272
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004273 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004274 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004275 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004276 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004277 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004278 return Result;
4279 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004280
4281 // Then check to see if we should lower the memmove with target-specific
4282 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004283 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004284 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004285 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004286 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004287 return Result;
4288
Mon P Wangbf862242010-04-06 08:27:51 +00004289 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4290 // not be safe. See memcpy above for more details.
4291
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004292 const TargetLowering *TLI = TM.getTargetLowering();
4293
Dan Gohman544ab2c2008-04-12 04:36:06 +00004294 // Emit a library call.
4295 TargetLowering::ArgListTy Args;
4296 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004297 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004298 Entry.Node = Dst; Args.push_back(Entry);
4299 Entry.Node = Src; Args.push_back(Entry);
4300 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004301 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004302 TargetLowering::CallLoweringInfo CLI(*this);
4303 CLI.setDebugLoc(dl).setChain(Chain)
4304 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4305 Type::getVoidTy(*getContext()),
4306 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004307 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004308 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004309 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004310
Dan Gohman544ab2c2008-04-12 04:36:06 +00004311 return CallResult.second;
4312}
4313
Andrew Trickef9de2a2013-05-25 02:42:55 +00004314SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004315 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004316 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004317 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004318 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004319
4320 // Check to see if we should lower the memset to stores first.
4321 // For cases within the target-specified limits, this is the best choice.
4322 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4323 if (ConstantSize) {
4324 // Memset with size zero? Just return the original chain.
4325 if (ConstantSize->isNullValue())
4326 return Chain;
4327
Mon P Wangc576ee92010-04-04 03:10:48 +00004328 SDValue Result =
4329 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004330 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004331
Gabor Greiff304a7a2008-08-28 21:40:38 +00004332 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004333 return Result;
4334 }
4335
4336 // Then check to see if we should lower the memset with target-specific
4337 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004338 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004339 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004340 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004341 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004342 return Result;
4343
Wesley Peck527da1b2010-11-23 03:31:01 +00004344 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004345 const TargetLowering *TLI = TM.getTargetLowering();
4346 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004347 TargetLowering::ArgListTy Args;
4348 TargetLowering::ArgListEntry Entry;
4349 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4350 Args.push_back(Entry);
4351 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004352 if (Src.getValueType().bitsGT(MVT::i32))
4353 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004354 else
Owen Anderson9f944592009-08-11 20:47:22 +00004355 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004356 Entry.Node = Src;
4357 Entry.Ty = Type::getInt32Ty(*getContext());
4358 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004359 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004360 Entry.Node = Size;
4361 Entry.Ty = IntPtrTy;
4362 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004363 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004364
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004365 // FIXME: pass in SDLoc
4366 TargetLowering::CallLoweringInfo CLI(*this);
4367 CLI.setDebugLoc(dl).setChain(Chain)
4368 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4369 Type::getVoidTy(*getContext()),
4370 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004371 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004372 .setDiscardResult();
4373
4374 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004375 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004376}
4377
Andrew Trickef9de2a2013-05-25 02:42:55 +00004378SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004379 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004380 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004381 AtomicOrdering SuccessOrdering,
4382 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004383 SynchronizationScope SynchScope) {
4384 FoldingSetNodeID ID;
4385 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004386 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004387 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004388 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4390 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4391 return SDValue(E, 0);
4392 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004393
4394 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4395 // SDNode doesn't have access to it. This memory will be "leaked" when
4396 // the node is deallocated, but recovered when the allocator is released.
4397 // If the number of operands is less than 5 we use AtomicSDNode's internal
4398 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004399 unsigned NumOps = Ops.size();
4400 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4401 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004402
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004403 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4404 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004405 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004406 SuccessOrdering, FailureOrdering,
4407 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004408 CSEMap.InsertNode(N, IP);
4409 AllNodes.push_back(N);
4410 return SDValue(N, 0);
4411}
4412
4413SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004414 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004415 MachineMemOperand *MMO,
4416 AtomicOrdering Ordering,
4417 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004418 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004419 Ordering, SynchScope);
4420}
4421
Tim Northover420a2162014-06-13 14:24:07 +00004422SDValue SelectionDAG::getAtomicCmpSwap(
4423 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4424 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4425 unsigned Alignment, AtomicOrdering SuccessOrdering,
4426 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4427 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4428 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4429 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4430
Dan Gohman48b185d2009-09-25 20:36:54 +00004431 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4432 Alignment = getEVTAlignment(MemVT);
4433
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004434 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004435
Eli Friedmane978d2f2011-09-07 02:23:42 +00004436 // FIXME: Volatile isn't really correct; we should keep track of atomic
4437 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004438 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004439 Flags |= MachineMemOperand::MOLoad;
4440 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004441
4442 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004443 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004444
Tim Northover420a2162014-06-13 14:24:07 +00004445 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4446 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004447}
4448
Tim Northover420a2162014-06-13 14:24:07 +00004449SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4450 SDVTList VTs, SDValue Chain, SDValue Ptr,
4451 SDValue Cmp, SDValue Swp,
4452 MachineMemOperand *MMO,
4453 AtomicOrdering SuccessOrdering,
4454 AtomicOrdering FailureOrdering,
4455 SynchronizationScope SynchScope) {
4456 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4457 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004458 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4459
Dale Johannesen839acbb2009-01-29 00:47:48 +00004460 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004461 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4462 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004463}
4464
Andrew Trickef9de2a2013-05-25 02:42:55 +00004465SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004466 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004467 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004468 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004469 unsigned Alignment,
4470 AtomicOrdering Ordering,
4471 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004472 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4473 Alignment = getEVTAlignment(MemVT);
4474
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004475 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004476 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004477 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004478 // For now, atomics are considered to be volatile always, and they are
4479 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004480 // FIXME: Volatile isn't really correct; we should keep track of atomic
4481 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004482 unsigned Flags = MachineMemOperand::MOVolatile;
4483 if (Opcode != ISD::ATOMIC_STORE)
4484 Flags |= MachineMemOperand::MOLoad;
4485 if (Opcode != ISD::ATOMIC_LOAD)
4486 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004487
4488 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004489 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004490 MemVT.getStoreSize(), Alignment);
4491
Eli Friedmanadec5872011-07-29 03:05:32 +00004492 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4493 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004494}
4495
Andrew Trickef9de2a2013-05-25 02:42:55 +00004496SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004497 SDValue Chain,
4498 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004499 MachineMemOperand *MMO,
4500 AtomicOrdering Ordering,
4501 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004502 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4503 Opcode == ISD::ATOMIC_LOAD_SUB ||
4504 Opcode == ISD::ATOMIC_LOAD_AND ||
4505 Opcode == ISD::ATOMIC_LOAD_OR ||
4506 Opcode == ISD::ATOMIC_LOAD_XOR ||
4507 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004508 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004509 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004510 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004511 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004512 Opcode == ISD::ATOMIC_SWAP ||
4513 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004514 "Invalid Atomic Op");
4515
Owen Anderson53aa7a92009-08-10 22:56:29 +00004516 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004517
Eli Friedman342e8df2011-08-24 20:50:09 +00004518 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4519 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004520 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004521 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004522}
4523
Andrew Trickef9de2a2013-05-25 02:42:55 +00004524SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004525 EVT VT, SDValue Chain,
4526 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004527 MachineMemOperand *MMO,
4528 AtomicOrdering Ordering,
4529 SynchronizationScope SynchScope) {
4530 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4531
4532 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004533 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004534 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004535}
4536
Duncan Sands739a0542008-07-02 17:40:58 +00004537/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004538SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4539 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004540 return Ops[0];
4541
Owen Anderson53aa7a92009-08-10 22:56:29 +00004542 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004543 VTs.reserve(Ops.size());
4544 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004545 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004546 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004547}
4548
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004549SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004550SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004551 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004552 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004553 unsigned Align, bool Vol,
4554 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004555 if (Align == 0) // Ensure that codegen never sees alignment 0
4556 Align = getEVTAlignment(MemVT);
4557
4558 MachineFunction &MF = getMachineFunction();
4559 unsigned Flags = 0;
4560 if (WriteMem)
4561 Flags |= MachineMemOperand::MOStore;
4562 if (ReadMem)
4563 Flags |= MachineMemOperand::MOLoad;
4564 if (Vol)
4565 Flags |= MachineMemOperand::MOVolatile;
4566 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004567 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004568
Craig Topper206fcd42014-04-26 19:29:41 +00004569 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004570}
4571
4572SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004573SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004574 ArrayRef<SDValue> Ops, EVT MemVT,
4575 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004576 assert((Opcode == ISD::INTRINSIC_VOID ||
4577 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004578 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004579 Opcode == ISD::LIFETIME_START ||
4580 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004581 (Opcode <= INT_MAX &&
4582 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4583 "Opcode is not a memory-accessing opcode!");
4584
Dale Johannesen839acbb2009-01-29 00:47:48 +00004585 // Memoize the node unless it returns a flag.
4586 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004587 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004588 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004589 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004590 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004591 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004592 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4593 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004594 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004595 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004596
Jack Carter170a5f22013-09-09 22:02:08 +00004597 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4598 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004599 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004600 CSEMap.InsertNode(N, IP);
4601 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004602 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4603 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004604 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004605 }
4606 AllNodes.push_back(N);
4607 return SDValue(N, 0);
4608}
4609
Chris Lattnerea952f02010-09-21 17:24:05 +00004610/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4611/// MachinePointerInfo record from it. This is particularly useful because the
4612/// code generator has many cases where it doesn't bother passing in a
4613/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4614static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4615 // If this is FI+Offset, we can model it.
4616 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4617 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4618
4619 // If this is (FI+Offset1)+Offset2, we can model it.
4620 if (Ptr.getOpcode() != ISD::ADD ||
4621 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4622 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4623 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004624
Chris Lattnerea952f02010-09-21 17:24:05 +00004625 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4626 return MachinePointerInfo::getFixedStack(FI, Offset+
4627 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4628}
4629
4630/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4631/// MachinePointerInfo record from it. This is particularly useful because the
4632/// code generator has many cases where it doesn't bother passing in a
4633/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4634static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4635 // If the 'Offset' value isn't a constant, we can't handle this.
4636 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4637 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4638 if (OffsetOp.getOpcode() == ISD::UNDEF)
4639 return InferPointerInfo(Ptr);
4640 return MachinePointerInfo();
4641}
Wesley Peck527da1b2010-11-23 03:31:01 +00004642
Chris Lattnerea952f02010-09-21 17:24:05 +00004643
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004644SDValue
4645SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004646 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004647 SDValue Ptr, SDValue Offset,
4648 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004649 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004650 unsigned Alignment, const MDNode *TBAAInfo,
4651 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004652 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004653 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004654 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4655 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004656
Dan Gohman48b185d2009-09-25 20:36:54 +00004657 unsigned Flags = MachineMemOperand::MOLoad;
4658 if (isVolatile)
4659 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004660 if (isNonTemporal)
4661 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004662 if (isInvariant)
4663 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004664
Chris Lattnerea952f02010-09-21 17:24:05 +00004665 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4666 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004667 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004668 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004669
Chris Lattnerea952f02010-09-21 17:24:05 +00004670 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004671 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004672 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004673 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004674 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004675}
4676
4677SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004678SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004679 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004680 SDValue Ptr, SDValue Offset, EVT MemVT,
4681 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004682 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004683 ExtType = ISD::NON_EXTLOAD;
4684 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004685 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004686 } else {
4687 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004688 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4689 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004690 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004691 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004692 assert(VT.isVector() == MemVT.isVector() &&
4693 "Cannot use trunc store to convert to or from a vector!");
4694 assert((!VT.isVector() ||
4695 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4696 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004697 }
4698
4699 bool Indexed = AM != ISD::UNINDEXED;
4700 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4701 "Unindexed load with an offset!");
4702
4703 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004704 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004705 SDValue Ops[] = { Chain, Ptr, Offset };
4706 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004707 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004708 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004709 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004710 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004711 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004712 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004713 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004714 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4715 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004716 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004717 }
Jack Carter170a5f22013-09-09 22:02:08 +00004718 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4719 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004720 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004721 CSEMap.InsertNode(N, IP);
4722 AllNodes.push_back(N);
4723 return SDValue(N, 0);
4724}
4725
Andrew Trickef9de2a2013-05-25 02:42:55 +00004726SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004727 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004728 MachinePointerInfo PtrInfo,
4729 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004730 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004731 const MDNode *TBAAInfo,
4732 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004733 SDValue Undef = getUNDEF(Ptr.getValueType());
4734 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004735 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4736 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004737}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004738
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004739SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4740 SDValue Chain, SDValue Ptr,
4741 MachineMemOperand *MMO) {
4742 SDValue Undef = getUNDEF(Ptr.getValueType());
4743 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4744 VT, MMO);
4745}
4746
Andrew Trickef9de2a2013-05-25 02:42:55 +00004747SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004748 SDValue Chain, SDValue Ptr,
4749 MachinePointerInfo PtrInfo, EVT MemVT,
4750 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004751 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004752 SDValue Undef = getUNDEF(Ptr.getValueType());
4753 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004754 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004755 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004756}
4757
4758
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004759SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4760 SDValue Chain, SDValue Ptr, EVT MemVT,
4761 MachineMemOperand *MMO) {
4762 SDValue Undef = getUNDEF(Ptr.getValueType());
4763 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4764 MemVT, MMO);
4765}
4766
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004767SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004768SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004769 SDValue Offset, ISD::MemIndexedMode AM) {
4770 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4771 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4772 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004773 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004774 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004775 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004776 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004777}
4778
Andrew Trickef9de2a2013-05-25 02:42:55 +00004779SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004780 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004781 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004782 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004783 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004784 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004785 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004786 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004787
Dan Gohman48b185d2009-09-25 20:36:54 +00004788 unsigned Flags = MachineMemOperand::MOStore;
4789 if (isVolatile)
4790 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004791 if (isNonTemporal)
4792 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004793
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004794 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004795 PtrInfo = InferPointerInfo(Ptr);
4796
4797 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004798 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004799 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004800 Val.getValueType().getStoreSize(), Alignment,
4801 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004802
4803 return getStore(Chain, dl, Val, Ptr, MMO);
4804}
4805
Andrew Trickef9de2a2013-05-25 02:42:55 +00004806SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004807 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004808 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004809 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004810 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004811 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004812 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004813 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4814 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004815 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004816 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004817 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004818 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004819 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004820 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004821 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4822 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004823 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004824 }
Jack Carter170a5f22013-09-09 22:02:08 +00004825 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4826 dl.getDebugLoc(), VTs,
4827 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004828 CSEMap.InsertNode(N, IP);
4829 AllNodes.push_back(N);
4830 return SDValue(N, 0);
4831}
4832
Andrew Trickef9de2a2013-05-25 02:42:55 +00004833SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004834 SDValue Ptr, MachinePointerInfo PtrInfo,
4835 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004836 unsigned Alignment,
4837 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004838 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004839 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004840 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4841 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +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 =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004854 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4855 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004856
4857 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4858}
4859
Andrew Trickef9de2a2013-05-25 02:42:55 +00004860SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004861 SDValue Ptr, EVT SVT,
4862 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004863 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004864
Michael Ilsemand5f91512012-09-10 16:56:31 +00004865 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004866 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004867 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004868 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004869
Dan Gohmancecad352009-12-14 23:40:38 +00004870 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4871 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004872 assert(VT.isInteger() == SVT.isInteger() &&
4873 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004874 assert(VT.isVector() == SVT.isVector() &&
4875 "Cannot use trunc store to convert to or from a vector!");
4876 assert((!VT.isVector() ||
4877 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4878 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004879
Owen Anderson9f944592009-08-11 20:47:22 +00004880 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004881 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004882 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4883 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004884 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004885 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004886 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004887 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004888 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004889 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004890 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4891 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004892 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004893 }
Jack Carter170a5f22013-09-09 22:02:08 +00004894 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4895 dl.getDebugLoc(), VTs,
4896 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004897 CSEMap.InsertNode(N, IP);
4898 AllNodes.push_back(N);
4899 return SDValue(N, 0);
4900}
4901
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004902SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004903SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004904 SDValue Offset, ISD::MemIndexedMode AM) {
4905 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4906 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4907 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004908 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004909 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4910 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004911 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004912 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004913 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004914 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004915 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004916 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004917 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004918
Jack Carter170a5f22013-09-09 22:02:08 +00004919 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4920 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004921 ST->isTruncatingStore(),
4922 ST->getMemoryVT(),
4923 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004924 CSEMap.InsertNode(N, IP);
4925 AllNodes.push_back(N);
4926 return SDValue(N, 0);
4927}
4928
Andrew Trickef9de2a2013-05-25 02:42:55 +00004929SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004930 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004931 SDValue SV,
4932 unsigned Align) {
4933 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00004934 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004935}
4936
Andrew Trickef9de2a2013-05-25 02:42:55 +00004937SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00004938 ArrayRef<SDUse> Ops) {
4939 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004940 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00004941 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004942 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4943 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004944 default: break;
4945 }
4946
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004947 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004948 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00004949 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00004950 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004951}
4952
Andrew Trickef9de2a2013-05-25 02:42:55 +00004953SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00004954 ArrayRef<SDValue> Ops) {
4955 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00004956 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004957 case 0: return getNode(Opcode, DL, VT);
4958 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4959 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4960 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004961 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004962 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004963
Chris Lattnerb0713c72005-04-09 03:27:28 +00004964 switch (Opcode) {
4965 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004966 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004967 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004968 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4969 "LHS and RHS of condition must have same type!");
4970 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4971 "True and False arms of SelectCC must have same type!");
4972 assert(Ops[2].getValueType() == VT &&
4973 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004974 break;
4975 }
4976 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004977 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004978 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4979 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004980 break;
4981 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004982 }
4983
Chris Lattner566307f2005-05-14 07:42:29 +00004984 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004985 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004986 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004987
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004988 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004989 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004990 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00004991 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004992
Bill Wendling022d18f2009-12-18 23:32:53 +00004993 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004994 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004995
Jack Carter170a5f22013-09-09 22:02:08 +00004996 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00004997 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004998 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004999 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005000 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005001 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005002 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005003
Chris Lattnerb0713c72005-04-09 03:27:28 +00005004 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005005#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005006 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005007#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005008 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005009}
5010
Andrew Trickef9de2a2013-05-25 02:42:55 +00005011SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005012 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5013 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005014}
5015
Andrew Trickef9de2a2013-05-25 02:42:55 +00005016SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005017 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005018 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005019 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005020
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005021#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005022 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005023 // FIXME: figure out how to safely handle things like
5024 // int foo(int x) { return 1 << (x & 255); }
5025 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005026 case ISD::SRA_PARTS:
5027 case ISD::SRL_PARTS:
5028 case ISD::SHL_PARTS:
5029 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005030 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005031 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005032 else if (N3.getOpcode() == ISD::AND)
5033 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5034 // If the and is only masking out bits that cannot effect the shift,
5035 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005036 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005037 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005038 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005039 }
5040 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005041 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005042#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005043
Chris Lattnerf9c19152005-08-25 19:12:10 +00005044 // Memoize the node unless it returns a flag.
5045 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005046 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005047 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005048 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005049 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005050 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005051 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005052 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005053
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005054 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005055 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5056 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005057 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005058 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5059 DL.getDebugLoc(), VTList, Ops[0],
5060 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005061 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005062 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5063 DL.getDebugLoc(), VTList, Ops[0],
5064 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005065 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005066 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005067 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005068 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005069 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005070 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005071 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005072 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5073 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005074 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005075 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5076 DL.getDebugLoc(), VTList, Ops[0],
5077 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005078 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005079 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5080 DL.getDebugLoc(), VTList, Ops[0],
5081 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005082 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005083 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005084 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005085 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005086 }
Chris Lattner006f56b2005-05-14 06:42:57 +00005087 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005088#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005089 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005090#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005091 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005092}
5093
Andrew Trickef9de2a2013-05-25 02:42:55 +00005094SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Topper48d114b2014-04-26 18:35:24 +00005095 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005096}
5097
Andrew Trickef9de2a2013-05-25 02:42:55 +00005098SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005099 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005100 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005101 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005102}
5103
Andrew Trickef9de2a2013-05-25 02:42:55 +00005104SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005105 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005106 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005107 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005108}
5109
Andrew Trickef9de2a2013-05-25 02:42:55 +00005110SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005111 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005112 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005113 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005114}
5115
Andrew Trickef9de2a2013-05-25 02:42:55 +00005116SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005117 SDValue N1, SDValue N2, SDValue N3,
5118 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005119 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005120 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005121}
5122
Andrew Trickef9de2a2013-05-25 02:42:55 +00005123SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005124 SDValue N1, SDValue N2, SDValue N3,
5125 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005126 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005127 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005128}
5129
Owen Anderson53aa7a92009-08-10 22:56:29 +00005130SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005131 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005132}
5133
Owen Anderson53aa7a92009-08-10 22:56:29 +00005134SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005135 FoldingSetNodeID ID;
5136 ID.AddInteger(2U);
5137 ID.AddInteger(VT1.getRawBits());
5138 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005139
Craig Topperc0196b12014-04-14 00:51:57 +00005140 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005141 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005142 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005143 EVT *Array = Allocator.Allocate<EVT>(2);
5144 Array[0] = VT1;
5145 Array[1] = VT2;
5146 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5147 VTListMap.InsertNode(Result, IP);
5148 }
5149 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005150}
Dan Gohman17059682008-07-17 19:10:17 +00005151
Owen Anderson53aa7a92009-08-10 22:56:29 +00005152SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005153 FoldingSetNodeID ID;
5154 ID.AddInteger(3U);
5155 ID.AddInteger(VT1.getRawBits());
5156 ID.AddInteger(VT2.getRawBits());
5157 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005158
Craig Topperc0196b12014-04-14 00:51:57 +00005159 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005160 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005161 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005162 EVT *Array = Allocator.Allocate<EVT>(3);
5163 Array[0] = VT1;
5164 Array[1] = VT2;
5165 Array[2] = VT3;
5166 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5167 VTListMap.InsertNode(Result, IP);
5168 }
5169 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005170}
5171
Owen Anderson53aa7a92009-08-10 22:56:29 +00005172SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005173 FoldingSetNodeID ID;
5174 ID.AddInteger(4U);
5175 ID.AddInteger(VT1.getRawBits());
5176 ID.AddInteger(VT2.getRawBits());
5177 ID.AddInteger(VT3.getRawBits());
5178 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005179
Craig Topperc0196b12014-04-14 00:51:57 +00005180 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005181 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005182 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005183 EVT *Array = Allocator.Allocate<EVT>(4);
5184 Array[0] = VT1;
5185 Array[1] = VT2;
5186 Array[2] = VT3;
5187 Array[3] = VT4;
5188 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5189 VTListMap.InsertNode(Result, IP);
5190 }
5191 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005192}
5193
Craig Topperabb4ac72014-04-16 06:10:51 +00005194SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5195 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005196 FoldingSetNodeID ID;
5197 ID.AddInteger(NumVTs);
5198 for (unsigned index = 0; index < NumVTs; index++) {
5199 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005200 }
5201
Craig Topperc0196b12014-04-14 00:51:57 +00005202 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005203 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005204 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005205 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005206 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005207 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5208 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005209 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005210 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005211}
5212
5213
Chris Lattnerf34156e2006-01-28 09:32:45 +00005214/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5215/// specified operands. If the resultant node already exists in the DAG,
5216/// this does not modify the specified node, instead it returns the node that
5217/// already exists. If the resultant node does not exist in the DAG, the
5218/// input node is returned. As a degenerate case, if you specify the same
5219/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005220SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005221 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005222
Chris Lattnerf34156e2006-01-28 09:32:45 +00005223 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005224 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005225
Chris Lattnerf34156e2006-01-28 09:32:45 +00005226 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005227 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005228 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005229 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005230
Dan Gohmanebeccb42008-07-21 22:38:59 +00005231 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005232 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005233 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005234 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005235
Chris Lattnerf34156e2006-01-28 09:32:45 +00005236 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005237 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005238
Chris Lattnerf34156e2006-01-28 09:32:45 +00005239 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005240 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005241 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005242}
5243
Dan Gohman92c11ac2010-06-18 15:30:29 +00005244SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005245 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005246
Chris Lattnerf34156e2006-01-28 09:32:45 +00005247 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005248 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005249 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005250
Chris Lattnerf34156e2006-01-28 09:32:45 +00005251 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005252 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005253 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005254 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005255
Dan Gohmanebeccb42008-07-21 22:38:59 +00005256 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005257 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005258 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005259 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005260
Chris Lattnerf34156e2006-01-28 09:32:45 +00005261 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005262 if (N->OperandList[0] != Op1)
5263 N->OperandList[0].set(Op1);
5264 if (N->OperandList[1] != Op2)
5265 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005266
Chris Lattnerf34156e2006-01-28 09:32:45 +00005267 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005268 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005269 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005270}
5271
Dan Gohman92c11ac2010-06-18 15:30:29 +00005272SDNode *SelectionDAG::
5273UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005274 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005275 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005276}
5277
Dan Gohman92c11ac2010-06-18 15:30:29 +00005278SDNode *SelectionDAG::
5279UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005280 SDValue Op3, SDValue Op4) {
5281 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005282 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005283}
5284
Dan Gohman92c11ac2010-06-18 15:30:29 +00005285SDNode *SelectionDAG::
5286UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005287 SDValue Op3, SDValue Op4, SDValue Op5) {
5288 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005289 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005290}
5291
Dan Gohman92c11ac2010-06-18 15:30:29 +00005292SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005293UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5294 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005295 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005296 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005297
Chris Lattnerf34156e2006-01-28 09:32:45 +00005298 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005299 bool AnyChange = false;
5300 for (unsigned i = 0; i != NumOps; ++i) {
5301 if (Ops[i] != N->getOperand(i)) {
5302 AnyChange = true;
5303 break;
5304 }
5305 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005306
Chris Lattnerf34156e2006-01-28 09:32:45 +00005307 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005308 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005309
Chris Lattnerf34156e2006-01-28 09:32:45 +00005310 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005311 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005312 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005313 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005314
Dan Gohman2f83b472008-05-02 00:05:03 +00005315 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005316 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005317 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005318 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005319
Chris Lattnerf34156e2006-01-28 09:32:45 +00005320 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005321 for (unsigned i = 0; i != NumOps; ++i)
5322 if (N->OperandList[i] != Ops[i])
5323 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005324
5325 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005326 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005327 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005328}
5329
Dan Gohman91697632008-07-07 20:57:48 +00005330/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005331/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005332void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005333 // Unlike the code in MorphNodeTo that does this, we don't need to
5334 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005335 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5336 SDUse &Use = *I++;
5337 Use.set(SDValue());
5338 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005339}
Chris Lattner19732782005-08-16 18:17:10 +00005340
Dan Gohman17059682008-07-17 19:10:17 +00005341/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5342/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005343///
Dan Gohman17059682008-07-17 19:10:17 +00005344SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005345 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005346 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005347 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005348}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005349
Dan Gohman17059682008-07-17 19:10:17 +00005350SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005351 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005352 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005353 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005354 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005355}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005356
Dan Gohman17059682008-07-17 19:10:17 +00005357SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005358 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005359 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005360 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005361 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005362 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005363}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005364
Dan Gohman17059682008-07-17 19:10:17 +00005365SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005366 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005367 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005368 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005369 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005370 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005371}
Chris Lattner466fece2005-08-21 22:30:30 +00005372
Dan Gohman17059682008-07-17 19:10:17 +00005373SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005374 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005375 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005376 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005377}
5378
Dan Gohman17059682008-07-17 19:10:17 +00005379SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005380 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005381 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005382 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005383}
5384
Dan Gohman17059682008-07-17 19:10:17 +00005385SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005386 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005387 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005388 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005389}
5390
Dan Gohman17059682008-07-17 19:10:17 +00005391SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005392 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005393 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005394 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005395 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005396}
5397
Bill Wendling2d598632008-12-01 23:28:22 +00005398SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005399 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005400 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005401 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005402 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005403}
5404
Scott Michelcf0da6c2009-02-17 22:15:04 +00005405SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005406 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005407 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005408 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005409 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005410 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005411}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005412
Scott Michelcf0da6c2009-02-17 22:15:04 +00005413SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005414 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005415 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005416 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005417 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005418 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005419}
5420
Dan Gohman17059682008-07-17 19:10:17 +00005421SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005422 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005423 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005424 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005425 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005426 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005427 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005428}
5429
Dan Gohman17059682008-07-17 19:10:17 +00005430SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005431 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005432 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005433 SDValue Op3) {
5434 SDVTList VTs = getVTList(VT1, VT2, VT3);
5435 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005436 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005437}
5438
5439SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005440 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005441 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005442 // Reset the NodeID to -1.
5443 N->setNodeId(-1);
5444 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005445}
5446
Andrew Trickef9de2a2013-05-25 02:42:55 +00005447/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005448/// the line number information on the merged node since it is not possible to
5449/// preserve the information that operation is associated with multiple lines.
5450/// This will make the debugger working better at -O0, were there is a higher
5451/// probability having other instructions associated with that line.
5452///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005453/// For IROrder, we keep the smaller of the two
5454SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005455 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005456 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5457 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005458 N->setDebugLoc(DebugLoc());
5459 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005460 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5461 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005462 return N;
5463}
5464
Chris Lattneraf197502010-02-28 21:36:14 +00005465/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005466/// return type, opcode, and operands.
5467///
5468/// Note that MorphNodeTo returns the resultant node. If there is already a
5469/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005470/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005471///
5472/// Using MorphNodeTo is faster than creating a new node and swapping it in
5473/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005474/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005475/// the node's users.
5476///
5477SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005478 SDVTList VTs, ArrayRef<SDValue> Ops) {
5479 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005480 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005481 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005482 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005483 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005484 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005485 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005486 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005487 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005488
Dan Gohmand3fe1742008-09-13 01:54:27 +00005489 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005490 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005491
Dan Gohman17059682008-07-17 19:10:17 +00005492 // Start the morphing.
5493 N->NodeType = Opc;
5494 N->ValueList = VTs.VTs;
5495 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005496
Dan Gohman17059682008-07-17 19:10:17 +00005497 // Clear the operands list, updating used nodes to remove this from their
5498 // use list. Keep track of any operands that become dead as a result.
5499 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005500 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5501 SDUse &Use = *I++;
5502 SDNode *Used = Use.getNode();
5503 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005504 if (Used->use_empty())
5505 DeadNodeSet.insert(Used);
5506 }
5507
Dan Gohman48b185d2009-09-25 20:36:54 +00005508 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5509 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005510 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005511 // If NumOps is larger than the # of operands we can have in a
5512 // MachineSDNode, reallocate the operand list.
5513 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5514 if (MN->OperandsNeedDelete)
5515 delete[] MN->OperandList;
5516 if (NumOps > array_lengthof(MN->LocalOperands))
5517 // We're creating a final node that will live unmorphed for the
5518 // remainder of the current SelectionDAG iteration, so we can allocate
5519 // the operands directly out of a pool with no recycling metadata.
5520 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005521 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005522 else
Craig Topper131de822014-04-27 19:21:16 +00005523 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005524 MN->OperandsNeedDelete = false;
5525 } else
Craig Topper131de822014-04-27 19:21:16 +00005526 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005527 } else {
5528 // If NumOps is larger than the # of operands we currently have, reallocate
5529 // the operand list.
5530 if (NumOps > N->NumOperands) {
5531 if (N->OperandsNeedDelete)
5532 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005533 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005534 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005535 } else
Craig Topper131de822014-04-27 19:21:16 +00005536 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005537 }
5538
5539 // Delete any nodes that are still dead after adding the uses for the
5540 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005541 if (!DeadNodeSet.empty()) {
5542 SmallVector<SDNode *, 16> DeadNodes;
5543 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5544 E = DeadNodeSet.end(); I != E; ++I)
5545 if ((*I)->use_empty())
5546 DeadNodes.push_back(*I);
5547 RemoveDeadNodes(DeadNodes);
5548 }
Dan Gohman91697632008-07-07 20:57:48 +00005549
Dan Gohman17059682008-07-17 19:10:17 +00005550 if (IP)
5551 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005552 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005553}
5554
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005555
Dan Gohman32f71d72009-09-25 18:54:59 +00005556/// getMachineNode - These are used for target selectors to create a new node
5557/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005558///
Dan Gohman32f71d72009-09-25 18:54:59 +00005559/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005560/// node of the specified opcode and operands, it returns that node instead of
5561/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005562MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005563SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005564 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005565 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005566}
Bill Wendlinga434d932009-01-29 09:01:55 +00005567
Dan Gohmana22f2d82009-10-10 01:29:16 +00005568MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005569SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005570 SDVTList VTs = getVTList(VT);
5571 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005572 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005573}
Bill Wendlinga434d932009-01-29 09:01:55 +00005574
Dan Gohmana22f2d82009-10-10 01:29:16 +00005575MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005576SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005577 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005578 SDVTList VTs = getVTList(VT);
5579 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005580 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005581}
Bill Wendlinga434d932009-01-29 09:01:55 +00005582
Dan Gohmana22f2d82009-10-10 01:29:16 +00005583MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005584SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005585 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005586 SDVTList VTs = getVTList(VT);
5587 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005588 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005589}
Bill Wendlinga434d932009-01-29 09:01:55 +00005590
Dan Gohmana22f2d82009-10-10 01:29:16 +00005591MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005592SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005593 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005594 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005595 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005596}
Bill Wendlinga434d932009-01-29 09:01:55 +00005597
Dan Gohmana22f2d82009-10-10 01:29:16 +00005598MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005599SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005600 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005601 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005602}
Bill Wendlinga434d932009-01-29 09:01:55 +00005603
Dan Gohmana22f2d82009-10-10 01:29:16 +00005604MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005605SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005606 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005607 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005608 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005609 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005610}
Bill Wendlinga434d932009-01-29 09:01:55 +00005611
Dan Gohmana22f2d82009-10-10 01:29:16 +00005612MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005613SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005614 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005615 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005616 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005617 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005618}
5619
Dan Gohmana22f2d82009-10-10 01:29:16 +00005620MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005621SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005622 EVT VT1, EVT VT2, SDValue Op1,
5623 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005624 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005625 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005626 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005627}
5628
Dan Gohmana22f2d82009-10-10 01:29:16 +00005629MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005630SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005631 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005632 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005633 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005634 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005635}
Bill Wendlinga434d932009-01-29 09:01:55 +00005636
Dan Gohmana22f2d82009-10-10 01:29:16 +00005637MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005638SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005639 EVT VT1, EVT VT2, EVT VT3,
5640 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005641 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005642 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005643 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005644}
Bill Wendlinga434d932009-01-29 09:01:55 +00005645
Dan Gohmana22f2d82009-10-10 01:29:16 +00005646MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005647SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005648 EVT VT1, EVT VT2, EVT VT3,
5649 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005650 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005651 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005652 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005653}
Bill Wendlinga434d932009-01-29 09:01:55 +00005654
Dan Gohmana22f2d82009-10-10 01:29:16 +00005655MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005656SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005657 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005658 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005659 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005660 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005661}
Bill Wendlinga434d932009-01-29 09:01:55 +00005662
Dan Gohmana22f2d82009-10-10 01:29:16 +00005663MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005664SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005665 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005666 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005667 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005668 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005669}
5670
Dan Gohmana22f2d82009-10-10 01:29:16 +00005671MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005672SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005673 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005674 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005675 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005676 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005677}
5678
Dan Gohmana22f2d82009-10-10 01:29:16 +00005679MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005680SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005681 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005682 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005683 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005684 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005685 const SDValue *Ops = OpsArray.data();
5686 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005687
5688 if (DoCSE) {
5689 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005690 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005691 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005692 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005693 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005694 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005695 }
5696
5697 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005698 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5699 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005700
5701 // Initialize the operands list.
5702 if (NumOps > array_lengthof(N->LocalOperands))
5703 // We're creating a final node that will live unmorphed for the
5704 // remainder of the current SelectionDAG iteration, so we can allocate
5705 // the operands directly out of a pool with no recycling metadata.
5706 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5707 Ops, NumOps);
5708 else
5709 N->InitOperands(N->LocalOperands, Ops, NumOps);
5710 N->OperandsNeedDelete = false;
5711
5712 if (DoCSE)
5713 CSEMap.InsertNode(N, IP);
5714
5715 AllNodes.push_back(N);
5716#ifndef NDEBUG
Chandler Carruth2fc9a2b2014-07-22 04:03:22 +00005717 VerifySDNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005718#endif
5719 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005720}
Evan Chengd3f1db92006-02-09 07:15:23 +00005721
Dan Gohmanac33a902009-08-19 18:16:17 +00005722/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005723/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005724SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005725SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005726 SDValue Operand) {
5727 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005728 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005729 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005730 return SDValue(Subreg, 0);
5731}
5732
Bob Wilson2a45a652009-10-08 18:49:46 +00005733/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005734/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005735SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005736SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005737 SDValue Operand, SDValue Subreg) {
5738 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005739 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005740 VT, Operand, Subreg, SRIdxVal);
5741 return SDValue(Result, 0);
5742}
5743
Evan Cheng31604a62008-03-22 01:55:50 +00005744/// getNodeIfExists - Get the specified node if it's already available, or
5745/// else return NULL.
5746SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005747 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5748 bool exact) {
5749 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005750 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005751 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005752 if (isBinOpWithFlags(Opcode))
5753 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005754 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005756 return E;
5757 }
Craig Topperc0196b12014-04-14 00:51:57 +00005758 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005759}
5760
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005761/// getDbgValue - Creates a SDDbgValue node.
5762///
Adrian Prantl32da8892014-04-25 20:49:25 +00005763/// SDNode
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005764SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005765SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
5766 bool IsIndirect, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005767 DebugLoc DL, unsigned O) {
Adrian Prantl32da8892014-04-25 20:49:25 +00005768 return new (Allocator) SDDbgValue(MDPtr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005769}
5770
Adrian Prantl32da8892014-04-25 20:49:25 +00005771/// Constant
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005772SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005773SelectionDAG::getConstantDbgValue(MDNode *MDPtr, const Value *C,
5774 uint64_t Off,
5775 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005776 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5777}
5778
Adrian Prantl32da8892014-04-25 20:49:25 +00005779/// FrameIndex
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005780SDDbgValue *
Adrian Prantl32da8892014-04-25 20:49:25 +00005781SelectionDAG::getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5782 DebugLoc DL, unsigned O) {
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005783 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5784}
5785
Dan Gohman7d099f72010-03-03 21:33:37 +00005786namespace {
5787
Dan Gohman9cc886b2010-03-04 19:11:28 +00005788/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005789/// pointed to by a use iterator is deleted, increment the use iterator
5790/// so that it doesn't dangle.
5791///
Dan Gohman7d099f72010-03-03 21:33:37 +00005792class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005793 SDNode::use_iterator &UI;
5794 SDNode::use_iterator &UE;
5795
Craig Topper7b883b32014-03-08 06:31:39 +00005796 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005797 // Increment the iterator as needed.
5798 while (UI != UE && N == *UI)
5799 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005800 }
5801
5802public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005803 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005804 SDNode::use_iterator &ui,
5805 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005806 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005807};
5808
5809}
5810
Evan Cheng445b91a2006-08-07 22:13:29 +00005811/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005812/// This can cause recursive merging of nodes in the DAG.
5813///
Chris Lattner76858912008-02-03 03:35:22 +00005814/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005815///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005816void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005817 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005818 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005819 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005820 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005821
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005822 // Iterate over all the existing uses of From. New uses will be added
5823 // to the beginning of the use list, which we avoid visiting.
5824 // This specifically avoids visiting uses of From that arise while the
5825 // replacement is happening, because any such uses would be the result
5826 // of CSE: If an existing node looks like From after one of its operands
5827 // is replaced by To, we don't want to replace of all its users with To
5828 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005829 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005830 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005831 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005832 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005833
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005834 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005835 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005836
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005837 // A user can appear in a use list multiple times, and when this
5838 // happens the uses are usually next to each other in the list.
5839 // To help reduce the number of CSE recomputations, process all
5840 // the uses of this user that we can find this way.
5841 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005842 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005843 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005844 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005845 } while (UI != UE && *UI == User);
5846
5847 // Now that we have modified User, add it back to the CSE maps. If it
5848 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005849 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005850 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005851
5852 // If we just RAUW'd the root, take note.
5853 if (FromN == getRoot())
5854 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005855}
5856
5857/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5858/// This can cause recursive merging of nodes in the DAG.
5859///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005860/// This version assumes that for each value of From, there is a
5861/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005862///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005863void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005864#ifndef NDEBUG
5865 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5866 assert((!From->hasAnyUseOfValue(i) ||
5867 From->getValueType(i) == To->getValueType(i)) &&
5868 "Cannot use this version of ReplaceAllUsesWith!");
5869#endif
Dan Gohman17059682008-07-17 19:10:17 +00005870
5871 // Handle the trivial case.
5872 if (From == To)
5873 return;
5874
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005875 // Iterate over just the existing users of From. See the comments in
5876 // the ReplaceAllUsesWith above.
5877 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005878 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005879 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005880 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005881
Chris Lattner373f0482005-08-26 18:36:28 +00005882 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005883 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005884
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005885 // A user can appear in a use list multiple times, and when this
5886 // happens the uses are usually next to each other in the list.
5887 // To help reduce the number of CSE recomputations, process all
5888 // the uses of this user that we can find this way.
5889 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005890 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005891 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005892 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005893 } while (UI != UE && *UI == User);
5894
5895 // Now that we have modified User, add it back to the CSE maps. If it
5896 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005897 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005898 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005899
5900 // If we just RAUW'd the root, take note.
5901 if (From == getRoot().getNode())
5902 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005903}
5904
Chris Lattner373f0482005-08-26 18:36:28 +00005905/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5906/// This can cause recursive merging of nodes in the DAG.
5907///
5908/// This version can replace From with any result values. To must match the
5909/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005910void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005911 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005912 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005913
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005914 // Iterate over just the existing users of From. See the comments in
5915 // the ReplaceAllUsesWith above.
5916 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005917 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005918 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005919 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005920
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005921 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005922 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005923
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005924 // A user can appear in a use list multiple times, and when this
5925 // happens the uses are usually next to each other in the list.
5926 // To help reduce the number of CSE recomputations, process all
5927 // the uses of this user that we can find this way.
5928 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005929 SDUse &Use = UI.getUse();
5930 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005931 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005932 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005933 } while (UI != UE && *UI == User);
5934
5935 // Now that we have modified User, add it back to the CSE maps. If it
5936 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005937 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005938 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005939
5940 // If we just RAUW'd the root, take note.
5941 if (From == getRoot().getNode())
5942 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005943}
5944
Chris Lattner375e1a72006-02-17 21:58:01 +00005945/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005946/// uses of other values produced by From.getNode() alone. The Deleted
5947/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005948void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005949 // Handle the really simple, really trivial case efficiently.
5950 if (From == To) return;
5951
Chris Lattner375e1a72006-02-17 21:58:01 +00005952 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005953 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005954 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005955 return;
5956 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005957
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005958 // Iterate over just the existing users of From. See the comments in
5959 // the ReplaceAllUsesWith above.
5960 SDNode::use_iterator UI = From.getNode()->use_begin(),
5961 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005962 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005963 while (UI != UE) {
5964 SDNode *User = *UI;
5965 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005966
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005967 // A user can appear in a use list multiple times, and when this
5968 // happens the uses are usually next to each other in the list.
5969 // To help reduce the number of CSE recomputations, process all
5970 // the uses of this user that we can find this way.
5971 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005972 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005973
5974 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005975 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005976 ++UI;
5977 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005978 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005979
5980 // If this node hasn't been modified yet, it's still in the CSE maps,
5981 // so remove its old self from the CSE maps.
5982 if (!UserRemovedFromCSEMaps) {
5983 RemoveNodeFromCSEMaps(User);
5984 UserRemovedFromCSEMaps = true;
5985 }
5986
5987 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005988 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005989 } while (UI != UE && *UI == User);
5990
5991 // We are iterating over all uses of the From node, so if a use
5992 // doesn't use the specific value, no changes are made.
5993 if (!UserRemovedFromCSEMaps)
5994 continue;
5995
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005996 // Now that we have modified User, add it back to the CSE maps. If it
5997 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005998 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005999 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006000
6001 // If we just RAUW'd the root, take note.
6002 if (From == getRoot())
6003 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006004}
6005
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006006namespace {
6007 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6008 /// to record information about a use.
6009 struct UseMemo {
6010 SDNode *User;
6011 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006012 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006013 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006014
6015 /// operator< - Sort Memos by User.
6016 bool operator<(const UseMemo &L, const UseMemo &R) {
6017 return (intptr_t)L.User < (intptr_t)R.User;
6018 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006019}
6020
Dan Gohman17059682008-07-17 19:10:17 +00006021/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006022/// uses of other values produced by From.getNode() alone. The same value
6023/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006024/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006025void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6026 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006027 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006028 // Handle the simple, trivial case efficiently.
6029 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006030 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006031
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006032 // Read up all the uses and make records of them. This helps
6033 // processing new uses that are introduced during the
6034 // replacement process.
6035 SmallVector<UseMemo, 4> Uses;
6036 for (unsigned i = 0; i != Num; ++i) {
6037 unsigned FromResNo = From[i].getResNo();
6038 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006039 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006040 E = FromNode->use_end(); UI != E; ++UI) {
6041 SDUse &Use = UI.getUse();
6042 if (Use.getResNo() == FromResNo) {
6043 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006044 Uses.push_back(Memo);
6045 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006046 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006047 }
Dan Gohman17059682008-07-17 19:10:17 +00006048
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006049 // Sort the uses, so that all the uses from a given User are together.
6050 std::sort(Uses.begin(), Uses.end());
6051
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006052 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6053 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006054 // We know that this user uses some value of From. If it is the right
6055 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006056 SDNode *User = Uses[UseIndex].User;
6057
6058 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006059 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006060
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006061 // The Uses array is sorted, so all the uses for a given User
6062 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006063 // To help reduce the number of CSE recomputations, process all
6064 // the uses of this user that we can find this way.
6065 do {
6066 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006067 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006068 ++UseIndex;
6069
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006070 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006071 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6072
Dan Gohman17059682008-07-17 19:10:17 +00006073 // Now that we have modified User, add it back to the CSE maps. If it
6074 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006075 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006076 }
6077}
6078
Evan Cheng9631a602006-08-01 08:20:41 +00006079/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006080/// based on their topological order. It returns the maximum id and a vector
6081/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006082unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006083
Dan Gohman86aa16a2008-09-30 18:30:35 +00006084 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006085
Dan Gohman86aa16a2008-09-30 18:30:35 +00006086 // SortedPos tracks the progress of the algorithm. Nodes before it are
6087 // sorted, nodes after it are unsorted. When the algorithm completes
6088 // it is at the end of the list.
6089 allnodes_iterator SortedPos = allnodes_begin();
6090
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006091 // Visit all the nodes. Move nodes with no operands to the front of
6092 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006093 // operand count. Before we do this, the Node Id fields of the nodes
6094 // may contain arbitrary values. After, the Node Id fields for nodes
6095 // before SortedPos will contain the topological sort index, and the
6096 // Node Id fields for nodes At SortedPos and after will contain the
6097 // count of outstanding operands.
6098 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6099 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006100 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006101 unsigned Degree = N->getNumOperands();
6102 if (Degree == 0) {
6103 // A node with no uses, add it to the result array immediately.
6104 N->setNodeId(DAGSize++);
6105 allnodes_iterator Q = N;
6106 if (Q != SortedPos)
6107 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006108 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006109 ++SortedPos;
6110 } else {
6111 // Temporarily use the Node Id as scratch space for the degree count.
6112 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006113 }
6114 }
6115
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006116 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006117 // such that by the time the end is reached all nodes will be sorted.
6118 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6119 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006120 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006121 // N is in sorted position, so all its uses have one less operand
6122 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006123 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6124 UI != UE; ++UI) {
6125 SDNode *P = *UI;
6126 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006127 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006128 --Degree;
6129 if (Degree == 0) {
6130 // All of P's operands are sorted, so P may sorted now.
6131 P->setNodeId(DAGSize++);
6132 if (P != SortedPos)
6133 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006134 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006135 ++SortedPos;
6136 } else {
6137 // Update P's outstanding operand count.
6138 P->setNodeId(Degree);
6139 }
6140 }
David Greene3b2a68c2010-01-20 00:59:23 +00006141 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006142#ifndef NDEBUG
6143 SDNode *S = ++I;
6144 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006145 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006146 dbgs() << "Checking if this is due to cycles\n";
6147 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006148#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006149 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006150 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006151 }
6152
6153 assert(SortedPos == AllNodes.end() &&
6154 "Topological sort incomplete!");
6155 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6156 "First node in topological sort is not the entry token!");
6157 assert(AllNodes.front().getNodeId() == 0 &&
6158 "First node in topological sort has non-zero id!");
6159 assert(AllNodes.front().getNumOperands() == 0 &&
6160 "First node in topological sort has operands!");
6161 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6162 "Last node in topologic sort has unexpected id!");
6163 assert(AllNodes.back().use_empty() &&
6164 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006165 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006166 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006167}
6168
Evan Cheng563fe3c2010-03-25 01:38:16 +00006169/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6170/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006171void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6172 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006173 if (SD)
6174 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006175}
Evan Cheng29eefc12006-07-27 06:39:06 +00006176
Devang Patelefc6b162011-01-25 23:27:42 +00006177/// TransferDbgValues - Transfer SDDbgValues.
6178void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6179 if (From == To || !From.getNode()->getHasDebugValue())
6180 return;
6181 SDNode *FromNode = From.getNode();
6182 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006183 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006184 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006185 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006186 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006187 SDDbgValue *Dbg = *I;
6188 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6189 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
Adrian Prantl32da8892014-04-25 20:49:25 +00006190 Dbg->isIndirect(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006191 Dbg->getOffset(), Dbg->getDebugLoc(),
6192 Dbg->getOrder());
6193 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006194 }
6195 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006196 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006197 E = ClonedDVs.end(); I != E; ++I)
6198 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006199}
6200
Jim Laskeyd66e6162005-08-17 20:08:02 +00006201//===----------------------------------------------------------------------===//
6202// SDNode Class
6203//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006204
Chris Lattner3bf17b62007-02-04 02:41:42 +00006205HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006206 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006207}
6208
Andrew Trickef9de2a2013-05-25 02:42:55 +00006209GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6210 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006211 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006212 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006213 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006214}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006215
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006216AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6217 SDValue X, unsigned SrcAS,
6218 unsigned DestAS)
6219 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6220 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6221
Andrew Trickef9de2a2013-05-25 02:42:55 +00006222MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6223 EVT memvt, MachineMemOperand *mmo)
6224 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006225 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006226 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006227 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006228 assert(isNonTemporal() == MMO->isNonTemporal() &&
6229 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006230 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006231}
6232
Andrew Trickef9de2a2013-05-25 02:42:55 +00006233MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006234 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6235 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006236 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006237 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006238 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006239 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6240 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006241}
6242
Jim Laskeyf576b422006-10-27 23:46:08 +00006243/// Profile - Gather unique data for the node.
6244///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006245void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006246 AddNodeIDNode(ID, this);
6247}
6248
Owen Anderson3b1665e2009-08-25 22:27:22 +00006249namespace {
6250 struct EVTArray {
6251 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006252
Owen Anderson3b1665e2009-08-25 22:27:22 +00006253 EVTArray() {
6254 VTs.reserve(MVT::LAST_VALUETYPE);
6255 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6256 VTs.push_back(MVT((MVT::SimpleValueType)i));
6257 }
6258 };
6259}
6260
Owen Anderson53aa7a92009-08-10 22:56:29 +00006261static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006262static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006263static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006264
Chris Lattner88fa11c2005-11-08 23:30:28 +00006265/// getValueTypeList - Return a pointer to the specified value type.
6266///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006267const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006268 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006269 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006270 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006271 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006272 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006273 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006274 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006275 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006276}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006277
Chris Lattner40e79822005-01-12 18:37:47 +00006278/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6279/// indicated value. This method ignores uses of other values defined by this
6280/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006281bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006282 assert(Value < getNumValues() && "Bad value!");
6283
Roman Levenstein51f532f2008-04-07 10:06:32 +00006284 // TODO: Only iterate over uses of a given value of the node
6285 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006286 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006287 if (NUses == 0)
6288 return false;
6289 --NUses;
6290 }
Chris Lattner40e79822005-01-12 18:37:47 +00006291 }
6292
6293 // Found exactly the right number of uses?
6294 return NUses == 0;
6295}
6296
6297
Evan Cheng358c3d12007-08-02 05:29:38 +00006298/// hasAnyUseOfValue - Return true if there are any use of the indicated
6299/// value. This method ignores uses of other values defined by this operation.
6300bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6301 assert(Value < getNumValues() && "Bad value!");
6302
Dan Gohman7a510c22008-07-09 22:39:01 +00006303 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006304 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006305 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006306
6307 return false;
6308}
6309
6310
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006311/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006312///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006313bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006314 bool Seen = false;
6315 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006316 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006317 if (User == this)
6318 Seen = true;
6319 else
6320 return false;
6321 }
6322
6323 return Seen;
6324}
6325
Evan Cheng9456dd82006-11-03 07:31:32 +00006326/// isOperand - Return true if this node is an operand of N.
6327///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006328bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006329 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6330 if (*this == N->getOperand(i))
6331 return true;
6332 return false;
6333}
6334
Evan Cheng567d2e52008-03-04 00:41:45 +00006335bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006336 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006337 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006338 return true;
6339 return false;
6340}
Evan Chengd37645c2006-02-05 06:29:23 +00006341
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006342/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006343/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006344/// side-effecting instructions on any chain path. In practice, this looks
6345/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006346/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006347bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006348 unsigned Depth) const {
6349 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006350
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006351 // Don't search too deeply, we just want to be able to see through
6352 // TokenFactor's etc.
6353 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006354
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006355 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006356 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006357 if (getOpcode() == ISD::TokenFactor) {
6358 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006359 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6360 return false;
6361 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006362 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006363
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006364 // Loads don't have side effects, look through them.
6365 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6366 if (!Ld->isVolatile())
6367 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6368 }
6369 return false;
6370}
6371
Lang Hames5a004992011-07-07 04:31:51 +00006372/// hasPredecessor - Return true if N is a predecessor of this node.
6373/// N is either an operand of this node, or can be reached by recursively
6374/// traversing up the operands.
6375/// NOTE: This is an expensive method. Use it carefully.
6376bool SDNode::hasPredecessor(const SDNode *N) const {
6377 SmallPtrSet<const SDNode *, 32> Visited;
6378 SmallVector<const SDNode *, 16> Worklist;
6379 return hasPredecessorHelper(N, Visited, Worklist);
6380}
Dan Gohmancd139c02009-10-28 03:44:30 +00006381
Craig Topperb94011f2013-07-14 04:42:23 +00006382bool
6383SDNode::hasPredecessorHelper(const SDNode *N,
6384 SmallPtrSet<const SDNode *, 32> &Visited,
6385 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006386 if (Visited.empty()) {
6387 Worklist.push_back(this);
6388 } else {
6389 // Take a look in the visited set. If we've already encountered this node
6390 // we needn't search further.
6391 if (Visited.count(N))
6392 return true;
6393 }
6394
6395 // Haven't visited N yet. Continue the search.
6396 while (!Worklist.empty()) {
6397 const SDNode *M = Worklist.pop_back_val();
6398 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6399 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006400 if (Visited.insert(Op))
6401 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006402 if (Op == N)
6403 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006404 }
Lang Hames5a004992011-07-07 04:31:51 +00006405 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006406
6407 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006408}
6409
Evan Cheng5d9fd972006-10-04 00:56:09 +00006410uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6411 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006412 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006413}
6414
Mon P Wang32f8bb92009-11-30 02:42:02 +00006415SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6416 assert(N->getNumValues() == 1 &&
6417 "Can't unroll a vector with multiple results!");
6418
6419 EVT VT = N->getValueType(0);
6420 unsigned NE = VT.getVectorNumElements();
6421 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006422 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006423
6424 SmallVector<SDValue, 8> Scalars;
6425 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6426
6427 // If ResNE is 0, fully unroll the vector op.
6428 if (ResNE == 0)
6429 ResNE = NE;
6430 else if (NE > ResNE)
6431 NE = ResNE;
6432
6433 unsigned i;
6434 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006435 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006436 SDValue Operand = N->getOperand(j);
6437 EVT OperandVT = Operand.getValueType();
6438 if (OperandVT.isVector()) {
6439 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006440 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006441 EVT OperandEltVT = OperandVT.getVectorElementType();
6442 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6443 OperandEltVT,
6444 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006445 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006446 } else {
6447 // A scalar operand; just use it as is.
6448 Operands[j] = Operand;
6449 }
6450 }
6451
6452 switch (N->getOpcode()) {
6453 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006454 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006455 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006456 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006457 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006458 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006459 case ISD::SHL:
6460 case ISD::SRA:
6461 case ISD::SRL:
6462 case ISD::ROTL:
6463 case ISD::ROTR:
6464 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006465 getShiftAmountOperand(Operands[0].getValueType(),
6466 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006467 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006468 case ISD::SIGN_EXTEND_INREG:
6469 case ISD::FP_ROUND_INREG: {
6470 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6471 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6472 Operands[0],
6473 getValueType(ExtVT)));
6474 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006475 }
6476 }
6477
6478 for (; i < ResNE; ++i)
6479 Scalars.push_back(getUNDEF(EltVT));
6480
6481 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006482 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006483}
6484
Evan Chengf5938d52009-12-09 01:36:00 +00006485
Wesley Peck527da1b2010-11-23 03:31:01 +00006486/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6487/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006488/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006489bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006490 unsigned Bytes, int Dist) const {
6491 if (LD->getChain() != Base->getChain())
6492 return false;
6493 EVT VT = LD->getValueType(0);
6494 if (VT.getSizeInBits() / 8 != Bytes)
6495 return false;
6496
6497 SDValue Loc = LD->getOperand(1);
6498 SDValue BaseLoc = Base->getOperand(1);
6499 if (Loc.getOpcode() == ISD::FrameIndex) {
6500 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6501 return false;
6502 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6503 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6504 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6505 int FS = MFI->getObjectSize(FI);
6506 int BFS = MFI->getObjectSize(BFI);
6507 if (FS != BFS || FS != (int)Bytes) return false;
6508 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6509 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006510
6511 // Handle X+C
6512 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6513 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6514 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006515
Craig Topperc0196b12014-04-14 00:51:57 +00006516 const GlobalValue *GV1 = nullptr;
6517 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006518 int64_t Offset1 = 0;
6519 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006520 const TargetLowering *TLI = TM.getTargetLowering();
6521 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6522 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006523 if (isGA1 && isGA2 && GV1 == GV2)
6524 return Offset1 == (Offset2 + Dist*Bytes);
6525 return false;
6526}
6527
6528
Evan Cheng34a23ea2009-12-09 01:04:59 +00006529/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6530/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006531unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006532 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006533 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006534 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006535 const TargetLowering *TLI = TM.getTargetLowering();
6536 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006537 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006538 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +00006539 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6540 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006541 unsigned AlignBits = KnownZero.countTrailingOnes();
6542 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6543 if (Align)
6544 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006545 }
Evan Chengd938faf2009-12-09 01:53:58 +00006546
Evan Cheng34a23ea2009-12-09 01:04:59 +00006547 // If this is a direct reference to a stack slot, use information about the
6548 // stack slot's alignment.
6549 int FrameIdx = 1 << 31;
6550 int64_t FrameOffset = 0;
6551 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6552 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006553 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006554 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006555 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006556 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6557 FrameOffset = Ptr.getConstantOperandVal(1);
6558 }
6559
6560 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006561 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006562 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6563 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006564 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006565 }
6566
6567 return 0;
6568}
6569
Juergen Ributzkab3487102013-11-19 21:20:17 +00006570/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6571/// which is split (or expanded) into two not necessarily identical pieces.
6572std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6573 // Currently all types are split in half.
6574 EVT LoVT, HiVT;
6575 if (!VT.isVector()) {
6576 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6577 } else {
6578 unsigned NumElements = VT.getVectorNumElements();
6579 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6580 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6581 NumElements/2);
6582 }
6583 return std::make_pair(LoVT, HiVT);
6584}
6585
6586/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6587/// low/high part.
6588std::pair<SDValue, SDValue>
6589SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6590 const EVT &HiVT) {
6591 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6592 N.getValueType().getVectorNumElements() &&
6593 "More vector elements requested than available!");
6594 SDValue Lo, Hi;
6595 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6596 getConstant(0, TLI->getVectorIdxTy()));
6597 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6598 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6599 return std::make_pair(Lo, Hi);
6600}
6601
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006602void SelectionDAG::ExtractVectorElements(SDValue Op,
6603 SmallVectorImpl<SDValue> &Args,
6604 unsigned Start, unsigned Count) {
6605 EVT VT = Op.getValueType();
6606 if (Count == 0)
6607 Count = VT.getVectorNumElements();
6608
6609 EVT EltVT = VT.getVectorElementType();
6610 EVT IdxTy = TLI->getVectorIdxTy();
6611 SDLoc SL(Op);
6612 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6613 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6614 Op, getConstant(i, IdxTy)));
6615 }
6616}
6617
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006618// getAddressSpace - Return the address space this GlobalAddress belongs to.
6619unsigned GlobalAddressSDNode::getAddressSpace() const {
6620 return getGlobal()->getType()->getAddressSpace();
6621}
6622
6623
Chris Lattner229907c2011-07-18 04:54:35 +00006624Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006625 if (isMachineConstantPoolEntry())
6626 return Val.MachineCPVal->getType();
6627 return Val.ConstVal->getType();
6628}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006629
Bob Wilson85cefe82009-03-02 23:24:16 +00006630bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6631 APInt &SplatUndef,
6632 unsigned &SplatBitSize,
6633 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006634 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006635 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006636 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006637 assert(VT.isVector() && "Expected a vector type");
6638 unsigned sz = VT.getSizeInBits();
6639 if (MinSplatBits > sz)
6640 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006641
Bob Wilson85cefe82009-03-02 23:24:16 +00006642 SplatValue = APInt(sz, 0);
6643 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006644
Bob Wilson85cefe82009-03-02 23:24:16 +00006645 // Get the bits. Bits with undefined values (when the corresponding element
6646 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6647 // in SplatValue. If any of the values are not constant, give up and return
6648 // false.
6649 unsigned int nOps = getNumOperands();
6650 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6651 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006652
6653 for (unsigned j = 0; j < nOps; ++j) {
6654 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006655 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006656 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006657
Bob Wilson85cefe82009-03-02 23:24:16 +00006658 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006659 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006660 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006661 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006662 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006663 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006664 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006665 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006666 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006667 }
6668
Bob Wilson85cefe82009-03-02 23:24:16 +00006669 // The build_vector is all constants or undefs. Find the smallest element
6670 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006671
Bob Wilson85cefe82009-03-02 23:24:16 +00006672 HasAnyUndefs = (SplatUndef != 0);
6673 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006674
Bob Wilson85cefe82009-03-02 23:24:16 +00006675 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006676 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6677 APInt LowValue = SplatValue.trunc(HalfSize);
6678 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6679 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006680
Bob Wilson85cefe82009-03-02 23:24:16 +00006681 // If the two halves do not match (ignoring undef bits), stop here.
6682 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6683 MinSplatBits > HalfSize)
6684 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006685
Bob Wilson85cefe82009-03-02 23:24:16 +00006686 SplatValue = HighValue | LowValue;
6687 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006688
Bob Wilson85cefe82009-03-02 23:24:16 +00006689 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006690 }
6691
Bob Wilson85cefe82009-03-02 23:24:16 +00006692 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006693 return true;
6694}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006695
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006696SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6697 if (UndefElements) {
6698 UndefElements->clear();
6699 UndefElements->resize(getNumOperands());
6700 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006701 SDValue Splatted;
6702 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6703 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006704 if (Op.getOpcode() == ISD::UNDEF) {
6705 if (UndefElements)
6706 (*UndefElements)[i] = true;
6707 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006708 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006709 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006710 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006711 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006712 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006713
Chandler Carruthb844e722014-07-08 07:19:55 +00006714 if (!Splatted) {
6715 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6716 "Can only have a splat without a constant for all undefs.");
6717 return getOperand(0);
6718 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006719
Chandler Carruthb844e722014-07-08 07:19:55 +00006720 return Splatted;
6721}
6722
6723ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006724BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006725 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006726 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006727}
6728
6729ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006730BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006731 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006732 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006733}
6734
Juergen Ributzka73844052014-01-13 20:51:35 +00006735bool BuildVectorSDNode::isConstant() const {
6736 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6737 unsigned Opc = getOperand(i).getOpcode();
6738 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6739 return false;
6740 }
6741 return true;
6742}
6743
Owen Anderson53aa7a92009-08-10 22:56:29 +00006744bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006745 // Find the first non-undef value in the shuffle mask.
6746 unsigned i, e;
6747 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6748 /* search */;
6749
Nate Begeman39b59db2009-04-29 18:13:31 +00006750 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006751
Nate Begeman5f829d82009-04-29 05:20:52 +00006752 // Make sure all remaining elements are either undef or the same as the first
6753 // non-undef value.
6754 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006755 if (Mask[i] >= 0 && Mask[i] != Idx)
6756 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006757 return true;
6758}
David Greene09851602010-01-20 20:13:31 +00006759
Adam Nemetb4690e32014-05-31 16:23:20 +00006760#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00006761static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006762 SmallPtrSet<const SDNode*, 32> &Visited,
Adam Nemet7d394302014-05-31 16:23:17 +00006763 SmallPtrSet<const SDNode*, 32> &Checked,
6764 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006765 // If this node has already been checked, don't check it again.
6766 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006767 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006768
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006769 // If a node has already been visited on this depth-first walk, reject it as
6770 // a cycle.
6771 if (!Visited.insert(N)) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006772 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006773 dbgs() << "Offending node:\n";
6774 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006775 abort();
David Greene09851602010-01-20 20:13:31 +00006776 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006777
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006778 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00006779 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00006780
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006781 Checked.insert(N);
6782 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006783}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006784#endif
David Greene09851602010-01-20 20:13:31 +00006785
Adam Nemet7d394302014-05-31 16:23:17 +00006786void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00006787 const llvm::SelectionDAG *DAG,
6788 bool force) {
6789#ifndef NDEBUG
6790 bool check = force;
David Greene09851602010-01-20 20:13:31 +00006791#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00006792 check = true;
6793#endif // XDEBUG
6794 if (check) {
6795 assert(N && "Checking nonexistent SDNode");
6796 SmallPtrSet<const SDNode*, 32> visited;
6797 SmallPtrSet<const SDNode*, 32> checked;
6798 checkForCyclesHelper(N, visited, checked, DAG);
6799 }
6800#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00006801}
6802
Adam Nemetb4690e32014-05-31 16:23:20 +00006803void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6804 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00006805}