blob: 5c89665ce665c2b11a90320acf43b5631b1fc721 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendling6de55a02009-12-21 19:34:59 +000013
Chris Lattner600d3082003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng00fd0b62010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
Bill Wendlingbd092622008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetIntrinsicInfo.h"
44#include "llvm/Target/TargetLowering.h"
45#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetOptions.h"
47#include "llvm/Target/TargetRegisterInfo.h"
48#include "llvm/Target/TargetSelectionDAGInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000049#include "llvm/Target/TargetSubtargetInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000050#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000051#include <cmath>
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +000052
Chris Lattner560b5e42004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000054
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000065
Jim Laskeyd66e6162005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000076}
77
Owen Anderson53aa7a92009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Dale Johannesend246b2c2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000089}
90
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000094
Evan Chengc70e33c2006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000098 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +000099 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Evan Chengc70e33c2006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000105
Chris Lattnerc2d28112006-03-25 22:57:01 +0000106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000109
Chris Lattnerc2d28112006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000112
Chris Lattnerc2d28112006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000114 // elements. We have to be a bit careful here, as the type of the constant
115 // may not be the same as the type of the vector elements due to type
116 // legalization (the elements are promoted to a legal type for the target and
117 // a vector of a type may be legal when the base element type is not).
118 // We only want to check enough bits to cover the vector elements, because
119 // we care if the resultant vector is all ones, not whether the individual
120 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000125 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000128 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000129 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Chris Lattnerc2d28112006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000133 // undefs. Even with the above element type twiddling, this should be OK, as
134 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Chenga6789912006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000146 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +0000147 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chenga6789912006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Kevin Qin93d45ec2014-06-24 05:37:27 +0000152 bool IsAllUndef = true;
153 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
154 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
155 continue;
156 IsAllUndef = false;
157 // Do not accept build_vectors that aren't all constants or which have non-0
158 // elements. We have to be a bit careful here, as the type of the constant
159 // may not be the same as the type of the vector elements due to type
160 // legalization (the elements are promoted to a legal type for the target
161 // and a vector of a type may be legal when the base element type is not).
162 // We only want to check enough bits to cover the vector elements, because
163 // we care if the resultant vector is all zeros, not whether the individual
164 // constants are.
165 SDValue Zero = N->getOperand(i);
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
168 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
169 return false;
170 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
171 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
172 return false;
173 } else
174 return false;
175 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000176
Evan Chengc70e33c2006-03-27 06:58:47 +0000177 // Do not accept an all-undef vector.
Kevin Qin93d45ec2014-06-24 05:37:27 +0000178 if (IsAllUndef)
Evan Chengc70e33c2006-03-27 06:58:47 +0000179 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000180 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000181}
182
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000183/// \brief Return true if the specified node is a BUILD_VECTOR node of
184/// all ConstantSDNode or undef.
185bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
186 if (N->getOpcode() != ISD::BUILD_VECTOR)
187 return false;
188
189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
190 SDValue Op = N->getOperand(i);
191 if (Op.getOpcode() == ISD::UNDEF)
192 continue;
193 if (!isa<ConstantSDNode>(Op))
194 return false;
195 }
196 return true;
197}
198
Evan Cheng6200c222008-02-18 23:04:32 +0000199/// isScalarToVector - Return true if the specified node is a
200/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
201/// element is not an undef.
202bool ISD::isScalarToVector(const SDNode *N) {
203 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
204 return true;
205
206 if (N->getOpcode() != ISD::BUILD_VECTOR)
207 return false;
208 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
209 return false;
210 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000211 if (NumElems == 1)
212 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000213 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000214 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000215 if (V.getOpcode() != ISD::UNDEF)
216 return false;
217 }
218 return true;
219}
220
Nadav Rotema62368c2012-07-15 08:38:23 +0000221/// allOperandsUndef - Return true if the node has at least one operand
222/// and all operands of the specified node are ISD::UNDEF.
223bool ISD::allOperandsUndef(const SDNode *N) {
224 // Return false if the node has no operands.
225 // This is "logically inconsistent" with the definition of "all" but
226 // is probably the desired behavior.
227 if (N->getNumOperands() == 0)
228 return false;
229
230 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
231 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
232 return false;
233
234 return true;
235}
236
Matt Arsenaultf9a995d2014-03-06 17:34:12 +0000237ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) {
238 switch (ExtType) {
239 case ISD::EXTLOAD:
240 return ISD::ANY_EXTEND;
241 case ISD::SEXTLOAD:
242 return ISD::SIGN_EXTEND;
243 case ISD::ZEXTLOAD:
244 return ISD::ZERO_EXTEND;
245 default:
246 break;
247 }
248
249 llvm_unreachable("Invalid LoadExtType");
250}
251
Chris Lattner061a1ea2005-01-07 07:46:32 +0000252/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
253/// when given the operation for (X op Y).
254ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
255 // To perform this operation, we just need to swap the L and G bits of the
256 // operation.
257 unsigned OldL = (Operation >> 2) & 1;
258 unsigned OldG = (Operation >> 1) & 1;
259 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
260 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000261 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000262}
263
264/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
265/// 'op' is a valid SetCC operation.
266ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
267 unsigned Operation = Op;
268 if (isInteger)
269 Operation ^= 7; // Flip L, G, E bits, but not U.
270 else
271 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000272
Chris Lattner061a1ea2005-01-07 07:46:32 +0000273 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000274 Operation &= ~8; // Don't let N and U bits get set.
275
Chris Lattner061a1ea2005-01-07 07:46:32 +0000276 return ISD::CondCode(Operation);
277}
278
279
280/// isSignedOp - For an integer comparison, return 1 if the comparison is a
281/// signed operation and 2 if the result is an unsigned comparison. Return zero
282/// if the operation does not depend on the sign of the input (setne and seteq).
283static int isSignedOp(ISD::CondCode Opcode) {
284 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000285 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000286 case ISD::SETEQ:
287 case ISD::SETNE: return 0;
288 case ISD::SETLT:
289 case ISD::SETLE:
290 case ISD::SETGT:
291 case ISD::SETGE: return 1;
292 case ISD::SETULT:
293 case ISD::SETULE:
294 case ISD::SETUGT:
295 case ISD::SETUGE: return 2;
296 }
297}
298
299/// getSetCCOrOperation - Return the result of a logical OR between different
300/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
301/// returns SETCC_INVALID if it is not possible to represent the resultant
302/// comparison.
303ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
304 bool isInteger) {
305 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
306 // Cannot fold a signed integer setcc with an unsigned integer setcc.
307 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000308
Chris Lattner061a1ea2005-01-07 07:46:32 +0000309 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000310
Chris Lattner061a1ea2005-01-07 07:46:32 +0000311 // If the N and U bits get set then the resultant comparison DOES suddenly
312 // care about orderedness, and is true when ordered.
313 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000314 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000315
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000316 // Canonicalize illegal integer setcc's.
317 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
318 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Chris Lattner061a1ea2005-01-07 07:46:32 +0000320 return ISD::CondCode(Op);
321}
322
323/// getSetCCAndOperation - Return the result of a logical AND between different
324/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
325/// function returns zero if it is not possible to represent the resultant
326/// comparison.
327ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
328 bool isInteger) {
329 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
330 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000331 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000332
333 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000334 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Chris Lattner393d96a2006-04-27 05:01:07 +0000336 // Canonicalize illegal integer setcc's.
337 if (isInteger) {
338 switch (Result) {
339 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000340 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000341 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000342 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
343 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
344 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000345 }
346 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000347
Chris Lattner393d96a2006-04-27 05:01:07 +0000348 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000349}
350
Jim Laskeyd66e6162005-08-17 20:08:02 +0000351//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000352// SDNode Profile Support
353//===----------------------------------------------------------------------===//
354
Jim Laskeybd0f0882006-10-27 23:52:51 +0000355/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
356///
Jim Laskeyf576b422006-10-27 23:46:08 +0000357static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
358 ID.AddInteger(OpC);
359}
360
361/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
362/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000363static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000364 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000365}
366
Jim Laskeybd0f0882006-10-27 23:52:51 +0000367/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
368///
Jim Laskeyf576b422006-10-27 23:46:08 +0000369static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000370 ArrayRef<SDValue> Ops) {
371 for (auto& Op : Ops) {
372 ID.AddPointer(Op.getNode());
373 ID.AddInteger(Op.getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000374 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000375}
376
Dan Gohman768f2c92008-07-07 18:26:29 +0000377/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
378///
379static void AddNodeIDOperands(FoldingSetNodeID &ID,
Craig Topper8c0b4d02014-04-28 05:57:50 +0000380 ArrayRef<SDUse> Ops) {
381 for (auto& Op : Ops) {
382 ID.AddPointer(Op.getNode());
383 ID.AddInteger(Op.getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000384 }
385}
386
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000387static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
388 bool exact) {
389 ID.AddBoolean(nuw);
390 ID.AddBoolean(nsw);
391 ID.AddBoolean(exact);
392}
393
394/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
395static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
396 bool nuw, bool nsw, bool exact) {
397 if (isBinOpWithFlags(Opcode))
398 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
399}
400
Craig Topper633d99b2014-04-27 23:22:43 +0000401static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
402 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000403 AddNodeIDOpcode(ID, OpC);
404 AddNodeIDValueTypes(ID, VTList);
Craig Topper8c0b4d02014-04-28 05:57:50 +0000405 AddNodeIDOperands(ID, OpList);
Jim Laskeyf576b422006-10-27 23:46:08 +0000406}
407
Duncan Sands835bdca2008-10-27 15:30:53 +0000408/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
409/// the NodeID data.
410static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000411 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000412 case ISD::TargetExternalSymbol:
413 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000414 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000415 default: break; // Normal nodes don't need extra info.
416 case ISD::TargetConstant:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000417 case ISD::Constant: {
418 const ConstantSDNode *C = cast<ConstantSDNode>(N);
419 ID.AddPointer(C->getConstantIntValue());
420 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000421 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000422 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000423 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000424 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000425 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000426 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000427 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000428 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000429 case ISD::GlobalAddress:
430 case ISD::TargetGlobalTLSAddress:
431 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000432 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000433 ID.AddPointer(GA->getGlobal());
434 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000435 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000436 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 break;
438 }
439 case ISD::BasicBlock:
440 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
441 break;
442 case ISD::Register:
443 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
444 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000445 case ISD::RegisterMask:
446 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
447 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000448 case ISD::SRCVALUE:
449 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
450 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000451 case ISD::FrameIndex:
452 case ISD::TargetFrameIndex:
453 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
454 break;
455 case ISD::JumpTable:
456 case ISD::TargetJumpTable:
457 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000458 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000459 break;
460 case ISD::ConstantPool:
461 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000462 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000463 ID.AddInteger(CP->getAlignment());
464 ID.AddInteger(CP->getOffset());
465 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000466 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000467 else
468 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000469 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000470 break;
471 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000472 case ISD::TargetIndex: {
473 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
474 ID.AddInteger(TI->getIndex());
475 ID.AddInteger(TI->getOffset());
476 ID.AddInteger(TI->getTargetFlags());
477 break;
478 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000479 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000480 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000481 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000482 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000483 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000484 break;
485 }
486 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000487 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000488 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000489 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000490 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000491 break;
492 }
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000493 case ISD::SDIV:
494 case ISD::UDIV:
495 case ISD::SRA:
496 case ISD::SRL:
497 case ISD::MUL:
498 case ISD::ADD:
499 case ISD::SUB:
500 case ISD::SHL: {
501 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
502 AddBinaryNodeIDCustom(ID, N->getOpcode(), BinNode->hasNoUnsignedWrap(),
503 BinNode->hasNoSignedWrap(), BinNode->isExact());
504 break;
505 }
Dan Gohman12f24902008-12-23 21:37:04 +0000506 case ISD::ATOMIC_CMP_SWAP:
Tim Northover420a2162014-06-13 14:24:07 +0000507 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman12f24902008-12-23 21:37:04 +0000508 case ISD::ATOMIC_SWAP:
509 case ISD::ATOMIC_LOAD_ADD:
510 case ISD::ATOMIC_LOAD_SUB:
511 case ISD::ATOMIC_LOAD_AND:
512 case ISD::ATOMIC_LOAD_OR:
513 case ISD::ATOMIC_LOAD_XOR:
514 case ISD::ATOMIC_LOAD_NAND:
515 case ISD::ATOMIC_LOAD_MIN:
516 case ISD::ATOMIC_LOAD_MAX:
517 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000518 case ISD::ATOMIC_LOAD_UMAX:
519 case ISD::ATOMIC_LOAD:
520 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000521 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000522 ID.AddInteger(AT->getMemoryVT().getRawBits());
523 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000524 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
525 break;
526 }
527 case ISD::PREFETCH: {
528 const MemSDNode *PF = cast<MemSDNode>(N);
529 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000530 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000531 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000532 case ISD::VECTOR_SHUFFLE: {
533 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000534 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000535 i != e; ++i)
536 ID.AddInteger(SVN->getMaskElt(i));
537 break;
538 }
Dan Gohman6c938802009-10-30 01:27:03 +0000539 case ISD::TargetBlockAddress:
540 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000541 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
542 ID.AddPointer(BA->getBlockAddress());
543 ID.AddInteger(BA->getOffset());
544 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000545 break;
546 }
Mon P Wang6a490372008-06-25 08:15:39 +0000547 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000548
549 // Target specific memory nodes could also have address spaces to check.
550 if (N->isTargetMemoryOpcode())
551 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000552}
553
Duncan Sands835bdca2008-10-27 15:30:53 +0000554/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
555/// data.
556static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
557 AddNodeIDOpcode(ID, N->getOpcode());
558 // Add the return value info.
559 AddNodeIDValueTypes(ID, N->getVTList());
560 // Add the operand info.
Craig Topper66e588b2014-06-29 00:40:57 +0000561 AddNodeIDOperands(ID, N->ops());
Duncan Sands835bdca2008-10-27 15:30:53 +0000562
563 // Handle SDNode leafs with special info.
564 AddNodeIDCustom(ID, N);
565}
566
Dan Gohman2da2bed2008-08-20 15:58:01 +0000567/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000568/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000569/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000570///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000571static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000572encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000573 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000574 assert((ConvType & 3) == ConvType &&
575 "ConvType may not require more than 2 bits!");
576 assert((AM & 7) == AM &&
577 "AM may not require more than 3 bits!");
578 return ConvType |
579 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000580 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000581 (isNonTemporal << 6) |
582 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000583}
584
Jim Laskeyf576b422006-10-27 23:46:08 +0000585//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000586// SelectionDAG Class
587//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000588
Duncan Sands835bdca2008-10-27 15:30:53 +0000589/// doNotCSE - Return true if CSE should not be performed for this node.
590static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000591 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000592 return true; // Never CSE anything that produces a flag.
593
594 switch (N->getOpcode()) {
595 default: break;
596 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000597 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000598 return true; // Never CSE these nodes.
599 }
600
601 // Check that remaining values produced are not flags.
602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000603 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000604 return true; // Never CSE anything that produces a flag.
605
606 return false;
607}
608
Chris Lattner9c667932005-01-07 21:09:16 +0000609/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000610/// SelectionDAG.
611void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000612 // Create a dummy node (which is not added to allnodes), that adds a reference
613 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000614 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000615
Chris Lattner8927c872006-08-04 17:45:20 +0000616 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000617
Chris Lattner8927c872006-08-04 17:45:20 +0000618 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000619 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000620 if (I->use_empty())
621 DeadNodes.push_back(I);
622
Dan Gohman91697632008-07-07 20:57:48 +0000623 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000624
Dan Gohman91697632008-07-07 20:57:48 +0000625 // If the root changed (e.g. it was a dead load, update the root).
626 setRoot(Dummy.getValue());
627}
628
629/// RemoveDeadNodes - This method deletes the unreachable nodes in the
630/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000631void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000632
Chris Lattner8927c872006-08-04 17:45:20 +0000633 // Process the worklist, deleting the nodes and adding their uses to the
634 // worklist.
635 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000636 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000637
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000638 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Craig Topperc0196b12014-04-14 00:51:57 +0000639 DUL->NodeDeleted(N, nullptr);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000640
Chris Lattner8927c872006-08-04 17:45:20 +0000641 // Take the node out of the appropriate CSE map.
642 RemoveNodeFromCSEMaps(N);
643
644 // Next, brutally remove the operand list. This is safe to do, as there are
645 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000646 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
647 SDUse &Use = *I++;
648 SDNode *Operand = Use.getNode();
649 Use.set(SDValue());
650
Chris Lattner8927c872006-08-04 17:45:20 +0000651 // Now that we removed this operand, see if there are no uses of it left.
652 if (Operand->use_empty())
653 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000654 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000655
Dan Gohman534c8a22009-01-19 22:39:36 +0000656 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000657 }
Chris Lattner9c667932005-01-07 21:09:16 +0000658}
659
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000660void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000661 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000662
663 // Create a dummy node that adds a reference to the root node, preventing
664 // it from being deleted. (This matters if the root is an operand of the
665 // dead node.)
666 HandleSDNode Dummy(getRoot());
667
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000668 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000669}
670
Chris Lattnerc738d002005-08-29 21:59:31 +0000671void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000672 // First take this out of the appropriate CSE map.
673 RemoveNodeFromCSEMaps(N);
674
Scott Michelcf0da6c2009-02-17 22:15:04 +0000675 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000676 // AllNodes list, and delete the node.
677 DeleteNodeNotInCSEMaps(N);
678}
679
680void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000681 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
682 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000683
Dan Gohman86aa16a2008-09-30 18:30:35 +0000684 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000685 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000686
Dan Gohman534c8a22009-01-19 22:39:36 +0000687 DeallocateNode(N);
688}
689
690void SelectionDAG::DeallocateNode(SDNode *N) {
691 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000692 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000693
Dan Gohman534c8a22009-01-19 22:39:36 +0000694 // Set the opcode to DELETED_NODE to help catch bugs when node
695 // memory is reallocated.
696 N->NodeType = ISD::DELETED_NODE;
697
Dan Gohman2e834902008-08-26 01:44:34 +0000698 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000699
Evan Cheng563fe3c2010-03-25 01:38:16 +0000700 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000701 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000702 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
703 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000704}
705
Chandler Carruth41b20e72014-07-22 04:07:55 +0000706#ifndef NDEBUG
707/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
708static void VerifySDNode(SDNode *N) {
709 switch (N->getOpcode()) {
710 default:
711 break;
712 case ISD::BUILD_PAIR: {
713 EVT VT = N->getValueType(0);
714 assert(N->getNumValues() == 1 && "Too many results!");
715 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
716 "Wrong return type!");
717 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
718 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
719 "Mismatched operand types!");
720 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
721 "Wrong operand type!");
722 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
723 "Wrong return type size");
724 break;
725 }
726 case ISD::BUILD_VECTOR: {
727 assert(N->getNumValues() == 1 && "Too many results!");
728 assert(N->getValueType(0).isVector() && "Wrong return type!");
729 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
730 "Wrong number of operands!");
731 EVT EltVT = N->getValueType(0).getVectorElementType();
732 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
733 assert((I->getValueType() == EltVT ||
734 (EltVT.isInteger() && I->getValueType().isInteger() &&
735 EltVT.bitsLE(I->getValueType()))) &&
736 "Wrong operand type!");
737 assert(I->getValueType() == N->getOperand(0).getValueType() &&
738 "Operands must all have the same type");
739 }
740 break;
741 }
742 }
743}
744#endif // NDEBUG
745
746/// \brief Insert a newly allocated node into the DAG.
747///
748/// Handles insertion into the all nodes list and CSE map, as well as
749/// verification and other common operations when a new node is allocated.
750void SelectionDAG::InsertNode(SDNode *N) {
751 AllNodes.push_back(N);
752#ifndef NDEBUG
753 VerifySDNode(N);
754#endif
755}
756
Chris Lattner19732782005-08-16 18:17:10 +0000757/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
758/// correspond to it. This is useful when we're about to delete or repurpose
759/// the node. We don't want future request for structurally identical nodes
760/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000761bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000762 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000763 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000764 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000765 case ISD::CONDCODE:
766 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
767 "Cond code doesn't exist!");
Craig Topperc0196b12014-04-14 00:51:57 +0000768 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
769 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000770 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000771 case ISD::ExternalSymbol:
772 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000773 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000774 case ISD::TargetExternalSymbol: {
775 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
776 Erased = TargetExternalSymbols.erase(
777 std::pair<std::string,unsigned char>(ESN->getSymbol(),
778 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000779 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000780 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000781 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000782 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000783 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000784 Erased = ExtendedValueTypeNodes.erase(VT);
785 } else {
Craig Topperc0196b12014-04-14 00:51:57 +0000786 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
787 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000788 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000789 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000790 }
Chris Lattner9c667932005-01-07 21:09:16 +0000791 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000792 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000793 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
794 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000795 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000796 break;
797 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000798#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000799 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000800 // flag result (which cannot be CSE'd) or is one of the special cases that are
801 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000802 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000803 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000804 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000805 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000806 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000807 }
808#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000809 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000810}
811
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000812/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
813/// maps and modified in place. Add it back to the CSE maps, unless an identical
814/// node already exists, in which case transfer all its users to the existing
815/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000816///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000817void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000818SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000819 // For node types that aren't CSE'd, just act as if no identical node
820 // already exists.
821 if (!doNotCSE(N)) {
822 SDNode *Existing = CSEMap.GetOrInsertNode(N);
823 if (Existing != N) {
824 // If there was already an existing matching node, use ReplaceAllUsesWith
825 // to replace the dead one with the existing one. This can cause
826 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000827 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000828
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000829 // N is now dead. Inform the listeners and delete it.
830 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
831 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000832 DeleteNodeNotInCSEMaps(N);
833 return;
834 }
835 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000836
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000837 // If the node doesn't already exist, we updated it. Inform listeners.
838 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
839 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000840}
841
Chris Lattnerf34156e2006-01-28 09:32:45 +0000842/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000843/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000844/// return null, otherwise return a pointer to the slot it would take. If a
845/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000846SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000847 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000848 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000849 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000850
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000851 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000852 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000853 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000854 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000855 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000856 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000857}
858
859/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000860/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000861/// return null, otherwise return a pointer to the slot it would take. If a
862/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000863SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000864 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000865 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000866 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000867 return nullptr;
Duncan Sands835bdca2008-10-27 15:30:53 +0000868
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000869 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000870 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +0000871 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000872 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000873 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000874 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000875}
876
877
878/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000879/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000880/// return null, otherwise return a pointer to the slot it would take. If a
881/// node already exists with these operands, the slot will be non-null.
Craig Topper8c0b4d02014-04-28 05:57:50 +0000882SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000883 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000884 if (doNotCSE(N))
Craig Topperc0196b12014-04-14 00:51:57 +0000885 return nullptr;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000886
Jim Laskeyf576b422006-10-27 23:46:08 +0000887 FoldingSetNodeID ID;
Craig Topper8c0b4d02014-04-28 05:57:50 +0000888 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands835bdca2008-10-27 15:30:53 +0000889 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000890 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000891 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000892}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000893
Owen Anderson53aa7a92009-08-10 22:56:29 +0000894/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000895/// given type.
896///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000897unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000898 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000899 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000900 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000901
Eric Christopherd9134482014-08-04 21:25:23 +0000902 return TM.getSubtargetImpl()
903 ->getTargetLowering()
904 ->getDataLayout()
905 ->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000906}
Chris Lattner9c667932005-01-07 21:09:16 +0000907
Dale Johannesen8ba713212009-02-07 02:15:05 +0000908// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000909SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Eric Christopherd9636c12014-10-08 00:32:59 +0000910 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
Eric Christopherd9134482014-08-04 21:25:23 +0000911 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
912 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
913 UpdateListeners(nullptr) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000914 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000915 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000916}
917
Eric Christopher8d07f442014-10-08 01:57:58 +0000918void SelectionDAG::init(MachineFunction &mf) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000919 MF = &mf;
Eric Christopher8d07f442014-10-08 01:57:58 +0000920 TLI = getSubtarget().getTargetLowering();
Eric Christopherd9636c12014-10-08 00:32:59 +0000921 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopherdfda92b2009-08-22 00:40:45 +0000922 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000923}
924
Chris Lattner600d3082003-08-11 14:57:33 +0000925SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000926 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000927 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000928 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000929}
930
931void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000932 assert(&*AllNodes.begin() == &EntryNode);
933 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000934 while (!AllNodes.empty())
935 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000936}
937
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +0000938BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
939 SDVTList VTs, SDValue N1,
940 SDValue N2, bool nuw, bool nsw,
941 bool exact) {
942 if (isBinOpWithFlags(Opcode)) {
943 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
944 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
945 FN->setHasNoUnsignedWrap(nuw);
946 FN->setHasNoSignedWrap(nsw);
947 FN->setIsExact(exact);
948
949 return FN;
950 }
951
952 BinarySDNode *N = new (NodeAllocator)
953 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
954 return N;
955}
956
Dan Gohmane1a9a782008-08-27 23:52:12 +0000957void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000958 allnodes_clear();
959 OperandAllocator.Reset();
960 CSEMap.clear();
961
962 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000963 ExternalSymbols.clear();
964 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000965 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000966 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000967 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Craig Topperc0196b12014-04-14 00:51:57 +0000968 static_cast<SDNode*>(nullptr));
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000969
Craig Topperc0196b12014-04-14 00:51:57 +0000970 EntryNode.UseList = nullptr;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000971 AllNodes.push_back(&EntryNode);
972 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000973 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000974}
975
Andrew Trickef9de2a2013-05-25 02:42:55 +0000976SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000977 return VT.bitsGT(Op.getValueType()) ?
978 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
979 getNode(ISD::TRUNCATE, DL, VT, Op);
980}
981
Andrew Trickef9de2a2013-05-25 02:42:55 +0000982SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000983 return VT.bitsGT(Op.getValueType()) ?
984 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
985 getNode(ISD::TRUNCATE, DL, VT, Op);
986}
987
Andrew Trickef9de2a2013-05-25 02:42:55 +0000988SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000989 return VT.bitsGT(Op.getValueType()) ?
990 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
991 getNode(ISD::TRUNCATE, DL, VT, Op);
992}
993
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000994SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
995 EVT OpVT) {
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +0000996 if (VT.bitsLE(Op.getValueType()))
997 return getNode(ISD::TRUNCATE, SL, VT, Op);
998
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000999 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Matt Arsenault5f2fd4b2014-05-07 18:26:58 +00001000 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1001}
1002
Andrew Trickef9de2a2013-05-25 02:42:55 +00001003SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +00001004 assert(!VT.isVector() &&
1005 "getZeroExtendInReg should use the vector element type instead of "
1006 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +00001007 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +00001008 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1009 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +00001010 VT.getSizeInBits());
1011 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1012 getConstant(Imm, Op.getValueType()));
1013}
1014
Chandler Carruth0b666e02014-07-10 12:32:32 +00001015SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1016 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1017 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1018 "The sizes of the input and result must match in order to perform the "
1019 "extend in-register.");
1020 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1021 "The destination vector type must have fewer lanes than the input.");
1022 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1023}
1024
1025SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1026 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1027 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1028 "The sizes of the input and result must match in order to perform the "
1029 "extend in-register.");
1030 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1031 "The destination vector type must have fewer lanes than the input.");
1032 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1033}
1034
Chandler Carruthafe4b252014-07-09 10:58:18 +00001035SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1036 assert(VT.isVector() && "This DAG node is restricted to vector types.");
Chandler Carruth5865a732014-07-09 12:36:54 +00001037 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1038 "The sizes of the input and result must match in order to perform the "
1039 "extend in-register.");
Chandler Carruthafe4b252014-07-09 10:58:18 +00001040 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1041 "The destination vector type must have fewer lanes than the input.");
1042 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1043}
1044
Bob Wilsonc5890052009-01-22 17:39:32 +00001045/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1046///
Andrew Trickef9de2a2013-05-25 02:42:55 +00001047SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001048 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +00001049 SDValue NegOne =
1050 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +00001051 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1052}
1053
Pete Cooper7fd1d722014-05-12 23:26:58 +00001054SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1055 EVT EltVT = VT.getScalarType();
1056 SDValue TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001057 switch (TLI->getBooleanContents(VT)) {
Pete Cooper7fd1d722014-05-12 23:26:58 +00001058 case TargetLowering::ZeroOrOneBooleanContent:
1059 case TargetLowering::UndefinedBooleanContent:
1060 TrueValue = getConstant(1, VT);
1061 break;
1062 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1063 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1064 VT);
1065 break;
1066 }
1067 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1068}
1069
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001070SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001071 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +00001072 assert((EltVT.getSizeInBits() >= 64 ||
1073 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1074 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001075 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +00001076}
1077
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001078SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1079{
1080 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001081}
1082
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001083SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1084 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001085 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +00001086
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001087 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +00001088 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +00001089
Eric Christopherd9134482014-08-04 21:25:23 +00001090 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001091
Duncan Sandsf2641e12011-09-06 19:07:46 +00001092 // In some cases the vector type is legal but the element type is illegal and
1093 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1094 // inserted value (the type does not need to match the vector element type).
1095 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001096 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001097 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001098 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001099 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1100 Elt = ConstantInt::get(*getContext(), NewVal);
1101 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001102 // In other cases the element type is illegal and needs to be expanded, for
1103 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1104 // the value into n parts and use a vector type with n-times the elements.
1105 // Then bitcast to the type requested.
1106 // Legalizing constants too early makes the DAGCombiner's job harder so we
1107 // only legalize if the DAG tells us we must produce legal types.
1108 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1109 TLI->getTypeAction(*getContext(), EltVT) ==
1110 TargetLowering::TypeExpandInteger) {
1111 APInt NewVal = Elt->getValue();
1112 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1113 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1114 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1115 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1116
1117 // Check the temporary vector is the correct size. If this fails then
1118 // getTypeToTransformTo() probably returned a type whose size (in bits)
1119 // isn't a power-of-2 factor of the requested type size.
1120 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1121
1122 SmallVector<SDValue, 2> EltParts;
1123 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1124 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1125 .trunc(ViaEltSizeInBits),
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001126 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001127 }
1128
1129 // EltParts is currently in little endian order. If we actually want
1130 // big-endian order then reverse it now.
1131 if (TLI->isBigEndian())
1132 std::reverse(EltParts.begin(), EltParts.end());
1133
1134 // The elements must be reversed when the element order is different
1135 // to the endianness of the elements (because the BITCAST is itself a
1136 // vector shuffle in this situation). However, we do not need any code to
1137 // perform this reversal because getConstant() is producing a vector
1138 // splat.
1139 // This situation occurs in MIPS MSA.
1140
1141 SmallVector<SDValue, 8> Ops;
1142 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1143 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1144
1145 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1146 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Craig Topper48d114b2014-04-26 18:35:24 +00001147 Ops));
Daniel Sanders50b80412013-11-15 12:56:49 +00001148 return Result;
1149 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001150
1151 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1152 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001153 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001154 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001155 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001156 ID.AddPointer(Elt);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001157 ID.AddBoolean(isO);
Craig Topperc0196b12014-04-14 00:51:57 +00001158 void *IP = nullptr;
1159 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001160 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001161 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001162 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001163
Dan Gohman7a7742c2007-12-12 22:21:26 +00001164 if (!N) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001165 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001166 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001167 InsertNode(N);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001168 }
1169
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001170 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001171 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001172 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001173 Ops.assign(VT.getVectorNumElements(), Result);
Craig Topper48d114b2014-04-26 18:35:24 +00001174 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001175 }
1176 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001177}
1178
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001179SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Eric Christopherd9134482014-08-04 21:25:23 +00001180 return getConstant(Val,
1181 TM.getSubtargetImpl()->getTargetLowering()->getPointerTy(),
1182 isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001183}
1184
1185
Owen Anderson53aa7a92009-08-10 22:56:29 +00001186SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001187 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001188}
1189
Owen Anderson53aa7a92009-08-10 22:56:29 +00001190SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001191 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001192
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001193 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001194
Chris Lattner381dddc2005-02-17 20:17:32 +00001195 // Do the map lookup using the actual bit pattern for the floating point
1196 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1197 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001198 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001199 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001200 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001201 ID.AddPointer(&V);
Craig Topperc0196b12014-04-14 00:51:57 +00001202 void *IP = nullptr;
1203 SDNode *N = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001204 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001205 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001206 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001207
Evan Cheng9458e6a2007-06-29 21:36:04 +00001208 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001209 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001210 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001211 InsertNode(N);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001212 }
1213
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001214 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001215 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001216 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001217 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001218 // FIXME SDLoc info might be appropriate here
Craig Topper48d114b2014-04-26 18:35:24 +00001219 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohmana8665142007-06-25 16:23:39 +00001220 }
1221 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001222}
1223
Owen Anderson53aa7a92009-08-10 22:56:29 +00001224SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001225 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001226 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001227 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001228 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001229 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001230 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1231 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001232 bool ignored;
1233 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001234 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001235 &ignored);
1236 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001237 } else
1238 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001239}
1240
Andrew Trickef9de2a2013-05-25 02:42:55 +00001241SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001242 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001243 bool isTargetGA,
1244 unsigned char TargetFlags) {
1245 assert((TargetFlags == 0 || isTargetGA) &&
1246 "Cannot set target flags on target-independent globals");
Eric Christopherd9134482014-08-04 21:25:23 +00001247 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001248
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001249 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001250 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001251 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001252 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001253
Chris Lattner8e34f982009-06-25 21:21:14 +00001254 unsigned Opc;
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001255 if (GV->isThreadLocal())
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001256 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1257 else
1258 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001259
Jim Laskeyf576b422006-10-27 23:46:08 +00001260 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001261 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001262 ID.AddPointer(GV);
1263 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001264 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001265 ID.AddInteger(GV->getType()->getAddressSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00001266 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001267 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001268 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001269
Andrew Trickef9de2a2013-05-25 02:42:55 +00001270 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1271 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001272 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001273 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001274 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001275 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001276}
1277
Owen Anderson53aa7a92009-08-10 22:56:29 +00001278SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001279 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001280 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001281 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001282 ID.AddInteger(FI);
Craig Topperc0196b12014-04-14 00:51:57 +00001283 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001284 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001285 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001286
Dan Gohman01c65a22010-03-18 18:49:47 +00001287 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001288 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001289 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001290 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001291}
1292
Owen Anderson53aa7a92009-08-10 22:56:29 +00001293SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001294 unsigned char TargetFlags) {
1295 assert((TargetFlags == 0 || isTarget) &&
1296 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001297 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001298 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001299 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001300 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001301 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001302 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001304 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001305
Dan Gohman01c65a22010-03-18 18:49:47 +00001306 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1307 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001308 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001309 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001310 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001311}
1312
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001313SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001314 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001315 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001316 unsigned char TargetFlags) {
1317 assert((TargetFlags == 0 || isTarget) &&
1318 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001319 if (Alignment == 0)
Eric Christopherd9134482014-08-04 21:25:23 +00001320 Alignment = TM.getSubtargetImpl()
1321 ->getTargetLowering()
1322 ->getDataLayout()
1323 ->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001324 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001325 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001326 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001327 ID.AddInteger(Alignment);
1328 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001329 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001330 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001331 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001332 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001333 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001334
Dan Gohman01c65a22010-03-18 18:49:47 +00001335 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1336 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001337 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001338 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001339 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001340}
1341
Chris Lattner061a1ea2005-01-07 07:46:32 +00001342
Owen Anderson53aa7a92009-08-10 22:56:29 +00001343SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001344 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001345 bool isTarget,
1346 unsigned char TargetFlags) {
1347 assert((TargetFlags == 0 || isTarget) &&
1348 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001349 if (Alignment == 0)
Eric Christopherd9134482014-08-04 21:25:23 +00001350 Alignment = TM.getSubtargetImpl()
1351 ->getTargetLowering()
1352 ->getDataLayout()
1353 ->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001354 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001355 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001356 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001357 ID.AddInteger(Alignment);
1358 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001359 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001360 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001361 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001362 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001363 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001364
Dan Gohman01c65a22010-03-18 18:49:47 +00001365 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1366 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001367 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001368 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001369 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001370}
1371
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001372SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1373 unsigned char TargetFlags) {
1374 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001375 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001376 ID.AddInteger(Index);
1377 ID.AddInteger(Offset);
1378 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001379 void *IP = nullptr;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001380 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1381 return SDValue(E, 0);
1382
1383 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1384 TargetFlags);
1385 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001386 InsertNode(N);
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001387 return SDValue(N, 0);
1388}
1389
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001390SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001391 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001392 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001393 ID.AddPointer(MBB);
Craig Topperc0196b12014-04-14 00:51:57 +00001394 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001396 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001397
Dan Gohman01c65a22010-03-18 18:49:47 +00001398 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001399 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001400 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001401 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001402}
1403
Owen Anderson53aa7a92009-08-10 22:56:29 +00001404SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001405 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1406 ValueTypeNodes.size())
1407 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001408
Duncan Sands13237ac2008-06-06 12:08:01 +00001409 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001410 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001411
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001412 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001413 N = new (NodeAllocator) VTSDNode(VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001414 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001415 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001416}
1417
Owen Anderson53aa7a92009-08-10 22:56:29 +00001418SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001419 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001420 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001421 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001422 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001423 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001424}
1425
Owen Anderson53aa7a92009-08-10 22:56:29 +00001426SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001427 unsigned char TargetFlags) {
1428 SDNode *&N =
1429 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1430 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001431 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001432 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001433 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001434 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001435}
1436
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001437SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001438 if ((unsigned)Cond >= CondCodeNodes.size())
1439 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001440
Craig Topperc0196b12014-04-14 00:51:57 +00001441 if (!CondCodeNodes[Cond]) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001442 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001443 CondCodeNodes[Cond] = N;
Chandler Carruth41b20e72014-07-22 04:07:55 +00001444 InsertNode(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001445 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001446
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001447 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001448}
1449
Nate Begeman5f829d82009-04-29 05:20:52 +00001450// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1451// the shuffle mask M that point at N1 to point at N2, and indices that point
1452// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001453static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1454 std::swap(N1, N2);
1455 int NElts = M.size();
1456 for (int i = 0; i != NElts; ++i) {
1457 if (M[i] >= NElts)
1458 M[i] -= NElts;
1459 else if (M[i] >= 0)
1460 M[i] += NElts;
1461 }
1462}
1463
Andrew Trickef9de2a2013-05-25 02:42:55 +00001464SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001465 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001466 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1467 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001468
1469 // Canonicalize shuffle undef, undef -> undef
1470 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001471 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001472
Eric Christopherdfda92b2009-08-22 00:40:45 +00001473 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001474 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001475 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001476 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001477 for (unsigned i = 0; i != NElts; ++i) {
1478 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001479 MaskVec.push_back(Mask[i]);
1480 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001481
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001482 // Canonicalize shuffle v, v -> v, undef
1483 if (N1 == N2) {
1484 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001485 for (unsigned i = 0; i != NElts; ++i)
1486 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001487 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001488
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001489 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1490 if (N1.getOpcode() == ISD::UNDEF)
1491 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001492
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001493 // Canonicalize all index into lhs, -> shuffle lhs, undef
1494 // Canonicalize all index into rhs, -> shuffle rhs, undef
1495 bool AllLHS = true, AllRHS = true;
1496 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001497 for (unsigned i = 0; i != NElts; ++i) {
1498 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001499 if (N2Undef)
1500 MaskVec[i] = -1;
1501 else
1502 AllLHS = false;
1503 } else if (MaskVec[i] >= 0) {
1504 AllRHS = false;
1505 }
1506 }
1507 if (AllLHS && AllRHS)
1508 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001509 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001510 N2 = getUNDEF(VT);
1511 if (AllRHS) {
1512 N1 = getUNDEF(VT);
1513 commuteShuffle(N1, N2, MaskVec);
1514 }
Chandler Carruth142e9662014-07-08 08:45:38 +00001515 // Reset our undef status after accounting for the mask.
1516 N2Undef = N2.getOpcode() == ISD::UNDEF;
1517 // Re-check whether both sides ended up undef.
1518 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1519 return getUNDEF(VT);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001520
Craig Topper9a39b072013-08-08 08:03:12 +00001521 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001522 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001523 for (unsigned i = 0; i != NElts; ++i) {
1524 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001525 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001526 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001527 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001528
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001529 // Shuffling a constant splat doesn't change the result.
Chandler Carruth142e9662014-07-08 08:45:38 +00001530 if (N2Undef) {
1531 SDValue V = N1;
1532
1533 // Look through any bitcasts. We check that these don't change the number
1534 // (and size) of elements and just changes their types.
1535 while (V.getOpcode() == ISD::BITCAST)
1536 V = V->getOperand(0);
1537
1538 // A splat should always show up as a build vector node.
1539 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001540 BitVector UndefElements;
1541 SDValue Splat = BV->getSplatValue(&UndefElements);
Chandler Carruth142e9662014-07-08 08:45:38 +00001542 // If this is a splat of an undef, shuffling it is also undef.
1543 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1544 return getUNDEF(VT);
1545
1546 // We only have a splat which can skip shuffles if there is a splatted
1547 // value and no undef lanes rearranged by the shuffle.
Chandler Carruthf0a33b72014-07-09 00:41:34 +00001548 if (Splat && UndefElements.none()) {
Chandler Carruth142e9662014-07-08 08:45:38 +00001549 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1550 // number of elements match or the value splatted is a zero constant.
1551 if (V.getValueType().getVectorNumElements() ==
1552 VT.getVectorNumElements())
1553 return N1;
1554 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1555 if (C->isNullValue())
1556 return N1;
1557 }
1558 }
1559 }
Benjamin Kramer6bca8ef2014-04-27 11:41:06 +00001560
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001561 FoldingSetNodeID ID;
1562 SDValue Ops[2] = { N1, N2 };
Craig Topper633d99b2014-04-27 23:22:43 +00001563 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5f829d82009-04-29 05:20:52 +00001564 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001565 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001566
Craig Topperc0196b12014-04-14 00:51:57 +00001567 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001568 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001569 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001570
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001571 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1572 // SDNode doesn't have access to it. This memory will be "leaked" when
1573 // the node is deallocated, but recovered when the NodeAllocator is released.
1574 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1575 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001576
Dan Gohman01c65a22010-03-18 18:49:47 +00001577 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001578 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1579 dl.getDebugLoc(), N1, N2,
1580 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001581 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001582 InsertNode(N);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001583 return SDValue(N, 0);
1584}
1585
Andrea Di Biagio4d8bd412014-07-21 07:28:51 +00001586SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1587 MVT VT = SV.getSimpleValueType(0);
1588 unsigned NumElems = VT.getVectorNumElements();
1589 SmallVector<int, 8> MaskVec;
1590
1591 for (unsigned i = 0; i != NumElems; ++i) {
1592 int Idx = SV.getMaskElt(i);
1593 if (Idx >= 0) {
1594 if (Idx < (int)NumElems)
1595 Idx += NumElems;
1596 else
1597 Idx -= NumElems;
1598 }
1599 MaskVec.push_back(Idx);
1600 }
1601
1602 SDValue Op0 = SV.getOperand(0);
1603 SDValue Op1 = SV.getOperand(1);
1604 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1605}
1606
Andrew Trickef9de2a2013-05-25 02:42:55 +00001607SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001608 SDValue Val, SDValue DTy,
1609 SDValue STy, SDValue Rnd, SDValue Sat,
1610 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001611 // If the src and dest types are the same and the conversion is between
1612 // integer types of the same sign or two floats, no conversion is necessary.
1613 if (DTy == STy &&
1614 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001615 return Val;
1616
1617 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001618 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Craig Topper633d99b2014-04-27 23:22:43 +00001619 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00001620 void* IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001621 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001622 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001623
Jack Carter170a5f22013-09-09 22:02:08 +00001624 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1625 dl.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00001626 Ops, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001627 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001628 InsertNode(N);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001629 return SDValue(N, 0);
1630}
1631
Owen Anderson53aa7a92009-08-10 22:56:29 +00001632SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001633 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001634 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001635 ID.AddInteger(RegNo);
Craig Topperc0196b12014-04-14 00:51:57 +00001636 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001637 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001638 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001639
Dan Gohman01c65a22010-03-18 18:49:47 +00001640 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001641 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001642 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001643 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001644}
1645
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001646SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1647 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001648 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001649 ID.AddPointer(RegMask);
Craig Topperc0196b12014-04-14 00:51:57 +00001650 void *IP = nullptr;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001651 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1652 return SDValue(E, 0);
1653
1654 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1655 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001656 InsertNode(N);
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001657 return SDValue(N, 0);
1658}
1659
Andrew Trickef9de2a2013-05-25 02:42:55 +00001660SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001661 FoldingSetNodeID ID;
1662 SDValue Ops[] = { Root };
Craig Topper633d99b2014-04-27 23:22:43 +00001663 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001664 ID.AddPointer(Label);
Craig Topperc0196b12014-04-14 00:51:57 +00001665 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001666 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001667 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001668
Jack Carter170a5f22013-09-09 22:02:08 +00001669 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1670 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001671 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001672 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001673 return SDValue(N, 0);
1674}
1675
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001676
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001677SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001678 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001679 bool isTarget,
1680 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001681 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1682
1683 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001684 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman6c938802009-10-30 01:27:03 +00001685 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001686 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001687 ID.AddInteger(TargetFlags);
Craig Topperc0196b12014-04-14 00:51:57 +00001688 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001689 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001690 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001691
Michael Liaoabb87d42012-09-12 21:43:09 +00001692 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1693 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001694 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001695 InsertNode(N);
Dan Gohman6c938802009-10-30 01:27:03 +00001696 return SDValue(N, 0);
1697}
1698
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001699SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001700 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001701 "SrcValue is not a pointer?");
1702
Jim Laskeyf576b422006-10-27 23:46:08 +00001703 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001704 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner3f16b202006-08-11 21:01:22 +00001705 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001706
Craig Topperc0196b12014-04-14 00:51:57 +00001707 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00001708 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001709 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001710
Dan Gohman01c65a22010-03-18 18:49:47 +00001711 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001712 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001713 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001714 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001715}
1716
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001717/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1718SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1719 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001720 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001721 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001722
Craig Topperc0196b12014-04-14 00:51:57 +00001723 void *IP = nullptr;
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001724 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1725 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001726
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001727 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1728 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001729 InsertNode(N);
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001730 return SDValue(N, 0);
1731}
1732
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001733/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1734SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1735 unsigned SrcAS, unsigned DestAS) {
1736 SDValue Ops[] = {Ptr};
1737 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00001738 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001739 ID.AddInteger(SrcAS);
1740 ID.AddInteger(DestAS);
1741
Craig Topperc0196b12014-04-14 00:51:57 +00001742 void *IP = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001743 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1744 return SDValue(E, 0);
1745
1746 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1747 dl.getDebugLoc(),
1748 VT, Ptr, SrcAS, DestAS);
1749 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00001750 InsertNode(N);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001751 return SDValue(N, 0);
1752}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001753
Duncan Sands41826032009-01-31 15:50:11 +00001754/// getShiftAmountOperand - Return the specified value casted to
1755/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001756SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001757 EVT OpTy = Op.getValueType();
Eric Christopherd9134482014-08-04 21:25:23 +00001758 EVT ShTy =
1759 TM.getSubtargetImpl()->getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001760 if (OpTy == ShTy || OpTy.isVector()) return Op;
1761
1762 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001763 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001764}
1765
Chris Lattner9eb7a822007-10-15 17:47:20 +00001766/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1767/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001768SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001769 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001770 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001771 Type *Ty = VT.getTypeForEVT(*getContext());
Eric Christopherd9134482014-08-04 21:25:23 +00001772 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001773 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001774 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001775
David Greene1fbe0542009-11-12 20:49:22 +00001776 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001777 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001778}
1779
Duncan Sands445071c2008-12-09 21:33:20 +00001780/// CreateStackTemporary - Create a stack temporary suitable for holding
1781/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001782SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001783 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1784 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001785 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1786 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Eric Christopherd9134482014-08-04 21:25:23 +00001787 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001788 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001789 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1790 TD->getPrefTypeAlignment(Ty2));
1791
1792 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001793 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001794 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001795}
1796
Owen Anderson53aa7a92009-08-10 22:56:29 +00001797SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001798 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001799 // These setcc operations always fold.
1800 switch (Cond) {
1801 default: break;
1802 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001803 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001804 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001805 case ISD::SETTRUE2: {
Eric Christopherd9134482014-08-04 21:25:23 +00001806 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Daniel Sanderscbd44c52014-07-10 10:18:12 +00001807 TargetLowering::BooleanContent Cnt =
1808 TLI->getBooleanContents(N1->getValueType(0));
Tim Northover950fcc02013-09-06 12:38:12 +00001809 return getConstant(
1810 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1811 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001812
Chris Lattner393d96a2006-04-27 05:01:07 +00001813 case ISD::SETOEQ:
1814 case ISD::SETOGT:
1815 case ISD::SETOGE:
1816 case ISD::SETOLT:
1817 case ISD::SETOLE:
1818 case ISD::SETONE:
1819 case ISD::SETO:
1820 case ISD::SETUO:
1821 case ISD::SETUEQ:
1822 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001823 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001824 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001825 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001826
Gabor Greiff304a7a2008-08-28 21:40:38 +00001827 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001828 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001829 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001830 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001831
Chris Lattner061a1ea2005-01-07 07:46:32 +00001832 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001833 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001834 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1835 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001836 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1837 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1838 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1839 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1840 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1841 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1842 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1843 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001844 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001845 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001846 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001847 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1848 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001849 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001850 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001851 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001852 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001853 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001854 // fall through
1855 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001856 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001857 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001858 // fall through
1859 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001860 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001861 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001862 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001863 // fall through
1864 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001865 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001866 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001867 // fall through
1868 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001869 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001870 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001871 // fall through
1872 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001873 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001874 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001875 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001876 // fall through
1877 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001878 R==APFloat::cmpEqual, VT);
1879 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1880 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1881 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1882 R==APFloat::cmpEqual, VT);
1883 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1884 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1885 R==APFloat::cmpLessThan, VT);
1886 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1887 R==APFloat::cmpUnordered, VT);
1888 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1889 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001890 }
1891 } else {
1892 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001893 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1894 MVT CompVT = N1.getValueType().getSimpleVT();
Eric Christopherd9134482014-08-04 21:25:23 +00001895 if (!TM.getSubtargetImpl()->getTargetLowering()->isCondCodeLegal(
1896 SwappedCond, CompVT))
Tom Stellardcd428182013-09-28 02:50:38 +00001897 return SDValue();
1898
1899 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001900 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001901 }
1902
Chris Lattnerd47675e2005-08-09 20:20:18 +00001903 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001904 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001905}
1906
Dan Gohman1f372ed2008-02-25 21:11:39 +00001907/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1908/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001909bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001910 // This predicate is not safe for vector operations.
1911 if (Op.getValueType().isVector())
1912 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001913
Dan Gohman1d459e42009-12-11 21:31:27 +00001914 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001915 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1916}
1917
Dan Gohman309d3d52007-06-22 14:59:07 +00001918/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1919/// this predicate to simplify operations downstream. Mask is known to be zero
1920/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001921bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001922 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001923 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001924 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001925 return (KnownZero & Mask) == Mask;
1926}
1927
Jay Foada0653a32014-05-14 21:14:37 +00001928/// Determine which bits of Op are known to be either zero or one and return
1929/// them in the KnownZero/KnownOne bitsets.
1930void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1931 APInt &KnownOne, unsigned Depth) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001932 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001933 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001934
Dan Gohmanf990faf2008-02-13 00:35:47 +00001935 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001936 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001937 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001938
Dan Gohmanf990faf2008-02-13 00:35:47 +00001939 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001940
1941 switch (Op.getOpcode()) {
1942 case ISD::Constant:
1943 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001944 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1945 KnownZero = ~KnownOne;
Jay Foad5a29c362014-05-15 12:12:55 +00001946 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001947 case ISD::AND:
1948 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +00001949 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1950 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001951
1952 // Output known-1 bits are only known if set in both the LHS & RHS.
1953 KnownOne &= KnownOne2;
1954 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1955 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001956 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001957 case ISD::OR:
Jay Foada0653a32014-05-14 21:14:37 +00001958 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1959 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001960
Dan Gohman309d3d52007-06-22 14:59:07 +00001961 // Output known-0 bits are only known if clear in both the LHS & RHS.
1962 KnownZero &= KnownZero2;
1963 // Output known-1 are known to be set if set in either the LHS | RHS.
1964 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001965 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001966 case ISD::XOR: {
Jay Foada0653a32014-05-14 21:14:37 +00001967 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1968 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001969
Dan Gohman309d3d52007-06-22 14:59:07 +00001970 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001971 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001972 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1973 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1974 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001975 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00001976 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001977 case ISD::MUL: {
Jay Foada0653a32014-05-14 21:14:37 +00001978 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1979 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001980
1981 // If low bits are zero in either operand, output low known-0 bits.
1982 // Also compute a conserative estimate for high known-0 bits.
1983 // More trickiness is possible, but this is sufficient for the
1984 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001985 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001986 unsigned TrailZ = KnownZero.countTrailingOnes() +
1987 KnownZero2.countTrailingOnes();
1988 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001989 KnownZero2.countLeadingOnes(),
1990 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001991
1992 TrailZ = std::min(TrailZ, BitWidth);
1993 LeadZ = std::min(LeadZ, BitWidth);
1994 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1995 APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001996 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001997 }
1998 case ISD::UDIV: {
1999 // For the purposes of computing leading zeros we can conservatively
2000 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00002001 // be less than the denominator.
Jay Foada0653a32014-05-14 21:14:37 +00002002 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002003 unsigned LeadZ = KnownZero2.countLeadingOnes();
2004
Jay Foad25a5e4c2010-12-01 08:53:58 +00002005 KnownOne2.clearAllBits();
2006 KnownZero2.clearAllBits();
Jay Foada0653a32014-05-14 21:14:37 +00002007 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00002008 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2009 if (RHSUnknownLeadingOnes != BitWidth)
2010 LeadZ = std::min(BitWidth,
2011 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002012
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002013 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00002014 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002015 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002016 case ISD::SELECT:
Jay Foada0653a32014-05-14 21:14:37 +00002017 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2018 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002019
Dan Gohman309d3d52007-06-22 14:59:07 +00002020 // Only known if known in both the LHS and RHS.
2021 KnownOne &= KnownOne2;
2022 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002023 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002024 case ISD::SELECT_CC:
Jay Foada0653a32014-05-14 21:14:37 +00002025 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2026 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002027
Dan Gohman309d3d52007-06-22 14:59:07 +00002028 // Only known if known in both the LHS and RHS.
2029 KnownOne &= KnownOne2;
2030 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00002031 break;
Bill Wendling5424e6d2008-11-22 07:24:01 +00002032 case ISD::SADDO:
2033 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002034 case ISD::SSUBO:
2035 case ISD::USUBO:
2036 case ISD::SMULO:
2037 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002038 if (Op.getResNo() != 1)
Jay Foad5a29c362014-05-15 12:12:55 +00002039 break;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002040 // The boolean result conforms to getBooleanContents.
2041 // If we know the result of a setcc has the top bits zero, use this info.
2042 // We know that we have an integer-based boolean since these operations
2043 // are only available for integer.
2044 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2045 TargetLowering::ZeroOrOneBooleanContent &&
2046 BitWidth > 1)
2047 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2048 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002049 case ISD::SETCC:
2050 // If we know the result of a setcc has the top bits zero, use this info.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002051 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2052 TargetLowering::ZeroOrOneBooleanContent &&
2053 BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00002054 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002055 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002056 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002057 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002058 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002059 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00002060
2061 // If the shift count is an invalid immediate, don't do anything.
2062 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002063 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002064
Jay Foada0653a32014-05-14 21:14:37 +00002065 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman9db0aa82008-02-26 18:50:50 +00002066 KnownZero <<= ShAmt;
2067 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00002068 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00002069 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002070 }
Jay Foad5a29c362014-05-15 12:12:55 +00002071 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002072 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002073 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002074 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002075 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002076
Dan Gohman9db0aa82008-02-26 18:50:50 +00002077 // If the shift count is an invalid immediate, don't do anything.
2078 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002079 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002080
Jay Foada0653a32014-05-14 21:14:37 +00002081 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002082 KnownZero = KnownZero.lshr(ShAmt);
2083 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002084
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002085 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00002086 KnownZero |= HighBits; // High bits known zero.
2087 }
Jay Foad5a29c362014-05-15 12:12:55 +00002088 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002089 case ISD::SRA:
2090 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002091 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002092
Dan Gohman9db0aa82008-02-26 18:50:50 +00002093 // If the shift count is an invalid immediate, don't do anything.
2094 if (ShAmt >= BitWidth)
Jay Foad5a29c362014-05-15 12:12:55 +00002095 break;
Dan Gohman9db0aa82008-02-26 18:50:50 +00002096
Dan Gohman309d3d52007-06-22 14:59:07 +00002097 // If any of the demanded bits are produced by the sign extension, we also
2098 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002099 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002100
Jay Foada0653a32014-05-14 21:14:37 +00002101 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002102 KnownZero = KnownZero.lshr(ShAmt);
2103 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002104
Dan Gohman309d3d52007-06-22 14:59:07 +00002105 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002106 APInt SignBit = APInt::getSignBit(BitWidth);
2107 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002108
Dan Gohmanb717fda2008-02-20 16:30:17 +00002109 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002110 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002111 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00002112 KnownOne |= HighBits; // New bits are known one.
2113 }
2114 }
Jay Foad5a29c362014-05-15 12:12:55 +00002115 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002116 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002117 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002118 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002119
2120 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00002121 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002122 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002123
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002124 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002125 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002126
Dan Gohman309d3d52007-06-22 14:59:07 +00002127 // If the sign extended bits are demanded, we know that the sign
2128 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00002129 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002130 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002131 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002132
Jay Foada0653a32014-05-14 21:14:37 +00002133 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002134 KnownOne &= InputDemandedBits;
2135 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002136
Dan Gohman309d3d52007-06-22 14:59:07 +00002137 // If the sign bit of the input is known set or clear, then we know the
2138 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00002139 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00002140 KnownZero |= NewBits;
2141 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00002142 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002143 KnownOne |= NewBits;
2144 KnownZero &= ~NewBits;
2145 } else { // Input sign bit unknown
2146 KnownZero &= ~NewBits;
2147 KnownOne &= ~NewBits;
2148 }
Jay Foad5a29c362014-05-15 12:12:55 +00002149 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002150 }
2151 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002152 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002153 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002154 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002155 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002156 unsigned LowBits = Log2_32(BitWidth)+1;
2157 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002158 KnownOne.clearAllBits();
Jay Foad5a29c362014-05-15 12:12:55 +00002159 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002160 }
2161 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002162 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002163 // If this is a ZEXTLoad and we are looking at the loaded value.
2164 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002165 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002166 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002167 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002168 } else if (const MDNode *Ranges = LD->getRanges()) {
Jingyue Wu37fcb592014-06-19 16:50:16 +00002169 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002170 }
Jay Foad5a29c362014-05-15 12:12:55 +00002171 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002172 }
2173 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002174 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002175 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002176 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002177 KnownZero = KnownZero.trunc(InBits);
2178 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002179 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002180 KnownZero = KnownZero.zext(BitWidth);
2181 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002182 KnownZero |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002183 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002184 }
2185 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002186 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002187 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002188 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002189
Jay Foad583abbc2010-12-07 08:25:19 +00002190 KnownZero = KnownZero.trunc(InBits);
2191 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002192 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002193
2194 // Note if the sign bit is known to be zero or one.
2195 bool SignBitKnownZero = KnownZero.isNegative();
2196 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002197
Jay Foad583abbc2010-12-07 08:25:19 +00002198 KnownZero = KnownZero.zext(BitWidth);
2199 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002200
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002201 // If the sign bit is known zero or one, the top bits match.
2202 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002203 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002204 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002205 KnownOne |= NewBits;
Jay Foad5a29c362014-05-15 12:12:55 +00002206 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002207 }
2208 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002209 EVT InVT = Op.getOperand(0).getValueType();
2210 unsigned InBits = InVT.getScalarType().getSizeInBits();
2211 KnownZero = KnownZero.trunc(InBits);
2212 KnownOne = KnownOne.trunc(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002213 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng9ec512d2012-12-06 19:13:27 +00002214 KnownZero = KnownZero.zext(BitWidth);
2215 KnownOne = KnownOne.zext(BitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00002216 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002217 }
2218 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002219 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002220 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002221 KnownZero = KnownZero.zext(InBits);
2222 KnownOne = KnownOne.zext(InBits);
Jay Foada0653a32014-05-14 21:14:37 +00002223 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002224 KnownZero = KnownZero.trunc(BitWidth);
2225 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002226 break;
2227 }
2228 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002229 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002230 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Jay Foada0653a32014-05-14 21:14:37 +00002231 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002232 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002233 KnownOne &= (~KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00002234 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002235 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002236 case ISD::FGETSIGN:
2237 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002238 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Jay Foad5a29c362014-05-15 12:12:55 +00002239 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002240
Dan Gohman72ec3f42008-04-28 17:02:21 +00002241 case ISD::SUB: {
2242 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2243 // We know that the top bits of C-X are clear if X contains less bits
2244 // than C (i.e. no wrap-around can happen). For example, 20-X is
2245 // positive if we can prove that X is >= 0 and < 16.
2246 if (CLHS->getAPIntValue().isNonNegative()) {
2247 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2248 // NLZ can't be BitWidth with no sign bit
2249 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Jay Foada0653a32014-05-14 21:14:37 +00002250 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002251
2252 // If all of the MaskV bits are known to be zero, then we know the
2253 // output top bits are zero, because we now know that the output is
2254 // from [0-C].
2255 if ((KnownZero2 & MaskV) == MaskV) {
2256 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2257 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002258 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002259 }
2260 }
2261 }
2262 }
2263 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002264 case ISD::ADD:
2265 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002266 // Output known-0 bits are known if clear or set in both the low clear bits
2267 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2268 // low 3 bits clear.
Jay Foada0653a32014-05-14 21:14:37 +00002269 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002270 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2271
Jay Foada0653a32014-05-14 21:14:37 +00002272 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002273 KnownZeroOut = std::min(KnownZeroOut,
2274 KnownZero2.countTrailingOnes());
2275
Chris Lattner440b2802010-12-19 20:38:28 +00002276 if (Op.getOpcode() == ISD::ADD) {
2277 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002278 break;
Chris Lattner440b2802010-12-19 20:38:28 +00002279 }
2280
2281 // With ADDE, a carry bit may be added in, so we can only use this
2282 // information if we know (at least) that the low two bits are clear. We
2283 // then return to the caller that the low bit is unknown but that other bits
2284 // are known zero.
2285 if (KnownZeroOut >= 2) // ADDE
2286 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Jay Foad5a29c362014-05-15 12:12:55 +00002287 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002288 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002289 case ISD::SREM:
2290 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002291 const APInt &RA = Rem->getAPIntValue().abs();
2292 if (RA.isPowerOf2()) {
2293 APInt LowBits = RA - 1;
Jay Foada0653a32014-05-14 21:14:37 +00002294 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002295
Duncan Sands33274982010-01-29 09:45:26 +00002296 // The low bits of the first operand are unchanged by the srem.
2297 KnownZero = KnownZero2 & LowBits;
2298 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002299
Duncan Sands33274982010-01-29 09:45:26 +00002300 // If the first operand is non-negative or has all low bits zero, then
2301 // the upper bits are all zero.
2302 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2303 KnownZero |= ~LowBits;
2304
2305 // If the first operand is negative and not all low bits are zero, then
2306 // the upper bits are all one.
2307 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2308 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002309 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002310 }
2311 }
Jay Foad5a29c362014-05-15 12:12:55 +00002312 break;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002313 case ISD::UREM: {
2314 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002315 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002316 if (RA.isPowerOf2()) {
2317 APInt LowBits = (RA - 1);
Rafael Espindola92945ee2014-05-30 15:00:45 +00002318 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2319
2320 // The upper bits are all zero, the lower ones are unchanged.
2321 KnownZero = KnownZero2 | ~LowBits;
2322 KnownOne = KnownOne2 & LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002323 break;
2324 }
2325 }
2326
2327 // Since the result is less than or equal to either operand, any leading
2328 // zero bits in either operand must also exist in the result.
Jay Foada0653a32014-05-14 21:14:37 +00002329 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2330 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002331
2332 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2333 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002334 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002335 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Jay Foad5a29c362014-05-15 12:12:55 +00002336 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002337 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002338 case ISD::FrameIndex:
2339 case ISD::TargetFrameIndex:
2340 if (unsigned Align = InferPtrAlignment(Op)) {
2341 // The low bits are known zero if the pointer is aligned.
2342 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Jay Foad5a29c362014-05-15 12:12:55 +00002343 break;
Chris Lattner46c01a32011-02-13 22:25:43 +00002344 }
2345 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002346
Dan Gohman309d3d52007-06-22 14:59:07 +00002347 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002348 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2349 break;
2350 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002351 case ISD::INTRINSIC_WO_CHAIN:
2352 case ISD::INTRINSIC_W_CHAIN:
2353 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002354 // Allow the target to implement this method for its nodes.
Jay Foada0653a32014-05-14 21:14:37 +00002355 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Jay Foad5a29c362014-05-15 12:12:55 +00002356 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002357 }
Jay Foad5a29c362014-05-15 12:12:55 +00002358
2359 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002360}
2361
2362/// ComputeNumSignBits - Return the number of times the sign bit of the
2363/// register is replicated into the other bits. We know that at least 1 bit
2364/// is always equal to the sign bit (itself), but other cases can give us
2365/// information. For example, immediately after an "SRA X, 2", we know that
2366/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002367unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Eric Christopherd9134482014-08-04 21:25:23 +00002368 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002369 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002370 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002371 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002372 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002373 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002374
Dan Gohman309d3d52007-06-22 14:59:07 +00002375 if (Depth == 6)
2376 return 1; // Limit search depth.
2377
2378 switch (Op.getOpcode()) {
2379 default: break;
2380 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002381 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002382 return VTBits-Tmp+1;
2383 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002384 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002385 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002386
Dan Gohman309d3d52007-06-22 14:59:07 +00002387 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002388 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002389 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002390 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002391
Dan Gohman309d3d52007-06-22 14:59:07 +00002392 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002393 Tmp =
2394 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002395 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002396
Dan Gohman309d3d52007-06-22 14:59:07 +00002397 case ISD::SIGN_EXTEND_INREG:
2398 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002399 Tmp =
2400 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002401 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002402
Dan Gohman309d3d52007-06-22 14:59:07 +00002403 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2404 return std::max(Tmp, Tmp2);
2405
2406 case ISD::SRA:
2407 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2408 // SRA X, C -> adds C sign bits.
2409 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002410 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002411 if (Tmp > VTBits) Tmp = VTBits;
2412 }
2413 return Tmp;
2414 case ISD::SHL:
2415 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2416 // shl destroys sign bits.
2417 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002418 if (C->getZExtValue() >= VTBits || // Bad shift.
2419 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2420 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002421 }
2422 break;
2423 case ISD::AND:
2424 case ISD::OR:
2425 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002426 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002427 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002428 if (Tmp != 1) {
2429 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2430 FirstAnswer = std::min(Tmp, Tmp2);
2431 // We computed what we know about the sign bits as our first
2432 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002433 // computeKnownBits, and pick whichever answer is better.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002434 }
2435 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002436
2437 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002438 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002439 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002440 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002441 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002442
2443 case ISD::SADDO:
2444 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002445 case ISD::SSUBO:
2446 case ISD::USUBO:
2447 case ISD::SMULO:
2448 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002449 if (Op.getResNo() != 1)
2450 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002451 // The boolean result conforms to getBooleanContents. Fall through.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002452 // If setcc returns 0/-1, all bits are sign bits.
2453 // We know that we have an integer-based boolean since these operations
2454 // are only available for integer.
2455 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2456 TargetLowering::ZeroOrNegativeOneBooleanContent)
2457 return VTBits;
2458 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002459 case ISD::SETCC:
2460 // If setcc returns 0/-1, all bits are sign bits.
Daniel Sanderscbd44c52014-07-10 10:18:12 +00002461 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002462 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002463 return VTBits;
2464 break;
2465 case ISD::ROTL:
2466 case ISD::ROTR:
2467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002468 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002469
Dan Gohman309d3d52007-06-22 14:59:07 +00002470 // Handle rotate right by N like a rotate left by 32-N.
2471 if (Op.getOpcode() == ISD::ROTR)
2472 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2473
2474 // If we aren't rotating out all of the known-in sign bits, return the
2475 // number that are left. This handles rotl(sext(x), 1) for example.
2476 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2477 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2478 }
2479 break;
2480 case ISD::ADD:
2481 // Add can have at most one carry bit. Thus we know that the output
2482 // is, at worst, one more bit than the inputs.
2483 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2484 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002485
Dan Gohman309d3d52007-06-22 14:59:07 +00002486 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002487 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002488 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002489 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002490 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002491
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 we are subtracting one from a positive number, there is no carry
2498 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002499 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002500 return Tmp;
2501 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002502
Dan Gohman309d3d52007-06-22 14:59:07 +00002503 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2504 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002505 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002506
Dan Gohman309d3d52007-06-22 14:59:07 +00002507 case ISD::SUB:
2508 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2509 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002510
Dan Gohman309d3d52007-06-22 14:59:07 +00002511 // Handle NEG.
2512 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002513 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002514 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002515 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002516 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2517 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002518 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002519 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002520
Dan Gohman309d3d52007-06-22 14:59:07 +00002521 // If the input is known to be positive (the sign bit is known clear),
2522 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002523 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002524 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002525
Dan Gohman309d3d52007-06-22 14:59:07 +00002526 // Otherwise, we treat this like a SUB.
2527 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002528
Dan Gohman309d3d52007-06-22 14:59:07 +00002529 // Sub can have at most one carry bit. Thus we know that the output
2530 // is, at worst, one more bit than the inputs.
2531 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2532 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002533 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002534 case ISD::TRUNCATE:
2535 // FIXME: it's tricky to do anything useful for this, but it is an important
2536 // case for targets like X86.
2537 break;
2538 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002539
Nadav Rotem4536d582013-03-20 22:53:44 +00002540 // If we are looking at the loaded value of the SDNode.
2541 if (Op.getResNo() == 0) {
2542 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2543 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2544 unsigned ExtType = LD->getExtensionType();
2545 switch (ExtType) {
2546 default: break;
2547 case ISD::SEXTLOAD: // '17' bits known
2548 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2549 return VTBits-Tmp+1;
2550 case ISD::ZEXTLOAD: // '16' bits known
2551 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2552 return VTBits-Tmp;
2553 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002554 }
2555 }
2556
2557 // Allow the target to implement this method for its nodes.
2558 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002559 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002560 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2561 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Matt Arsenaultcf6f6882014-04-04 20:13:13 +00002562 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002563 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002564 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002565
Dan Gohman309d3d52007-06-22 14:59:07 +00002566 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2567 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002568 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00002569 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002570
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002571 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002572 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002573 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002574 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002575 Mask = KnownOne;
2576 } else {
2577 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002578 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002579 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002580
Dan Gohman309d3d52007-06-22 14:59:07 +00002581 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2582 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002583 Mask = ~Mask;
2584 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002585 // Return # leading zeros. We use 'min' here in case Val was zero before
2586 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002587 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002588}
2589
Chris Lattner46c01a32011-02-13 22:25:43 +00002590/// isBaseWithConstantOffset - Return true if the specified operand is an
2591/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2592/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2593/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002594/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002595bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2596 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2597 !isa<ConstantSDNode>(Op.getOperand(1)))
2598 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002599
2600 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002601 !MaskedValueIsZero(Op.getOperand(0),
2602 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2603 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002604
Chris Lattner46c01a32011-02-13 22:25:43 +00002605 return true;
2606}
2607
2608
Dan Gohmand0d5e682009-09-03 20:34:31 +00002609bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2610 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002611 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002612 return true;
2613
2614 // If the value is a constant, we can obviously see if it is a NaN or not.
2615 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2616 return !C->getValueAPF().isNaN();
2617
2618 // TODO: Recognize more cases here.
2619
2620 return false;
2621}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002622
Dan Gohman38605212010-02-24 06:52:40 +00002623bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2624 // If the value is a constant, we can obviously see if it is a zero or not.
2625 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2626 return !C->isZero();
2627
2628 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002629 switch (Op.getOpcode()) {
2630 default: break;
2631 case ISD::OR:
2632 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2633 return !C->isNullValue();
2634 break;
2635 }
Dan Gohman38605212010-02-24 06:52:40 +00002636
2637 return false;
2638}
2639
2640bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2641 // Check the obvious case.
2642 if (A == B) return true;
2643
2644 // For for negative and positive zero.
2645 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2646 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2647 if (CA->isZero() && CB->isZero()) return true;
2648
2649 // Otherwise they may not be equal.
2650 return false;
2651}
2652
Chris Lattner061a1ea2005-01-07 07:46:32 +00002653/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002654///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002655SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002656 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00002657 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
Craig Topperc0196b12014-04-14 00:51:57 +00002658 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002659 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002660 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002661
Jack Carter170a5f22013-09-09 22:02:08 +00002662 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2663 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002664 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002665
Chandler Carruth41b20e72014-07-22 04:07:55 +00002666 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002667 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002668}
2669
Andrew Trickef9de2a2013-05-25 02:42:55 +00002670SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002671 EVT VT, SDValue Operand) {
Juergen Ributzkafcd2e942014-04-02 22:21:01 +00002672 // Constant fold unary operations with an integer constant operand. Even
2673 // opaque constant will be folded, because the folding of unary operations
2674 // doesn't create new constants with different values. Nevertheless, the
2675 // opaque flag is preserved during folding to prevent future folding with
2676 // other constants.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002677 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002678 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002679 switch (Opcode) {
2680 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002681 case ISD::SIGN_EXTEND:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002682 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2683 C->isTargetOpcode(), C->isOpaque());
Chris Lattner8c393c22005-09-02 00:17:32 +00002684 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002685 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002686 case ISD::TRUNCATE:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002687 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2688 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen7d67e542007-09-19 23:55:34 +00002689 case ISD::UINT_TO_FP:
2690 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002691 APFloat apf(EVTToAPFloatSemantics(VT),
2692 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002693 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002694 Opcode==ISD::SINT_TO_FP,
2695 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002696 return getConstantFP(apf, VT);
2697 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002698 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002699 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002700 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002701 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002702 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002703 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002704 case ISD::BSWAP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002705 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2706 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002707 case ISD::CTPOP:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002708 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2709 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002710 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002711 case ISD::CTLZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002712 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2713 C->isOpaque());
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002714 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002715 case ISD::CTTZ_ZERO_UNDEF:
Juergen Ributzkae2e16842014-03-25 18:01:20 +00002716 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2717 C->isOpaque());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002718 }
2719 }
2720
Dale Johannesen446b9002007-08-31 23:34:27 +00002721 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002722 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002723 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002724 switch (Opcode) {
2725 case ISD::FNEG:
2726 V.changeSign();
2727 return getConstantFP(V, VT);
2728 case ISD::FABS:
2729 V.clearSign();
2730 return getConstantFP(V, VT);
2731 case ISD::FCEIL: {
2732 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2733 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002734 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002735 break;
2736 }
2737 case ISD::FTRUNC: {
2738 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2739 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002740 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002741 break;
2742 }
2743 case ISD::FFLOOR: {
2744 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2745 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002746 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002747 break;
2748 }
2749 case ISD::FP_EXTEND: {
2750 bool ignored;
2751 // This can return overflow, underflow, or inexact; we don't care.
2752 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002753 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002754 APFloat::rmNearestTiesToEven, &ignored);
2755 return getConstantFP(V, VT);
2756 }
2757 case ISD::FP_TO_SINT:
2758 case ISD::FP_TO_UINT: {
2759 integerPart x[2];
2760 bool ignored;
2761 assert(integerPartWidth >= 64);
2762 // FIXME need to be more flexible about rounding mode.
2763 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2764 Opcode==ISD::FP_TO_SINT,
2765 APFloat::rmTowardZero, &ignored);
2766 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002767 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002768 APInt api(VT.getSizeInBits(), x);
2769 return getConstant(api, VT);
2770 }
2771 case ISD::BITCAST:
2772 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2773 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2774 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2775 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2776 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002777 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002778 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002779
Jim Grosbachf7502c42014-07-18 00:40:52 +00002780 // Constant fold unary operations with a vector integer operand.
2781 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002782 if (BV->isConstant()) {
Jim Grosbachf7502c42014-07-18 00:40:52 +00002783 switch (Opcode) {
2784 default:
2785 // FIXME: Entirely reasonable to perform folding of other unary
2786 // operations here as the need arises.
2787 break;
2788 case ISD::UINT_TO_FP:
2789 case ISD::SINT_TO_FP: {
Jim Grosbach19dd3082014-07-23 20:41:31 +00002790 SmallVector<SDValue, 8> Ops;
2791 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2792 SDValue OpN = BV->getOperand(i);
2793 // Let the above scalar folding handle the conversion of each
2794 // element.
2795 OpN = getNode(ISD::SINT_TO_FP, DL, VT.getVectorElementType(),
2796 OpN);
2797 Ops.push_back(OpN);
2798 }
2799 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
Jim Grosbachf7502c42014-07-18 00:40:52 +00002800 }
2801 }
2802 }
2803 }
2804
Gabor Greiff304a7a2008-08-28 21:40:38 +00002805 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002806 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002807 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002808 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002809 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002810 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002811 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002812 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002813 assert(VT.isFloatingPoint() &&
2814 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002815 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002816 assert((!VT.isVector() ||
2817 VT.getVectorNumElements() ==
2818 Operand.getValueType().getVectorNumElements()) &&
2819 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002820 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002821 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002822 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002823 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002824 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002825 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002826 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002827 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2828 "Invalid sext node, dst < src!");
2829 assert((!VT.isVector() ||
2830 VT.getVectorNumElements() ==
2831 Operand.getValueType().getVectorNumElements()) &&
2832 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002833 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2834 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2835 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002836 // sext(undef) = 0, because the top bits will all be the same.
2837 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002838 break;
2839 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002840 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002841 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002842 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002843 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2844 "Invalid zext node, dst < src!");
2845 assert((!VT.isVector() ||
2846 VT.getVectorNumElements() ==
2847 Operand.getValueType().getVectorNumElements()) &&
2848 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002849 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002850 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002851 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002852 else if (OpOpcode == ISD::UNDEF)
2853 // zext(undef) = 0, because the top bits will be zero.
2854 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002855 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002856 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002857 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002858 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002859 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002860 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2861 "Invalid anyext node, dst < src!");
2862 assert((!VT.isVector() ||
2863 VT.getVectorNumElements() ==
2864 Operand.getValueType().getVectorNumElements()) &&
2865 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002866
Dan Gohman08837892010-06-18 00:08:30 +00002867 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2868 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002869 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002870 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002871 else if (OpOpcode == ISD::UNDEF)
2872 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002873
2874 // (ext (trunx x)) -> x
2875 if (OpOpcode == ISD::TRUNCATE) {
2876 SDValue OpOp = Operand.getNode()->getOperand(0);
2877 if (OpOp.getValueType() == VT)
2878 return OpOp;
2879 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002880 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002881 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002882 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002883 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002884 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002885 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2886 "Invalid truncate node, src < dst!");
2887 assert((!VT.isVector() ||
2888 VT.getVectorNumElements() ==
2889 Operand.getValueType().getVectorNumElements()) &&
2890 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002891 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002892 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002893 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2894 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002895 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002896 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2897 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002898 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002899 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002900 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002901 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002902 }
Craig Topper201c1a32012-01-15 01:05:11 +00002903 if (OpOpcode == ISD::UNDEF)
2904 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002905 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002906 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002907 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002908 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002909 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002910 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002911 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2912 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002913 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002914 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002915 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002916 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002917 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002918 (VT.getVectorElementType() == Operand.getValueType() ||
2919 (VT.getVectorElementType().isInteger() &&
2920 Operand.getValueType().isInteger() &&
2921 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002922 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002923 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002924 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002925 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2926 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2927 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2928 Operand.getConstantOperandVal(1) == 0 &&
2929 Operand.getOperand(0).getValueType() == VT)
2930 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002931 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002932 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002933 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002934 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002935 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002936 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002937 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002938 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002939 break;
2940 case ISD::FABS:
2941 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002942 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002943 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002944 }
2945
Chris Lattnerf9c19152005-08-25 19:12:10 +00002946 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002947 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002948 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002949 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002950 SDValue Ops[1] = { Operand };
Craig Topper633d99b2014-04-27 23:22:43 +00002951 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00002952 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00002953 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002954 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002955
Jack Carter170a5f22013-09-09 22:02:08 +00002956 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2957 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002958 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002959 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002960 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2961 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002962 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002963
Chandler Carruth41b20e72014-07-22 04:07:55 +00002964 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002965 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002966}
2967
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002968SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2969 SDNode *Cst1, SDNode *Cst2) {
Jim Grosbachcad4cd62014-04-09 23:28:11 +00002970 // If the opcode is a target-specific ISD node, there's nothing we can
2971 // do here and the operand rules may not line up with the below, so
2972 // bail early.
2973 if (Opcode >= ISD::BUILTIN_OP_END)
2974 return SDValue();
2975
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002976 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2977 SmallVector<SDValue, 4> Outputs;
2978 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002979
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002980 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2981 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002982 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2983 return SDValue();
2984
2985 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002986 // Scalar instruction.
2987 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00002988 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002989 // For vectors extract each constant element into Inputs so we can constant
2990 // fold them individually.
2991 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2992 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2993 if (!BV1 || !BV2)
2994 return SDValue();
2995
2996 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2997
2998 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2999 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3000 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3001 if (!V1 || !V2) // Not a constant, bail.
3002 return SDValue();
3003
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003004 if (V1->isOpaque() || V2->isOpaque())
3005 return SDValue();
3006
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003007 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3008 // FIXME: This is valid and could be handled by truncating the APInts.
3009 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3010 return SDValue();
3011
3012 Inputs.push_back(std::make_pair(V1, V2));
3013 }
Bill Wendling162c26d2008-09-24 10:16:24 +00003014 }
3015
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003016 // We have a number of constant values, constant fold them element by element.
3017 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3018 const APInt &C1 = Inputs[I].first->getAPIntValue();
3019 const APInt &C2 = Inputs[I].second->getAPIntValue();
3020
3021 switch (Opcode) {
3022 case ISD::ADD:
3023 Outputs.push_back(getConstant(C1 + C2, SVT));
3024 break;
3025 case ISD::SUB:
3026 Outputs.push_back(getConstant(C1 - C2, SVT));
3027 break;
3028 case ISD::MUL:
3029 Outputs.push_back(getConstant(C1 * C2, SVT));
3030 break;
3031 case ISD::UDIV:
3032 if (!C2.getBoolValue())
3033 return SDValue();
3034 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3035 break;
3036 case ISD::UREM:
3037 if (!C2.getBoolValue())
3038 return SDValue();
3039 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3040 break;
3041 case ISD::SDIV:
3042 if (!C2.getBoolValue())
3043 return SDValue();
3044 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3045 break;
3046 case ISD::SREM:
3047 if (!C2.getBoolValue())
3048 return SDValue();
3049 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3050 break;
3051 case ISD::AND:
3052 Outputs.push_back(getConstant(C1 & C2, SVT));
3053 break;
3054 case ISD::OR:
3055 Outputs.push_back(getConstant(C1 | C2, SVT));
3056 break;
3057 case ISD::XOR:
3058 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3059 break;
3060 case ISD::SHL:
3061 Outputs.push_back(getConstant(C1 << C2, SVT));
3062 break;
3063 case ISD::SRL:
3064 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3065 break;
3066 case ISD::SRA:
3067 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3068 break;
3069 case ISD::ROTL:
3070 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3071 break;
3072 case ISD::ROTR:
3073 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3074 break;
3075 default:
3076 return SDValue();
3077 }
3078 }
3079
Benjamin Kramer6dd9f8f2014-05-02 21:28:49 +00003080 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3081 "Expected a scalar or vector!"));
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003082
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003083 // Handle the scalar case first.
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003084 if (!VT.isVector())
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003085 return Outputs.back();
3086
Benjamin Kramer42d262f2014-05-02 12:35:22 +00003087 // We may have a vector type but a scalar result. Create a splat.
3088 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3089
3090 // Build a big vector out of the scalar elements we generated.
Craig Topper48d114b2014-04-26 18:35:24 +00003091 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling162c26d2008-09-24 10:16:24 +00003092}
3093
Andrew Trickef9de2a2013-05-25 02:42:55 +00003094SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003095 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003096 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3097 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00003098 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00003099 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00003100 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00003101 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3102 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00003103 // Fold trivial token factors.
3104 if (N1.getOpcode() == ISD::EntryToken) return N2;
3105 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00003106 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00003107 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00003108 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00003109 // Concat of UNDEFs is UNDEF.
3110 if (N1.getOpcode() == ISD::UNDEF &&
3111 N2.getOpcode() == ISD::UNDEF)
3112 return getUNDEF(VT);
3113
Dan Gohman550c9af2008-08-14 20:04:46 +00003114 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3115 // one big BUILD_VECTOR.
3116 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3117 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003118 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3119 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003120 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003121 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003122 }
3123 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003124 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003125 assert(VT.isInteger() && "This operator does not apply to FP types!");
3126 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003127 N1.getValueType() == VT && "Binary operator types must match!");
3128 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3129 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003130 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003131 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00003132 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3133 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00003134 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003135 case ISD::OR:
3136 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00003137 case ISD::ADD:
3138 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003139 assert(VT.isInteger() && "This operator does not apply to FP types!");
3140 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00003141 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00003142 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3143 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00003144 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00003145 return N1;
3146 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003147 case ISD::UDIV:
3148 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00003149 case ISD::MULHU:
3150 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003151 case ISD::MUL:
3152 case ISD::SDIV:
3153 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00003154 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003155 assert(N1.getValueType() == N2.getValueType() &&
3156 N1.getValueType() == VT && "Binary operator types must match!");
3157 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003158 case ISD::FADD:
3159 case ISD::FSUB:
3160 case ISD::FMUL:
3161 case ISD::FDIV:
3162 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003163 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00003164 if (Opcode == ISD::FADD) {
3165 // 0+x --> x
3166 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3167 if (CFP->getValueAPF().isZero())
3168 return N2;
3169 // x+0 --> x
3170 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3171 if (CFP->getValueAPF().isZero())
3172 return N1;
3173 } else if (Opcode == ISD::FSUB) {
3174 // x-0 --> x
3175 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3176 if (CFP->getValueAPF().isZero())
3177 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00003178 } else if (Opcode == ISD::FMUL) {
3179 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3180 SDValue V = N2;
3181
3182 // If the first operand isn't the constant, try the second
3183 if (!CFP) {
3184 CFP = dyn_cast<ConstantFPSDNode>(N2);
3185 V = N1;
3186 }
3187
3188 if (CFP) {
3189 // 0*x --> 0
3190 if (CFP->isZero())
3191 return SDValue(CFP,0);
3192 // 1*x --> x
3193 if (CFP->isExactlyValue(1.0))
3194 return V;
3195 }
Dan Gohman1275e282009-01-23 19:10:37 +00003196 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003197 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003198 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003199 assert(N1.getValueType() == N2.getValueType() &&
3200 N1.getValueType() == VT && "Binary operator types must match!");
3201 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003202 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3203 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003204 N1.getValueType().isFloatingPoint() &&
3205 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003206 "Invalid FCOPYSIGN!");
3207 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003208 case ISD::SHL:
3209 case ISD::SRA:
3210 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003211 case ISD::ROTL:
3212 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003213 assert(VT == N1.getValueType() &&
3214 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003215 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003216 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003217 assert((!VT.isVector() || VT == N2.getValueType()) &&
3218 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003219 // Verify that the shift amount VT is bit enough to hold valid shift
3220 // amounts. This catches things like trying to shift an i1024 value by an
3221 // i8, which is easy to fall into in generic code that uses
3222 // TLI.getShiftAmount().
3223 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003224 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003225 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003226
3227 // Always fold shifts of i1 values so the code generator doesn't need to
3228 // handle them. Since we know the size of the shift has to be less than the
3229 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003230 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003231 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003232 if (N2C && N2C->isNullValue())
3233 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003234 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003235 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003236 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003237 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003238 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003239 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003240 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003241 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003242 "type is vector!");
3243 assert((!EVT.isVector() ||
3244 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3245 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003246 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003247 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003248 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003249 break;
3250 }
Chris Lattner72733e52008-01-17 07:00:52 +00003251 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003252 assert(VT.isFloatingPoint() &&
3253 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003254 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003255 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003256 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003257 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003258 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003259 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003260 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003261 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003262 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003263 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003264 assert(!EVT.isVector() &&
3265 "AssertSExt/AssertZExt type should be the vector element type "
3266 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003267 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003268 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003269 break;
3270 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003271 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003272 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003273 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003274 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003275 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003276 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003277 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003278 "type is vector!");
3279 assert((!EVT.isVector() ||
3280 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3281 "Vector element counts must match in SIGN_EXTEND_INREG");
3282 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003283 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003284
Chris Lattner16713612008-01-22 19:09:33 +00003285 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003286 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003287 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003288 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003289 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003290 return getConstant(Val, VT);
3291 }
Chris Lattner16713612008-01-22 19:09:33 +00003292 break;
3293 }
3294 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003295 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3296 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003297 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003298
Chris Lattner16713612008-01-22 19:09:33 +00003299 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3300 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003301 if (N2C &&
3302 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003303 N1.getNumOperands() > 0) {
3304 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003305 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003306 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003307 N1.getOperand(N2C->getZExtValue() / Factor),
3308 getConstant(N2C->getZExtValue() % Factor,
3309 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003310 }
3311
3312 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3313 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003314 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3315 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003316
3317 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003318 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003319 // are promoted and implicitly truncated, and the result implicitly
3320 // extended. Make that explicit here.
3321 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003322
Bob Wilson59dbbb22009-04-13 22:05:19 +00003323 return Elt;
3324 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003325
Chris Lattner16713612008-01-22 19:09:33 +00003326 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3327 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003328 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003329 // If the indices are the same, return the inserted element else
3330 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003331 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003332 SDValue N1Op2 = N1.getOperand(2);
3333 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3334
3335 if (N1Op2C && N2C) {
3336 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3337 if (VT == N1.getOperand(1).getValueType())
3338 return N1.getOperand(1);
3339 else
3340 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3341 }
3342
Dale Johannesendb393622009-02-03 01:55:44 +00003343 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003344 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003345 }
Chris Lattner16713612008-01-22 19:09:33 +00003346 break;
3347 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003348 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003349 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3350 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003351 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003352 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003353
Chris Lattner16713612008-01-22 19:09:33 +00003354 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3355 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003356 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003357 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003358 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003359
Chris Lattner16713612008-01-22 19:09:33 +00003360 // EXTRACT_ELEMENT of a constant int is also very common.
3361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003362 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003363 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003364 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3365 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003366 }
3367 break;
David Greeneb6f16112011-01-26 15:38:49 +00003368 case ISD::EXTRACT_SUBVECTOR: {
3369 SDValue Index = N2;
3370 if (VT.isSimple() && N1.getValueType().isSimple()) {
3371 assert(VT.isVector() && N1.getValueType().isVector() &&
3372 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003373 assert(VT.getVectorElementType() ==
3374 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003375 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003376 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003377 "Extract subvector must be from larger vector to smaller vector!");
3378
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003379 if (isa<ConstantSDNode>(Index.getNode())) {
3380 assert((VT.getVectorNumElements() +
3381 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003382 <= N1.getValueType().getVectorNumElements())
3383 && "Extract subvector overflow!");
3384 }
3385
3386 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003387 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003388 return N1;
3389 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003390 break;
Chris Lattner16713612008-01-22 19:09:33 +00003391 }
David Greeneb6f16112011-01-26 15:38:49 +00003392 }
Chris Lattner16713612008-01-22 19:09:33 +00003393
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003394 // Perform trivial constant folding.
3395 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3396 if (SV.getNode()) return SV;
3397
3398 // Canonicalize constant to RHS if commutative.
3399 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3400 std::swap(N1C, N2C);
3401 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003402 }
3403
Chris Lattner16713612008-01-22 19:09:33 +00003404 // Constant fold FP operations.
Pedro Artigascaa56582014-08-08 16:46:53 +00003405 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003406 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3407 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003408 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003409 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003410 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003411 std::swap(N1CFP, N2CFP);
3412 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003413 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003414 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3415 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003416 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003417 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003418 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003419 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003420 return getConstantFP(V1, VT);
3421 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003422 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003423 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003424 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003425 return getConstantFP(V1, VT);
3426 break;
3427 case ISD::FMUL:
3428 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003429 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003430 return getConstantFP(V1, VT);
3431 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003432 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003433 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003434 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3435 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003436 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003437 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003438 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003439 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003440 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Pedro Artigascaa56582014-08-08 16:46:53 +00003441 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3442 s!=APFloat::opDivByZero)) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003443 return getConstantFP(V1, VT);
Pedro Artigascaa56582014-08-08 16:46:53 +00003444 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003445 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003446 case ISD::FCOPYSIGN:
3447 V1.copySign(V2);
3448 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003449 default: break;
3450 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003451 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003452
3453 if (Opcode == ISD::FP_ROUND) {
3454 APFloat V = N1CFP->getValueAPF(); // make copy
3455 bool ignored;
3456 // This can return overflow, underflow, or inexact; we don't care.
3457 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003458 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003459 APFloat::rmNearestTiesToEven, &ignored);
3460 return getConstantFP(V, VT);
3461 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003462 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003463
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003464 // Canonicalize an UNDEF to the RHS, even over a constant.
3465 if (N1.getOpcode() == ISD::UNDEF) {
3466 if (isCommutativeBinOp(Opcode)) {
3467 std::swap(N1, N2);
3468 } else {
3469 switch (Opcode) {
3470 case ISD::FP_ROUND_INREG:
3471 case ISD::SIGN_EXTEND_INREG:
3472 case ISD::SUB:
3473 case ISD::FSUB:
3474 case ISD::FDIV:
3475 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003476 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003477 return N1; // fold op(undef, arg2) -> undef
3478 case ISD::UDIV:
3479 case ISD::SDIV:
3480 case ISD::UREM:
3481 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003482 case ISD::SRL:
3483 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003484 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003485 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3486 // For vectors, we can't easily build an all zero vector, just return
3487 // the LHS.
3488 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003489 }
3490 }
3491 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003492
3493 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003494 if (N2.getOpcode() == ISD::UNDEF) {
3495 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003496 case ISD::XOR:
3497 if (N1.getOpcode() == ISD::UNDEF)
3498 // Handle undef ^ undef -> 0 special case. This is a common
3499 // idiom (misuse).
3500 return getConstant(0, VT);
3501 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003502 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003503 case ISD::ADDC:
3504 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003505 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003506 case ISD::UDIV:
3507 case ISD::SDIV:
3508 case ISD::UREM:
3509 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003510 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003511 case ISD::FADD:
3512 case ISD::FSUB:
3513 case ISD::FMUL:
3514 case ISD::FDIV:
3515 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003516 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003517 return N2;
3518 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003519 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003520 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003521 case ISD::SRL:
3522 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003523 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003524 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3525 // For vectors, we can't easily build an all zero vector, just return
3526 // the LHS.
3527 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003528 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003529 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003530 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003531 // For vectors, we can't easily build an all one vector, just return
3532 // the LHS.
3533 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003534 case ISD::SRA:
3535 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003536 }
3537 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003538
Chris Lattner724f7ee2005-05-11 18:57:39 +00003539 // Memoize this node if possible.
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003540 BinarySDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003541 SDVTList VTs = getVTList(VT);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003542 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003543 if (VT != MVT::Glue) {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003544 SDValue Ops[] = {N1, N2};
Jim Laskeyf576b422006-10-27 23:46:08 +00003545 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003546 AddNodeIDNode(ID, Opcode, VTs, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003547 if (BinOpHasFlags)
3548 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00003549 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003550 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003551 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003552
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003553 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3554
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003555 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003556 } else {
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00003557
3558 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003559 }
3560
Chandler Carruth41b20e72014-07-22 04:07:55 +00003561 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003562 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003563}
3564
Andrew Trickef9de2a2013-05-25 02:42:55 +00003565SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003566 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003567 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003568 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003569 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003570 case ISD::FMA: {
3571 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3572 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3573 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3574 if (N1CFP && N2CFP && N3CFP) {
3575 APFloat V1 = N1CFP->getValueAPF();
3576 const APFloat &V2 = N2CFP->getValueAPF();
3577 const APFloat &V3 = N3CFP->getValueAPF();
3578 APFloat::opStatus s =
3579 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3580 if (s != APFloat::opInvalidOp)
3581 return getConstantFP(V1, VT);
3582 }
3583 break;
3584 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003585 case ISD::CONCAT_VECTORS:
3586 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3587 // one big BUILD_VECTOR.
3588 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3589 N2.getOpcode() == ISD::BUILD_VECTOR &&
3590 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003591 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3592 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003593 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3594 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Craig Topper48d114b2014-04-26 18:35:24 +00003595 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman550c9af2008-08-14 20:04:46 +00003596 }
3597 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003598 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003599 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003600 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003601 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003602 break;
3603 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003604 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003605 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003606 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003607 return N2; // select true, X, Y -> X
3608 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003609 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003610
3611 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003612 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003613 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003614 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003615 case ISD::INSERT_SUBVECTOR: {
3616 SDValue Index = N3;
3617 if (VT.isSimple() && N1.getValueType().isSimple()
3618 && N2.getValueType().isSimple()) {
3619 assert(VT.isVector() && N1.getValueType().isVector() &&
3620 N2.getValueType().isVector() &&
3621 "Insert subvector VTs must be a vectors");
3622 assert(VT == N1.getValueType() &&
3623 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003624 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003625 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003626 if (isa<ConstantSDNode>(Index.getNode())) {
3627 assert((N2.getValueType().getVectorNumElements() +
3628 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003629 <= VT.getVectorNumElements())
3630 && "Insert subvector overflow!");
3631 }
3632
3633 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003634 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003635 return N2;
3636 }
3637 break;
3638 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003639 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003640 // Fold bit_convert nodes from a type to themselves.
3641 if (N1.getValueType() == VT)
3642 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003643 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003644 }
3645
Chris Lattnerf9c19152005-08-25 19:12:10 +00003646 // Memoize node if it doesn't produce a flag.
3647 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003648 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003649 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003650 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003651 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00003652 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00003653 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00003654 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003655 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003656
Jack Carter170a5f22013-09-09 22:02:08 +00003657 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3658 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003659 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003660 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003661 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3662 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003663 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003664
Chandler Carruth41b20e72014-07-22 04:07:55 +00003665 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003666 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003667}
3668
Andrew Trickef9de2a2013-05-25 02:42:55 +00003669SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003670 SDValue N1, SDValue N2, SDValue N3,
3671 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003672 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00003673 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003674}
3675
Andrew Trickef9de2a2013-05-25 02:42:55 +00003676SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003677 SDValue N1, SDValue N2, SDValue N3,
3678 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003679 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00003680 return getNode(Opcode, DL, VT, Ops);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003681}
3682
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003683/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3684/// the incoming stack arguments to be loaded from the stack.
3685SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3686 SmallVector<SDValue, 8> ArgChains;
3687
3688 // Include the original chain at the beginning of the list. When this is
3689 // used by target LowerCall hooks, this helps legalize find the
3690 // CALLSEQ_BEGIN node.
3691 ArgChains.push_back(Chain);
3692
3693 // Add a chain value for each stack argument.
3694 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3695 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3696 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3697 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3698 if (FI->getIndex() < 0)
3699 ArgChains.push_back(SDValue(L, 1));
3700
3701 // Build a tokenfactor for all the chains.
Craig Topper48d114b2014-04-26 18:35:24 +00003702 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003703}
3704
Dan Gohman544ab2c2008-04-12 04:36:06 +00003705/// getMemsetValue - Vectorized representation of the memset value
3706/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003707static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003708 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003709 assert(Value.getOpcode() != ISD::UNDEF);
3710
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003711 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003712 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003713 assert(C->getAPIntValue().getBitWidth() == 8);
3714 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003715 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003716 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003717 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003718 }
Evan Chengef377ad2008-05-15 08:39:06 +00003719
Dale Johannesenabf66b82009-02-03 22:26:09 +00003720 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003721 if (NumBits > 8) {
3722 // Use a multiplication with 0x010101... to extend the input to the
3723 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003724 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003725 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003726 }
3727
3728 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003729}
3730
Dan Gohman544ab2c2008-04-12 04:36:06 +00003731/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3732/// used when a memcpy is turned into a memset when the source is a constant
3733/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003734static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003735 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003736 // Handle vector with all elements zero.
3737 if (Str.empty()) {
3738 if (VT.isInteger())
3739 return DAG.getConstant(0, VT);
Chad Rosier52359732014-06-27 21:05:09 +00003740 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003741 return DAG.getConstantFP(0.0, VT);
3742 else if (VT.isVector()) {
3743 unsigned NumElts = VT.getVectorNumElements();
3744 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003745 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003746 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3747 EltVT, NumElts)));
3748 } else
3749 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003750 }
3751
Duncan Sands13237ac2008-06-06 12:08:01 +00003752 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003753 unsigned NumVTBits = VT.getSizeInBits();
3754 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003755 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3756
Evan Chengc8444b12013-01-10 22:13:27 +00003757 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003758 if (TLI.isLittleEndian()) {
3759 for (unsigned i = 0; i != NumBytes; ++i)
3760 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3761 } else {
3762 for (unsigned i = 0; i != NumBytes; ++i)
3763 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003764 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003765
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003766 // If the "cost" of materializing the integer immediate is less than the cost
3767 // of a load, then it is cost effective to turn the load into the immediate.
Juergen Ributzka659ce002014-01-28 01:20:14 +00003768 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3769 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng79e2ca92012-12-10 23:21:26 +00003770 return DAG.getConstant(Val, VT);
Craig Topperc0196b12014-04-14 00:51:57 +00003771 return SDValue(nullptr, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003772}
3773
Scott Michelcf0da6c2009-02-17 22:15:04 +00003774/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003775///
Andrew Tricke2431c62013-05-25 03:08:10 +00003776static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003777 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003778 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003779 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003780 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003781}
3782
Evan Chengef377ad2008-05-15 08:39:06 +00003783/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3784///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003785static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003786 unsigned SrcDelta = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003787 GlobalAddressSDNode *G = nullptr;
Evan Chengef377ad2008-05-15 08:39:06 +00003788 if (Src.getOpcode() == ISD::GlobalAddress)
3789 G = cast<GlobalAddressSDNode>(Src);
3790 else if (Src.getOpcode() == ISD::ADD &&
3791 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3792 Src.getOperand(1).getOpcode() == ISD::Constant) {
3793 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003794 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003795 }
3796 if (!G)
3797 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003798
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003799 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003800}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003801
Evan Cheng43cd9e32010-04-01 06:04:33 +00003802/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3803/// to replace the memset / memcpy. Return true if the number of memory ops
3804/// is below the threshold. It returns the types of the sequence of
3805/// memory ops to perform memset / memcpy by reference.
3806static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003807 unsigned Limit, uint64_t Size,
3808 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003809 bool IsMemset,
3810 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003811 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003812 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003813 SelectionDAG &DAG,
3814 const TargetLowering &TLI) {
3815 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3816 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003817 // If 'SrcAlign' is zero, that means the memory operation does not need to
3818 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3819 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3820 // is the specified alignment of the memory operation. If it is zero, that
3821 // means it's possible to change the alignment of the destination.
3822 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3823 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003824 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003825 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003826 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003827
Chris Lattner28dc6c12010-03-07 07:45:08 +00003828 if (VT == MVT::Other) {
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003829 unsigned AS = 0;
3830 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003831 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003832 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003833 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003834 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003835 case 0: VT = MVT::i64; break;
3836 case 4: VT = MVT::i32; break;
3837 case 2: VT = MVT::i16; break;
3838 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003839 }
3840 }
3841
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003842 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003843 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003844 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003845 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003846
Duncan Sands11dd4242008-06-08 20:54:56 +00003847 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003848 VT = LVT;
3849 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003850
Dan Gohman544ab2c2008-04-12 04:36:06 +00003851 unsigned NumMemOps = 0;
3852 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003853 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003854 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003855 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003856 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003857 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003858
3859 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003860 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003861 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003862 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003863 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003864 Found = true;
3865 else if (NewVT == MVT::i64 &&
3866 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003867 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003868 // i64 is usually not legal on 32-bit targets, but f64 may be.
3869 NewVT = MVT::f64;
3870 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003871 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003872 }
3873
Evan Cheng04e55182012-12-12 00:42:09 +00003874 if (!Found) {
3875 do {
3876 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3877 if (NewVT == MVT::i8)
3878 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003879 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003880 }
3881 NewVTSize = NewVT.getSizeInBits() / 8;
3882
Evan Cheng79e2ca92012-12-10 23:21:26 +00003883 // If the new VT cannot cover all of the remaining bits, then consider
3884 // issuing a (or a pair of) unaligned and overlapping load / store.
3885 // FIXME: Only does this for 64-bit or more since we don't have proper
3886 // cost model for unaligned load / store.
3887 bool Fast;
Matt Arsenault1b55dd92014-02-05 23:16:05 +00003888 unsigned AS = 0;
Evan Chengb7d3d032012-12-12 20:43:23 +00003889 if (NumMemOps && AllowOverlap &&
3890 VTSize >= 8 && NewVTSize < Size &&
Matt Arsenault6f2a5262014-07-27 17:46:40 +00003891 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003892 VTSize = Size;
3893 else {
3894 VT = NewVT;
3895 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003896 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003897 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003898
Evan Chengb7d3d032012-12-12 20:43:23 +00003899 if (++NumMemOps > Limit)
3900 return false;
3901
Dan Gohman544ab2c2008-04-12 04:36:06 +00003902 MemOps.push_back(VT);
3903 Size -= VTSize;
3904 }
3905
3906 return true;
3907}
3908
Andrew Trickef9de2a2013-05-25 02:42:55 +00003909static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003910 SDValue Chain, SDValue Dst,
3911 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003912 unsigned Align, bool isVol,
3913 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003914 MachinePointerInfo DstPtrInfo,
3915 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003916 // Turn a memcpy of undef to nop.
3917 if (Src.getOpcode() == ISD::UNDEF)
3918 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003919
Dan Gohman714663a2008-05-29 19:42:22 +00003920 // Expand memcpy to a series of load and store ops if the size operand falls
3921 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003922 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3923 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003924 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003925 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003926 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003927 MachineFunction &MF = DAG.getMachineFunction();
3928 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003929 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003930 MF.getFunction()->getAttributes().
3931 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003932 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3933 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3934 DstAlignCanChange = true;
3935 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3936 if (Align > SrcAlign)
3937 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003938 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003939 bool CopyFromStr = isMemSrcFromString(Src, Str);
3940 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003941 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003942
Evan Cheng4c014c82010-04-01 18:19:11 +00003943 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003944 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003945 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003946 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003947 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003948
Evan Cheng43cd9e32010-04-01 06:04:33 +00003949 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003950 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003951 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003952
3953 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003954 // realignment.
Eric Christopherfc6de422014-08-05 02:39:49 +00003955 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hamesdd478042013-01-31 20:23:43 +00003956 if (!TRI->needsStackRealignment(MF))
3957 while (NewAlign > Align &&
3958 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3959 NewAlign /= 2;
3960
Evan Cheng43cd9e32010-04-01 06:04:33 +00003961 if (NewAlign > Align) {
3962 // Give the stack frame object a larger alignment if needed.
3963 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3964 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3965 Align = NewAlign;
3966 }
3967 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003968
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003969 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003970 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003971 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003972 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003973 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003974 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003975 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003976
Evan Cheng79e2ca92012-12-10 23:21:26 +00003977 if (VTSize > Size) {
3978 // Issuing an unaligned load / store pair that overlaps with the previous
3979 // pair. Adjust the offset accordingly.
3980 assert(i == NumMemOps-1 && i != 0);
3981 SrcOff -= VTSize - Size;
3982 DstOff -= VTSize - Size;
3983 }
3984
Evan Cheng43cd9e32010-04-01 06:04:33 +00003985 if (CopyFromStr &&
3986 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003987 // It's unlikely a store of a vector immediate can be done in a single
3988 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003989 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003990 // FIXME: Handle other cases where store of vector immediate is done in
3991 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003992 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003993 if (Value.getNode())
3994 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003995 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003996 DstPtrInfo.getWithOffset(DstOff), isVol,
3997 false, Align);
3998 }
3999
4000 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00004001 // The type might not be legal for the target. This should only happen
4002 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00004003 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4004 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00004005 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00004006 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00004007 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00004008 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004009 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004010 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004011 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00004012 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004013 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004014 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4015 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004016 }
4017 OutChains.push_back(Store);
4018 SrcOff += VTSize;
4019 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004020 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004021 }
4022
Craig Topper48d114b2014-04-26 18:35:24 +00004023 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004024}
4025
Andrew Trickef9de2a2013-05-25 02:42:55 +00004026static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004027 SDValue Chain, SDValue Dst,
4028 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004029 unsigned Align, bool isVol,
4030 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004031 MachinePointerInfo DstPtrInfo,
4032 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004033 // Turn a memmove of undef to nop.
4034 if (Src.getOpcode() == ISD::UNDEF)
4035 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00004036
4037 // Expand memmove to a series of load and store ops if the size operand falls
4038 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004039 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004040 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004041 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004042 MachineFunction &MF = DAG.getMachineFunction();
4043 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004044 bool OptSize = MF.getFunction()->getAttributes().
4045 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004046 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4047 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4048 DstAlignCanChange = true;
4049 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4050 if (Align > SrcAlign)
4051 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004052 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004053
Evan Cheng4c014c82010-04-01 18:19:11 +00004054 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00004055 (DstAlignCanChange ? 0 : Align), SrcAlign,
4056 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004057 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00004058
Evan Cheng43cd9e32010-04-01 06:04:33 +00004059 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004060 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004061 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004062 if (NewAlign > Align) {
4063 // Give the stack frame object a larger alignment if needed.
4064 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4065 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4066 Align = NewAlign;
4067 }
4068 }
Dan Gohman714663a2008-05-29 19:42:22 +00004069
Evan Cheng43cd9e32010-04-01 06:04:33 +00004070 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004071 SmallVector<SDValue, 8> LoadValues;
4072 SmallVector<SDValue, 8> LoadChains;
4073 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00004074 unsigned NumMemOps = MemOps.size();
4075 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004076 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004077 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004078 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00004079
Dale Johannesenabf66b82009-02-03 22:26:09 +00004080 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00004081 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004082 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004083 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00004084 LoadValues.push_back(Value);
4085 LoadChains.push_back(Value.getValue(1));
4086 SrcOff += VTSize;
4087 }
Craig Topper48d114b2014-04-26 18:35:24 +00004088 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004089 OutChains.clear();
4090 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004091 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00004092 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00004093 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00004094
Dale Johannesenabf66b82009-02-03 22:26:09 +00004095 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00004096 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004097 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00004098 OutChains.push_back(Store);
4099 DstOff += VTSize;
4100 }
4101
Craig Topper48d114b2014-04-26 18:35:24 +00004102 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman714663a2008-05-29 19:42:22 +00004103}
4104
Serge Pavlov8ec39992013-09-17 16:24:42 +00004105/// \brief Lower the call to 'memset' intrinsic function into a series of store
4106/// operations.
4107///
4108/// \param DAG Selection DAG where lowered code is placed.
4109/// \param dl Link to corresponding IR location.
4110/// \param Chain Control flow dependency.
4111/// \param Dst Pointer to destination memory location.
4112/// \param Src Value of byte to write into the memory.
4113/// \param Size Number of bytes to write.
4114/// \param Align Alignment of the destination in bytes.
4115/// \param isVol True if destination is volatile.
4116/// \param DstPtrInfo IR information on the memory pointer.
4117/// \returns New head in the control flow, if lowering was successful, empty
4118/// SDValue otherwise.
4119///
4120/// The function tries to replace 'llvm.memset' intrinsic with several store
4121/// operations and value calculation code. This is usually profitable for small
4122/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004123static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00004124 SDValue Chain, SDValue Dst,
4125 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004126 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004127 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00004128 // Turn a memset of undef to nop.
4129 if (Src.getOpcode() == ISD::UNDEF)
4130 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004131
4132 // Expand memset to a series of load/store ops if the size operand
4133 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00004134 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004135 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00004136 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00004137 MachineFunction &MF = DAG.getMachineFunction();
4138 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00004139 bool OptSize = MF.getFunction()->getAttributes().
4140 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004141 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4142 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4143 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00004144 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00004145 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00004146 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00004147 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00004148 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004149 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00004150
Evan Cheng43cd9e32010-04-01 06:04:33 +00004151 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00004152 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00004153 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00004154 if (NewAlign > Align) {
4155 // Give the stack frame object a larger alignment if needed.
4156 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4157 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4158 Align = NewAlign;
4159 }
4160 }
4161
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004162 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00004163 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004164 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004165
4166 // Find the largest store and generate the bit pattern for it.
4167 EVT LargestVT = MemOps[0];
4168 for (unsigned i = 1; i < NumMemOps; i++)
4169 if (MemOps[i].bitsGT(LargestVT))
4170 LargestVT = MemOps[i];
4171 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4172
Dan Gohman544ab2c2008-04-12 04:36:06 +00004173 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004174 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00004175 unsigned VTSize = VT.getSizeInBits() / 8;
4176 if (VTSize > Size) {
4177 // Issuing an unaligned load / store pair that overlaps with the previous
4178 // pair. Adjust the offset accordingly.
4179 assert(i == NumMemOps-1 && i != 0);
4180 DstOff -= VTSize - Size;
4181 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004182
4183 // If this store is smaller than the largest store see whether we can get
4184 // the smaller value for free with a truncate.
4185 SDValue Value = MemSetValue;
4186 if (VT.bitsLT(LargestVT)) {
4187 if (!LargestVT.isVector() && !VT.isVector() &&
4188 TLI.isTruncateFree(LargestVT, VT))
4189 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4190 else
4191 Value = getMemsetValue(Src, VT, DAG, dl);
4192 }
4193 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004194 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004195 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004196 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004197 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004198 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004199 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004200 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004201 }
4202
Craig Topper48d114b2014-04-26 18:35:24 +00004203 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004204}
4205
Andrew Trickef9de2a2013-05-25 02:42:55 +00004206SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004207 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004208 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004209 MachinePointerInfo DstPtrInfo,
4210 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004211 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004212
4213 // Check to see if we should lower the memcpy to loads and stores first.
4214 // For cases within the target-specified limits, this is the best choice.
4215 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4216 if (ConstantSize) {
4217 // Memcpy with size zero? Just return the original chain.
4218 if (ConstantSize->isNullValue())
4219 return Chain;
4220
Evan Cheng43cd9e32010-04-01 06:04:33 +00004221 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4222 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004223 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004224 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004225 return Result;
4226 }
4227
4228 // Then check to see if we should lower the memcpy with target-specific
4229 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004230 SDValue Result =
Alexey Samsonove229ec52014-08-20 21:40:15 +00004231 TSI->EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
4232 isVol, AlwaysInline, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004233 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004234 return Result;
4235
4236 // If we really need inline code and the target declined to provide it,
4237 // use a (potentially long) sequence of loads and stores.
4238 if (AlwaysInline) {
4239 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004240 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004241 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004242 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004243 }
4244
Dan Gohmanf38547c2010-04-05 20:24:08 +00004245 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4246 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4247 // respect volatile, so they may do things like read or write memory
4248 // beyond the given memory regions. But fixing this isn't easy, and most
4249 // people don't care.
4250
Eric Christopherd9134482014-08-04 21:25:23 +00004251 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004252
Dan Gohman544ab2c2008-04-12 04:36:06 +00004253 // Emit a library call.
4254 TargetLowering::ArgListTy Args;
4255 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004256 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004257 Entry.Node = Dst; Args.push_back(Entry);
4258 Entry.Node = Src; Args.push_back(Entry);
4259 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004260 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004261 TargetLowering::CallLoweringInfo CLI(*this);
4262 CLI.setDebugLoc(dl).setChain(Chain)
4263 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4264 Type::getVoidTy(*getContext()),
4265 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004266 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004267 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004268 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004269
Dan Gohman544ab2c2008-04-12 04:36:06 +00004270 return CallResult.second;
4271}
4272
Andrew Trickef9de2a2013-05-25 02:42:55 +00004273SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004274 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004275 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004276 MachinePointerInfo DstPtrInfo,
4277 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004278 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004279
Dan Gohman714663a2008-05-29 19:42:22 +00004280 // Check to see if we should lower the memmove to loads and stores first.
4281 // For cases within the target-specified limits, this is the best choice.
4282 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4283 if (ConstantSize) {
4284 // Memmove with size zero? Just return the original chain.
4285 if (ConstantSize->isNullValue())
4286 return Chain;
4287
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004288 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004289 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004290 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004291 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004292 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004293 return Result;
4294 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004295
4296 // Then check to see if we should lower the memmove with target-specific
4297 // code. If the target chooses to do this, this is the next best.
Alexey Samsonove229ec52014-08-20 21:40:15 +00004298 SDValue Result = TSI->EmitTargetCodeForMemmove(
4299 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004300 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004301 return Result;
4302
Mon P Wangbf862242010-04-06 08:27:51 +00004303 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4304 // not be safe. See memcpy above for more details.
4305
Eric Christopherd9134482014-08-04 21:25:23 +00004306 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004307
Dan Gohman544ab2c2008-04-12 04:36:06 +00004308 // Emit a library call.
4309 TargetLowering::ArgListTy Args;
4310 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004311 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004312 Entry.Node = Dst; Args.push_back(Entry);
4313 Entry.Node = Src; Args.push_back(Entry);
4314 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004315 // FIXME: pass in SDLoc
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004316 TargetLowering::CallLoweringInfo CLI(*this);
4317 CLI.setDebugLoc(dl).setChain(Chain)
4318 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4319 Type::getVoidTy(*getContext()),
4320 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004321 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004322 .setDiscardResult();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004323 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004324
Dan Gohman544ab2c2008-04-12 04:36:06 +00004325 return CallResult.second;
4326}
4327
Andrew Trickef9de2a2013-05-25 02:42:55 +00004328SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004329 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004330 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004331 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004332 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004333
4334 // Check to see if we should lower the memset to stores first.
4335 // For cases within the target-specified limits, this is the best choice.
4336 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4337 if (ConstantSize) {
4338 // Memset with size zero? Just return the original chain.
4339 if (ConstantSize->isNullValue())
4340 return Chain;
4341
Mon P Wangc576ee92010-04-04 03:10:48 +00004342 SDValue Result =
4343 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004344 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004345
Gabor Greiff304a7a2008-08-28 21:40:38 +00004346 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004347 return Result;
4348 }
4349
4350 // Then check to see if we should lower the memset with target-specific
4351 // code. If the target chooses to do this, this is the next best.
Alexey Samsonove229ec52014-08-20 21:40:15 +00004352 SDValue Result = TSI->EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src,
4353 Size, Align, isVol, DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004354 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004355 return Result;
4356
Wesley Peck527da1b2010-11-23 03:31:01 +00004357 // Emit a library call.
Eric Christopherd9134482014-08-04 21:25:23 +00004358 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004359 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004360 TargetLowering::ArgListTy Args;
4361 TargetLowering::ArgListEntry Entry;
4362 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4363 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004364 Entry.Node = Src;
Job Noorman9b31bd62014-08-29 08:23:53 +00004365 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004366 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004367 Entry.Node = Size;
4368 Entry.Ty = IntPtrTy;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004369 Args.push_back(Entry);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004370
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004371 // FIXME: pass in SDLoc
4372 TargetLowering::CallLoweringInfo CLI(*this);
4373 CLI.setDebugLoc(dl).setChain(Chain)
4374 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4375 Type::getVoidTy(*getContext()),
4376 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00004377 TLI->getPointerTy()), std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00004378 .setDiscardResult();
4379
4380 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004381 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004382}
4383
Andrew Trickef9de2a2013-05-25 02:42:55 +00004384SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004385 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004386 MachineMemOperand *MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004387 AtomicOrdering SuccessOrdering,
4388 AtomicOrdering FailureOrdering,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004389 SynchronizationScope SynchScope) {
4390 FoldingSetNodeID ID;
4391 ID.AddInteger(MemVT.getRawBits());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004392 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004393 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004394 void* IP = nullptr;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4396 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4397 return SDValue(E, 0);
4398 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004399
4400 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4401 // SDNode doesn't have access to it. This memory will be "leaked" when
4402 // the node is deallocated, but recovered when the allocator is released.
4403 // If the number of operands is less than 5 we use AtomicSDNode's internal
4404 // storage.
Craig Topper8c0b4d02014-04-28 05:57:50 +00004405 unsigned NumOps = Ops.size();
4406 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4407 : nullptr;
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004408
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004409 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4410 dl.getDebugLoc(), VTList, MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004411 Ops.data(), DynOps, NumOps, MMO,
Tim Northovere94a5182014-03-11 10:48:52 +00004412 SuccessOrdering, FailureOrdering,
4413 SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004414 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004415 InsertNode(N);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004416 return SDValue(N, 0);
4417}
4418
4419SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Craig Topper8c0b4d02014-04-28 05:57:50 +00004420 SDVTList VTList, ArrayRef<SDValue> Ops,
Tim Northovere94a5182014-03-11 10:48:52 +00004421 MachineMemOperand *MMO,
4422 AtomicOrdering Ordering,
4423 SynchronizationScope SynchScope) {
Craig Topper8c0b4d02014-04-28 05:57:50 +00004424 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Tim Northovere94a5182014-03-11 10:48:52 +00004425 Ordering, SynchScope);
4426}
4427
Tim Northover420a2162014-06-13 14:24:07 +00004428SDValue SelectionDAG::getAtomicCmpSwap(
4429 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4430 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4431 unsigned Alignment, AtomicOrdering SuccessOrdering,
4432 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4433 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4434 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4435 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4436
Dan Gohman48b185d2009-09-25 20:36:54 +00004437 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4438 Alignment = getEVTAlignment(MemVT);
4439
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004440 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004441
Eli Friedmane978d2f2011-09-07 02:23:42 +00004442 // FIXME: Volatile isn't really correct; we should keep track of atomic
4443 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004444 unsigned Flags = MachineMemOperand::MOVolatile;
Tim Northover420a2162014-06-13 14:24:07 +00004445 Flags |= MachineMemOperand::MOLoad;
4446 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004447
4448 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004449 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004450
Tim Northover420a2162014-06-13 14:24:07 +00004451 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4452 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004453}
4454
Tim Northover420a2162014-06-13 14:24:07 +00004455SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4456 SDVTList VTs, SDValue Chain, SDValue Ptr,
4457 SDValue Cmp, SDValue Swp,
4458 MachineMemOperand *MMO,
4459 AtomicOrdering SuccessOrdering,
4460 AtomicOrdering FailureOrdering,
4461 SynchronizationScope SynchScope) {
4462 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4463 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004464 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4465
Dale Johannesen839acbb2009-01-29 00:47:48 +00004466 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Tim Northover420a2162014-06-13 14:24:07 +00004467 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4468 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004469}
4470
Andrew Trickef9de2a2013-05-25 02:42:55 +00004471SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004472 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004473 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004474 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004475 unsigned Alignment,
4476 AtomicOrdering Ordering,
4477 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004478 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4479 Alignment = getEVTAlignment(MemVT);
4480
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004481 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004482 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004483 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004484 // For now, atomics are considered to be volatile always, and they are
4485 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004486 // FIXME: Volatile isn't really correct; we should keep track of atomic
4487 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004488 unsigned Flags = MachineMemOperand::MOVolatile;
4489 if (Opcode != ISD::ATOMIC_STORE)
4490 Flags |= MachineMemOperand::MOLoad;
4491 if (Opcode != ISD::ATOMIC_LOAD)
4492 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004493
4494 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004495 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004496 MemVT.getStoreSize(), Alignment);
4497
Eli Friedmanadec5872011-07-29 03:05:32 +00004498 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4499 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004500}
4501
Andrew Trickef9de2a2013-05-25 02:42:55 +00004502SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004503 SDValue Chain,
4504 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004505 MachineMemOperand *MMO,
4506 AtomicOrdering Ordering,
4507 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004508 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4509 Opcode == ISD::ATOMIC_LOAD_SUB ||
4510 Opcode == ISD::ATOMIC_LOAD_AND ||
4511 Opcode == ISD::ATOMIC_LOAD_OR ||
4512 Opcode == ISD::ATOMIC_LOAD_XOR ||
4513 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004514 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004515 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004516 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004517 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004518 Opcode == ISD::ATOMIC_SWAP ||
4519 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004520 "Invalid Atomic Op");
4521
Owen Anderson53aa7a92009-08-10 22:56:29 +00004522 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004523
Eli Friedman342e8df2011-08-24 20:50:09 +00004524 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4525 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004526 SDValue Ops[] = {Chain, Ptr, Val};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004527 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004528}
4529
Andrew Trickef9de2a2013-05-25 02:42:55 +00004530SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004531 EVT VT, SDValue Chain,
4532 SDValue Ptr,
Eli Friedman342e8df2011-08-24 20:50:09 +00004533 MachineMemOperand *MMO,
4534 AtomicOrdering Ordering,
4535 SynchronizationScope SynchScope) {
4536 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4537
4538 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004539 SDValue Ops[] = {Chain, Ptr};
Craig Topper8c0b4d02014-04-28 05:57:50 +00004540 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004541}
4542
Duncan Sands739a0542008-07-02 17:40:58 +00004543/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Craig Topper64941d92014-04-27 19:20:57 +00004544SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4545 if (Ops.size() == 1)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004546 return Ops[0];
4547
Owen Anderson53aa7a92009-08-10 22:56:29 +00004548 SmallVector<EVT, 4> VTs;
Craig Topper64941d92014-04-27 19:20:57 +00004549 VTs.reserve(Ops.size());
4550 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesenae7992a2009-02-02 20:47:48 +00004551 VTs.push_back(Ops[i].getValueType());
Craig Topperbb533072014-04-27 19:21:02 +00004552 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesenae7992a2009-02-02 20:47:48 +00004553}
4554
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004555SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004556SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004557 ArrayRef<SDValue> Ops,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004558 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004559 unsigned Align, bool Vol,
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004560 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004561 if (Align == 0) // Ensure that codegen never sees alignment 0
4562 Align = getEVTAlignment(MemVT);
4563
4564 MachineFunction &MF = getMachineFunction();
4565 unsigned Flags = 0;
4566 if (WriteMem)
4567 Flags |= MachineMemOperand::MOStore;
4568 if (ReadMem)
4569 Flags |= MachineMemOperand::MOLoad;
4570 if (Vol)
4571 Flags |= MachineMemOperand::MOVolatile;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004572 if (!Size)
4573 Size = MemVT.getStoreSize();
Dan Gohman48b185d2009-09-25 20:36:54 +00004574 MachineMemOperand *MMO =
Hal Finkel46ef7ce2014-08-13 01:15:40 +00004575 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004576
Craig Topper206fcd42014-04-26 19:29:41 +00004577 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004578}
4579
4580SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004581SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Craig Topper206fcd42014-04-26 19:29:41 +00004582 ArrayRef<SDValue> Ops, EVT MemVT,
4583 MachineMemOperand *MMO) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004584 assert((Opcode == ISD::INTRINSIC_VOID ||
4585 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004586 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004587 Opcode == ISD::LIFETIME_START ||
4588 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004589 (Opcode <= INT_MAX &&
4590 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4591 "Opcode is not a memory-accessing opcode!");
4592
Dale Johannesen839acbb2009-01-29 00:47:48 +00004593 // Memoize the node unless it returns a flag.
4594 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004595 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004596 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004597 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper91244262012-07-30 20:23:19 +00004598 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004599 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004600 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4601 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004602 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004603 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004604
Jack Carter170a5f22013-09-09 22:02:08 +00004605 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4606 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004607 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004608 CSEMap.InsertNode(N, IP);
4609 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004610 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4611 dl.getDebugLoc(), VTList, Ops,
Craig Topper206fcd42014-04-26 19:29:41 +00004612 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004613 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00004614 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004615 return SDValue(N, 0);
4616}
4617
Chris Lattnerea952f02010-09-21 17:24:05 +00004618/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4619/// MachinePointerInfo record from it. This is particularly useful because the
4620/// code generator has many cases where it doesn't bother passing in a
4621/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4622static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4623 // If this is FI+Offset, we can model it.
4624 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4625 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4626
4627 // If this is (FI+Offset1)+Offset2, we can model it.
4628 if (Ptr.getOpcode() != ISD::ADD ||
4629 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4630 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4631 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004632
Chris Lattnerea952f02010-09-21 17:24:05 +00004633 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4634 return MachinePointerInfo::getFixedStack(FI, Offset+
4635 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4636}
4637
4638/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4639/// MachinePointerInfo record from it. This is particularly useful because the
4640/// code generator has many cases where it doesn't bother passing in a
4641/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4642static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4643 // If the 'Offset' value isn't a constant, we can't handle this.
4644 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4645 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4646 if (OffsetOp.getOpcode() == ISD::UNDEF)
4647 return InferPointerInfo(Ptr);
4648 return MachinePointerInfo();
4649}
Wesley Peck527da1b2010-11-23 03:31:01 +00004650
Chris Lattnerea952f02010-09-21 17:24:05 +00004651
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004652SDValue
4653SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004654 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004655 SDValue Ptr, SDValue Offset,
4656 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004657 bool isVolatile, bool isNonTemporal, bool isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00004658 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004659 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004660 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004661 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004662 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4663 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004664
Dan Gohman48b185d2009-09-25 20:36:54 +00004665 unsigned Flags = MachineMemOperand::MOLoad;
4666 if (isVolatile)
4667 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004668 if (isNonTemporal)
4669 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004670 if (isInvariant)
4671 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004672
Chris Lattnerea952f02010-09-21 17:24:05 +00004673 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4674 // clients.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004675 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004676 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004677
Chris Lattnerea952f02010-09-21 17:24:05 +00004678 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004679 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004680 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004681 AAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004682 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004683}
4684
4685SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004686SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004687 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004688 SDValue Ptr, SDValue Offset, EVT MemVT,
4689 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004690 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004691 ExtType = ISD::NON_EXTLOAD;
4692 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004693 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004694 } else {
4695 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004696 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4697 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004698 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004699 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004700 assert(VT.isVector() == MemVT.isVector() &&
4701 "Cannot use trunc store to convert to or from a vector!");
4702 assert((!VT.isVector() ||
4703 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4704 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004705 }
4706
4707 bool Indexed = AM != ISD::UNINDEXED;
4708 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4709 "Unindexed load with an offset!");
4710
4711 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004712 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004713 SDValue Ops[] = { Chain, Ptr, Offset };
4714 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004715 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman08c0a952009-09-23 21:02:20 +00004716 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004717 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004718 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004719 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004720 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004721 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004722 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4723 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004724 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004725 }
Jack Carter170a5f22013-09-09 22:02:08 +00004726 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4727 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004728 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004729 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004730 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004731 return SDValue(N, 0);
4732}
4733
Andrew Trickef9de2a2013-05-25 02:42:55 +00004734SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004735 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004736 MachinePointerInfo PtrInfo,
4737 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004738 bool isInvariant, unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004739 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004740 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004741 SDValue Undef = getUNDEF(Ptr.getValueType());
4742 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004743 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004744 AAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004745}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004746
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004747SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4748 SDValue Chain, SDValue Ptr,
4749 MachineMemOperand *MMO) {
4750 SDValue Undef = getUNDEF(Ptr.getValueType());
4751 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4752 VT, MMO);
4753}
4754
Andrew Trickef9de2a2013-05-25 02:42:55 +00004755SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004756 SDValue Chain, SDValue Ptr,
4757 MachinePointerInfo PtrInfo, EVT MemVT,
4758 bool isVolatile, bool isNonTemporal,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004759 bool isInvariant, unsigned Alignment,
4760 const AAMDNodes &AAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004761 SDValue Undef = getUNDEF(Ptr.getValueType());
4762 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004763 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4764 Alignment, AAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004765}
4766
4767
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004768SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4769 SDValue Chain, SDValue Ptr, EVT MemVT,
4770 MachineMemOperand *MMO) {
4771 SDValue Undef = getUNDEF(Ptr.getValueType());
4772 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4773 MemVT, MMO);
4774}
4775
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004776SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004777SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004778 SDValue Offset, ISD::MemIndexedMode AM) {
4779 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4780 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4781 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004782 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004783 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004784 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004785 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004786}
4787
Andrew Trickef9de2a2013-05-25 02:42:55 +00004788SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004789 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004790 bool isVolatile, bool isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +00004791 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004792 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004793 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004794 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004795 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004796
Dan Gohman48b185d2009-09-25 20:36:54 +00004797 unsigned Flags = MachineMemOperand::MOStore;
4798 if (isVolatile)
4799 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004800 if (isNonTemporal)
4801 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004802
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004803 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004804 PtrInfo = InferPointerInfo(Ptr);
4805
4806 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004807 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004808 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004809 Val.getValueType().getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004810 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004811
4812 return getStore(Chain, dl, Val, Ptr, MMO);
4813}
4814
Andrew Trickef9de2a2013-05-25 02:42:55 +00004815SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004816 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004817 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004818 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004819 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004820 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004821 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004822 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4823 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004824 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004825 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004826 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004827 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004828 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004829 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004830 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4831 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004832 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004833 }
Jack Carter170a5f22013-09-09 22:02:08 +00004834 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4835 dl.getDebugLoc(), VTs,
4836 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004837 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004838 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004839 return SDValue(N, 0);
4840}
4841
Andrew Trickef9de2a2013-05-25 02:42:55 +00004842SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004843 SDValue Ptr, MachinePointerInfo PtrInfo,
4844 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004845 unsigned Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004846 const AAMDNodes &AAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004847 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004848 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004849 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4850 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004851
Dan Gohman48b185d2009-09-25 20:36:54 +00004852 unsigned Flags = MachineMemOperand::MOStore;
4853 if (isVolatile)
4854 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004855 if (isNonTemporal)
4856 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004857
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004858 if (PtrInfo.V.isNull())
Chris Lattnerea952f02010-09-21 17:24:05 +00004859 PtrInfo = InferPointerInfo(Ptr);
4860
4861 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004862 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004863 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +00004864 AAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004865
4866 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4867}
4868
Andrew Trickef9de2a2013-05-25 02:42:55 +00004869SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004870 SDValue Ptr, EVT SVT,
4871 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004872 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004873
Michael Ilsemand5f91512012-09-10 16:56:31 +00004874 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004875 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004876 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004877 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004878
Dan Gohmancecad352009-12-14 23:40:38 +00004879 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4880 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004881 assert(VT.isInteger() == SVT.isInteger() &&
4882 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004883 assert(VT.isVector() == SVT.isVector() &&
4884 "Cannot use trunc store to convert to or from a vector!");
4885 assert((!VT.isVector() ||
4886 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4887 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004888
Owen Anderson9f944592009-08-11 20:47:22 +00004889 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004890 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004891 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4892 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004893 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004894 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004895 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004896 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004897 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004898 void *IP = nullptr;
Dan Gohman48b185d2009-09-25 20:36:54 +00004899 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4900 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004901 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004902 }
Jack Carter170a5f22013-09-09 22:02:08 +00004903 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4904 dl.getDebugLoc(), VTs,
4905 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004906 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004907 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004908 return SDValue(N, 0);
4909}
4910
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004911SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004912SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004913 SDValue Offset, ISD::MemIndexedMode AM) {
4914 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4915 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4916 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004917 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004918 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4919 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004920 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004921 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004922 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004923 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Craig Topperc0196b12014-04-14 00:51:57 +00004924 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00004925 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004926 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004927
Jack Carter170a5f22013-09-09 22:02:08 +00004928 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4929 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004930 ST->isTruncatingStore(),
4931 ST->getMemoryVT(),
4932 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004933 CSEMap.InsertNode(N, IP);
Chandler Carruth41b20e72014-07-22 04:07:55 +00004934 InsertNode(N);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004935 return SDValue(N, 0);
4936}
4937
Andrew Trickef9de2a2013-05-25 02:42:55 +00004938SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004939 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004940 SDValue SV,
4941 unsigned Align) {
4942 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Craig Topper48d114b2014-04-26 18:35:24 +00004943 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004944}
4945
Andrew Trickef9de2a2013-05-25 02:42:55 +00004946SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topperdd5e16d2014-04-27 19:21:06 +00004947 ArrayRef<SDUse> Ops) {
4948 switch (Ops.size()) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004949 case 0: return getNode(Opcode, DL, VT);
Craig Topperdd5e16d2014-04-27 19:21:06 +00004950 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004951 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4952 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004953 default: break;
4954 }
4955
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004956 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004957 // the regular getNode logic.
Craig Topperdd5e16d2014-04-27 19:21:06 +00004958 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
Craig Topper48d114b2014-04-26 18:35:24 +00004959 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004960}
4961
Andrew Trickef9de2a2013-05-25 02:42:55 +00004962SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Craig Topper48d114b2014-04-26 18:35:24 +00004963 ArrayRef<SDValue> Ops) {
4964 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00004965 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004966 case 0: return getNode(Opcode, DL, VT);
4967 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4968 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4969 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004970 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004971 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004972
Chris Lattnerb0713c72005-04-09 03:27:28 +00004973 switch (Opcode) {
4974 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004975 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004976 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004977 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4978 "LHS and RHS of condition must have same type!");
4979 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4980 "True and False arms of SelectCC must have same type!");
4981 assert(Ops[2].getValueType() == VT &&
4982 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004983 break;
4984 }
4985 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004986 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004987 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4988 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004989 break;
4990 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004991 }
4992
Chris Lattner566307f2005-05-14 07:42:29 +00004993 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004994 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004995 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004996
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004997 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004998 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00004999 AddNodeIDNode(ID, Opcode, VTs, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005000 void *IP = nullptr;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005001
Bill Wendling022d18f2009-12-18 23:32:53 +00005002 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005003 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005004
Jack Carter170a5f22013-09-09 22:02:08 +00005005 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005006 VTs, Ops);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005007 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005008 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005009 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005010 VTs, Ops);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005011 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005012
Chandler Carruth41b20e72014-07-22 04:07:55 +00005013 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005014 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00005015}
5016
Andrew Trickef9de2a2013-05-25 02:42:55 +00005017SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Craig Topper48d114b2014-04-26 18:35:24 +00005018 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5019 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Chris Lattner3bf4be42006-08-14 23:31:51 +00005020}
5021
Andrew Trickef9de2a2013-05-25 02:42:55 +00005022SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Craig Topper48d114b2014-04-26 18:35:24 +00005023 ArrayRef<SDValue> Ops) {
Chris Lattner65879ca2006-08-16 22:57:46 +00005024 if (VTList.NumVTs == 1)
Craig Topper48d114b2014-04-26 18:35:24 +00005025 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattnerd5531332005-05-14 06:20:26 +00005026
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005027#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005028 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00005029 // FIXME: figure out how to safely handle things like
5030 // int foo(int x) { return 1 << (x & 255); }
5031 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00005032 case ISD::SRA_PARTS:
5033 case ISD::SRL_PARTS:
5034 case ISD::SHL_PARTS:
5035 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00005036 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005037 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005038 else if (N3.getOpcode() == ISD::AND)
5039 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5040 // If the and is only masking out bits that cannot effect the shift,
5041 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005042 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00005043 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005044 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00005045 }
5046 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00005047 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00005048#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00005049
Chris Lattnerf9c19152005-08-25 19:12:10 +00005050 // Memoize the node unless it returns a flag.
5051 SDNode *N;
Craig Topper48d114b2014-04-26 18:35:24 +00005052 unsigned NumOps = Ops.size();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005053 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00005054 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005055 AddNodeIDNode(ID, Opcode, VTList, Ops);
Craig Topperc0196b12014-04-14 00:51:57 +00005056 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005057 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005058 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00005059
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005060 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005061 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5062 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005063 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005064 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5065 DL.getDebugLoc(), VTList, Ops[0],
5066 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005067 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005068 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5069 DL.getDebugLoc(), VTList, Ops[0],
5070 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005071 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005072 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005073 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005074 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005075 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00005076 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005077 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00005078 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5079 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005080 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00005081 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5082 DL.getDebugLoc(), VTList, Ops[0],
5083 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005084 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00005085 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5086 DL.getDebugLoc(), VTList, Ops[0],
5087 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005088 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00005089 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Craig Topperbb533072014-04-27 19:21:02 +00005090 VTList, Ops);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00005091 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00005092 }
Chandler Carruth41b20e72014-07-22 04:07:55 +00005093 InsertNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005094 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00005095}
5096
Andrew Trickef9de2a2013-05-25 02:42:55 +00005097SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Craig Toppere1d12942014-08-27 05:25:25 +00005098 return getNode(Opcode, DL, VTList, None);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005099}
5100
Andrew Trickef9de2a2013-05-25 02:42:55 +00005101SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005102 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005103 SDValue Ops[] = { N1 };
Craig Topper48d114b2014-04-26 18:35:24 +00005104 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005105}
5106
Andrew Trickef9de2a2013-05-25 02:42:55 +00005107SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005108 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005109 SDValue Ops[] = { N1, N2 };
Craig Topper48d114b2014-04-26 18:35:24 +00005110 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005111}
5112
Andrew Trickef9de2a2013-05-25 02:42:55 +00005113SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005114 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005115 SDValue Ops[] = { N1, N2, N3 };
Craig Topper48d114b2014-04-26 18:35:24 +00005116 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005117}
5118
Andrew Trickef9de2a2013-05-25 02:42:55 +00005119SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005120 SDValue N1, SDValue N2, SDValue N3,
5121 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005122 SDValue Ops[] = { N1, N2, N3, N4 };
Craig Topper48d114b2014-04-26 18:35:24 +00005123 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005124}
5125
Andrew Trickef9de2a2013-05-25 02:42:55 +00005126SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005127 SDValue N1, SDValue N2, SDValue N3,
5128 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005129 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Craig Topper48d114b2014-04-26 18:35:24 +00005130 return getNode(Opcode, DL, VTList, Ops);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005131}
5132
Owen Anderson53aa7a92009-08-10 22:56:29 +00005133SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005134 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005135}
5136
Owen Anderson53aa7a92009-08-10 22:56:29 +00005137SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005138 FoldingSetNodeID ID;
5139 ID.AddInteger(2U);
5140 ID.AddInteger(VT1.getRawBits());
5141 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005142
Craig Topperc0196b12014-04-14 00:51:57 +00005143 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005144 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005145 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005146 EVT *Array = Allocator.Allocate<EVT>(2);
5147 Array[0] = VT1;
5148 Array[1] = VT2;
5149 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5150 VTListMap.InsertNode(Result, IP);
5151 }
5152 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005153}
Dan Gohman17059682008-07-17 19:10:17 +00005154
Owen Anderson53aa7a92009-08-10 22:56:29 +00005155SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005156 FoldingSetNodeID ID;
5157 ID.AddInteger(3U);
5158 ID.AddInteger(VT1.getRawBits());
5159 ID.AddInteger(VT2.getRawBits());
5160 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005161
Craig Topperc0196b12014-04-14 00:51:57 +00005162 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005163 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005164 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005165 EVT *Array = Allocator.Allocate<EVT>(3);
5166 Array[0] = VT1;
5167 Array[1] = VT2;
5168 Array[2] = VT3;
5169 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5170 VTListMap.InsertNode(Result, IP);
5171 }
5172 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005173}
5174
Owen Anderson53aa7a92009-08-10 22:56:29 +00005175SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005176 FoldingSetNodeID ID;
5177 ID.AddInteger(4U);
5178 ID.AddInteger(VT1.getRawBits());
5179 ID.AddInteger(VT2.getRawBits());
5180 ID.AddInteger(VT3.getRawBits());
5181 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005182
Craig Topperc0196b12014-04-14 00:51:57 +00005183 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005184 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005185 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005186 EVT *Array = Allocator.Allocate<EVT>(4);
5187 Array[0] = VT1;
5188 Array[1] = VT2;
5189 Array[2] = VT3;
5190 Array[3] = VT4;
5191 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5192 VTListMap.InsertNode(Result, IP);
5193 }
5194 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005195}
5196
Craig Topperabb4ac72014-04-16 06:10:51 +00005197SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5198 unsigned NumVTs = VTs.size();
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005199 FoldingSetNodeID ID;
5200 ID.AddInteger(NumVTs);
5201 for (unsigned index = 0; index < NumVTs; index++) {
5202 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005203 }
5204
Craig Topperc0196b12014-04-14 00:51:57 +00005205 void *IP = nullptr;
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005206 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Craig Topperc0196b12014-04-14 00:51:57 +00005207 if (!Result) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005208 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Craig Topperabb4ac72014-04-16 06:10:51 +00005209 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005210 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5211 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005212 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005213 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005214}
5215
5216
Chris Lattnerf34156e2006-01-28 09:32:45 +00005217/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5218/// specified operands. If the resultant node already exists in the DAG,
5219/// this does not modify the specified node, instead it returns the node that
5220/// already exists. If the resultant node does not exist in the DAG, the
5221/// input node is returned. As a degenerate case, if you specify the same
5222/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005223SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005224 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005225
Chris Lattnerf34156e2006-01-28 09:32:45 +00005226 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005227 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005228
Chris Lattnerf34156e2006-01-28 09:32:45 +00005229 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005230 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005231 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005232 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005233
Dan Gohmanebeccb42008-07-21 22:38:59 +00005234 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005235 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005236 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005237 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005238
Chris Lattnerf34156e2006-01-28 09:32:45 +00005239 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005240 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005241
Chris Lattnerf34156e2006-01-28 09:32:45 +00005242 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005243 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005244 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005245}
5246
Dan Gohman92c11ac2010-06-18 15:30:29 +00005247SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005248 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005249
Chris Lattnerf34156e2006-01-28 09:32:45 +00005250 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005251 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005252 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005253
Chris Lattnerf34156e2006-01-28 09:32:45 +00005254 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005255 void *InsertPos = nullptr;
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005256 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005257 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005258
Dan Gohmanebeccb42008-07-21 22:38:59 +00005259 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005260 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005261 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005262 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005263
Chris Lattnerf34156e2006-01-28 09:32:45 +00005264 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005265 if (N->OperandList[0] != Op1)
5266 N->OperandList[0].set(Op1);
5267 if (N->OperandList[1] != Op2)
5268 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005269
Chris Lattnerf34156e2006-01-28 09:32:45 +00005270 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005271 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005272 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005273}
5274
Dan Gohman92c11ac2010-06-18 15:30:29 +00005275SDNode *SelectionDAG::
5276UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005277 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005278 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005279}
5280
Dan Gohman92c11ac2010-06-18 15:30:29 +00005281SDNode *SelectionDAG::
5282UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005283 SDValue Op3, SDValue Op4) {
5284 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005285 return UpdateNodeOperands(N, Ops);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005286}
5287
Dan Gohman92c11ac2010-06-18 15:30:29 +00005288SDNode *SelectionDAG::
5289UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005290 SDValue Op3, SDValue Op4, SDValue Op5) {
5291 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Craig Topper8c0b4d02014-04-28 05:57:50 +00005292 return UpdateNodeOperands(N, Ops);
Chris Lattner580b12a2006-01-28 10:09:25 +00005293}
5294
Dan Gohman92c11ac2010-06-18 15:30:29 +00005295SDNode *SelectionDAG::
Craig Topper8c0b4d02014-04-28 05:57:50 +00005296UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5297 unsigned NumOps = Ops.size();
Chris Lattner97af9d52006-08-08 01:09:31 +00005298 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005299 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005300
Chris Lattnerf34156e2006-01-28 09:32:45 +00005301 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005302 bool AnyChange = false;
5303 for (unsigned i = 0; i != NumOps; ++i) {
5304 if (Ops[i] != N->getOperand(i)) {
5305 AnyChange = true;
5306 break;
5307 }
5308 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005309
Chris Lattnerf34156e2006-01-28 09:32:45 +00005310 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005311 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005312
Chris Lattnerf34156e2006-01-28 09:32:45 +00005313 // See if the modified node already exists.
Craig Topperc0196b12014-04-14 00:51:57 +00005314 void *InsertPos = nullptr;
Craig Topper8c0b4d02014-04-28 05:57:50 +00005315 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005316 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005317
Dan Gohman2f83b472008-05-02 00:05:03 +00005318 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005319 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005320 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005321 InsertPos = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005322
Chris Lattnerf34156e2006-01-28 09:32:45 +00005323 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005324 for (unsigned i = 0; i != NumOps; ++i)
5325 if (N->OperandList[i] != Ops[i])
5326 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005327
5328 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005329 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005330 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005331}
5332
Dan Gohman91697632008-07-07 20:57:48 +00005333/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005334/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005335void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005336 // Unlike the code in MorphNodeTo that does this, we don't need to
5337 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005338 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5339 SDUse &Use = *I++;
5340 Use.set(SDValue());
5341 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005342}
Chris Lattner19732782005-08-16 18:17:10 +00005343
Dan Gohman17059682008-07-17 19:10:17 +00005344/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5345/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005346///
Dan Gohman17059682008-07-17 19:10:17 +00005347SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005348 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005349 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005350 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005351}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005352
Dan Gohman17059682008-07-17 19:10:17 +00005353SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005354 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005355 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005356 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005357 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005358}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005359
Dan Gohman17059682008-07-17 19:10:17 +00005360SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005361 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005362 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005363 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005364 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005365 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005366}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005367
Dan Gohman17059682008-07-17 19:10:17 +00005368SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005369 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005370 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005371 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005372 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005373 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner19732782005-08-16 18:17:10 +00005374}
Chris Lattner466fece2005-08-21 22:30:30 +00005375
Dan Gohman17059682008-07-17 19:10:17 +00005376SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005377 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005378 SDVTList VTs = getVTList(VT);
Craig Topper481fb282014-04-27 19:21:11 +00005379 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005380}
5381
Dan Gohman17059682008-07-17 19:10:17 +00005382SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005383 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005384 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005385 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005386}
5387
Dan Gohman17059682008-07-17 19:10:17 +00005388SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005389 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005390 SDVTList VTs = getVTList(VT1, VT2);
Craig Topper481fb282014-04-27 19:21:11 +00005391 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohman22e97072008-07-02 23:23:19 +00005392}
5393
Dan Gohman17059682008-07-17 19:10:17 +00005394SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005395 EVT VT1, EVT VT2, EVT VT3,
Craig Topper481fb282014-04-27 19:21:11 +00005396 ArrayRef<SDValue> Ops) {
Dan Gohman22e97072008-07-02 23:23:19 +00005397 SDVTList VTs = getVTList(VT1, VT2, VT3);
Craig Topper481fb282014-04-27 19:21:11 +00005398 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005399}
5400
Bill Wendling2d598632008-12-01 23:28:22 +00005401SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005402 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Craig Topper481fb282014-04-27 19:21:11 +00005403 ArrayRef<SDValue> Ops) {
Bill Wendling2d598632008-12-01 23:28:22 +00005404 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Craig Topper481fb282014-04-27 19:21:11 +00005405 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005406}
5407
Scott Michelcf0da6c2009-02-17 22:15:04 +00005408SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005409 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005410 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005411 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005412 SDValue Ops[] = { Op1 };
Craig Topper481fb282014-04-27 19:21:11 +00005413 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth683352382006-01-23 21:51:14 +00005414}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005415
Scott Michelcf0da6c2009-02-17 22:15:04 +00005416SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005417 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005418 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005419 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005420 SDValue Ops[] = { Op1, Op2 };
Craig Topper481fb282014-04-27 19:21:11 +00005421 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005422}
5423
Dan Gohman17059682008-07-17 19:10:17 +00005424SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005425 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005426 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005427 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005428 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005429 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005430 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohman22e97072008-07-02 23:23:19 +00005431}
5432
Dan Gohman17059682008-07-17 19:10:17 +00005433SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005434 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005435 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005436 SDValue Op3) {
5437 SDVTList VTs = getVTList(VT1, VT2, VT3);
5438 SDValue Ops[] = { Op1, Op2, Op3 };
Craig Topper481fb282014-04-27 19:21:11 +00005439 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling2d598632008-12-01 23:28:22 +00005440}
5441
5442SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Craig Topper481fb282014-04-27 19:21:11 +00005443 SDVTList VTs,ArrayRef<SDValue> Ops) {
Craig Topper131de822014-04-27 19:21:16 +00005444 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner625916d2010-02-23 23:01:35 +00005445 // Reset the NodeID to -1.
5446 N->setNodeId(-1);
5447 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005448}
5449
Andrew Trickef9de2a2013-05-25 02:42:55 +00005450/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005451/// the line number information on the merged node since it is not possible to
5452/// preserve the information that operation is associated with multiple lines.
5453/// This will make the debugger working better at -O0, were there is a higher
5454/// probability having other instructions associated with that line.
5455///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005456/// For IROrder, we keep the smaller of the two
5457SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005458 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005459 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5460 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005461 N->setDebugLoc(DebugLoc());
5462 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005463 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5464 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005465 return N;
5466}
5467
Chris Lattneraf197502010-02-28 21:36:14 +00005468/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005469/// return type, opcode, and operands.
5470///
5471/// Note that MorphNodeTo returns the resultant node. If there is already a
5472/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005473/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005474///
5475/// Using MorphNodeTo is faster than creating a new node and swapping it in
5476/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005477/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005478/// the node's users.
5479///
Chandler Carruth356665a2014-08-01 22:09:43 +00005480/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5481/// As a consequence it isn't appropriate to use from within the DAG combiner or
5482/// the legalizer which maintain worklists that would need to be updated when
5483/// deleting things.
Dan Gohman17059682008-07-17 19:10:17 +00005484SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Craig Topper131de822014-04-27 19:21:16 +00005485 SDVTList VTs, ArrayRef<SDValue> Ops) {
5486 unsigned NumOps = Ops.size();
Dan Gohman22e97072008-07-02 23:23:19 +00005487 // If an identical node already exists, use it.
Craig Topperc0196b12014-04-14 00:51:57 +00005488 void *IP = nullptr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005489 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005490 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005491 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendling022d18f2009-12-18 23:32:53 +00005492 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005493 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005494 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005495
Dan Gohmand3fe1742008-09-13 01:54:27 +00005496 if (!RemoveNodeFromCSEMaps(N))
Craig Topperc0196b12014-04-14 00:51:57 +00005497 IP = nullptr;
Chris Lattner486edfb2007-02-04 02:32:44 +00005498
Dan Gohman17059682008-07-17 19:10:17 +00005499 // Start the morphing.
5500 N->NodeType = Opc;
5501 N->ValueList = VTs.VTs;
5502 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005503
Dan Gohman17059682008-07-17 19:10:17 +00005504 // Clear the operands list, updating used nodes to remove this from their
5505 // use list. Keep track of any operands that become dead as a result.
5506 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005507 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5508 SDUse &Use = *I++;
5509 SDNode *Used = Use.getNode();
5510 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005511 if (Used->use_empty())
5512 DeadNodeSet.insert(Used);
5513 }
5514
Dan Gohman48b185d2009-09-25 20:36:54 +00005515 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5516 // Initialize the memory references information.
Craig Topperc0196b12014-04-14 00:51:57 +00005517 MN->setMemRefs(nullptr, nullptr);
Dan Gohman48b185d2009-09-25 20:36:54 +00005518 // If NumOps is larger than the # of operands we can have in a
5519 // MachineSDNode, reallocate the operand list.
5520 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5521 if (MN->OperandsNeedDelete)
5522 delete[] MN->OperandList;
5523 if (NumOps > array_lengthof(MN->LocalOperands))
5524 // We're creating a final node that will live unmorphed for the
5525 // remainder of the current SelectionDAG iteration, so we can allocate
5526 // the operands directly out of a pool with no recycling metadata.
5527 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Craig Topper131de822014-04-27 19:21:16 +00005528 Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005529 else
Craig Topper131de822014-04-27 19:21:16 +00005530 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005531 MN->OperandsNeedDelete = false;
5532 } else
Craig Topper131de822014-04-27 19:21:16 +00005533 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005534 } else {
5535 // If NumOps is larger than the # of operands we currently have, reallocate
5536 // the operand list.
5537 if (NumOps > N->NumOperands) {
5538 if (N->OperandsNeedDelete)
5539 delete[] N->OperandList;
Craig Topper131de822014-04-27 19:21:16 +00005540 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005541 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005542 } else
Craig Topper131de822014-04-27 19:21:16 +00005543 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005544 }
5545
5546 // Delete any nodes that are still dead after adding the uses for the
5547 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005548 if (!DeadNodeSet.empty()) {
5549 SmallVector<SDNode *, 16> DeadNodes;
Craig Topper46276792014-08-24 23:23:06 +00005550 for (SDNode *N : DeadNodeSet)
5551 if (N->use_empty())
5552 DeadNodes.push_back(N);
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005553 RemoveDeadNodes(DeadNodes);
5554 }
Dan Gohman91697632008-07-07 20:57:48 +00005555
Dan Gohman17059682008-07-17 19:10:17 +00005556 if (IP)
5557 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005558 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005559}
5560
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005561
Dan Gohman32f71d72009-09-25 18:54:59 +00005562/// getMachineNode - These are used for target selectors to create a new node
5563/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005564///
Dan Gohman32f71d72009-09-25 18:54:59 +00005565/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005566/// node of the specified opcode and operands, it returns that node instead of
5567/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005568MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005569SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005570 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005571 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005572}
Bill Wendlinga434d932009-01-29 09:01:55 +00005573
Dan Gohmana22f2d82009-10-10 01:29:16 +00005574MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005575SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005576 SDVTList VTs = getVTList(VT);
5577 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005578 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005579}
Bill Wendlinga434d932009-01-29 09:01:55 +00005580
Dan Gohmana22f2d82009-10-10 01:29:16 +00005581MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005582SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005583 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005584 SDVTList VTs = getVTList(VT);
5585 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005586 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005587}
Bill Wendlinga434d932009-01-29 09:01:55 +00005588
Dan Gohmana22f2d82009-10-10 01:29:16 +00005589MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005590SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005591 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005592 SDVTList VTs = getVTList(VT);
5593 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005594 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005595}
Bill Wendlinga434d932009-01-29 09:01:55 +00005596
Dan Gohmana22f2d82009-10-10 01:29:16 +00005597MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005598SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005599 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005600 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005601 return getMachineNode(Opcode, dl, VTs, Ops);
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, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005606 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005607 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005608}
Bill Wendlinga434d932009-01-29 09:01:55 +00005609
Dan Gohmana22f2d82009-10-10 01:29:16 +00005610MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005611SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005612 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005613 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005614 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005615 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005616}
Bill Wendlinga434d932009-01-29 09:01:55 +00005617
Dan Gohmana22f2d82009-10-10 01:29:16 +00005618MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005619SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005620 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005621 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005622 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005623 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005624}
5625
Dan Gohmana22f2d82009-10-10 01:29:16 +00005626MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005627SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005628 EVT VT1, EVT VT2, SDValue Op1,
5629 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005630 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005631 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005632 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005633}
5634
Dan Gohmana22f2d82009-10-10 01:29:16 +00005635MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005636SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005637 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005638 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005639 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005640 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005641}
Bill Wendlinga434d932009-01-29 09:01:55 +00005642
Dan Gohmana22f2d82009-10-10 01:29:16 +00005643MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005644SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005645 EVT VT1, EVT VT2, EVT VT3,
5646 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005647 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005648 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005649 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005650}
Bill Wendlinga434d932009-01-29 09:01:55 +00005651
Dan Gohmana22f2d82009-10-10 01:29:16 +00005652MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005653SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005654 EVT VT1, EVT VT2, EVT VT3,
5655 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005656 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005657 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005658 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005659}
Bill Wendlinga434d932009-01-29 09:01:55 +00005660
Dan Gohmana22f2d82009-10-10 01:29:16 +00005661MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005662SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005663 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005664 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005665 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005666 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005667}
Bill Wendlinga434d932009-01-29 09:01:55 +00005668
Dan Gohmana22f2d82009-10-10 01:29:16 +00005669MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005670SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005671 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005672 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005673 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005674 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005675}
5676
Dan Gohmana22f2d82009-10-10 01:29:16 +00005677MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005678SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005679 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005680 ArrayRef<SDValue> Ops) {
Craig Topperabb4ac72014-04-16 06:10:51 +00005681 SDVTList VTs = getVTList(ResultTys);
Michael Liaob53d8962013-04-19 22:22:57 +00005682 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005683}
5684
Dan Gohmana22f2d82009-10-10 01:29:16 +00005685MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005686SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005687 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005688 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005689 MachineSDNode *N;
Craig Topperc0196b12014-04-14 00:51:57 +00005690 void *IP = nullptr;
Michael Liaob53d8962013-04-19 22:22:57 +00005691 const SDValue *Ops = OpsArray.data();
5692 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005693
5694 if (DoCSE) {
5695 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005696 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
Craig Topperc0196b12014-04-14 00:51:57 +00005697 IP = nullptr;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005698 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005699 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005700 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005701 }
5702
5703 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005704 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5705 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005706
5707 // Initialize the operands list.
5708 if (NumOps > array_lengthof(N->LocalOperands))
5709 // We're creating a final node that will live unmorphed for the
5710 // remainder of the current SelectionDAG iteration, so we can allocate
5711 // the operands directly out of a pool with no recycling metadata.
5712 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5713 Ops, NumOps);
5714 else
5715 N->InitOperands(N->LocalOperands, Ops, NumOps);
5716 N->OperandsNeedDelete = false;
5717
5718 if (DoCSE)
5719 CSEMap.InsertNode(N, IP);
5720
Chandler Carruth41b20e72014-07-22 04:07:55 +00005721 InsertNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005722 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005723}
Evan Chengd3f1db92006-02-09 07:15:23 +00005724
Dan Gohmanac33a902009-08-19 18:16:17 +00005725/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005726/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005727SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005728SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005729 SDValue Operand) {
5730 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005731 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005732 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005733 return SDValue(Subreg, 0);
5734}
5735
Bob Wilson2a45a652009-10-08 18:49:46 +00005736/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005737/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005738SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005739SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005740 SDValue Operand, SDValue Subreg) {
5741 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005742 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005743 VT, Operand, Subreg, SRIdxVal);
5744 return SDValue(Result, 0);
5745}
5746
Evan Cheng31604a62008-03-22 01:55:50 +00005747/// getNodeIfExists - Get the specified node if it's already available, or
5748/// else return NULL.
5749SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005750 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5751 bool exact) {
5752 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005753 FoldingSetNodeID ID;
Craig Topper633d99b2014-04-27 23:22:43 +00005754 AddNodeIDNode(ID, Opcode, VTList, Ops);
Andrea Di Biagio4db1abe2014-06-09 12:32:53 +00005755 if (isBinOpWithFlags(Opcode))
5756 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Craig Topperc0196b12014-04-14 00:51:57 +00005757 void *IP = nullptr;
Bill Wendling022d18f2009-12-18 23:32:53 +00005758 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005759 return E;
5760 }
Craig Topperc0196b12014-04-14 00:51:57 +00005761 return nullptr;
Evan Cheng31604a62008-03-22 01:55:50 +00005762}
5763
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005764/// getDbgValue - Creates a SDDbgValue node.
5765///
Adrian Prantl32da8892014-04-25 20:49:25 +00005766/// SDNode
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005767SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5768 unsigned R, bool IsIndirect, uint64_t Off,
5769 DebugLoc DL, unsigned O) {
5770 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005771}
5772
Adrian Prantl32da8892014-04-25 20:49:25 +00005773/// Constant
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005774SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5775 const Value *C, uint64_t Off,
5776 DebugLoc DL, unsigned O) {
5777 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005778}
5779
Adrian Prantl32da8892014-04-25 20:49:25 +00005780/// FrameIndex
Adrian Prantl87b7eb92014-10-01 18:55:02 +00005781SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
5782 unsigned FI, uint64_t Off,
5783 DebugLoc DL, unsigned O) {
5784 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005785}
5786
Dan Gohman7d099f72010-03-03 21:33:37 +00005787namespace {
5788
Dan Gohman9cc886b2010-03-04 19:11:28 +00005789/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005790/// pointed to by a use iterator is deleted, increment the use iterator
5791/// so that it doesn't dangle.
5792///
Dan Gohman7d099f72010-03-03 21:33:37 +00005793class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005794 SDNode::use_iterator &UI;
5795 SDNode::use_iterator &UE;
5796
Craig Topper7b883b32014-03-08 06:31:39 +00005797 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohman7d099f72010-03-03 21:33:37 +00005798 // Increment the iterator as needed.
5799 while (UI != UE && N == *UI)
5800 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005801 }
5802
5803public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005804 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005805 SDNode::use_iterator &ui,
5806 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005807 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005808};
5809
5810}
5811
Evan Cheng445b91a2006-08-07 22:13:29 +00005812/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005813/// This can cause recursive merging of nodes in the DAG.
5814///
Chris Lattner76858912008-02-03 03:35:22 +00005815/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005816///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005817void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005818 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005819 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005820 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005821 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005822
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005823 // Iterate over all the existing uses of From. New uses will be added
5824 // to the beginning of the use list, which we avoid visiting.
5825 // This specifically avoids visiting uses of From that arise while the
5826 // replacement is happening, because any such uses would be the result
5827 // of CSE: If an existing node looks like From after one of its operands
5828 // is replaced by To, we don't want to replace of all its users with To
5829 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005830 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005831 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005832 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005833 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005834
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005835 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005836 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005837
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005838 // A user can appear in a use list multiple times, and when this
5839 // happens the uses are usually next to each other in the list.
5840 // To help reduce the number of CSE recomputations, process all
5841 // the uses of this user that we can find this way.
5842 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005843 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005844 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005845 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005846 } while (UI != UE && *UI == User);
5847
5848 // Now that we have modified User, add it back to the CSE maps. If it
5849 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005850 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005851 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005852
5853 // If we just RAUW'd the root, take note.
5854 if (FromN == getRoot())
5855 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005856}
5857
5858/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5859/// This can cause recursive merging of nodes in the DAG.
5860///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005861/// This version assumes that for each value of From, there is a
5862/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005863///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005864void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005865#ifndef NDEBUG
5866 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5867 assert((!From->hasAnyUseOfValue(i) ||
5868 From->getValueType(i) == To->getValueType(i)) &&
5869 "Cannot use this version of ReplaceAllUsesWith!");
5870#endif
Dan Gohman17059682008-07-17 19:10:17 +00005871
5872 // Handle the trivial case.
5873 if (From == To)
5874 return;
5875
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005876 // Iterate over just the existing users of From. See the comments in
5877 // the ReplaceAllUsesWith above.
5878 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005879 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005880 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005881 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005882
Chris Lattner373f0482005-08-26 18:36:28 +00005883 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005884 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005885
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005886 // A user can appear in a use list multiple times, and when this
5887 // happens the uses are usually next to each other in the list.
5888 // To help reduce the number of CSE recomputations, process all
5889 // the uses of this user that we can find this way.
5890 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005891 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005892 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005893 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005894 } while (UI != UE && *UI == User);
5895
5896 // Now that we have modified User, add it back to the CSE maps. If it
5897 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005898 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005899 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005900
5901 // If we just RAUW'd the root, take note.
5902 if (From == getRoot().getNode())
5903 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005904}
5905
Chris Lattner373f0482005-08-26 18:36:28 +00005906/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5907/// This can cause recursive merging of nodes in the DAG.
5908///
5909/// This version can replace From with any result values. To must match the
5910/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005911void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005912 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005913 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005914
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005915 // Iterate over just the existing users of From. See the comments in
5916 // the ReplaceAllUsesWith above.
5917 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005918 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005919 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005920 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005921
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005922 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005923 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005924
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005925 // A user can appear in a use list multiple times, and when this
5926 // happens the uses are usually next to each other in the list.
5927 // To help reduce the number of CSE recomputations, process all
5928 // the uses of this user that we can find this way.
5929 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005930 SDUse &Use = UI.getUse();
5931 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005932 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005933 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005934 } while (UI != UE && *UI == User);
5935
5936 // Now that we have modified User, add it back to the CSE maps. If it
5937 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005938 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005939 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005940
5941 // If we just RAUW'd the root, take note.
5942 if (From == getRoot().getNode())
5943 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005944}
5945
Chris Lattner375e1a72006-02-17 21:58:01 +00005946/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005947/// uses of other values produced by From.getNode() alone. The Deleted
5948/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005949void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005950 // Handle the really simple, really trivial case efficiently.
5951 if (From == To) return;
5952
Chris Lattner375e1a72006-02-17 21:58:01 +00005953 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005954 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005955 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005956 return;
5957 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005958
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005959 // Iterate over just the existing users of From. See the comments in
5960 // the ReplaceAllUsesWith above.
5961 SDNode::use_iterator UI = From.getNode()->use_begin(),
5962 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005963 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005964 while (UI != UE) {
5965 SDNode *User = *UI;
5966 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005967
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005968 // A user can appear in a use list multiple times, and when this
5969 // happens the uses are usually next to each other in the list.
5970 // To help reduce the number of CSE recomputations, process all
5971 // the uses of this user that we can find this way.
5972 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005973 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005974
5975 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005976 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005977 ++UI;
5978 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005979 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005980
5981 // If this node hasn't been modified yet, it's still in the CSE maps,
5982 // so remove its old self from the CSE maps.
5983 if (!UserRemovedFromCSEMaps) {
5984 RemoveNodeFromCSEMaps(User);
5985 UserRemovedFromCSEMaps = true;
5986 }
5987
5988 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005989 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005990 } while (UI != UE && *UI == User);
5991
5992 // We are iterating over all uses of the From node, so if a use
5993 // doesn't use the specific value, no changes are made.
5994 if (!UserRemovedFromCSEMaps)
5995 continue;
5996
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005997 // Now that we have modified User, add it back to the CSE maps. If it
5998 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005999 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00006000 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006001
6002 // If we just RAUW'd the root, take note.
6003 if (From == getRoot())
6004 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00006005}
6006
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006007namespace {
6008 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6009 /// to record information about a use.
6010 struct UseMemo {
6011 SDNode *User;
6012 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006013 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006014 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006015
6016 /// operator< - Sort Memos by User.
6017 bool operator<(const UseMemo &L, const UseMemo &R) {
6018 return (intptr_t)L.User < (intptr_t)R.User;
6019 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006020}
6021
Dan Gohman17059682008-07-17 19:10:17 +00006022/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006023/// uses of other values produced by From.getNode() alone. The same value
6024/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00006025/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006026void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6027 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006028 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00006029 // Handle the simple, trivial case efficiently.
6030 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006031 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00006032
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006033 // Read up all the uses and make records of them. This helps
6034 // processing new uses that are introduced during the
6035 // replacement process.
6036 SmallVector<UseMemo, 4> Uses;
6037 for (unsigned i = 0; i != Num; ++i) {
6038 unsigned FromResNo = From[i].getResNo();
6039 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006040 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006041 E = FromNode->use_end(); UI != E; ++UI) {
6042 SDUse &Use = UI.getUse();
6043 if (Use.getResNo() == FromResNo) {
6044 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006045 Uses.push_back(Memo);
6046 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006047 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006048 }
Dan Gohman17059682008-07-17 19:10:17 +00006049
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006050 // Sort the uses, so that all the uses from a given User are together.
6051 std::sort(Uses.begin(), Uses.end());
6052
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006053 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6054 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00006055 // We know that this user uses some value of From. If it is the right
6056 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006057 SDNode *User = Uses[UseIndex].User;
6058
6059 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00006060 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006061
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006062 // The Uses array is sorted, so all the uses for a given User
6063 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006064 // To help reduce the number of CSE recomputations, process all
6065 // the uses of this user that we can find this way.
6066 do {
6067 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006068 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006069 ++UseIndex;
6070
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006071 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00006072 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6073
Dan Gohman17059682008-07-17 19:10:17 +00006074 // Now that we have modified User, add it back to the CSE maps. If it
6075 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00006076 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00006077 }
6078}
6079
Evan Cheng9631a602006-08-01 08:20:41 +00006080/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00006081/// based on their topological order. It returns the maximum id and a vector
6082/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006083unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00006084
Dan Gohman86aa16a2008-09-30 18:30:35 +00006085 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00006086
Dan Gohman86aa16a2008-09-30 18:30:35 +00006087 // SortedPos tracks the progress of the algorithm. Nodes before it are
6088 // sorted, nodes after it are unsorted. When the algorithm completes
6089 // it is at the end of the list.
6090 allnodes_iterator SortedPos = allnodes_begin();
6091
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006092 // Visit all the nodes. Move nodes with no operands to the front of
6093 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00006094 // operand count. Before we do this, the Node Id fields of the nodes
6095 // may contain arbitrary values. After, the Node Id fields for nodes
6096 // before SortedPos will contain the topological sort index, and the
6097 // Node Id fields for nodes At SortedPos and after will contain the
6098 // count of outstanding operands.
6099 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6100 SDNode *N = I++;
Adam Nemet7d394302014-05-31 16:23:17 +00006101 checkForCycles(N, this);
Dan Gohman86aa16a2008-09-30 18:30:35 +00006102 unsigned Degree = N->getNumOperands();
6103 if (Degree == 0) {
6104 // A node with no uses, add it to the result array immediately.
6105 N->setNodeId(DAGSize++);
6106 allnodes_iterator Q = N;
6107 if (Q != SortedPos)
6108 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00006109 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006110 ++SortedPos;
6111 } else {
6112 // Temporarily use the Node Id as scratch space for the degree count.
6113 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00006114 }
6115 }
6116
Chad Rosier5d1f5d22012-05-21 17:13:41 +00006117 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00006118 // such that by the time the end is reached all nodes will be sorted.
6119 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6120 SDNode *N = I;
Adam Nemet7d394302014-05-31 16:23:17 +00006121 checkForCycles(N, this);
David Greene3b2a68c2010-01-20 00:59:23 +00006122 // N is in sorted position, so all its uses have one less operand
6123 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00006124 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6125 UI != UE; ++UI) {
6126 SDNode *P = *UI;
6127 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00006128 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006129 --Degree;
6130 if (Degree == 0) {
6131 // All of P's operands are sorted, so P may sorted now.
6132 P->setNodeId(DAGSize++);
6133 if (P != SortedPos)
6134 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006135 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006136 ++SortedPos;
6137 } else {
6138 // Update P's outstanding operand count.
6139 P->setNodeId(Degree);
6140 }
6141 }
David Greene3b2a68c2010-01-20 00:59:23 +00006142 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006143#ifndef NDEBUG
6144 SDNode *S = ++I;
6145 dbgs() << "Overran sorted position:\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006146 S->dumprFull(this); dbgs() << "\n";
Adam Nemetb4690e32014-05-31 16:23:20 +00006147 dbgs() << "Checking if this is due to cycles\n";
6148 checkForCycles(this, true);
David Greene893047d2010-02-09 23:03:05 +00006149#endif
Craig Topperc0196b12014-04-14 00:51:57 +00006150 llvm_unreachable(nullptr);
David Greene3b2a68c2010-01-20 00:59:23 +00006151 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006152 }
6153
6154 assert(SortedPos == AllNodes.end() &&
6155 "Topological sort incomplete!");
6156 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6157 "First node in topological sort is not the entry token!");
6158 assert(AllNodes.front().getNodeId() == 0 &&
6159 "First node in topological sort has non-zero id!");
6160 assert(AllNodes.front().getNumOperands() == 0 &&
6161 "First node in topological sort has operands!");
6162 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6163 "Last node in topologic sort has unexpected id!");
6164 assert(AllNodes.back().use_empty() &&
6165 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006166 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006167 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006168}
6169
Evan Cheng563fe3c2010-03-25 01:38:16 +00006170/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6171/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006172void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6173 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006174 if (SD)
6175 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006176}
Evan Cheng29eefc12006-07-27 06:39:06 +00006177
Devang Patelefc6b162011-01-25 23:27:42 +00006178/// TransferDbgValues - Transfer SDDbgValues.
6179void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6180 if (From == To || !From.getNode()->getHasDebugValue())
6181 return;
6182 SDNode *FromNode = From.getNode();
6183 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006184 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006185 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006186 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006187 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006188 SDDbgValue *Dbg = *I;
6189 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +00006190 SDDbgValue *Clone =
6191 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6192 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6193 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006194 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006195 }
6196 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006197 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006198 E = ClonedDVs.end(); I != E; ++I)
6199 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006200}
6201
Jim Laskeyd66e6162005-08-17 20:08:02 +00006202//===----------------------------------------------------------------------===//
6203// SDNode Class
6204//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006205
Chris Lattner3bf17b62007-02-04 02:41:42 +00006206HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006207 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006208}
6209
Andrew Trickef9de2a2013-05-25 02:42:55 +00006210GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6211 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006212 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006213 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006214 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006215}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006216
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006217AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6218 SDValue X, unsigned SrcAS,
6219 unsigned DestAS)
6220 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6221 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6222
Andrew Trickef9de2a2013-05-25 02:42:55 +00006223MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6224 EVT memvt, MachineMemOperand *mmo)
6225 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006226 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006227 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006228 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006229 assert(isNonTemporal() == MMO->isNonTemporal() &&
6230 "Non-temporal encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006231 // We check here that the size of the memory operand fits within the size of
6232 // the MMO. This is because the MMO might indicate only a possible address
6233 // range instead of specifying the affected memory addresses precisely.
6234 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006235}
6236
Andrew Trickef9de2a2013-05-25 02:42:55 +00006237MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Craig Topperbb533072014-04-27 19:21:02 +00006238 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6239 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohman48b185d2009-09-25 20:36:54 +00006240 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006241 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006242 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006243 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00006244 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006245}
6246
Jim Laskeyf576b422006-10-27 23:46:08 +00006247/// Profile - Gather unique data for the node.
6248///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006249void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006250 AddNodeIDNode(ID, this);
6251}
6252
Owen Anderson3b1665e2009-08-25 22:27:22 +00006253namespace {
6254 struct EVTArray {
6255 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006256
Owen Anderson3b1665e2009-08-25 22:27:22 +00006257 EVTArray() {
6258 VTs.reserve(MVT::LAST_VALUETYPE);
6259 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6260 VTs.push_back(MVT((MVT::SimpleValueType)i));
6261 }
6262 };
6263}
6264
Owen Anderson53aa7a92009-08-10 22:56:29 +00006265static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006266static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006267static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006268
Chris Lattner88fa11c2005-11-08 23:30:28 +00006269/// getValueTypeList - Return a pointer to the specified value type.
6270///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006271const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006272 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006273 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006274 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006275 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006276 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006277 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006278 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006279 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006280}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006281
Chris Lattner40e79822005-01-12 18:37:47 +00006282/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6283/// indicated value. This method ignores uses of other values defined by this
6284/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006285bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006286 assert(Value < getNumValues() && "Bad value!");
6287
Roman Levenstein51f532f2008-04-07 10:06:32 +00006288 // TODO: Only iterate over uses of a given value of the node
6289 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006290 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006291 if (NUses == 0)
6292 return false;
6293 --NUses;
6294 }
Chris Lattner40e79822005-01-12 18:37:47 +00006295 }
6296
6297 // Found exactly the right number of uses?
6298 return NUses == 0;
6299}
6300
6301
Evan Cheng358c3d12007-08-02 05:29:38 +00006302/// hasAnyUseOfValue - Return true if there are any use of the indicated
6303/// value. This method ignores uses of other values defined by this operation.
6304bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6305 assert(Value < getNumValues() && "Bad value!");
6306
Dan Gohman7a510c22008-07-09 22:39:01 +00006307 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006308 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006309 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006310
6311 return false;
6312}
6313
6314
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006315/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006316///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006317bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006318 bool Seen = false;
6319 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006320 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006321 if (User == this)
6322 Seen = true;
6323 else
6324 return false;
6325 }
6326
6327 return Seen;
6328}
6329
Evan Cheng9456dd82006-11-03 07:31:32 +00006330/// isOperand - Return true if this node is an operand of N.
6331///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006332bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006333 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6334 if (*this == N->getOperand(i))
6335 return true;
6336 return false;
6337}
6338
Evan Cheng567d2e52008-03-04 00:41:45 +00006339bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006340 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006341 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006342 return true;
6343 return false;
6344}
Evan Chengd37645c2006-02-05 06:29:23 +00006345
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006346/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006347/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006348/// side-effecting instructions on any chain path. In practice, this looks
6349/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006350/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006351bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006352 unsigned Depth) const {
6353 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006354
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006355 // Don't search too deeply, we just want to be able to see through
6356 // TokenFactor's etc.
6357 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006358
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006359 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006360 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006361 if (getOpcode() == ISD::TokenFactor) {
6362 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006363 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6364 return false;
6365 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006366 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006367
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006368 // Loads don't have side effects, look through them.
6369 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6370 if (!Ld->isVolatile())
6371 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6372 }
6373 return false;
6374}
6375
Lang Hames5a004992011-07-07 04:31:51 +00006376/// hasPredecessor - Return true if N is a predecessor of this node.
6377/// N is either an operand of this node, or can be reached by recursively
6378/// traversing up the operands.
6379/// NOTE: This is an expensive method. Use it carefully.
6380bool SDNode::hasPredecessor(const SDNode *N) const {
6381 SmallPtrSet<const SDNode *, 32> Visited;
6382 SmallVector<const SDNode *, 16> Worklist;
6383 return hasPredecessorHelper(N, Visited, Worklist);
6384}
Dan Gohmancd139c02009-10-28 03:44:30 +00006385
Craig Topperb94011f2013-07-14 04:42:23 +00006386bool
6387SDNode::hasPredecessorHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006388 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Topperb94011f2013-07-14 04:42:23 +00006389 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006390 if (Visited.empty()) {
6391 Worklist.push_back(this);
6392 } else {
6393 // Take a look in the visited set. If we've already encountered this node
6394 // we needn't search further.
6395 if (Visited.count(N))
6396 return true;
6397 }
6398
6399 // Haven't visited N yet. Continue the search.
6400 while (!Worklist.empty()) {
6401 const SDNode *M = Worklist.pop_back_val();
6402 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6403 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006404 if (Visited.insert(Op))
6405 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006406 if (Op == N)
6407 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006408 }
Lang Hames5a004992011-07-07 04:31:51 +00006409 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006410
6411 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006412}
6413
Evan Cheng5d9fd972006-10-04 00:56:09 +00006414uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6415 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006416 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006417}
6418
Mon P Wang32f8bb92009-11-30 02:42:02 +00006419SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6420 assert(N->getNumValues() == 1 &&
6421 "Can't unroll a vector with multiple results!");
6422
6423 EVT VT = N->getValueType(0);
6424 unsigned NE = VT.getVectorNumElements();
6425 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006426 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006427
6428 SmallVector<SDValue, 8> Scalars;
6429 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6430
6431 // If ResNE is 0, fully unroll the vector op.
6432 if (ResNE == 0)
6433 ResNE = NE;
6434 else if (NE > ResNE)
6435 NE = ResNE;
6436
6437 unsigned i;
6438 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006439 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006440 SDValue Operand = N->getOperand(j);
6441 EVT OperandVT = Operand.getValueType();
6442 if (OperandVT.isVector()) {
6443 // A vector operand; extract a single element.
Eric Christopherd9134482014-08-04 21:25:23 +00006444 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006445 EVT OperandEltVT = OperandVT.getVectorElementType();
6446 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6447 OperandEltVT,
6448 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006449 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006450 } else {
6451 // A scalar operand; just use it as is.
6452 Operands[j] = Operand;
6453 }
6454 }
6455
6456 switch (N->getOpcode()) {
6457 default:
Craig Topper48d114b2014-04-26 18:35:24 +00006458 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006459 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006460 case ISD::VSELECT:
Craig Topper48d114b2014-04-26 18:35:24 +00006461 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotem52202fb2011-09-13 19:17:42 +00006462 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006463 case ISD::SHL:
6464 case ISD::SRA:
6465 case ISD::SRL:
6466 case ISD::ROTL:
6467 case ISD::ROTR:
6468 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006469 getShiftAmountOperand(Operands[0].getValueType(),
6470 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006471 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006472 case ISD::SIGN_EXTEND_INREG:
6473 case ISD::FP_ROUND_INREG: {
6474 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6475 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6476 Operands[0],
6477 getValueType(ExtVT)));
6478 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006479 }
6480 }
6481
6482 for (; i < ResNE; ++i)
6483 Scalars.push_back(getUNDEF(EltVT));
6484
6485 return getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006486 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006487}
6488
Evan Chengf5938d52009-12-09 01:36:00 +00006489
Wesley Peck527da1b2010-11-23 03:31:01 +00006490/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6491/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006492/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006493bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006494 unsigned Bytes, int Dist) const {
6495 if (LD->getChain() != Base->getChain())
6496 return false;
6497 EVT VT = LD->getValueType(0);
6498 if (VT.getSizeInBits() / 8 != Bytes)
6499 return false;
6500
6501 SDValue Loc = LD->getOperand(1);
6502 SDValue BaseLoc = Base->getOperand(1);
6503 if (Loc.getOpcode() == ISD::FrameIndex) {
6504 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6505 return false;
6506 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6507 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6508 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6509 int FS = MFI->getObjectSize(FI);
6510 int BFS = MFI->getObjectSize(BFI);
6511 if (FS != BFS || FS != (int)Bytes) return false;
6512 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6513 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006514
6515 // Handle X+C
6516 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6517 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6518 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006519
Craig Topperc0196b12014-04-14 00:51:57 +00006520 const GlobalValue *GV1 = nullptr;
6521 const GlobalValue *GV2 = nullptr;
Evan Chengf5938d52009-12-09 01:36:00 +00006522 int64_t Offset1 = 0;
6523 int64_t Offset2 = 0;
Eric Christopherd9134482014-08-04 21:25:23 +00006524 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006525 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6526 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006527 if (isGA1 && isGA2 && GV1 == GV2)
6528 return Offset1 == (Offset2 + Dist*Bytes);
6529 return false;
6530}
6531
6532
Evan Cheng34a23ea2009-12-09 01:04:59 +00006533/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6534/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006535unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006536 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006537 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006538 int64_t GVOffset = 0;
Eric Christopherd9134482014-08-04 21:25:23 +00006539 const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006540 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006541 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006542 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +00006543 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6544 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006545 unsigned AlignBits = KnownZero.countTrailingOnes();
6546 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6547 if (Align)
6548 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006549 }
Evan Chengd938faf2009-12-09 01:53:58 +00006550
Evan Cheng34a23ea2009-12-09 01:04:59 +00006551 // If this is a direct reference to a stack slot, use information about the
6552 // stack slot's alignment.
6553 int FrameIdx = 1 << 31;
6554 int64_t FrameOffset = 0;
6555 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6556 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006557 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006558 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006559 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006560 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6561 FrameOffset = Ptr.getConstantOperandVal(1);
6562 }
6563
6564 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006565 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006566 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6567 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006568 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006569 }
6570
6571 return 0;
6572}
6573
Juergen Ributzkab3487102013-11-19 21:20:17 +00006574/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6575/// which is split (or expanded) into two not necessarily identical pieces.
6576std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6577 // Currently all types are split in half.
6578 EVT LoVT, HiVT;
6579 if (!VT.isVector()) {
6580 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6581 } else {
6582 unsigned NumElements = VT.getVectorNumElements();
6583 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6584 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6585 NumElements/2);
6586 }
6587 return std::make_pair(LoVT, HiVT);
6588}
6589
6590/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6591/// low/high part.
6592std::pair<SDValue, SDValue>
6593SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6594 const EVT &HiVT) {
6595 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6596 N.getValueType().getVectorNumElements() &&
6597 "More vector elements requested than available!");
6598 SDValue Lo, Hi;
6599 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6600 getConstant(0, TLI->getVectorIdxTy()));
6601 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6602 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6603 return std::make_pair(Lo, Hi);
6604}
6605
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00006606void SelectionDAG::ExtractVectorElements(SDValue Op,
6607 SmallVectorImpl<SDValue> &Args,
6608 unsigned Start, unsigned Count) {
6609 EVT VT = Op.getValueType();
6610 if (Count == 0)
6611 Count = VT.getVectorNumElements();
6612
6613 EVT EltVT = VT.getVectorElementType();
6614 EVT IdxTy = TLI->getVectorIdxTy();
6615 SDLoc SL(Op);
6616 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6617 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6618 Op, getConstant(i, IdxTy)));
6619 }
6620}
6621
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006622// getAddressSpace - Return the address space this GlobalAddress belongs to.
6623unsigned GlobalAddressSDNode::getAddressSpace() const {
6624 return getGlobal()->getType()->getAddressSpace();
6625}
6626
6627
Chris Lattner229907c2011-07-18 04:54:35 +00006628Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006629 if (isMachineConstantPoolEntry())
6630 return Val.MachineCPVal->getType();
6631 return Val.ConstVal->getType();
6632}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006633
Bob Wilson85cefe82009-03-02 23:24:16 +00006634bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6635 APInt &SplatUndef,
6636 unsigned &SplatBitSize,
6637 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006638 unsigned MinSplatBits,
Matt Arsenaultb598f7b2014-02-24 21:01:18 +00006639 bool isBigEndian) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006640 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006641 assert(VT.isVector() && "Expected a vector type");
6642 unsigned sz = VT.getSizeInBits();
6643 if (MinSplatBits > sz)
6644 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006645
Bob Wilson85cefe82009-03-02 23:24:16 +00006646 SplatValue = APInt(sz, 0);
6647 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006648
Bob Wilson85cefe82009-03-02 23:24:16 +00006649 // Get the bits. Bits with undefined values (when the corresponding element
6650 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6651 // in SplatValue. If any of the values are not constant, give up and return
6652 // false.
6653 unsigned int nOps = getNumOperands();
6654 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6655 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006656
6657 for (unsigned j = 0; j < nOps; ++j) {
6658 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006659 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006660 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006661
Bob Wilson85cefe82009-03-02 23:24:16 +00006662 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006663 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006664 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006665 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006666 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006667 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006668 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006669 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006670 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006671 }
6672
Bob Wilson85cefe82009-03-02 23:24:16 +00006673 // The build_vector is all constants or undefs. Find the smallest element
6674 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006675
Bob Wilson85cefe82009-03-02 23:24:16 +00006676 HasAnyUndefs = (SplatUndef != 0);
6677 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006678
Bob Wilson85cefe82009-03-02 23:24:16 +00006679 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006680 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6681 APInt LowValue = SplatValue.trunc(HalfSize);
6682 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6683 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006684
Bob Wilson85cefe82009-03-02 23:24:16 +00006685 // If the two halves do not match (ignoring undef bits), stop here.
6686 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6687 MinSplatBits > HalfSize)
6688 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006689
Bob Wilson85cefe82009-03-02 23:24:16 +00006690 SplatValue = HighValue | LowValue;
6691 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006692
Bob Wilson85cefe82009-03-02 23:24:16 +00006693 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006694 }
6695
Bob Wilson85cefe82009-03-02 23:24:16 +00006696 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006697 return true;
6698}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006699
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006700SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6701 if (UndefElements) {
6702 UndefElements->clear();
6703 UndefElements->resize(getNumOperands());
6704 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006705 SDValue Splatted;
6706 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6707 SDValue Op = getOperand(i);
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006708 if (Op.getOpcode() == ISD::UNDEF) {
6709 if (UndefElements)
6710 (*UndefElements)[i] = true;
6711 } else if (!Splatted) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006712 Splatted = Op;
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006713 } else if (Splatted != Op) {
Chandler Carruthb844e722014-07-08 07:19:55 +00006714 return SDValue();
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006715 }
Chandler Carruthb844e722014-07-08 07:19:55 +00006716 }
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +00006717
Chandler Carruthb844e722014-07-08 07:19:55 +00006718 if (!Splatted) {
6719 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6720 "Can only have a splat without a constant for all undefs.");
6721 return getOperand(0);
6722 }
Matt Arsenault985b9de2014-03-17 18:58:01 +00006723
Chandler Carruthb844e722014-07-08 07:19:55 +00006724 return Splatted;
6725}
6726
6727ConstantSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006728BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006729 return dyn_cast_or_null<ConstantSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006730 getSplatValue(UndefElements).getNode());
Chandler Carruthb844e722014-07-08 07:19:55 +00006731}
6732
6733ConstantFPSDNode *
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006734BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
Chandler Carruthb844e722014-07-08 07:19:55 +00006735 return dyn_cast_or_null<ConstantFPSDNode>(
Chandler Carruthf0a33b72014-07-09 00:41:34 +00006736 getSplatValue(UndefElements).getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00006737}
6738
Juergen Ributzka73844052014-01-13 20:51:35 +00006739bool BuildVectorSDNode::isConstant() const {
6740 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6741 unsigned Opc = getOperand(i).getOpcode();
6742 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6743 return false;
6744 }
6745 return true;
6746}
6747
Owen Anderson53aa7a92009-08-10 22:56:29 +00006748bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006749 // Find the first non-undef value in the shuffle mask.
6750 unsigned i, e;
6751 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6752 /* search */;
6753
Nate Begeman39b59db2009-04-29 18:13:31 +00006754 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006755
Nate Begeman5f829d82009-04-29 05:20:52 +00006756 // Make sure all remaining elements are either undef or the same as the first
6757 // non-undef value.
6758 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006759 if (Mask[i] >= 0 && Mask[i] != Idx)
6760 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006761 return true;
6762}
David Greene09851602010-01-20 20:13:31 +00006763
Adam Nemetb4690e32014-05-31 16:23:20 +00006764#ifndef NDEBUG
David Greene09851602010-01-20 20:13:31 +00006765static void checkForCyclesHelper(const SDNode *N,
Craig Topper71b7b682014-08-21 05:55:13 +00006766 SmallPtrSetImpl<const SDNode*> &Visited,
6767 SmallPtrSetImpl<const SDNode*> &Checked,
Adam Nemet7d394302014-05-31 16:23:17 +00006768 const llvm::SelectionDAG *DAG) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006769 // If this node has already been checked, don't check it again.
6770 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006771 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006772
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006773 // If a node has already been visited on this depth-first walk, reject it as
6774 // a cycle.
6775 if (!Visited.insert(N)) {
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006776 errs() << "Detected cycle in SelectionDAG\n";
Adam Nemet7d394302014-05-31 16:23:17 +00006777 dbgs() << "Offending node:\n";
6778 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006779 abort();
David Greene09851602010-01-20 20:13:31 +00006780 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006781
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006782 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Adam Nemet7d394302014-05-31 16:23:17 +00006783 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peck527da1b2010-11-23 03:31:01 +00006784
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006785 Checked.insert(N);
6786 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006787}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006788#endif
David Greene09851602010-01-20 20:13:31 +00006789
Adam Nemet7d394302014-05-31 16:23:17 +00006790void llvm::checkForCycles(const llvm::SDNode *N,
Adam Nemetb4690e32014-05-31 16:23:20 +00006791 const llvm::SelectionDAG *DAG,
6792 bool force) {
6793#ifndef NDEBUG
6794 bool check = force;
David Greene09851602010-01-20 20:13:31 +00006795#ifdef XDEBUG
Adam Nemetb4690e32014-05-31 16:23:20 +00006796 check = true;
6797#endif // XDEBUG
6798 if (check) {
6799 assert(N && "Checking nonexistent SDNode");
6800 SmallPtrSet<const SDNode*, 32> visited;
6801 SmallPtrSet<const SDNode*, 32> checked;
6802 checkForCyclesHelper(N, visited, checked, DAG);
6803 }
6804#endif // !NDEBUG
David Greene09851602010-01-20 20:13:31 +00006805}
6806
Adam Nemetb4690e32014-05-31 16:23:20 +00006807void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6808 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greene09851602010-01-20 20:13:31 +00006809}