blob: e003caeddb177231ec9896d72aadc71a08d47155 [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"
Chandler Carruthd3e73552013-01-07 03:08:10 +000021#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000027#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/DerivedTypes.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/GlobalAlias.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/IR/Intrinsics.h"
Bill Wendlingbd092622008-09-30 21:22:07 +000036#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000037#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000039#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000040#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000041#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/raw_ostream.h"
43#include "llvm/Target/TargetInstrInfo.h"
44#include "llvm/Target/TargetIntrinsicInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
47#include "llvm/Target/TargetOptions.h"
48#include "llvm/Target/TargetRegisterInfo.h"
49#include "llvm/Target/TargetSelectionDAGInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000050#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000051#include <cmath>
Chris Lattner560b5e42004-06-02 04:28:06 +000052using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000053
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000054/// makeVTList - Return an instance of the SDVTList struct initialized with the
55/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000056static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000057 SDVTList Res = {VTs, NumVTs};
58 return Res;
59}
60
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000061// Default null implementations of the callbacks.
62void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
63void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000064
Jim Laskeyd66e6162005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
66// ConstantFPSDNode Class
67//===----------------------------------------------------------------------===//
68
69/// isExactlyValue - We don't rely on operator== working on double values, as
70/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
71/// As such, this method can be used to do an exact bit-for-bit comparison of
72/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000073bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000074 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000075}
76
Owen Anderson53aa7a92009-08-10 22:56:29 +000077bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000078 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000079 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000080
Dale Johannesend246b2c2007-08-30 00:23:21 +000081 // convert modifies in place, so make a copy.
82 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000083 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000084 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
85 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000086 &losesInfo);
87 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000088}
89
Jim Laskeyd66e6162005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000093
Evan Chengc70e33c2006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000097 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +000098 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +000099 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000100
Evan Chengc70e33c2006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000102
Chris Lattnerc2d28112006-03-25 22:57:01 +0000103 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000104
Chris Lattnerc2d28112006-03-25 22:57:01 +0000105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000108
Chris Lattnerc2d28112006-03-25 22:57:01 +0000109 // Do not accept an all-undef vector.
110 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000111
Chris Lattnerc2d28112006-03-25 22:57:01 +0000112 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000113 // elements. We have to be a bit careful here, as the type of the constant
114 // may not be the same as the type of the vector elements due to type
115 // legalization (the elements are promoted to a legal type for the target and
116 // a vector of a type may be legal when the base element type is not).
117 // We only want to check enough bits to cover the vector elements, because
118 // we care if the resultant vector is all ones, not whether the individual
119 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000120 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000121 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000122 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
123 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000124 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000125 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
126 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000127 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000128 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000129 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000130
Chris Lattnerc2d28112006-03-25 22:57:01 +0000131 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000132 // undefs. Even with the above element type twiddling, this should be OK, as
133 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000134 for (++i; i != e; ++i)
135 if (N->getOperand(i) != NotZero &&
136 N->getOperand(i).getOpcode() != ISD::UNDEF)
137 return false;
138 return true;
139}
140
141
Evan Chenga6789912006-03-26 09:50:58 +0000142/// isBuildVectorAllZeros - Return true if the specified node is a
143/// BUILD_VECTOR where all of the elements are 0 or undef.
144bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000145 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +0000146 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000147 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000148
Evan Chenga6789912006-03-26 09:50:58 +0000149 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000150
Evan Chengc70e33c2006-03-27 06:58:47 +0000151 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000152
Evan Chengc70e33c2006-03-27 06:58:47 +0000153 // Skip over all of the undef values.
154 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
155 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000156
Evan Chengc70e33c2006-03-27 06:58:47 +0000157 // Do not accept an all-undef vector.
158 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000159
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000160 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chengc70e33c2006-03-27 06:58:47 +0000161 // elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000162 SDValue Zero = N->getOperand(i);
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000163 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
164 if (!CN->isNullValue())
Evan Chengc70e33c2006-03-27 06:58:47 +0000165 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000166 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
167 if (!CFPN->getValueAPF().isPosZero())
Evan Chengc70e33c2006-03-27 06:58:47 +0000168 return false;
169 } else
170 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000171
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000172 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chengc70e33c2006-03-27 06:58:47 +0000173 // undefs.
174 for (++i; i != e; ++i)
175 if (N->getOperand(i) != Zero &&
176 N->getOperand(i).getOpcode() != ISD::UNDEF)
177 return false;
178 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000179}
180
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000181/// \brief Return true if the specified node is a BUILD_VECTOR node of
182/// all ConstantSDNode or undef.
183bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
184 if (N->getOpcode() != ISD::BUILD_VECTOR)
185 return false;
186
187 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
188 SDValue Op = N->getOperand(i);
189 if (Op.getOpcode() == ISD::UNDEF)
190 continue;
191 if (!isa<ConstantSDNode>(Op))
192 return false;
193 }
194 return true;
195}
196
Evan Cheng6200c222008-02-18 23:04:32 +0000197/// isScalarToVector - Return true if the specified node is a
198/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
199/// element is not an undef.
200bool ISD::isScalarToVector(const SDNode *N) {
201 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
202 return true;
203
204 if (N->getOpcode() != ISD::BUILD_VECTOR)
205 return false;
206 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
207 return false;
208 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000209 if (NumElems == 1)
210 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000211 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000212 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000213 if (V.getOpcode() != ISD::UNDEF)
214 return false;
215 }
216 return true;
217}
218
Nadav Rotema62368c2012-07-15 08:38:23 +0000219/// allOperandsUndef - Return true if the node has at least one operand
220/// and all operands of the specified node are ISD::UNDEF.
221bool ISD::allOperandsUndef(const SDNode *N) {
222 // Return false if the node has no operands.
223 // This is "logically inconsistent" with the definition of "all" but
224 // is probably the desired behavior.
225 if (N->getNumOperands() == 0)
226 return false;
227
228 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
229 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
230 return false;
231
232 return true;
233}
234
Chris Lattner061a1ea2005-01-07 07:46:32 +0000235/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
236/// when given the operation for (X op Y).
237ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
238 // To perform this operation, we just need to swap the L and G bits of the
239 // operation.
240 unsigned OldL = (Operation >> 2) & 1;
241 unsigned OldG = (Operation >> 1) & 1;
242 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
243 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000244 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000245}
246
247/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
248/// 'op' is a valid SetCC operation.
249ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
250 unsigned Operation = Op;
251 if (isInteger)
252 Operation ^= 7; // Flip L, G, E bits, but not U.
253 else
254 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000255
Chris Lattner061a1ea2005-01-07 07:46:32 +0000256 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000257 Operation &= ~8; // Don't let N and U bits get set.
258
Chris Lattner061a1ea2005-01-07 07:46:32 +0000259 return ISD::CondCode(Operation);
260}
261
262
263/// isSignedOp - For an integer comparison, return 1 if the comparison is a
264/// signed operation and 2 if the result is an unsigned comparison. Return zero
265/// if the operation does not depend on the sign of the input (setne and seteq).
266static int isSignedOp(ISD::CondCode Opcode) {
267 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000268 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000269 case ISD::SETEQ:
270 case ISD::SETNE: return 0;
271 case ISD::SETLT:
272 case ISD::SETLE:
273 case ISD::SETGT:
274 case ISD::SETGE: return 1;
275 case ISD::SETULT:
276 case ISD::SETULE:
277 case ISD::SETUGT:
278 case ISD::SETUGE: return 2;
279 }
280}
281
282/// getSetCCOrOperation - Return the result of a logical OR between different
283/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
284/// returns SETCC_INVALID if it is not possible to represent the resultant
285/// comparison.
286ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
287 bool isInteger) {
288 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
289 // Cannot fold a signed integer setcc with an unsigned integer setcc.
290 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000291
Chris Lattner061a1ea2005-01-07 07:46:32 +0000292 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000293
Chris Lattner061a1ea2005-01-07 07:46:32 +0000294 // If the N and U bits get set then the resultant comparison DOES suddenly
295 // care about orderedness, and is true when ordered.
296 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000297 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000298
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000299 // Canonicalize illegal integer setcc's.
300 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
301 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000302
Chris Lattner061a1ea2005-01-07 07:46:32 +0000303 return ISD::CondCode(Op);
304}
305
306/// getSetCCAndOperation - Return the result of a logical AND between different
307/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
308/// function returns zero if it is not possible to represent the resultant
309/// comparison.
310ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
311 bool isInteger) {
312 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
313 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000314 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000315
316 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000317 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000318
Chris Lattner393d96a2006-04-27 05:01:07 +0000319 // Canonicalize illegal integer setcc's.
320 if (isInteger) {
321 switch (Result) {
322 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000323 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000324 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000325 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
326 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
327 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000328 }
329 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000330
Chris Lattner393d96a2006-04-27 05:01:07 +0000331 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000332}
333
Jim Laskeyd66e6162005-08-17 20:08:02 +0000334//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000335// SDNode Profile Support
336//===----------------------------------------------------------------------===//
337
Jim Laskeybd0f0882006-10-27 23:52:51 +0000338/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
339///
Jim Laskeyf576b422006-10-27 23:46:08 +0000340static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
341 ID.AddInteger(OpC);
342}
343
344/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
345/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000346static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000347 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000348}
349
Jim Laskeybd0f0882006-10-27 23:52:51 +0000350/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
351///
Jim Laskeyf576b422006-10-27 23:46:08 +0000352static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000353 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf17b4222007-02-04 07:28:00 +0000354 for (; NumOps; --NumOps, ++Ops) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000355 ID.AddPointer(Ops->getNode());
Gabor Greifabfdf922008-08-26 22:36:50 +0000356 ID.AddInteger(Ops->getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000357 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000358}
359
Dan Gohman768f2c92008-07-07 18:26:29 +0000360/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
361///
362static void AddNodeIDOperands(FoldingSetNodeID &ID,
363 const SDUse *Ops, unsigned NumOps) {
364 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000365 ID.AddPointer(Ops->getNode());
366 ID.AddInteger(Ops->getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000367 }
368}
369
Jim Laskeyf576b422006-10-27 23:46:08 +0000370static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelcf0da6c2009-02-17 22:15:04 +0000371 unsigned short OpC, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000372 const SDValue *OpList, unsigned N) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000373 AddNodeIDOpcode(ID, OpC);
374 AddNodeIDValueTypes(ID, VTList);
375 AddNodeIDOperands(ID, OpList, N);
376}
377
Duncan Sands835bdca2008-10-27 15:30:53 +0000378/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
379/// the NodeID data.
380static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000381 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000382 case ISD::TargetExternalSymbol:
383 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000384 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000385 default: break; // Normal nodes don't need extra info.
386 case ISD::TargetConstant:
Hans Wennborg4d67a2e2014-01-25 01:18:18 +0000387 case ISD::Constant:
388 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000389 break;
390 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000391 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000392 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000393 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000394 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000395 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000396 case ISD::GlobalAddress:
397 case ISD::TargetGlobalTLSAddress:
398 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000399 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000400 ID.AddPointer(GA->getGlobal());
401 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000402 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000403 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000404 break;
405 }
406 case ISD::BasicBlock:
407 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
408 break;
409 case ISD::Register:
410 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
411 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000412 case ISD::RegisterMask:
413 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
414 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000415 case ISD::SRCVALUE:
416 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
417 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000418 case ISD::FrameIndex:
419 case ISD::TargetFrameIndex:
420 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
421 break;
422 case ISD::JumpTable:
423 case ISD::TargetJumpTable:
424 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000425 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000426 break;
427 case ISD::ConstantPool:
428 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000429 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000430 ID.AddInteger(CP->getAlignment());
431 ID.AddInteger(CP->getOffset());
432 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000433 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000434 else
435 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000436 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 break;
438 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000439 case ISD::TargetIndex: {
440 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
441 ID.AddInteger(TI->getIndex());
442 ID.AddInteger(TI->getOffset());
443 ID.AddInteger(TI->getTargetFlags());
444 break;
445 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000446 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000447 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000448 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000449 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000450 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000451 break;
452 }
453 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000454 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000455 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000456 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000457 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000458 break;
459 }
Dan Gohman12f24902008-12-23 21:37:04 +0000460 case ISD::ATOMIC_CMP_SWAP:
461 case ISD::ATOMIC_SWAP:
462 case ISD::ATOMIC_LOAD_ADD:
463 case ISD::ATOMIC_LOAD_SUB:
464 case ISD::ATOMIC_LOAD_AND:
465 case ISD::ATOMIC_LOAD_OR:
466 case ISD::ATOMIC_LOAD_XOR:
467 case ISD::ATOMIC_LOAD_NAND:
468 case ISD::ATOMIC_LOAD_MIN:
469 case ISD::ATOMIC_LOAD_MAX:
470 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000471 case ISD::ATOMIC_LOAD_UMAX:
472 case ISD::ATOMIC_LOAD:
473 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000474 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000475 ID.AddInteger(AT->getMemoryVT().getRawBits());
476 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000477 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
478 break;
479 }
480 case ISD::PREFETCH: {
481 const MemSDNode *PF = cast<MemSDNode>(N);
482 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000483 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000484 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000485 case ISD::VECTOR_SHUFFLE: {
486 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000487 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000488 i != e; ++i)
489 ID.AddInteger(SVN->getMaskElt(i));
490 break;
491 }
Dan Gohman6c938802009-10-30 01:27:03 +0000492 case ISD::TargetBlockAddress:
493 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000494 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
495 ID.AddPointer(BA->getBlockAddress());
496 ID.AddInteger(BA->getOffset());
497 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000498 break;
499 }
Mon P Wang6a490372008-06-25 08:15:39 +0000500 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000501
502 // Target specific memory nodes could also have address spaces to check.
503 if (N->isTargetMemoryOpcode())
504 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000505}
506
Duncan Sands835bdca2008-10-27 15:30:53 +0000507/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
508/// data.
509static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
510 AddNodeIDOpcode(ID, N->getOpcode());
511 // Add the return value info.
512 AddNodeIDValueTypes(ID, N->getVTList());
513 // Add the operand info.
514 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
515
516 // Handle SDNode leafs with special info.
517 AddNodeIDCustom(ID, N);
518}
519
Dan Gohman2da2bed2008-08-20 15:58:01 +0000520/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000521/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000522/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000523///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000524static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000525encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000526 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000527 assert((ConvType & 3) == ConvType &&
528 "ConvType may not require more than 2 bits!");
529 assert((AM & 7) == AM &&
530 "AM may not require more than 3 bits!");
531 return ConvType |
532 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000533 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000534 (isNonTemporal << 6) |
535 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000536}
537
Jim Laskeyf576b422006-10-27 23:46:08 +0000538//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000539// SelectionDAG Class
540//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000541
Duncan Sands835bdca2008-10-27 15:30:53 +0000542/// doNotCSE - Return true if CSE should not be performed for this node.
543static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000544 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000545 return true; // Never CSE anything that produces a flag.
546
547 switch (N->getOpcode()) {
548 default: break;
549 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000550 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000551 return true; // Never CSE these nodes.
552 }
553
554 // Check that remaining values produced are not flags.
555 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000556 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000557 return true; // Never CSE anything that produces a flag.
558
559 return false;
560}
561
Chris Lattner9c667932005-01-07 21:09:16 +0000562/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000563/// SelectionDAG.
564void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000565 // Create a dummy node (which is not added to allnodes), that adds a reference
566 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000567 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000568
Chris Lattner8927c872006-08-04 17:45:20 +0000569 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000570
Chris Lattner8927c872006-08-04 17:45:20 +0000571 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000572 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000573 if (I->use_empty())
574 DeadNodes.push_back(I);
575
Dan Gohman91697632008-07-07 20:57:48 +0000576 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000577
Dan Gohman91697632008-07-07 20:57:48 +0000578 // If the root changed (e.g. it was a dead load, update the root).
579 setRoot(Dummy.getValue());
580}
581
582/// RemoveDeadNodes - This method deletes the unreachable nodes in the
583/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000584void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000585
Chris Lattner8927c872006-08-04 17:45:20 +0000586 // Process the worklist, deleting the nodes and adding their uses to the
587 // worklist.
588 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000589 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000590
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000591 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
592 DUL->NodeDeleted(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000593
Chris Lattner8927c872006-08-04 17:45:20 +0000594 // Take the node out of the appropriate CSE map.
595 RemoveNodeFromCSEMaps(N);
596
597 // Next, brutally remove the operand list. This is safe to do, as there are
598 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000599 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
600 SDUse &Use = *I++;
601 SDNode *Operand = Use.getNode();
602 Use.set(SDValue());
603
Chris Lattner8927c872006-08-04 17:45:20 +0000604 // Now that we removed this operand, see if there are no uses of it left.
605 if (Operand->use_empty())
606 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000607 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000608
Dan Gohman534c8a22009-01-19 22:39:36 +0000609 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000610 }
Chris Lattner9c667932005-01-07 21:09:16 +0000611}
612
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000613void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000614 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000615
616 // Create a dummy node that adds a reference to the root node, preventing
617 // it from being deleted. (This matters if the root is an operand of the
618 // dead node.)
619 HandleSDNode Dummy(getRoot());
620
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000621 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000622}
623
Chris Lattnerc738d002005-08-29 21:59:31 +0000624void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000625 // First take this out of the appropriate CSE map.
626 RemoveNodeFromCSEMaps(N);
627
Scott Michelcf0da6c2009-02-17 22:15:04 +0000628 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000629 // AllNodes list, and delete the node.
630 DeleteNodeNotInCSEMaps(N);
631}
632
633void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000634 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
635 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000636
Dan Gohman86aa16a2008-09-30 18:30:35 +0000637 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000638 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000639
Dan Gohman534c8a22009-01-19 22:39:36 +0000640 DeallocateNode(N);
641}
642
643void SelectionDAG::DeallocateNode(SDNode *N) {
644 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000645 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000646
Dan Gohman534c8a22009-01-19 22:39:36 +0000647 // Set the opcode to DELETED_NODE to help catch bugs when node
648 // memory is reallocated.
649 N->NodeType = ISD::DELETED_NODE;
650
Dan Gohman2e834902008-08-26 01:44:34 +0000651 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000652
Evan Cheng563fe3c2010-03-25 01:38:16 +0000653 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000654 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000655 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
656 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000657}
658
Chris Lattner19732782005-08-16 18:17:10 +0000659/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
660/// correspond to it. This is useful when we're about to delete or repurpose
661/// the node. We don't want future request for structurally identical nodes
662/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000663bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000664 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000665 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000666 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000667 case ISD::CONDCODE:
668 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
669 "Cond code doesn't exist!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000670 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000671 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
672 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000673 case ISD::ExternalSymbol:
674 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000675 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000676 case ISD::TargetExternalSymbol: {
677 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
678 Erased = TargetExternalSymbols.erase(
679 std::pair<std::string,unsigned char>(ESN->getSymbol(),
680 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000681 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000682 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000683 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000684 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000685 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000686 Erased = ExtendedValueTypeNodes.erase(VT);
687 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000688 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
689 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000690 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000691 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000692 }
Chris Lattner9c667932005-01-07 21:09:16 +0000693 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000694 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000695 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
696 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000697 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000698 break;
699 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000700#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000701 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000702 // flag result (which cannot be CSE'd) or is one of the special cases that are
703 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000704 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000705 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000706 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000707 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000708 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000709 }
710#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000711 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000712}
713
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000714/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
715/// maps and modified in place. Add it back to the CSE maps, unless an identical
716/// node already exists, in which case transfer all its users to the existing
717/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000718///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000719void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000720SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000721 // For node types that aren't CSE'd, just act as if no identical node
722 // already exists.
723 if (!doNotCSE(N)) {
724 SDNode *Existing = CSEMap.GetOrInsertNode(N);
725 if (Existing != N) {
726 // If there was already an existing matching node, use ReplaceAllUsesWith
727 // to replace the dead one with the existing one. This can cause
728 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000729 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000730
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000731 // N is now dead. Inform the listeners and delete it.
732 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
733 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000734 DeleteNodeNotInCSEMaps(N);
735 return;
736 }
737 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000738
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000739 // If the node doesn't already exist, we updated it. Inform listeners.
740 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
741 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000742}
743
Chris Lattnerf34156e2006-01-28 09:32:45 +0000744/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000745/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000746/// return null, otherwise return a pointer to the slot it would take. If a
747/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000748SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000749 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000750 if (doNotCSE(N))
751 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000752
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000753 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000754 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000755 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands835bdca2008-10-27 15:30:53 +0000756 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000757 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000758 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000759}
760
761/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000762/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000763/// return null, otherwise return a pointer to the slot it would take. If a
764/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000765SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000766 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000767 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000768 if (doNotCSE(N))
769 return 0;
770
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000771 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000772 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000773 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands835bdca2008-10-27 15:30:53 +0000774 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000775 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000776 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000777}
778
779
780/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000781/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000782/// return null, otherwise return a pointer to the slot it would take. If a
783/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000784SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000785 const SDValue *Ops,unsigned NumOps,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000786 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000787 if (doNotCSE(N))
788 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000789
Jim Laskeyf576b422006-10-27 23:46:08 +0000790 FoldingSetNodeID ID;
Dan Gohman92a7f3a2007-06-04 15:49:41 +0000791 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands835bdca2008-10-27 15:30:53 +0000792 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000793 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000794 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000795}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000796
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000797#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000798/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
799static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsb0e39382008-07-21 10:20:31 +0000800 switch (N->getOpcode()) {
801 default:
802 break;
Duncan Sands17e678b2008-10-29 14:22:20 +0000803 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000804 EVT VT = N->getValueType(0);
Duncan Sands17e678b2008-10-29 14:22:20 +0000805 assert(N->getNumValues() == 1 && "Too many results!");
806 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
807 "Wrong return type!");
808 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
809 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
810 "Mismatched operand types!");
811 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
812 "Wrong operand type!");
813 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
814 "Wrong return type size");
815 break;
816 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000817 case ISD::BUILD_VECTOR: {
Duncan Sands17e678b2008-10-29 14:22:20 +0000818 assert(N->getNumValues() == 1 && "Too many results!");
819 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsb0e39382008-07-21 10:20:31 +0000820 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands17e678b2008-10-29 14:22:20 +0000821 "Wrong number of operands!");
Owen Anderson53aa7a92009-08-10 22:56:29 +0000822 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedmanb7910b72011-09-09 21:04:06 +0000823 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindolab93db662009-04-24 12:40:33 +0000824 assert((I->getValueType() == EltVT ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000825 (EltVT.isInteger() && I->getValueType().isInteger() &&
826 EltVT.bitsLE(I->getValueType()))) &&
827 "Wrong operand type!");
Eli Friedmanb7910b72011-09-09 21:04:06 +0000828 assert(I->getValueType() == N->getOperand(0).getValueType() &&
829 "Operands must all have the same type");
830 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000831 break;
832 }
833 }
834}
835
Duncan Sands7c601de2010-11-20 11:25:00 +0000836/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
837static void VerifySDNode(SDNode *N) {
838 // The SDNode allocators cannot be used to allocate nodes with fields that are
839 // not present in an SDNode!
840 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
841 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
842 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
843 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
844 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
845 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
846 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
847 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
848 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
849 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
850 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
851 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
852 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
853 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
854 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
855 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
856 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
857 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
858 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
859
860 VerifyNodeCommon(N);
861}
862
863/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
864/// invalid.
865static void VerifyMachineNode(SDNode *N) {
866 // The MachineNode allocators cannot be used to allocate nodes with fields
867 // that are not present in a MachineNode!
868 // Currently there are no such nodes.
869
870 VerifyNodeCommon(N);
871}
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000872#endif // NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000873
Owen Anderson53aa7a92009-08-10 22:56:29 +0000874/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000875/// given type.
876///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000877unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000878 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000879 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000880 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000881
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000882 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000883}
Chris Lattner9c667932005-01-07 21:09:16 +0000884
Dale Johannesen8ba713212009-02-07 02:15:05 +0000885// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000886SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Juergen Ributzkab3487102013-11-19 21:20:17 +0000887 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TTI(0), TLI(0), OptLevel(OL),
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000888 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sanders50b80412013-11-15 12:56:49 +0000889 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
890 UpdateListeners(0) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000891 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000892 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000893}
894
Juergen Ributzkab3487102013-11-19 21:20:17 +0000895void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti,
896 const TargetLowering *tli) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000897 MF = &mf;
Chandler Carruth42e96112013-01-05 12:32:17 +0000898 TTI = tti;
Juergen Ributzkab3487102013-11-19 21:20:17 +0000899 TLI = tli;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000900 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000901}
902
Chris Lattner600d3082003-08-11 14:57:33 +0000903SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000904 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000905 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000906 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000907}
908
909void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000910 assert(&*AllNodes.begin() == &EntryNode);
911 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000912 while (!AllNodes.empty())
913 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000914}
915
Dan Gohmane1a9a782008-08-27 23:52:12 +0000916void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000917 allnodes_clear();
918 OperandAllocator.Reset();
919 CSEMap.clear();
920
921 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000922 ExternalSymbols.clear();
923 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000924 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
925 static_cast<CondCodeSDNode*>(0));
926 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
927 static_cast<SDNode*>(0));
928
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000929 EntryNode.UseList = 0;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000930 AllNodes.push_back(&EntryNode);
931 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000932 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000933}
934
Andrew Trickef9de2a2013-05-25 02:42:55 +0000935SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000936 return VT.bitsGT(Op.getValueType()) ?
937 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
938 getNode(ISD::TRUNCATE, DL, VT, Op);
939}
940
Andrew Trickef9de2a2013-05-25 02:42:55 +0000941SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000942 return VT.bitsGT(Op.getValueType()) ?
943 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
944 getNode(ISD::TRUNCATE, DL, VT, Op);
945}
946
Andrew Trickef9de2a2013-05-25 02:42:55 +0000947SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000948 return VT.bitsGT(Op.getValueType()) ?
949 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
950 getNode(ISD::TRUNCATE, DL, VT, Op);
951}
952
Andrew Trickef9de2a2013-05-25 02:42:55 +0000953SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000954 assert(!VT.isVector() &&
955 "getZeroExtendInReg should use the vector element type instead of "
956 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000957 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000958 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
959 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000960 VT.getSizeInBits());
961 return getNode(ISD::AND, DL, Op.getValueType(), Op,
962 getConstant(Imm, Op.getValueType()));
963}
964
Bob Wilsonc5890052009-01-22 17:39:32 +0000965/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
966///
Andrew Trickef9de2a2013-05-25 02:42:55 +0000967SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000968 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +0000969 SDValue NegOne =
970 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +0000971 return getNode(ISD::XOR, DL, VT, Val, NegOne);
972}
973
Hans Wennborg4d67a2e2014-01-25 01:18:18 +0000974SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000975 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +0000976 assert((EltVT.getSizeInBits() >= 64 ||
977 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
978 "getConstant with a uint64_t value that doesn't fit in the type!");
Hans Wennborg4d67a2e2014-01-25 01:18:18 +0000979 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman65f63eb2008-02-08 22:59:30 +0000980}
981
Hans Wennborg4d67a2e2014-01-25 01:18:18 +0000982SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
983 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000984}
985
Hans Wennborg4d67a2e2014-01-25 01:18:18 +0000986SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000987 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +0000988
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000989 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +0000990 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +0000991
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000992 const TargetLowering *TLI = TM.getTargetLowering();
993
Duncan Sandsf2641e12011-09-06 19:07:46 +0000994 // In some cases the vector type is legal but the element type is illegal and
995 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
996 // inserted value (the type does not need to match the vector element type).
997 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000998 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +0000999 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001000 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001001 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1002 Elt = ConstantInt::get(*getContext(), NewVal);
1003 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001004 // In other cases the element type is illegal and needs to be expanded, for
1005 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1006 // the value into n parts and use a vector type with n-times the elements.
1007 // Then bitcast to the type requested.
1008 // Legalizing constants too early makes the DAGCombiner's job harder so we
1009 // only legalize if the DAG tells us we must produce legal types.
1010 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1011 TLI->getTypeAction(*getContext(), EltVT) ==
1012 TargetLowering::TypeExpandInteger) {
1013 APInt NewVal = Elt->getValue();
1014 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1015 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1016 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1017 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1018
1019 // Check the temporary vector is the correct size. If this fails then
1020 // getTypeToTransformTo() probably returned a type whose size (in bits)
1021 // isn't a power-of-2 factor of the requested type size.
1022 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1023
1024 SmallVector<SDValue, 2> EltParts;
1025 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1026 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1027 .trunc(ViaEltSizeInBits),
Hans Wennborg4d67a2e2014-01-25 01:18:18 +00001028 ViaEltVT, isT));
Daniel Sanders50b80412013-11-15 12:56:49 +00001029 }
1030
1031 // EltParts is currently in little endian order. If we actually want
1032 // big-endian order then reverse it now.
1033 if (TLI->isBigEndian())
1034 std::reverse(EltParts.begin(), EltParts.end());
1035
1036 // The elements must be reversed when the element order is different
1037 // to the endianness of the elements (because the BITCAST is itself a
1038 // vector shuffle in this situation). However, we do not need any code to
1039 // perform this reversal because getConstant() is producing a vector
1040 // splat.
1041 // This situation occurs in MIPS MSA.
1042
1043 SmallVector<SDValue, 8> Ops;
1044 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1045 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1046
1047 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1048 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
1049 &Ops[0], Ops.size()));
1050 return Result;
1051 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001052
1053 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1054 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001055 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001056 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001057 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001058 ID.AddPointer(Elt);
Chris Lattner3f16b202006-08-11 21:01:22 +00001059 void *IP = 0;
Dan Gohman7a7742c2007-12-12 22:21:26 +00001060 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +00001061 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001062 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001063 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001064
Dan Gohman7a7742c2007-12-12 22:21:26 +00001065 if (!N) {
Hans Wennborg4d67a2e2014-01-25 01:18:18 +00001066 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001067 CSEMap.InsertNode(N, IP);
1068 AllNodes.push_back(N);
1069 }
1070
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001071 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001072 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001073 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001074 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001075 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7a7742c2007-12-12 22:21:26 +00001076 }
1077 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001078}
1079
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001080SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001081 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001082}
1083
1084
Owen Anderson53aa7a92009-08-10 22:56:29 +00001085SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001086 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001087}
1088
Owen Anderson53aa7a92009-08-10 22:56:29 +00001089SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001090 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001091
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001092 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001093
Chris Lattner381dddc2005-02-17 20:17:32 +00001094 // Do the map lookup using the actual bit pattern for the floating point
1095 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1096 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001097 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001098 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001099 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001100 ID.AddPointer(&V);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001101 void *IP = 0;
Evan Cheng9458e6a2007-06-29 21:36:04 +00001102 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +00001103 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001104 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001105 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001106
Evan Cheng9458e6a2007-06-29 21:36:04 +00001107 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001108 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001109 CSEMap.InsertNode(N, IP);
1110 AllNodes.push_back(N);
1111 }
1112
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001113 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001114 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001115 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001116 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001117 // FIXME SDLoc info might be appropriate here
1118 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohmana8665142007-06-25 16:23:39 +00001119 }
1120 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001121}
1122
Owen Anderson53aa7a92009-08-10 22:56:29 +00001123SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001124 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001125 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001126 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001127 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001128 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001129 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1130 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001131 bool ignored;
1132 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001133 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001134 &ignored);
1135 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001136 } else
1137 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001138}
1139
Andrew Trickef9de2a2013-05-25 02:42:55 +00001140SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001141 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001142 bool isTargetGA,
1143 unsigned char TargetFlags) {
1144 assert((TargetFlags == 0 || isTargetGA) &&
1145 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001146 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001147
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001148 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001149 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001150 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001151 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001152
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001153 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1154 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001155 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001156 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov1a114042008-09-09 20:05:04 +00001157 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001158 }
1159
Chris Lattner8e34f982009-06-25 21:21:14 +00001160 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001161 if (GVar && GVar->isThreadLocal())
1162 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1163 else
1164 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001165
Jim Laskeyf576b422006-10-27 23:46:08 +00001166 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001167 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001168 ID.AddPointer(GV);
1169 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001170 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001171 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner3f16b202006-08-11 21:01:22 +00001172 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001173 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001174 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001175
Andrew Trickef9de2a2013-05-25 02:42:55 +00001176 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1177 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001178 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001179 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001180 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001181 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001182}
1183
Owen Anderson53aa7a92009-08-10 22:56:29 +00001184SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001185 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001186 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001187 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001188 ID.AddInteger(FI);
1189 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001190 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001191 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001192
Dan Gohman01c65a22010-03-18 18:49:47 +00001193 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001194 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001195 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001196 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001197}
1198
Owen Anderson53aa7a92009-08-10 22:56:29 +00001199SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001200 unsigned char TargetFlags) {
1201 assert((TargetFlags == 0 || isTarget) &&
1202 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001203 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001204 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001205 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001206 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001207 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001208 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001209 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001210 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001211
Dan Gohman01c65a22010-03-18 18:49:47 +00001212 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1213 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001214 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001215 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001216 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001217}
1218
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001219SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001220 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001221 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001222 unsigned char TargetFlags) {
1223 assert((TargetFlags == 0 || isTarget) &&
1224 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001225 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001226 Alignment =
1227 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001228 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001229 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001230 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001231 ID.AddInteger(Alignment);
1232 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001233 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001234 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001235 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001236 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001237 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001238
Dan Gohman01c65a22010-03-18 18:49:47 +00001239 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1240 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001241 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001242 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001243 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001244}
1245
Chris Lattner061a1ea2005-01-07 07:46:32 +00001246
Owen Anderson53aa7a92009-08-10 22:56:29 +00001247SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001248 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001249 bool isTarget,
1250 unsigned char TargetFlags) {
1251 assert((TargetFlags == 0 || isTarget) &&
1252 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001253 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001254 Alignment =
1255 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001256 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001257 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001258 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001259 ID.AddInteger(Alignment);
1260 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001261 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001262 ID.AddInteger(TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001263 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001264 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001265 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001266
Dan Gohman01c65a22010-03-18 18:49:47 +00001267 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1268 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001269 CSEMap.InsertNode(N, IP);
1270 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001271 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001272}
1273
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001274SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1275 unsigned char TargetFlags) {
1276 FoldingSetNodeID ID;
1277 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1278 ID.AddInteger(Index);
1279 ID.AddInteger(Offset);
1280 ID.AddInteger(TargetFlags);
1281 void *IP = 0;
1282 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1283 return SDValue(E, 0);
1284
1285 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1286 TargetFlags);
1287 CSEMap.InsertNode(N, IP);
1288 AllNodes.push_back(N);
1289 return SDValue(N, 0);
1290}
1291
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001292SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001293 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001294 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001295 ID.AddPointer(MBB);
1296 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001297 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001298 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001299
Dan Gohman01c65a22010-03-18 18:49:47 +00001300 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001301 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001302 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001303 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001304}
1305
Owen Anderson53aa7a92009-08-10 22:56:29 +00001306SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001307 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1308 ValueTypeNodes.size())
1309 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001310
Duncan Sands13237ac2008-06-06 12:08:01 +00001311 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001312 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001313
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001314 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001315 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001316 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001317 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001318}
1319
Owen Anderson53aa7a92009-08-10 22:56:29 +00001320SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001321 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001322 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001323 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001324 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001325 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001326}
1327
Owen Anderson53aa7a92009-08-10 22:56:29 +00001328SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001329 unsigned char TargetFlags) {
1330 SDNode *&N =
1331 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1332 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001333 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001334 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001335 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001336 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001337}
1338
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001339SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001340 if ((unsigned)Cond >= CondCodeNodes.size())
1341 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001342
Chris Lattner14e060f2005-08-09 20:40:02 +00001343 if (CondCodeNodes[Cond] == 0) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001344 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001345 CondCodeNodes[Cond] = N;
1346 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001347 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001348
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001349 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001350}
1351
Nate Begeman5f829d82009-04-29 05:20:52 +00001352// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1353// the shuffle mask M that point at N1 to point at N2, and indices that point
1354// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001355static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1356 std::swap(N1, N2);
1357 int NElts = M.size();
1358 for (int i = 0; i != NElts; ++i) {
1359 if (M[i] >= NElts)
1360 M[i] -= NElts;
1361 else if (M[i] >= 0)
1362 M[i] += NElts;
1363 }
1364}
1365
Andrew Trickef9de2a2013-05-25 02:42:55 +00001366SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001367 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001368 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1369 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001370
1371 // Canonicalize shuffle undef, undef -> undef
1372 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001373 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001374
Eric Christopherdfda92b2009-08-22 00:40:45 +00001375 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001376 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001377 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001378 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001379 for (unsigned i = 0; i != NElts; ++i) {
1380 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001381 MaskVec.push_back(Mask[i]);
1382 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001383
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001384 // Canonicalize shuffle v, v -> v, undef
1385 if (N1 == N2) {
1386 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001387 for (unsigned i = 0; i != NElts; ++i)
1388 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001389 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001390
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001391 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1392 if (N1.getOpcode() == ISD::UNDEF)
1393 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001394
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001395 // Canonicalize all index into lhs, -> shuffle lhs, undef
1396 // Canonicalize all index into rhs, -> shuffle rhs, undef
1397 bool AllLHS = true, AllRHS = true;
1398 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001399 for (unsigned i = 0; i != NElts; ++i) {
1400 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001401 if (N2Undef)
1402 MaskVec[i] = -1;
1403 else
1404 AllLHS = false;
1405 } else if (MaskVec[i] >= 0) {
1406 AllRHS = false;
1407 }
1408 }
1409 if (AllLHS && AllRHS)
1410 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001411 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001412 N2 = getUNDEF(VT);
1413 if (AllRHS) {
1414 N1 = getUNDEF(VT);
1415 commuteShuffle(N1, N2, MaskVec);
1416 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001417
Craig Topper9a39b072013-08-08 08:03:12 +00001418 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001419 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001420 for (unsigned i = 0; i != NElts; ++i) {
1421 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001422 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001423 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001424 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001425
1426 FoldingSetNodeID ID;
1427 SDValue Ops[2] = { N1, N2 };
1428 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5f829d82009-04-29 05:20:52 +00001429 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001430 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001431
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001432 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001433 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001434 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001435
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001436 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1437 // SDNode doesn't have access to it. This memory will be "leaked" when
1438 // the node is deallocated, but recovered when the NodeAllocator is released.
1439 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1440 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001441
Dan Gohman01c65a22010-03-18 18:49:47 +00001442 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001443 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1444 dl.getDebugLoc(), N1, N2,
1445 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001446 CSEMap.InsertNode(N, IP);
1447 AllNodes.push_back(N);
1448 return SDValue(N, 0);
1449}
1450
Andrew Trickef9de2a2013-05-25 02:42:55 +00001451SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001452 SDValue Val, SDValue DTy,
1453 SDValue STy, SDValue Rnd, SDValue Sat,
1454 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001455 // If the src and dest types are the same and the conversion is between
1456 // integer types of the same sign or two floats, no conversion is necessary.
1457 if (DTy == STy &&
1458 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001459 return Val;
1460
1461 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001462 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1463 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001464 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001465 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001466 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001467
Jack Carter170a5f22013-09-09 22:02:08 +00001468 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1469 dl.getDebugLoc(),
1470 Ops, 5, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001471 CSEMap.InsertNode(N, IP);
1472 AllNodes.push_back(N);
1473 return SDValue(N, 0);
1474}
1475
Owen Anderson53aa7a92009-08-10 22:56:29 +00001476SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001477 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001478 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001479 ID.AddInteger(RegNo);
1480 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001481 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001482 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001483
Dan Gohman01c65a22010-03-18 18:49:47 +00001484 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001485 CSEMap.InsertNode(N, IP);
1486 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001487 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001488}
1489
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001490SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1491 FoldingSetNodeID ID;
1492 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1493 ID.AddPointer(RegMask);
1494 void *IP = 0;
1495 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1496 return SDValue(E, 0);
1497
1498 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1499 CSEMap.InsertNode(N, IP);
1500 AllNodes.push_back(N);
1501 return SDValue(N, 0);
1502}
1503
Andrew Trickef9de2a2013-05-25 02:42:55 +00001504SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001505 FoldingSetNodeID ID;
1506 SDValue Ops[] = { Root };
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001507 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1508 ID.AddPointer(Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001509 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001511 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001512
Jack Carter170a5f22013-09-09 22:02:08 +00001513 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1514 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001515 CSEMap.InsertNode(N, IP);
1516 AllNodes.push_back(N);
1517 return SDValue(N, 0);
1518}
1519
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001520
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001521SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001522 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001523 bool isTarget,
1524 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001525 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1526
1527 FoldingSetNodeID ID;
Dan Gohman7a6611792009-11-20 23:18:13 +00001528 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman6c938802009-10-30 01:27:03 +00001529 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001530 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001531 ID.AddInteger(TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001532 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001533 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001534 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001535
Michael Liaoabb87d42012-09-12 21:43:09 +00001536 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1537 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001538 CSEMap.InsertNode(N, IP);
1539 AllNodes.push_back(N);
1540 return SDValue(N, 0);
1541}
1542
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001543SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001544 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001545 "SrcValue is not a pointer?");
1546
Jim Laskeyf576b422006-10-27 23:46:08 +00001547 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001548 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001549 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001550
Chris Lattner3f16b202006-08-11 21:01:22 +00001551 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001553 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001554
Dan Gohman01c65a22010-03-18 18:49:47 +00001555 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001556 CSEMap.InsertNode(N, IP);
1557 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001558 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001559}
1560
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001561/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1562SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1563 FoldingSetNodeID ID;
1564 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1565 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001566
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001567 void *IP = 0;
1568 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1569 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001570
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001571 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1572 CSEMap.InsertNode(N, IP);
1573 AllNodes.push_back(N);
1574 return SDValue(N, 0);
1575}
1576
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001577/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1578SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1579 unsigned SrcAS, unsigned DestAS) {
1580 SDValue Ops[] = {Ptr};
1581 FoldingSetNodeID ID;
1582 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), &Ops[0], 1);
1583 ID.AddInteger(SrcAS);
1584 ID.AddInteger(DestAS);
1585
1586 void *IP = 0;
1587 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1588 return SDValue(E, 0);
1589
1590 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1591 dl.getDebugLoc(),
1592 VT, Ptr, SrcAS, DestAS);
1593 CSEMap.InsertNode(N, IP);
1594 AllNodes.push_back(N);
1595 return SDValue(N, 0);
1596}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001597
Duncan Sands41826032009-01-31 15:50:11 +00001598/// getShiftAmountOperand - Return the specified value casted to
1599/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001600SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001601 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001602 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001603 if (OpTy == ShTy || OpTy.isVector()) return Op;
1604
1605 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001606 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001607}
1608
Chris Lattner9eb7a822007-10-15 17:47:20 +00001609/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1610/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001611SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001612 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001613 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001614 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001615 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001616 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001617 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001618
David Greene1fbe0542009-11-12 20:49:22 +00001619 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001620 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001621}
1622
Duncan Sands445071c2008-12-09 21:33:20 +00001623/// CreateStackTemporary - Create a stack temporary suitable for holding
1624/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001625SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001626 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1627 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001628 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1629 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001630 const TargetLowering *TLI = TM.getTargetLowering();
1631 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001632 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1633 TD->getPrefTypeAlignment(Ty2));
1634
1635 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001636 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001637 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001638}
1639
Owen Anderson53aa7a92009-08-10 22:56:29 +00001640SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001641 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001642 // These setcc operations always fold.
1643 switch (Cond) {
1644 default: break;
1645 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001646 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001647 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001648 case ISD::SETTRUE2: {
1649 const TargetLowering *TLI = TM.getTargetLowering();
1650 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1651 return getConstant(
1652 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1653 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001654
Chris Lattner393d96a2006-04-27 05:01:07 +00001655 case ISD::SETOEQ:
1656 case ISD::SETOGT:
1657 case ISD::SETOGE:
1658 case ISD::SETOLT:
1659 case ISD::SETOLE:
1660 case ISD::SETONE:
1661 case ISD::SETO:
1662 case ISD::SETUO:
1663 case ISD::SETUEQ:
1664 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001665 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001666 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001667 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001668
Gabor Greiff304a7a2008-08-28 21:40:38 +00001669 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001670 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001671 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001672 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001673
Chris Lattner061a1ea2005-01-07 07:46:32 +00001674 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001675 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001676 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1677 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001678 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1679 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1680 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1681 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1682 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1683 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1684 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1685 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001686 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001687 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001688 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001689 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1690 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001691 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001692 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001693 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001694 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001695 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001696 // fall through
1697 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001698 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001699 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001700 // fall through
1701 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001702 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001703 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001704 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001705 // fall through
1706 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001707 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001708 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001709 // fall through
1710 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001711 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001712 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001713 // fall through
1714 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001715 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001716 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001717 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001718 // fall through
1719 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001720 R==APFloat::cmpEqual, VT);
1721 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1722 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1723 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1724 R==APFloat::cmpEqual, VT);
1725 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1726 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1727 R==APFloat::cmpLessThan, VT);
1728 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1729 R==APFloat::cmpUnordered, VT);
1730 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1731 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001732 }
1733 } else {
1734 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001735 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1736 MVT CompVT = N1.getValueType().getSimpleVT();
1737 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1738 return SDValue();
1739
1740 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001741 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001742 }
1743
Chris Lattnerd47675e2005-08-09 20:20:18 +00001744 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001745 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001746}
1747
Dan Gohman1f372ed2008-02-25 21:11:39 +00001748/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1749/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001750bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001751 // This predicate is not safe for vector operations.
1752 if (Op.getValueType().isVector())
1753 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001754
Dan Gohman1d459e42009-12-11 21:31:27 +00001755 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001756 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1757}
1758
Dan Gohman309d3d52007-06-22 14:59:07 +00001759/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1760/// this predicate to simplify operations downstream. Mask is known to be zero
1761/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001762bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001763 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001764 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001765 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001766 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001767 return (KnownZero & Mask) == Mask;
1768}
1769
1770/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1771/// known to be either zero or one and return them in the KnownZero/KnownOne
1772/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1773/// processing.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001774void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1775 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001776 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001777 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001778
Dan Gohmanf990faf2008-02-13 00:35:47 +00001779 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001780 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001781 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001782
Dan Gohmanf990faf2008-02-13 00:35:47 +00001783 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001784
1785 switch (Op.getOpcode()) {
1786 case ISD::Constant:
1787 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001788 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1789 KnownZero = ~KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00001790 return;
1791 case ISD::AND:
1792 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001793 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1794 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001795 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1796 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001797
1798 // Output known-1 bits are only known if set in both the LHS & RHS.
1799 KnownOne &= KnownOne2;
1800 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1801 KnownZero |= KnownZero2;
1802 return;
1803 case ISD::OR:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001804 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1805 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001806 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1807 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1808
Dan Gohman309d3d52007-06-22 14:59:07 +00001809 // Output known-0 bits are only known if clear in both the LHS & RHS.
1810 KnownZero &= KnownZero2;
1811 // Output known-1 are known to be set if set in either the LHS | RHS.
1812 KnownOne |= KnownOne2;
1813 return;
1814 case ISD::XOR: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001815 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1816 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001817 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1818 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1819
Dan Gohman309d3d52007-06-22 14:59:07 +00001820 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001821 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001822 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1823 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1824 KnownZero = KnownZeroOut;
1825 return;
1826 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001827 case ISD::MUL: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001828 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1829 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001830 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1831 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1832
1833 // If low bits are zero in either operand, output low known-0 bits.
1834 // Also compute a conserative estimate for high known-0 bits.
1835 // More trickiness is possible, but this is sufficient for the
1836 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001837 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001838 unsigned TrailZ = KnownZero.countTrailingOnes() +
1839 KnownZero2.countTrailingOnes();
1840 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001841 KnownZero2.countLeadingOnes(),
1842 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001843
1844 TrailZ = std::min(TrailZ, BitWidth);
1845 LeadZ = std::min(LeadZ, BitWidth);
1846 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1847 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001848 return;
1849 }
1850 case ISD::UDIV: {
1851 // For the purposes of computing leading zeros we can conservatively
1852 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001853 // be less than the denominator.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001854 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001855 unsigned LeadZ = KnownZero2.countLeadingOnes();
1856
Jay Foad25a5e4c2010-12-01 08:53:58 +00001857 KnownOne2.clearAllBits();
1858 KnownZero2.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001859 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001860 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1861 if (RHSUnknownLeadingOnes != BitWidth)
1862 LeadZ = std::min(BitWidth,
1863 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001864
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001865 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001866 return;
1867 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001868 case ISD::SELECT:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001869 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1870 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001871 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1872 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1873
Dan Gohman309d3d52007-06-22 14:59:07 +00001874 // Only known if known in both the LHS and RHS.
1875 KnownOne &= KnownOne2;
1876 KnownZero &= KnownZero2;
1877 return;
1878 case ISD::SELECT_CC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001879 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1880 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001881 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1882 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1883
Dan Gohman309d3d52007-06-22 14:59:07 +00001884 // Only known if known in both the LHS and RHS.
1885 KnownOne &= KnownOne2;
1886 KnownZero &= KnownZero2;
1887 return;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001888 case ISD::SADDO:
1889 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001890 case ISD::SSUBO:
1891 case ISD::USUBO:
1892 case ISD::SMULO:
1893 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001894 if (Op.getResNo() != 1)
1895 return;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001896 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001897 case ISD::SETCC:
1898 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001899 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001900 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001901 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001902 return;
1903 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001904 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001905 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001906 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001907
1908 // If the shift count is an invalid immediate, don't do anything.
1909 if (ShAmt >= BitWidth)
1910 return;
1911
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001912 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001913 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman9db0aa82008-02-26 18:50:50 +00001914 KnownZero <<= ShAmt;
1915 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00001916 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00001917 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001918 }
1919 return;
1920 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001921 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001922 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001923 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001924
Dan Gohman9db0aa82008-02-26 18:50:50 +00001925 // If the shift count is an invalid immediate, don't do anything.
1926 if (ShAmt >= BitWidth)
1927 return;
1928
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001929 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001930 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001931 KnownZero = KnownZero.lshr(ShAmt);
1932 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001933
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001934 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001935 KnownZero |= HighBits; // High bits known zero.
1936 }
1937 return;
1938 case ISD::SRA:
1939 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001940 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001941
Dan Gohman9db0aa82008-02-26 18:50:50 +00001942 // If the shift count is an invalid immediate, don't do anything.
1943 if (ShAmt >= BitWidth)
1944 return;
1945
Dan Gohman309d3d52007-06-22 14:59:07 +00001946 // If any of the demanded bits are produced by the sign extension, we also
1947 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001948 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001949
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001950 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001951 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001952 KnownZero = KnownZero.lshr(ShAmt);
1953 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001954
Dan Gohman309d3d52007-06-22 14:59:07 +00001955 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001956 APInt SignBit = APInt::getSignBit(BitWidth);
1957 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001958
Dan Gohmanb717fda2008-02-20 16:30:17 +00001959 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001960 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001961 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001962 KnownOne |= HighBits; // New bits are known one.
1963 }
1964 }
1965 return;
1966 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001967 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001968 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001969
1970 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00001971 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001972 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00001973
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001974 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001975 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001976
Dan Gohman309d3d52007-06-22 14:59:07 +00001977 // If the sign extended bits are demanded, we know that the sign
1978 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00001979 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001980 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00001981 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001982
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001983 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1984 KnownOne &= InputDemandedBits;
1985 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001986 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1987
Dan Gohman309d3d52007-06-22 14:59:07 +00001988 // If the sign bit of the input is known set or clear, then we know the
1989 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001990 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00001991 KnownZero |= NewBits;
1992 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00001993 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00001994 KnownOne |= NewBits;
1995 KnownZero &= ~NewBits;
1996 } else { // Input sign bit unknown
1997 KnownZero &= ~NewBits;
1998 KnownOne &= ~NewBits;
1999 }
2000 return;
2001 }
2002 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002003 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002004 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002005 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002006 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002007 unsigned LowBits = Log2_32(BitWidth)+1;
2008 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002009 KnownOne.clearAllBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002010 return;
2011 }
2012 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002013 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002014 // If this is a ZEXTLoad and we are looking at the loaded value.
2015 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002016 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002017 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002018 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002019 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002020 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002021 }
2022 return;
2023 }
2024 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002025 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002026 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002027 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002028 KnownZero = KnownZero.trunc(InBits);
2029 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002030 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002031 KnownZero = KnownZero.zext(BitWidth);
2032 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002033 KnownZero |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002034 return;
2035 }
2036 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002037 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002038 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002039 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002040
Jay Foad583abbc2010-12-07 08:25:19 +00002041 KnownZero = KnownZero.trunc(InBits);
2042 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002043 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002044
2045 // Note if the sign bit is known to be zero or one.
2046 bool SignBitKnownZero = KnownZero.isNegative();
2047 bool SignBitKnownOne = KnownOne.isNegative();
2048 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2049 "Sign bit can't be known to be both zero and one!");
2050
Jay Foad583abbc2010-12-07 08:25:19 +00002051 KnownZero = KnownZero.zext(BitWidth);
2052 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002053
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002054 // If the sign bit is known zero or one, the top bits match.
2055 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002056 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002057 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002058 KnownOne |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002059 return;
2060 }
2061 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002062 EVT InVT = Op.getOperand(0).getValueType();
2063 unsigned InBits = InVT.getScalarType().getSizeInBits();
2064 KnownZero = KnownZero.trunc(InBits);
2065 KnownOne = KnownOne.trunc(InBits);
2066 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2067 KnownZero = KnownZero.zext(BitWidth);
2068 KnownOne = KnownOne.zext(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002069 return;
2070 }
2071 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002072 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002073 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002074 KnownZero = KnownZero.zext(InBits);
2075 KnownOne = KnownOne.zext(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002076 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002077 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +00002078 KnownZero = KnownZero.trunc(BitWidth);
2079 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002080 break;
2081 }
2082 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002083 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002084 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002085 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2086 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002087 KnownOne &= (~KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002088 return;
2089 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002090 case ISD::FGETSIGN:
2091 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002092 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerafc8f132007-12-22 21:26:52 +00002093 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002094
Dan Gohman72ec3f42008-04-28 17:02:21 +00002095 case ISD::SUB: {
2096 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2097 // We know that the top bits of C-X are clear if X contains less bits
2098 // than C (i.e. no wrap-around can happen). For example, 20-X is
2099 // positive if we can prove that X is >= 0 and < 16.
2100 if (CLHS->getAPIntValue().isNonNegative()) {
2101 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2102 // NLZ can't be BitWidth with no sign bit
2103 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002104 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002105
2106 // If all of the MaskV bits are known to be zero, then we know the
2107 // output top bits are zero, because we now know that the output is
2108 // from [0-C].
2109 if ((KnownZero2 & MaskV) == MaskV) {
2110 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2111 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002112 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002113 }
2114 }
2115 }
2116 }
2117 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002118 case ISD::ADD:
2119 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002120 // Output known-0 bits are known if clear or set in both the low clear bits
2121 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2122 // low 3 bits clear.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002123 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002124 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002125 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2126
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002127 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002128 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002129 KnownZeroOut = std::min(KnownZeroOut,
2130 KnownZero2.countTrailingOnes());
2131
Chris Lattner440b2802010-12-19 20:38:28 +00002132 if (Op.getOpcode() == ISD::ADD) {
2133 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2134 return;
2135 }
2136
2137 // With ADDE, a carry bit may be added in, so we can only use this
2138 // information if we know (at least) that the low two bits are clear. We
2139 // then return to the caller that the low bit is unknown but that other bits
2140 // are known zero.
2141 if (KnownZeroOut >= 2) // ADDE
2142 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohman309d3d52007-06-22 14:59:07 +00002143 return;
2144 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002145 case ISD::SREM:
2146 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002147 const APInt &RA = Rem->getAPIntValue().abs();
2148 if (RA.isPowerOf2()) {
2149 APInt LowBits = RA - 1;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002150 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002151
Duncan Sands33274982010-01-29 09:45:26 +00002152 // The low bits of the first operand are unchanged by the srem.
2153 KnownZero = KnownZero2 & LowBits;
2154 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002155
Duncan Sands33274982010-01-29 09:45:26 +00002156 // If the first operand is non-negative or has all low bits zero, then
2157 // the upper bits are all zero.
2158 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2159 KnownZero |= ~LowBits;
2160
2161 // If the first operand is negative and not all low bits are zero, then
2162 // the upper bits are all one.
2163 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2164 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002165 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002166 }
2167 }
2168 return;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002169 case ISD::UREM: {
2170 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002171 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002172 if (RA.isPowerOf2()) {
2173 APInt LowBits = (RA - 1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002174 KnownZero |= ~LowBits;
2175 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002176 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2177 break;
2178 }
2179 }
2180
2181 // Since the result is less than or equal to either operand, any leading
2182 // zero bits in either operand must also exist in the result.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002183 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2184 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002185
2186 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2187 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002188 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002189 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002190 return;
Dan Gohman309d3d52007-06-22 14:59:07 +00002191 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002192 case ISD::FrameIndex:
2193 case ISD::TargetFrameIndex:
2194 if (unsigned Align = InferPtrAlignment(Op)) {
2195 // The low bits are known zero if the pointer is aligned.
2196 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2197 return;
2198 }
2199 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002200
Dan Gohman309d3d52007-06-22 14:59:07 +00002201 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002202 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2203 break;
2204 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002205 case ISD::INTRINSIC_WO_CHAIN:
2206 case ISD::INTRINSIC_W_CHAIN:
2207 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002208 // Allow the target to implement this method for its nodes.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002209 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002210 return;
2211 }
2212}
2213
2214/// ComputeNumSignBits - Return the number of times the sign bit of the
2215/// register is replicated into the other bits. We know that at least 1 bit
2216/// is always equal to the sign bit (itself), but other cases can give us
2217/// information. For example, immediately after an "SRA X, 2", we know that
2218/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002219unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002220 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002221 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002222 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002223 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002224 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002225 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002226
Dan Gohman309d3d52007-06-22 14:59:07 +00002227 if (Depth == 6)
2228 return 1; // Limit search depth.
2229
2230 switch (Op.getOpcode()) {
2231 default: break;
2232 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002233 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002234 return VTBits-Tmp+1;
2235 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002236 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002237 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002238
Dan Gohman309d3d52007-06-22 14:59:07 +00002239 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002240 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002241 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002242 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002243
Dan Gohman309d3d52007-06-22 14:59:07 +00002244 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002245 Tmp =
2246 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002247 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002248
Dan Gohman309d3d52007-06-22 14:59:07 +00002249 case ISD::SIGN_EXTEND_INREG:
2250 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002251 Tmp =
2252 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002253 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002254
Dan Gohman309d3d52007-06-22 14:59:07 +00002255 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2256 return std::max(Tmp, Tmp2);
2257
2258 case ISD::SRA:
2259 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2260 // SRA X, C -> adds C sign bits.
2261 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002262 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002263 if (Tmp > VTBits) Tmp = VTBits;
2264 }
2265 return Tmp;
2266 case ISD::SHL:
2267 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2268 // shl destroys sign bits.
2269 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002270 if (C->getZExtValue() >= VTBits || // Bad shift.
2271 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2272 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002273 }
2274 break;
2275 case ISD::AND:
2276 case ISD::OR:
2277 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002278 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002279 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002280 if (Tmp != 1) {
2281 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2282 FirstAnswer = std::min(Tmp, Tmp2);
2283 // We computed what we know about the sign bits as our first
2284 // answer. Now proceed to the generic code that uses
2285 // ComputeMaskedBits, and pick whichever answer is better.
2286 }
2287 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002288
2289 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002290 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002291 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002292 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002293 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002294
2295 case ISD::SADDO:
2296 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002297 case ISD::SSUBO:
2298 case ISD::USUBO:
2299 case ISD::SMULO:
2300 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002301 if (Op.getResNo() != 1)
2302 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002303 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002304 case ISD::SETCC:
2305 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002306 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002307 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002308 return VTBits;
2309 break;
2310 case ISD::ROTL:
2311 case ISD::ROTR:
2312 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002313 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002314
Dan Gohman309d3d52007-06-22 14:59:07 +00002315 // Handle rotate right by N like a rotate left by 32-N.
2316 if (Op.getOpcode() == ISD::ROTR)
2317 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2318
2319 // If we aren't rotating out all of the known-in sign bits, return the
2320 // number that are left. This handles rotl(sext(x), 1) for example.
2321 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2322 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2323 }
2324 break;
2325 case ISD::ADD:
2326 // Add can have at most one carry bit. Thus we know that the output
2327 // is, at worst, one more bit than the inputs.
2328 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2329 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002330
Dan Gohman309d3d52007-06-22 14:59:07 +00002331 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002332 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002333 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002334 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002335 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002336
Dan Gohman309d3d52007-06-22 14:59:07 +00002337 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2338 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002339 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002340 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002341
Dan Gohman309d3d52007-06-22 14:59:07 +00002342 // If we are subtracting one from a positive number, there is no carry
2343 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002344 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002345 return Tmp;
2346 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002347
Dan Gohman309d3d52007-06-22 14:59:07 +00002348 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2349 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002350 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002351
Dan Gohman309d3d52007-06-22 14:59:07 +00002352 case ISD::SUB:
2353 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2354 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002355
Dan Gohman309d3d52007-06-22 14:59:07 +00002356 // Handle NEG.
2357 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002358 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002359 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002360 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002361 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2362 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002363 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002364 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002365
Dan Gohman309d3d52007-06-22 14:59:07 +00002366 // If the input is known to be positive (the sign bit is known clear),
2367 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002368 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002369 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002370
Dan Gohman309d3d52007-06-22 14:59:07 +00002371 // Otherwise, we treat this like a SUB.
2372 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002373
Dan Gohman309d3d52007-06-22 14:59:07 +00002374 // Sub can have at most one carry bit. Thus we know that the output
2375 // is, at worst, one more bit than the inputs.
2376 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2377 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002378 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002379 case ISD::TRUNCATE:
2380 // FIXME: it's tricky to do anything useful for this, but it is an important
2381 // case for targets like X86.
2382 break;
2383 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002384
Nadav Rotem4536d582013-03-20 22:53:44 +00002385 // If we are looking at the loaded value of the SDNode.
2386 if (Op.getResNo() == 0) {
2387 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2388 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2389 unsigned ExtType = LD->getExtensionType();
2390 switch (ExtType) {
2391 default: break;
2392 case ISD::SEXTLOAD: // '17' bits known
2393 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2394 return VTBits-Tmp+1;
2395 case ISD::ZEXTLOAD: // '16' bits known
2396 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2397 return VTBits-Tmp;
2398 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002399 }
2400 }
2401
2402 // Allow the target to implement this method for its nodes.
2403 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002404 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002405 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2406 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002407 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002408 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002409 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002410
Dan Gohman309d3d52007-06-22 14:59:07 +00002411 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2412 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002413 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002414 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002415
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002416 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002417 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002418 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002419 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002420 Mask = KnownOne;
2421 } else {
2422 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002423 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002424 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002425
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2427 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002428 Mask = ~Mask;
2429 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002430 // Return # leading zeros. We use 'min' here in case Val was zero before
2431 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002432 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002433}
2434
Chris Lattner46c01a32011-02-13 22:25:43 +00002435/// isBaseWithConstantOffset - Return true if the specified operand is an
2436/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2437/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2438/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002439/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002440bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2441 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2442 !isa<ConstantSDNode>(Op.getOperand(1)))
2443 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002444
2445 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002446 !MaskedValueIsZero(Op.getOperand(0),
2447 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2448 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002449
Chris Lattner46c01a32011-02-13 22:25:43 +00002450 return true;
2451}
2452
2453
Dan Gohmand0d5e682009-09-03 20:34:31 +00002454bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2455 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002456 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002457 return true;
2458
2459 // If the value is a constant, we can obviously see if it is a NaN or not.
2460 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2461 return !C->getValueAPF().isNaN();
2462
2463 // TODO: Recognize more cases here.
2464
2465 return false;
2466}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002467
Dan Gohman38605212010-02-24 06:52:40 +00002468bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2469 // If the value is a constant, we can obviously see if it is a zero or not.
2470 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2471 return !C->isZero();
2472
2473 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002474 switch (Op.getOpcode()) {
2475 default: break;
2476 case ISD::OR:
2477 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2478 return !C->isNullValue();
2479 break;
2480 }
Dan Gohman38605212010-02-24 06:52:40 +00002481
2482 return false;
2483}
2484
2485bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2486 // Check the obvious case.
2487 if (A == B) return true;
2488
2489 // For for negative and positive zero.
2490 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2491 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2492 if (CA->isZero() && CB->isZero()) return true;
2493
2494 // Otherwise they may not be equal.
2495 return false;
2496}
2497
Chris Lattner061a1ea2005-01-07 07:46:32 +00002498/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002499///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002500SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002501 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00002502 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattnerfcb16472006-08-11 18:38:11 +00002503 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002504 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002505 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002506
Jack Carter170a5f22013-09-09 22:02:08 +00002507 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2508 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002509 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002510
Chris Lattnerfcb16472006-08-11 18:38:11 +00002511 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002512#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002513 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002514#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002515 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002516}
2517
Andrew Trickef9de2a2013-05-25 02:42:55 +00002518SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002519 EVT VT, SDValue Operand) {
Chris Lattnera1874602005-12-23 05:30:37 +00002520 // Constant fold unary operations with an integer constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002521 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002522 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002523 switch (Opcode) {
2524 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002525 case ISD::SIGN_EXTEND:
Jay Foad583abbc2010-12-07 08:25:19 +00002526 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner8c393c22005-09-02 00:17:32 +00002527 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002528 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002529 case ISD::TRUNCATE:
Jay Foad583abbc2010-12-07 08:25:19 +00002530 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002531 case ISD::UINT_TO_FP:
2532 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002533 APFloat apf(EVTToAPFloatSemantics(VT),
2534 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002535 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002536 Opcode==ISD::SINT_TO_FP,
2537 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002538 return getConstantFP(apf, VT);
2539 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002540 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002541 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002542 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002543 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002544 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002545 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002546 case ISD::BSWAP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002547 return getConstant(Val.byteSwap(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002548 case ISD::CTPOP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002549 return getConstant(Val.countPopulation(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002550 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002551 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002552 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002553 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002554 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002555 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002556 }
2557 }
2558
Dale Johannesen446b9002007-08-31 23:34:27 +00002559 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002560 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002561 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002562 switch (Opcode) {
2563 case ISD::FNEG:
2564 V.changeSign();
2565 return getConstantFP(V, VT);
2566 case ISD::FABS:
2567 V.clearSign();
2568 return getConstantFP(V, VT);
2569 case ISD::FCEIL: {
2570 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2571 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002572 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002573 break;
2574 }
2575 case ISD::FTRUNC: {
2576 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2577 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002578 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002579 break;
2580 }
2581 case ISD::FFLOOR: {
2582 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2583 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002584 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002585 break;
2586 }
2587 case ISD::FP_EXTEND: {
2588 bool ignored;
2589 // This can return overflow, underflow, or inexact; we don't care.
2590 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002591 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002592 APFloat::rmNearestTiesToEven, &ignored);
2593 return getConstantFP(V, VT);
2594 }
2595 case ISD::FP_TO_SINT:
2596 case ISD::FP_TO_UINT: {
2597 integerPart x[2];
2598 bool ignored;
2599 assert(integerPartWidth >= 64);
2600 // FIXME need to be more flexible about rounding mode.
2601 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2602 Opcode==ISD::FP_TO_SINT,
2603 APFloat::rmTowardZero, &ignored);
2604 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002605 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002606 APInt api(VT.getSizeInBits(), x);
2607 return getConstant(api, VT);
2608 }
2609 case ISD::BITCAST:
2610 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2611 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2612 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2613 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2614 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002615 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002616 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002617
Gabor Greiff304a7a2008-08-28 21:40:38 +00002618 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002619 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002620 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002621 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002622 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002623 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002624 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002625 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002626 assert(VT.isFloatingPoint() &&
2627 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002628 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002629 assert((!VT.isVector() ||
2630 VT.getVectorNumElements() ==
2631 Operand.getValueType().getVectorNumElements()) &&
2632 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002633 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002634 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002635 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002636 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002637 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002638 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002639 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002640 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2641 "Invalid sext node, dst < src!");
2642 assert((!VT.isVector() ||
2643 VT.getVectorNumElements() ==
2644 Operand.getValueType().getVectorNumElements()) &&
2645 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002646 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2647 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2648 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002649 // sext(undef) = 0, because the top bits will all be the same.
2650 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002651 break;
2652 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002653 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002654 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002655 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002656 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2657 "Invalid zext node, dst < src!");
2658 assert((!VT.isVector() ||
2659 VT.getVectorNumElements() ==
2660 Operand.getValueType().getVectorNumElements()) &&
2661 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002662 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002663 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002664 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002665 else if (OpOpcode == ISD::UNDEF)
2666 // zext(undef) = 0, because the top bits will be zero.
2667 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002668 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002669 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002670 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002671 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002672 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002673 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2674 "Invalid anyext node, dst < src!");
2675 assert((!VT.isVector() ||
2676 VT.getVectorNumElements() ==
2677 Operand.getValueType().getVectorNumElements()) &&
2678 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002679
Dan Gohman08837892010-06-18 00:08:30 +00002680 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2681 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002682 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002683 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002684 else if (OpOpcode == ISD::UNDEF)
2685 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002686
2687 // (ext (trunx x)) -> x
2688 if (OpOpcode == ISD::TRUNCATE) {
2689 SDValue OpOp = Operand.getNode()->getOperand(0);
2690 if (OpOp.getValueType() == VT)
2691 return OpOp;
2692 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002693 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002694 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002695 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002696 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002697 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002698 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2699 "Invalid truncate node, src < dst!");
2700 assert((!VT.isVector() ||
2701 VT.getVectorNumElements() ==
2702 Operand.getValueType().getVectorNumElements()) &&
2703 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002704 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002705 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002706 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2707 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002708 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002709 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2710 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002711 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002712 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002713 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002714 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002715 }
Craig Topper201c1a32012-01-15 01:05:11 +00002716 if (OpOpcode == ISD::UNDEF)
2717 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002718 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002719 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002720 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002721 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002722 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002723 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002724 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2725 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002726 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002727 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002728 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002729 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002730 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002731 (VT.getVectorElementType() == Operand.getValueType() ||
2732 (VT.getVectorElementType().isInteger() &&
2733 Operand.getValueType().isInteger() &&
2734 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002735 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002736 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002737 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002738 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2739 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2740 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2741 Operand.getConstantOperandVal(1) == 0 &&
2742 Operand.getOperand(0).getValueType() == VT)
2743 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002744 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002745 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002746 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002747 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002748 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002749 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002750 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002751 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002752 break;
2753 case ISD::FABS:
2754 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002755 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002756 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002757 }
2758
Chris Lattnerf9c19152005-08-25 19:12:10 +00002759 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002760 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002761 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002762 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002763 SDValue Ops[1] = { Operand };
Chris Lattnerf17b4222007-02-04 07:28:00 +00002764 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002765 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002766 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002767 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002768
Jack Carter170a5f22013-09-09 22:02:08 +00002769 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2770 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002771 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002772 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002773 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2774 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002775 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002776
Chris Lattner061a1ea2005-01-07 07:46:32 +00002777 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002778#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002779 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002780#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002781 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002782}
2783
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002784SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2785 SDNode *Cst1, SDNode *Cst2) {
2786 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2787 SmallVector<SDValue, 4> Outputs;
2788 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002789
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002790 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2791 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Hans Wennborg4d67a2e2014-01-25 01:18:18 +00002792 if (Scalar1 && Scalar2) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002793 // Scalar instruction.
2794 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Hans Wennborg4d67a2e2014-01-25 01:18:18 +00002795 } else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002796 // For vectors extract each constant element into Inputs so we can constant
2797 // fold them individually.
2798 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2799 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2800 if (!BV1 || !BV2)
2801 return SDValue();
2802
2803 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2804
2805 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2806 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2807 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2808 if (!V1 || !V2) // Not a constant, bail.
2809 return SDValue();
2810
2811 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2812 // FIXME: This is valid and could be handled by truncating the APInts.
2813 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2814 return SDValue();
2815
2816 Inputs.push_back(std::make_pair(V1, V2));
2817 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002818 }
2819
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002820 // We have a number of constant values, constant fold them element by element.
2821 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2822 const APInt &C1 = Inputs[I].first->getAPIntValue();
2823 const APInt &C2 = Inputs[I].second->getAPIntValue();
2824
2825 switch (Opcode) {
2826 case ISD::ADD:
2827 Outputs.push_back(getConstant(C1 + C2, SVT));
2828 break;
2829 case ISD::SUB:
2830 Outputs.push_back(getConstant(C1 - C2, SVT));
2831 break;
2832 case ISD::MUL:
2833 Outputs.push_back(getConstant(C1 * C2, SVT));
2834 break;
2835 case ISD::UDIV:
2836 if (!C2.getBoolValue())
2837 return SDValue();
2838 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2839 break;
2840 case ISD::UREM:
2841 if (!C2.getBoolValue())
2842 return SDValue();
2843 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2844 break;
2845 case ISD::SDIV:
2846 if (!C2.getBoolValue())
2847 return SDValue();
2848 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2849 break;
2850 case ISD::SREM:
2851 if (!C2.getBoolValue())
2852 return SDValue();
2853 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2854 break;
2855 case ISD::AND:
2856 Outputs.push_back(getConstant(C1 & C2, SVT));
2857 break;
2858 case ISD::OR:
2859 Outputs.push_back(getConstant(C1 | C2, SVT));
2860 break;
2861 case ISD::XOR:
2862 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2863 break;
2864 case ISD::SHL:
2865 Outputs.push_back(getConstant(C1 << C2, SVT));
2866 break;
2867 case ISD::SRL:
2868 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2869 break;
2870 case ISD::SRA:
2871 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2872 break;
2873 case ISD::ROTL:
2874 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2875 break;
2876 case ISD::ROTR:
2877 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2878 break;
2879 default:
2880 return SDValue();
2881 }
2882 }
2883
2884 // Handle the scalar case first.
Silviu Baranga4ad2bc52013-04-25 09:32:33 +00002885 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002886 return Outputs.back();
2887
2888 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002889 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002890 Outputs.size());
Bill Wendling162c26d2008-09-24 10:16:24 +00002891}
2892
Andrew Trickef9de2a2013-05-25 02:42:55 +00002893SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002894 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002895 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2896 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002897 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002898 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002899 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002900 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2901 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002902 // Fold trivial token factors.
2903 if (N1.getOpcode() == ISD::EntryToken) return N2;
2904 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002905 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002906 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002907 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002908 // Concat of UNDEFs is UNDEF.
2909 if (N1.getOpcode() == ISD::UNDEF &&
2910 N2.getOpcode() == ISD::UNDEF)
2911 return getUNDEF(VT);
2912
Dan Gohman550c9af2008-08-14 20:04:46 +00002913 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2914 // one big BUILD_VECTOR.
2915 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2916 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002917 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2918 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002919 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00002920 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00002921 }
2922 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002923 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002924 assert(VT.isInteger() && "This operator does not apply to FP types!");
2925 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002926 N1.getValueType() == VT && "Binary operator types must match!");
2927 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2928 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002929 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002930 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002931 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2932 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002933 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002934 case ISD::OR:
2935 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002936 case ISD::ADD:
2937 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002938 assert(VT.isInteger() && "This operator does not apply to FP types!");
2939 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002940 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002941 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2942 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002943 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002944 return N1;
2945 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002946 case ISD::UDIV:
2947 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00002948 case ISD::MULHU:
2949 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002950 case ISD::MUL:
2951 case ISD::SDIV:
2952 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00002953 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002954 assert(N1.getValueType() == N2.getValueType() &&
2955 N1.getValueType() == VT && "Binary operator types must match!");
2956 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002957 case ISD::FADD:
2958 case ISD::FSUB:
2959 case ISD::FMUL:
2960 case ISD::FDIV:
2961 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002962 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00002963 if (Opcode == ISD::FADD) {
2964 // 0+x --> x
2965 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2966 if (CFP->getValueAPF().isZero())
2967 return N2;
2968 // x+0 --> x
2969 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2970 if (CFP->getValueAPF().isZero())
2971 return N1;
2972 } else if (Opcode == ISD::FSUB) {
2973 // x-0 --> x
2974 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2975 if (CFP->getValueAPF().isZero())
2976 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00002977 } else if (Opcode == ISD::FMUL) {
2978 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2979 SDValue V = N2;
2980
2981 // If the first operand isn't the constant, try the second
2982 if (!CFP) {
2983 CFP = dyn_cast<ConstantFPSDNode>(N2);
2984 V = N1;
2985 }
2986
2987 if (CFP) {
2988 // 0*x --> 0
2989 if (CFP->isZero())
2990 return SDValue(CFP,0);
2991 // 1*x --> x
2992 if (CFP->isExactlyValue(1.0))
2993 return V;
2994 }
Dan Gohman1275e282009-01-23 19:10:37 +00002995 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00002996 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002997 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00002998 assert(N1.getValueType() == N2.getValueType() &&
2999 N1.getValueType() == VT && "Binary operator types must match!");
3000 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003001 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3002 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003003 N1.getValueType().isFloatingPoint() &&
3004 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003005 "Invalid FCOPYSIGN!");
3006 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003007 case ISD::SHL:
3008 case ISD::SRA:
3009 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003010 case ISD::ROTL:
3011 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003012 assert(VT == N1.getValueType() &&
3013 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003014 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003015 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003016 assert((!VT.isVector() || VT == N2.getValueType()) &&
3017 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003018 // Verify that the shift amount VT is bit enough to hold valid shift
3019 // amounts. This catches things like trying to shift an i1024 value by an
3020 // i8, which is easy to fall into in generic code that uses
3021 // TLI.getShiftAmount().
3022 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003023 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003024 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003025
3026 // Always fold shifts of i1 values so the code generator doesn't need to
3027 // handle them. Since we know the size of the shift has to be less than the
3028 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003029 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003030 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003031 if (N2C && N2C->isNullValue())
3032 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003033 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003034 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003035 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003036 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003037 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003038 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003039 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003040 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003041 "type is vector!");
3042 assert((!EVT.isVector() ||
3043 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3044 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003045 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003046 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003047 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003048 break;
3049 }
Chris Lattner72733e52008-01-17 07:00:52 +00003050 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003051 assert(VT.isFloatingPoint() &&
3052 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003053 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003054 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003055 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003056 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003057 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003058 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003059 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003060 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003061 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003062 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003063 assert(!EVT.isVector() &&
3064 "AssertSExt/AssertZExt type should be the vector element type "
3065 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003066 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003067 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003068 break;
3069 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003070 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003071 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003072 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003073 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003074 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003075 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003076 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003077 "type is vector!");
3078 assert((!EVT.isVector() ||
3079 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3080 "Vector element counts must match in SIGN_EXTEND_INREG");
3081 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003082 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003083
Chris Lattner16713612008-01-22 19:09:33 +00003084 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003085 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003086 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003087 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003088 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003089 return getConstant(Val, VT);
3090 }
Chris Lattner16713612008-01-22 19:09:33 +00003091 break;
3092 }
3093 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003094 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3095 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003096 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003097
Chris Lattner16713612008-01-22 19:09:33 +00003098 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3099 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003100 if (N2C &&
3101 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003102 N1.getNumOperands() > 0) {
3103 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003104 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003105 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003106 N1.getOperand(N2C->getZExtValue() / Factor),
3107 getConstant(N2C->getZExtValue() % Factor,
3108 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003109 }
3110
3111 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3112 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003113 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3114 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003115
3116 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003117 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003118 // are promoted and implicitly truncated, and the result implicitly
3119 // extended. Make that explicit here.
3120 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003121
Bob Wilson59dbbb22009-04-13 22:05:19 +00003122 return Elt;
3123 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003124
Chris Lattner16713612008-01-22 19:09:33 +00003125 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3126 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003127 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003128 // If the indices are the same, return the inserted element else
3129 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003130 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003131 SDValue N1Op2 = N1.getOperand(2);
3132 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3133
3134 if (N1Op2C && N2C) {
3135 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3136 if (VT == N1.getOperand(1).getValueType())
3137 return N1.getOperand(1);
3138 else
3139 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3140 }
3141
Dale Johannesendb393622009-02-03 01:55:44 +00003142 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003143 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003144 }
Chris Lattner16713612008-01-22 19:09:33 +00003145 break;
3146 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003147 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003148 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3149 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003150 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003151 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003152
Chris Lattner16713612008-01-22 19:09:33 +00003153 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3154 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003155 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003156 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003157 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003158
Chris Lattner16713612008-01-22 19:09:33 +00003159 // EXTRACT_ELEMENT of a constant int is also very common.
3160 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003161 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003162 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003163 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3164 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003165 }
3166 break;
David Greeneb6f16112011-01-26 15:38:49 +00003167 case ISD::EXTRACT_SUBVECTOR: {
3168 SDValue Index = N2;
3169 if (VT.isSimple() && N1.getValueType().isSimple()) {
3170 assert(VT.isVector() && N1.getValueType().isVector() &&
3171 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003172 assert(VT.getVectorElementType() ==
3173 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003174 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003175 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003176 "Extract subvector must be from larger vector to smaller vector!");
3177
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003178 if (isa<ConstantSDNode>(Index.getNode())) {
3179 assert((VT.getVectorNumElements() +
3180 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003181 <= N1.getValueType().getVectorNumElements())
3182 && "Extract subvector overflow!");
3183 }
3184
3185 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003186 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003187 return N1;
3188 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003189 break;
Chris Lattner16713612008-01-22 19:09:33 +00003190 }
David Greeneb6f16112011-01-26 15:38:49 +00003191 }
Chris Lattner16713612008-01-22 19:09:33 +00003192
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003193 // Perform trivial constant folding.
3194 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3195 if (SV.getNode()) return SV;
3196
3197 // Canonicalize constant to RHS if commutative.
3198 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3199 std::swap(N1C, N2C);
3200 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003201 }
3202
Chris Lattner16713612008-01-22 19:09:33 +00003203 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003204 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3205 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003206 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003207 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003208 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003209 std::swap(N1CFP, N2CFP);
3210 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003211 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003212 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3213 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003214 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003215 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003216 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003217 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003218 return getConstantFP(V1, VT);
3219 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003220 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003221 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3222 if (s!=APFloat::opInvalidOp)
3223 return getConstantFP(V1, VT);
3224 break;
3225 case ISD::FMUL:
3226 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3227 if (s!=APFloat::opInvalidOp)
3228 return getConstantFP(V1, VT);
3229 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003230 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003231 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3232 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3233 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003234 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003235 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003236 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3237 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3238 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003239 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003240 case ISD::FCOPYSIGN:
3241 V1.copySign(V2);
3242 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003243 default: break;
3244 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003245 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003246
3247 if (Opcode == ISD::FP_ROUND) {
3248 APFloat V = N1CFP->getValueAPF(); // make copy
3249 bool ignored;
3250 // This can return overflow, underflow, or inexact; we don't care.
3251 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003252 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003253 APFloat::rmNearestTiesToEven, &ignored);
3254 return getConstantFP(V, VT);
3255 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003256 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003257
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003258 // Canonicalize an UNDEF to the RHS, even over a constant.
3259 if (N1.getOpcode() == ISD::UNDEF) {
3260 if (isCommutativeBinOp(Opcode)) {
3261 std::swap(N1, N2);
3262 } else {
3263 switch (Opcode) {
3264 case ISD::FP_ROUND_INREG:
3265 case ISD::SIGN_EXTEND_INREG:
3266 case ISD::SUB:
3267 case ISD::FSUB:
3268 case ISD::FDIV:
3269 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003270 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003271 return N1; // fold op(undef, arg2) -> undef
3272 case ISD::UDIV:
3273 case ISD::SDIV:
3274 case ISD::UREM:
3275 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003276 case ISD::SRL:
3277 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003278 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003279 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3280 // For vectors, we can't easily build an all zero vector, just return
3281 // the LHS.
3282 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003283 }
3284 }
3285 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003286
3287 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003288 if (N2.getOpcode() == ISD::UNDEF) {
3289 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003290 case ISD::XOR:
3291 if (N1.getOpcode() == ISD::UNDEF)
3292 // Handle undef ^ undef -> 0 special case. This is a common
3293 // idiom (misuse).
3294 return getConstant(0, VT);
3295 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003296 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003297 case ISD::ADDC:
3298 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003299 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003300 case ISD::UDIV:
3301 case ISD::SDIV:
3302 case ISD::UREM:
3303 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003304 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003305 case ISD::FADD:
3306 case ISD::FSUB:
3307 case ISD::FMUL:
3308 case ISD::FDIV:
3309 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003310 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003311 return N2;
3312 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003313 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003314 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003315 case ISD::SRL:
3316 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003317 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003318 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3319 // For vectors, we can't easily build an all zero vector, just return
3320 // the LHS.
3321 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003322 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003323 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003324 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003325 // For vectors, we can't easily build an all one vector, just return
3326 // the LHS.
3327 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003328 case ISD::SRA:
3329 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003330 }
3331 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003332
Chris Lattner724f7ee2005-05-11 18:57:39 +00003333 // Memoize this node if possible.
3334 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003335 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003336 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003337 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003338 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003339 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003340 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003341 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003342 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003343
Jack Carter170a5f22013-09-09 22:02:08 +00003344 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3345 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003346 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003347 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003348 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3349 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003350 }
3351
Chris Lattner061a1ea2005-01-07 07:46:32 +00003352 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003353#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003354 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003355#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003356 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003357}
3358
Andrew Trickef9de2a2013-05-25 02:42:55 +00003359SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003360 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003361 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003362 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003363 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003364 case ISD::FMA: {
3365 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3366 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3367 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3368 if (N1CFP && N2CFP && N3CFP) {
3369 APFloat V1 = N1CFP->getValueAPF();
3370 const APFloat &V2 = N2CFP->getValueAPF();
3371 const APFloat &V3 = N3CFP->getValueAPF();
3372 APFloat::opStatus s =
3373 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3374 if (s != APFloat::opInvalidOp)
3375 return getConstantFP(V1, VT);
3376 }
3377 break;
3378 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003379 case ISD::CONCAT_VECTORS:
3380 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3381 // one big BUILD_VECTOR.
3382 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3383 N2.getOpcode() == ISD::BUILD_VECTOR &&
3384 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003385 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3386 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003387 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3388 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00003389 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00003390 }
3391 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003392 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003393 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003394 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003395 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003396 break;
3397 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003398 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003399 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003400 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003401 return N2; // select true, X, Y -> X
3402 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003403 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003404
3405 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003406 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003407 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003408 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003409 case ISD::INSERT_SUBVECTOR: {
3410 SDValue Index = N3;
3411 if (VT.isSimple() && N1.getValueType().isSimple()
3412 && N2.getValueType().isSimple()) {
3413 assert(VT.isVector() && N1.getValueType().isVector() &&
3414 N2.getValueType().isVector() &&
3415 "Insert subvector VTs must be a vectors");
3416 assert(VT == N1.getValueType() &&
3417 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003418 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003419 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003420 if (isa<ConstantSDNode>(Index.getNode())) {
3421 assert((N2.getValueType().getVectorNumElements() +
3422 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003423 <= VT.getVectorNumElements())
3424 && "Insert subvector overflow!");
3425 }
3426
3427 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003428 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003429 return N2;
3430 }
3431 break;
3432 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003433 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003434 // Fold bit_convert nodes from a type to themselves.
3435 if (N1.getValueType() == VT)
3436 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003437 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003438 }
3439
Chris Lattnerf9c19152005-08-25 19:12:10 +00003440 // Memoize node if it doesn't produce a flag.
3441 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003442 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003443 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003444 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003445 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003446 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003447 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003448 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003449 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003450
Jack Carter170a5f22013-09-09 22:02:08 +00003451 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3452 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003453 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003454 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003455 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3456 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003457 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003458
Chris Lattner061a1ea2005-01-07 07:46:32 +00003459 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003460#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003461 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003462#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003463 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003464}
3465
Andrew Trickef9de2a2013-05-25 02:42:55 +00003466SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003467 SDValue N1, SDValue N2, SDValue N3,
3468 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003469 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003470 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003471}
3472
Andrew Trickef9de2a2013-05-25 02:42:55 +00003473SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003474 SDValue N1, SDValue N2, SDValue N3,
3475 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003476 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003477 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003478}
3479
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003480/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3481/// the incoming stack arguments to be loaded from the stack.
3482SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3483 SmallVector<SDValue, 8> ArgChains;
3484
3485 // Include the original chain at the beginning of the list. When this is
3486 // used by target LowerCall hooks, this helps legalize find the
3487 // CALLSEQ_BEGIN node.
3488 ArgChains.push_back(Chain);
3489
3490 // Add a chain value for each stack argument.
3491 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3492 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3493 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3494 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3495 if (FI->getIndex() < 0)
3496 ArgChains.push_back(SDValue(L, 1));
3497
3498 // Build a tokenfactor for all the chains.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003499 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003500 &ArgChains[0], ArgChains.size());
3501}
3502
Dan Gohman544ab2c2008-04-12 04:36:06 +00003503/// getMemsetValue - Vectorized representation of the memset value
3504/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003505static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003506 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003507 assert(Value.getOpcode() != ISD::UNDEF);
3508
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003509 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003510 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003511 assert(C->getAPIntValue().getBitWidth() == 8);
3512 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003513 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003514 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003515 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003516 }
Evan Chengef377ad2008-05-15 08:39:06 +00003517
Dale Johannesenabf66b82009-02-03 22:26:09 +00003518 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003519 if (NumBits > 8) {
3520 // Use a multiplication with 0x010101... to extend the input to the
3521 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003522 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003523 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003524 }
3525
3526 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003527}
3528
Dan Gohman544ab2c2008-04-12 04:36:06 +00003529/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3530/// used when a memcpy is turned into a memset when the source is a constant
3531/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003532static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003533 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003534 // Handle vector with all elements zero.
3535 if (Str.empty()) {
3536 if (VT.isInteger())
3537 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003538 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003539 return DAG.getConstantFP(0.0, VT);
3540 else if (VT.isVector()) {
3541 unsigned NumElts = VT.getVectorNumElements();
3542 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003543 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003544 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3545 EltVT, NumElts)));
3546 } else
3547 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003548 }
3549
Duncan Sands13237ac2008-06-06 12:08:01 +00003550 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003551 unsigned NumVTBits = VT.getSizeInBits();
3552 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003553 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3554
Evan Chengc8444b12013-01-10 22:13:27 +00003555 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003556 if (TLI.isLittleEndian()) {
3557 for (unsigned i = 0; i != NumBytes; ++i)
3558 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3559 } else {
3560 for (unsigned i = 0; i != NumBytes; ++i)
3561 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003562 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003563
Hans Wennborg4d67a2e2014-01-25 01:18:18 +00003564 // If the "cost" of materializing the integer immediate is 1 or free, then
3565 // it is cost effective to turn the load into the immediate.
Chandler Carruth42e96112013-01-05 12:32:17 +00003566 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
Hans Wennborg4d67a2e2014-01-25 01:18:18 +00003567 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003568 return DAG.getConstant(Val, VT);
3569 return SDValue(0, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003570}
3571
Scott Michelcf0da6c2009-02-17 22:15:04 +00003572/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003573///
Andrew Tricke2431c62013-05-25 03:08:10 +00003574static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003575 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003576 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003577 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003578 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003579}
3580
Evan Chengef377ad2008-05-15 08:39:06 +00003581/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3582///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003583static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003584 unsigned SrcDelta = 0;
3585 GlobalAddressSDNode *G = NULL;
3586 if (Src.getOpcode() == ISD::GlobalAddress)
3587 G = cast<GlobalAddressSDNode>(Src);
3588 else if (Src.getOpcode() == ISD::ADD &&
3589 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3590 Src.getOperand(1).getOpcode() == ISD::Constant) {
3591 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003592 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003593 }
3594 if (!G)
3595 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003596
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003597 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003598}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003599
Evan Cheng43cd9e32010-04-01 06:04:33 +00003600/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3601/// to replace the memset / memcpy. Return true if the number of memory ops
3602/// is below the threshold. It returns the types of the sequence of
3603/// memory ops to perform memset / memcpy by reference.
3604static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003605 unsigned Limit, uint64_t Size,
3606 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003607 bool IsMemset,
3608 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003609 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003610 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003611 SelectionDAG &DAG,
3612 const TargetLowering &TLI) {
3613 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3614 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003615 // If 'SrcAlign' is zero, that means the memory operation does not need to
3616 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3617 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3618 // is the specified alignment of the memory operation. If it is zero, that
3619 // means it's possible to change the alignment of the destination.
3620 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3621 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003622 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003623 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003624 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003625
Chris Lattner28dc6c12010-03-07 07:45:08 +00003626 if (VT == MVT::Other) {
Chandler Carruth5da3f052012-11-01 09:14:31 +00003627 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng43cd9e32010-04-01 06:04:33 +00003628 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003629 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003630 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003631 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003632 case 0: VT = MVT::i64; break;
3633 case 4: VT = MVT::i32; break;
3634 case 2: VT = MVT::i16; break;
3635 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003636 }
3637 }
3638
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003639 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003640 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003641 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003642 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003643
Duncan Sands11dd4242008-06-08 20:54:56 +00003644 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003645 VT = LVT;
3646 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003647
Dan Gohman544ab2c2008-04-12 04:36:06 +00003648 unsigned NumMemOps = 0;
3649 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003650 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003651 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003652 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003653 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003654 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003655
3656 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003657 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003658 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003659 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003660 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003661 Found = true;
3662 else if (NewVT == MVT::i64 &&
3663 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003664 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003665 // i64 is usually not legal on 32-bit targets, but f64 may be.
3666 NewVT = MVT::f64;
3667 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003668 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003669 }
3670
Evan Cheng04e55182012-12-12 00:42:09 +00003671 if (!Found) {
3672 do {
3673 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3674 if (NewVT == MVT::i8)
3675 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003676 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003677 }
3678 NewVTSize = NewVT.getSizeInBits() / 8;
3679
Evan Cheng79e2ca92012-12-10 23:21:26 +00003680 // If the new VT cannot cover all of the remaining bits, then consider
3681 // issuing a (or a pair of) unaligned and overlapping load / store.
3682 // FIXME: Only does this for 64-bit or more since we don't have proper
3683 // cost model for unaligned load / store.
3684 bool Fast;
Evan Chengb7d3d032012-12-12 20:43:23 +00003685 if (NumMemOps && AllowOverlap &&
3686 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00003687 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3688 VTSize = Size;
3689 else {
3690 VT = NewVT;
3691 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003692 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003693 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003694
Evan Chengb7d3d032012-12-12 20:43:23 +00003695 if (++NumMemOps > Limit)
3696 return false;
3697
Dan Gohman544ab2c2008-04-12 04:36:06 +00003698 MemOps.push_back(VT);
3699 Size -= VTSize;
3700 }
3701
3702 return true;
3703}
3704
Andrew Trickef9de2a2013-05-25 02:42:55 +00003705static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003706 SDValue Chain, SDValue Dst,
3707 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003708 unsigned Align, bool isVol,
3709 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003710 MachinePointerInfo DstPtrInfo,
3711 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003712 // Turn a memcpy of undef to nop.
3713 if (Src.getOpcode() == ISD::UNDEF)
3714 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003715
Dan Gohman714663a2008-05-29 19:42:22 +00003716 // Expand memcpy to a series of load and store ops if the size operand falls
3717 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003718 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3719 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003720 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003721 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003722 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003723 MachineFunction &MF = DAG.getMachineFunction();
3724 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003725 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003726 MF.getFunction()->getAttributes().
3727 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003728 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3729 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3730 DstAlignCanChange = true;
3731 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3732 if (Align > SrcAlign)
3733 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003734 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003735 bool CopyFromStr = isMemSrcFromString(Src, Str);
3736 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003737 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003738
Evan Cheng4c014c82010-04-01 18:19:11 +00003739 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003740 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003741 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003742 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003743 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003744
Evan Cheng43cd9e32010-04-01 06:04:33 +00003745 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003746 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003747 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003748
3749 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003750 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003751 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3752 if (!TRI->needsStackRealignment(MF))
3753 while (NewAlign > Align &&
3754 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3755 NewAlign /= 2;
3756
Evan Cheng43cd9e32010-04-01 06:04:33 +00003757 if (NewAlign > Align) {
3758 // Give the stack frame object a larger alignment if needed.
3759 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3760 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3761 Align = NewAlign;
3762 }
3763 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003764
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003765 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003766 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003767 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003768 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003769 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003770 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003771 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003772
Evan Cheng79e2ca92012-12-10 23:21:26 +00003773 if (VTSize > Size) {
3774 // Issuing an unaligned load / store pair that overlaps with the previous
3775 // pair. Adjust the offset accordingly.
3776 assert(i == NumMemOps-1 && i != 0);
3777 SrcOff -= VTSize - Size;
3778 DstOff -= VTSize - Size;
3779 }
3780
Evan Cheng43cd9e32010-04-01 06:04:33 +00003781 if (CopyFromStr &&
3782 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003783 // It's unlikely a store of a vector immediate can be done in a single
3784 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003785 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003786 // FIXME: Handle other cases where store of vector immediate is done in
3787 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003788 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003789 if (Value.getNode())
3790 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003791 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003792 DstPtrInfo.getWithOffset(DstOff), isVol,
3793 false, Align);
3794 }
3795
3796 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003797 // The type might not be legal for the target. This should only happen
3798 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003799 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3800 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003801 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003802 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003803 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003804 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003805 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003806 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003807 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003808 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003809 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003810 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3811 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003812 }
3813 OutChains.push_back(Store);
3814 SrcOff += VTSize;
3815 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003816 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003817 }
3818
Owen Anderson9f944592009-08-11 20:47:22 +00003819 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003820 &OutChains[0], OutChains.size());
3821}
3822
Andrew Trickef9de2a2013-05-25 02:42:55 +00003823static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003824 SDValue Chain, SDValue Dst,
3825 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003826 unsigned Align, bool isVol,
3827 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003828 MachinePointerInfo DstPtrInfo,
3829 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003830 // Turn a memmove of undef to nop.
3831 if (Src.getOpcode() == ISD::UNDEF)
3832 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003833
3834 // Expand memmove to a series of load and store ops if the size operand falls
3835 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003836 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003837 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003838 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003839 MachineFunction &MF = DAG.getMachineFunction();
3840 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003841 bool OptSize = MF.getFunction()->getAttributes().
3842 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003843 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3844 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3845 DstAlignCanChange = true;
3846 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3847 if (Align > SrcAlign)
3848 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003849 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003850
Evan Cheng4c014c82010-04-01 18:19:11 +00003851 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003852 (DstAlignCanChange ? 0 : Align), SrcAlign,
3853 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003854 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003855
Evan Cheng43cd9e32010-04-01 06:04:33 +00003856 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003857 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003858 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003859 if (NewAlign > Align) {
3860 // Give the stack frame object a larger alignment if needed.
3861 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3862 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3863 Align = NewAlign;
3864 }
3865 }
Dan Gohman714663a2008-05-29 19:42:22 +00003866
Evan Cheng43cd9e32010-04-01 06:04:33 +00003867 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003868 SmallVector<SDValue, 8> LoadValues;
3869 SmallVector<SDValue, 8> LoadChains;
3870 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003871 unsigned NumMemOps = MemOps.size();
3872 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003873 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003874 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003875 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00003876
Dale Johannesenabf66b82009-02-03 22:26:09 +00003877 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003878 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003879 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003880 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003881 LoadValues.push_back(Value);
3882 LoadChains.push_back(Value.getValue(1));
3883 SrcOff += VTSize;
3884 }
Owen Anderson9f944592009-08-11 20:47:22 +00003885 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003886 &LoadChains[0], LoadChains.size());
3887 OutChains.clear();
3888 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003889 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003890 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003891 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003892
Dale Johannesenabf66b82009-02-03 22:26:09 +00003893 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003894 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003895 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003896 OutChains.push_back(Store);
3897 DstOff += VTSize;
3898 }
3899
Owen Anderson9f944592009-08-11 20:47:22 +00003900 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003901 &OutChains[0], OutChains.size());
3902}
3903
Serge Pavlov8ec39992013-09-17 16:24:42 +00003904/// \brief Lower the call to 'memset' intrinsic function into a series of store
3905/// operations.
3906///
3907/// \param DAG Selection DAG where lowered code is placed.
3908/// \param dl Link to corresponding IR location.
3909/// \param Chain Control flow dependency.
3910/// \param Dst Pointer to destination memory location.
3911/// \param Src Value of byte to write into the memory.
3912/// \param Size Number of bytes to write.
3913/// \param Align Alignment of the destination in bytes.
3914/// \param isVol True if destination is volatile.
3915/// \param DstPtrInfo IR information on the memory pointer.
3916/// \returns New head in the control flow, if lowering was successful, empty
3917/// SDValue otherwise.
3918///
3919/// The function tries to replace 'llvm.memset' intrinsic with several store
3920/// operations and value calculation code. This is usually profitable for small
3921/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003922static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003923 SDValue Chain, SDValue Dst,
3924 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003925 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003926 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003927 // Turn a memset of undef to nop.
3928 if (Src.getOpcode() == ISD::UNDEF)
3929 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003930
3931 // Expand memset to a series of load/store ops if the size operand
3932 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003933 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003934 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003935 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003936 MachineFunction &MF = DAG.getMachineFunction();
3937 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003938 bool OptSize = MF.getFunction()->getAttributes().
3939 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003940 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3941 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3942 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003943 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003944 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003945 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00003946 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00003947 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003948 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003949
Evan Cheng43cd9e32010-04-01 06:04:33 +00003950 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003951 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003952 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003953 if (NewAlign > Align) {
3954 // Give the stack frame object a larger alignment if needed.
3955 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3956 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3957 Align = NewAlign;
3958 }
3959 }
3960
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003961 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00003962 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003963 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003964
3965 // Find the largest store and generate the bit pattern for it.
3966 EVT LargestVT = MemOps[0];
3967 for (unsigned i = 1; i < NumMemOps; i++)
3968 if (MemOps[i].bitsGT(LargestVT))
3969 LargestVT = MemOps[i];
3970 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3971
Dan Gohman544ab2c2008-04-12 04:36:06 +00003972 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003973 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00003974 unsigned VTSize = VT.getSizeInBits() / 8;
3975 if (VTSize > Size) {
3976 // Issuing an unaligned load / store pair that overlaps with the previous
3977 // pair. Adjust the offset accordingly.
3978 assert(i == NumMemOps-1 && i != 0);
3979 DstOff -= VTSize - Size;
3980 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003981
3982 // If this store is smaller than the largest store see whether we can get
3983 // the smaller value for free with a truncate.
3984 SDValue Value = MemSetValue;
3985 if (VT.bitsLT(LargestVT)) {
3986 if (!LargestVT.isVector() && !VT.isVector() &&
3987 TLI.isTruncateFree(LargestVT, VT))
3988 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3989 else
3990 Value = getMemsetValue(Src, VT, DAG, dl);
3991 }
3992 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00003993 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003994 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003995 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00003996 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003997 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003998 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003999 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004000 }
4001
Owen Anderson9f944592009-08-11 20:47:22 +00004002 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00004003 &OutChains[0], OutChains.size());
4004}
4005
Andrew Trickef9de2a2013-05-25 02:42:55 +00004006SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004007 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004008 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004009 MachinePointerInfo DstPtrInfo,
4010 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004011 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004012
4013 // Check to see if we should lower the memcpy to loads and stores first.
4014 // For cases within the target-specified limits, this is the best choice.
4015 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4016 if (ConstantSize) {
4017 // Memcpy with size zero? Just return the original chain.
4018 if (ConstantSize->isNullValue())
4019 return Chain;
4020
Evan Cheng43cd9e32010-04-01 06:04:33 +00004021 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4022 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004023 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004024 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004025 return Result;
4026 }
4027
4028 // Then check to see if we should lower the memcpy with target-specific
4029 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004030 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004031 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004032 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004033 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004034 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004035 return Result;
4036
4037 // If we really need inline code and the target declined to provide it,
4038 // use a (potentially long) sequence of loads and stores.
4039 if (AlwaysInline) {
4040 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004041 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004042 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004043 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004044 }
4045
Dan Gohmanf38547c2010-04-05 20:24:08 +00004046 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4047 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4048 // respect volatile, so they may do things like read or write memory
4049 // beyond the given memory regions. But fixing this isn't easy, and most
4050 // people don't care.
4051
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004052 const TargetLowering *TLI = TM.getTargetLowering();
4053
Dan Gohman544ab2c2008-04-12 04:36:06 +00004054 // Emit a library call.
4055 TargetLowering::ArgListTy Args;
4056 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004057 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004058 Entry.Node = Dst; Args.push_back(Entry);
4059 Entry.Node = Src; Args.push_back(Entry);
4060 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004061 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004062 TargetLowering::
4063 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004064 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004065 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00004066 /*isTailCall=*/false,
4067 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004068 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4069 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004070 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004071 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004072
Dan Gohman544ab2c2008-04-12 04:36:06 +00004073 return CallResult.second;
4074}
4075
Andrew Trickef9de2a2013-05-25 02:42:55 +00004076SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004077 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004078 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004079 MachinePointerInfo DstPtrInfo,
4080 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004081 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004082
Dan Gohman714663a2008-05-29 19:42:22 +00004083 // Check to see if we should lower the memmove to loads and stores first.
4084 // For cases within the target-specified limits, this is the best choice.
4085 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4086 if (ConstantSize) {
4087 // Memmove with size zero? Just return the original chain.
4088 if (ConstantSize->isNullValue())
4089 return Chain;
4090
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004091 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004092 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004093 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004094 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004095 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004096 return Result;
4097 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004098
4099 // Then check to see if we should lower the memmove with target-specific
4100 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004101 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004102 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004103 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004104 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004105 return Result;
4106
Mon P Wangbf862242010-04-06 08:27:51 +00004107 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4108 // not be safe. See memcpy above for more details.
4109
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004110 const TargetLowering *TLI = TM.getTargetLowering();
4111
Dan Gohman544ab2c2008-04-12 04:36:06 +00004112 // Emit a library call.
4113 TargetLowering::ArgListTy Args;
4114 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004115 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004116 Entry.Node = Dst; Args.push_back(Entry);
4117 Entry.Node = Src; Args.push_back(Entry);
4118 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004119 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004120 TargetLowering::
4121 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004122 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004123 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004124 /*isTailCall=*/false,
4125 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004126 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4127 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004128 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004129 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004130
Dan Gohman544ab2c2008-04-12 04:36:06 +00004131 return CallResult.second;
4132}
4133
Andrew Trickef9de2a2013-05-25 02:42:55 +00004134SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004135 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004136 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004137 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004138 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004139
4140 // Check to see if we should lower the memset to stores first.
4141 // For cases within the target-specified limits, this is the best choice.
4142 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4143 if (ConstantSize) {
4144 // Memset with size zero? Just return the original chain.
4145 if (ConstantSize->isNullValue())
4146 return Chain;
4147
Mon P Wangc576ee92010-04-04 03:10:48 +00004148 SDValue Result =
4149 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004150 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004151
Gabor Greiff304a7a2008-08-28 21:40:38 +00004152 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004153 return Result;
4154 }
4155
4156 // Then check to see if we should lower the memset with target-specific
4157 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004158 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004159 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004160 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004161 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004162 return Result;
4163
Wesley Peck527da1b2010-11-23 03:31:01 +00004164 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004165 const TargetLowering *TLI = TM.getTargetLowering();
4166 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004167 TargetLowering::ArgListTy Args;
4168 TargetLowering::ArgListEntry Entry;
4169 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4170 Args.push_back(Entry);
4171 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004172 if (Src.getValueType().bitsGT(MVT::i32))
4173 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004174 else
Owen Anderson9f944592009-08-11 20:47:22 +00004175 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004176 Entry.Node = Src;
4177 Entry.Ty = Type::getInt32Ty(*getContext());
4178 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004179 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004180 Entry.Node = Size;
4181 Entry.Ty = IntPtrTy;
4182 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004183 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004184 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004185 TargetLowering::
4186 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004187 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004188 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004189 /*isTailCall=*/false,
4190 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004191 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4192 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004193 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004194 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004195
Dan Gohman544ab2c2008-04-12 04:36:06 +00004196 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004197}
4198
Andrew Trickef9de2a2013-05-25 02:42:55 +00004199SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004200 SDVTList VTList, SDValue* Ops, unsigned NumOps,
4201 MachineMemOperand *MMO,
4202 AtomicOrdering Ordering,
4203 SynchronizationScope SynchScope) {
4204 FoldingSetNodeID ID;
4205 ID.AddInteger(MemVT.getRawBits());
4206 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4207 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4208 void* IP = 0;
4209 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4210 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4211 return SDValue(E, 0);
4212 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004213
4214 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4215 // SDNode doesn't have access to it. This memory will be "leaked" when
4216 // the node is deallocated, but recovered when the allocator is released.
4217 // If the number of operands is less than 5 we use AtomicSDNode's internal
4218 // storage.
4219 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : 0;
4220
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004221 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4222 dl.getDebugLoc(), VTList, MemVT,
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004223 Ops, DynOps, NumOps, MMO,
4224 Ordering, SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004225 CSEMap.InsertNode(N, IP);
4226 AllNodes.push_back(N);
4227 return SDValue(N, 0);
4228}
4229
4230SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004231 SDValue Chain, SDValue Ptr, SDValue Cmp,
4232 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004233 unsigned Alignment,
4234 AtomicOrdering Ordering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004235 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004236 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4237 Alignment = getEVTAlignment(MemVT);
4238
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004239 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004240
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004241 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004242 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004243 // FIXME: Volatile isn't really correct; we should keep track of atomic
4244 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004245 unsigned Flags = MachineMemOperand::MOVolatile;
4246 if (Opcode != ISD::ATOMIC_STORE)
4247 Flags |= MachineMemOperand::MOLoad;
4248 if (Opcode != ISD::ATOMIC_LOAD)
4249 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004250
4251 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004252 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004253
Eli Friedmanadec5872011-07-29 03:05:32 +00004254 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4255 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004256}
4257
Andrew Trickef9de2a2013-05-25 02:42:55 +00004258SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004259 SDValue Chain,
4260 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004261 SDValue Swp, MachineMemOperand *MMO,
4262 AtomicOrdering Ordering,
4263 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004264 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4265 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4266
Owen Anderson53aa7a92009-08-10 22:56:29 +00004267 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004268
Owen Anderson9f944592009-08-11 20:47:22 +00004269 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004270 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004271 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004272}
4273
Andrew Trickef9de2a2013-05-25 02:42:55 +00004274SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004275 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004276 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004277 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004278 unsigned Alignment,
4279 AtomicOrdering Ordering,
4280 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004281 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4282 Alignment = getEVTAlignment(MemVT);
4283
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004284 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004285 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004286 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004287 // For now, atomics are considered to be volatile always, and they are
4288 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004289 // FIXME: Volatile isn't really correct; we should keep track of atomic
4290 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004291 unsigned Flags = MachineMemOperand::MOVolatile;
4292 if (Opcode != ISD::ATOMIC_STORE)
4293 Flags |= MachineMemOperand::MOLoad;
4294 if (Opcode != ISD::ATOMIC_LOAD)
4295 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004296
4297 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004298 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004299 MemVT.getStoreSize(), Alignment);
4300
Eli Friedmanadec5872011-07-29 03:05:32 +00004301 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4302 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004303}
4304
Andrew Trickef9de2a2013-05-25 02:42:55 +00004305SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004306 SDValue Chain,
4307 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004308 MachineMemOperand *MMO,
4309 AtomicOrdering Ordering,
4310 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004311 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4312 Opcode == ISD::ATOMIC_LOAD_SUB ||
4313 Opcode == ISD::ATOMIC_LOAD_AND ||
4314 Opcode == ISD::ATOMIC_LOAD_OR ||
4315 Opcode == ISD::ATOMIC_LOAD_XOR ||
4316 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004317 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004318 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004319 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004320 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004321 Opcode == ISD::ATOMIC_SWAP ||
4322 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004323 "Invalid Atomic Op");
4324
Owen Anderson53aa7a92009-08-10 22:56:29 +00004325 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004326
Eli Friedman342e8df2011-08-24 20:50:09 +00004327 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4328 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004329 SDValue Ops[] = {Chain, Ptr, Val};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004330 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004331}
4332
Andrew Trickef9de2a2013-05-25 02:42:55 +00004333SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004334 EVT VT, SDValue Chain,
4335 SDValue Ptr,
4336 const Value* PtrVal,
4337 unsigned Alignment,
4338 AtomicOrdering Ordering,
4339 SynchronizationScope SynchScope) {
4340 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4341 Alignment = getEVTAlignment(MemVT);
4342
4343 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004344 // An atomic store does not load. An atomic load does not store.
4345 // (An atomicrmw obviously both loads and stores.)
4346 // For now, atomics are considered to be volatile always, and they are
4347 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004348 // FIXME: Volatile isn't really correct; we should keep track of atomic
4349 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004350 unsigned Flags = MachineMemOperand::MOVolatile;
4351 if (Opcode != ISD::ATOMIC_STORE)
4352 Flags |= MachineMemOperand::MOLoad;
4353 if (Opcode != ISD::ATOMIC_LOAD)
4354 Flags |= MachineMemOperand::MOStore;
Eli Friedman342e8df2011-08-24 20:50:09 +00004355
4356 MachineMemOperand *MMO =
4357 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4358 MemVT.getStoreSize(), Alignment);
4359
4360 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4361 Ordering, SynchScope);
4362}
4363
Andrew Trickef9de2a2013-05-25 02:42:55 +00004364SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004365 EVT VT, SDValue Chain,
4366 SDValue Ptr,
4367 MachineMemOperand *MMO,
4368 AtomicOrdering Ordering,
4369 SynchronizationScope SynchScope) {
4370 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4371
4372 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004373 SDValue Ops[] = {Chain, Ptr};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004374 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004375}
4376
Duncan Sands739a0542008-07-02 17:40:58 +00004377/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesenae7992a2009-02-02 20:47:48 +00004378SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004379 SDLoc dl) {
Dale Johannesenae7992a2009-02-02 20:47:48 +00004380 if (NumOps == 1)
4381 return Ops[0];
4382
Owen Anderson53aa7a92009-08-10 22:56:29 +00004383 SmallVector<EVT, 4> VTs;
Dale Johannesenae7992a2009-02-02 20:47:48 +00004384 VTs.reserve(NumOps);
4385 for (unsigned i = 0; i < NumOps; ++i)
4386 VTs.push_back(Ops[i].getValueType());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004387 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesenae7992a2009-02-02 20:47:48 +00004388 Ops, NumOps);
4389}
4390
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004391SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004392SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004393 const EVT *VTs, unsigned NumVTs,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004394 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004395 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004396 unsigned Align, bool Vol,
4397 bool ReadMem, bool WriteMem) {
4398 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004399 MemVT, PtrInfo, Align, Vol,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004400 ReadMem, WriteMem);
4401}
4402
4403SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004404SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004405 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004406 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004407 unsigned Align, bool Vol,
4408 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004409 if (Align == 0) // Ensure that codegen never sees alignment 0
4410 Align = getEVTAlignment(MemVT);
4411
4412 MachineFunction &MF = getMachineFunction();
4413 unsigned Flags = 0;
4414 if (WriteMem)
4415 Flags |= MachineMemOperand::MOStore;
4416 if (ReadMem)
4417 Flags |= MachineMemOperand::MOLoad;
4418 if (Vol)
4419 Flags |= MachineMemOperand::MOVolatile;
4420 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004421 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004422
4423 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4424}
4425
4426SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004427SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohman48b185d2009-09-25 20:36:54 +00004428 const SDValue *Ops, unsigned NumOps,
4429 EVT MemVT, MachineMemOperand *MMO) {
4430 assert((Opcode == ISD::INTRINSIC_VOID ||
4431 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004432 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004433 Opcode == ISD::LIFETIME_START ||
4434 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004435 (Opcode <= INT_MAX &&
4436 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4437 "Opcode is not a memory-accessing opcode!");
4438
Dale Johannesen839acbb2009-01-29 00:47:48 +00004439 // Memoize the node unless it returns a flag.
4440 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004441 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004442 FoldingSetNodeID ID;
4443 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper91244262012-07-30 20:23:19 +00004444 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004445 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004446 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4447 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004448 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004449 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004450
Jack Carter170a5f22013-09-09 22:02:08 +00004451 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4452 dl.getDebugLoc(), VTList, Ops,
4453 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004454 CSEMap.InsertNode(N, IP);
4455 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004456 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4457 dl.getDebugLoc(), VTList, Ops,
4458 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004459 }
4460 AllNodes.push_back(N);
4461 return SDValue(N, 0);
4462}
4463
Chris Lattnerea952f02010-09-21 17:24:05 +00004464/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4465/// MachinePointerInfo record from it. This is particularly useful because the
4466/// code generator has many cases where it doesn't bother passing in a
4467/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4468static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4469 // If this is FI+Offset, we can model it.
4470 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4471 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4472
4473 // If this is (FI+Offset1)+Offset2, we can model it.
4474 if (Ptr.getOpcode() != ISD::ADD ||
4475 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4476 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4477 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004478
Chris Lattnerea952f02010-09-21 17:24:05 +00004479 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4480 return MachinePointerInfo::getFixedStack(FI, Offset+
4481 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4482}
4483
4484/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4485/// MachinePointerInfo record from it. This is particularly useful because the
4486/// code generator has many cases where it doesn't bother passing in a
4487/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4488static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4489 // If the 'Offset' value isn't a constant, we can't handle this.
4490 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4491 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4492 if (OffsetOp.getOpcode() == ISD::UNDEF)
4493 return InferPointerInfo(Ptr);
4494 return MachinePointerInfo();
4495}
Wesley Peck527da1b2010-11-23 03:31:01 +00004496
Chris Lattnerea952f02010-09-21 17:24:05 +00004497
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004498SDValue
4499SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004500 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004501 SDValue Ptr, SDValue Offset,
4502 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004503 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004504 unsigned Alignment, const MDNode *TBAAInfo,
4505 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004506 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004507 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004508 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4509 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004510
Dan Gohman48b185d2009-09-25 20:36:54 +00004511 unsigned Flags = MachineMemOperand::MOLoad;
4512 if (isVolatile)
4513 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004514 if (isNonTemporal)
4515 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004516 if (isInvariant)
4517 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004518
Chris Lattnerea952f02010-09-21 17:24:05 +00004519 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4520 // clients.
4521 if (PtrInfo.V == 0)
4522 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004523
Chris Lattnerea952f02010-09-21 17:24:05 +00004524 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004525 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004526 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004527 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004528 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004529}
4530
4531SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004532SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004533 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004534 SDValue Ptr, SDValue Offset, EVT MemVT,
4535 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004536 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004537 ExtType = ISD::NON_EXTLOAD;
4538 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004539 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004540 } else {
4541 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004542 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4543 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004544 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004545 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004546 assert(VT.isVector() == MemVT.isVector() &&
4547 "Cannot use trunc store to convert to or from a vector!");
4548 assert((!VT.isVector() ||
4549 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4550 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004551 }
4552
4553 bool Indexed = AM != ISD::UNINDEXED;
4554 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4555 "Unindexed load with an offset!");
4556
4557 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004558 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004559 SDValue Ops[] = { Chain, Ptr, Offset };
4560 FoldingSetNodeID ID;
4561 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman08c0a952009-09-23 21:02:20 +00004562 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004563 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004564 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004565 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004566 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004567 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004568 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4569 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004570 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004571 }
Jack Carter170a5f22013-09-09 22:02:08 +00004572 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4573 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004574 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004575 CSEMap.InsertNode(N, IP);
4576 AllNodes.push_back(N);
4577 return SDValue(N, 0);
4578}
4579
Andrew Trickef9de2a2013-05-25 02:42:55 +00004580SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004581 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004582 MachinePointerInfo PtrInfo,
4583 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004584 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004585 const MDNode *TBAAInfo,
4586 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004587 SDValue Undef = getUNDEF(Ptr.getValueType());
4588 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004589 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4590 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004591}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004592
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004593SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4594 SDValue Chain, SDValue Ptr,
4595 MachineMemOperand *MMO) {
4596 SDValue Undef = getUNDEF(Ptr.getValueType());
4597 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4598 VT, MMO);
4599}
4600
Andrew Trickef9de2a2013-05-25 02:42:55 +00004601SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004602 SDValue Chain, SDValue Ptr,
4603 MachinePointerInfo PtrInfo, EVT MemVT,
4604 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004605 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004606 SDValue Undef = getUNDEF(Ptr.getValueType());
4607 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004608 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004609 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004610}
4611
4612
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004613SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4614 SDValue Chain, SDValue Ptr, EVT MemVT,
4615 MachineMemOperand *MMO) {
4616 SDValue Undef = getUNDEF(Ptr.getValueType());
4617 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4618 MemVT, MMO);
4619}
4620
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004621SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004622SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004623 SDValue Offset, ISD::MemIndexedMode AM) {
4624 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4625 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4626 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004627 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004628 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004629 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004630 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004631}
4632
Andrew Trickef9de2a2013-05-25 02:42:55 +00004633SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004634 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004635 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004636 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004637 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004638 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004639 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004640 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004641
Dan Gohman48b185d2009-09-25 20:36:54 +00004642 unsigned Flags = MachineMemOperand::MOStore;
4643 if (isVolatile)
4644 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004645 if (isNonTemporal)
4646 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004647
Chris Lattnerea952f02010-09-21 17:24:05 +00004648 if (PtrInfo.V == 0)
4649 PtrInfo = InferPointerInfo(Ptr);
4650
4651 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004652 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004653 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004654 Val.getValueType().getStoreSize(), Alignment,
4655 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004656
4657 return getStore(Chain, dl, Val, Ptr, MMO);
4658}
4659
Andrew Trickef9de2a2013-05-25 02:42:55 +00004660SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004661 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004662 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004663 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004664 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004665 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004666 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004667 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4668 FoldingSetNodeID ID;
4669 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004670 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004671 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004672 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004673 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004674 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004675 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4676 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004677 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004678 }
Jack Carter170a5f22013-09-09 22:02:08 +00004679 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4680 dl.getDebugLoc(), VTs,
4681 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004682 CSEMap.InsertNode(N, IP);
4683 AllNodes.push_back(N);
4684 return SDValue(N, 0);
4685}
4686
Andrew Trickef9de2a2013-05-25 02:42:55 +00004687SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004688 SDValue Ptr, MachinePointerInfo PtrInfo,
4689 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004690 unsigned Alignment,
4691 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004692 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004693 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004694 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4695 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004696
Dan Gohman48b185d2009-09-25 20:36:54 +00004697 unsigned Flags = MachineMemOperand::MOStore;
4698 if (isVolatile)
4699 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004700 if (isNonTemporal)
4701 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004702
Chris Lattnerea952f02010-09-21 17:24:05 +00004703 if (PtrInfo.V == 0)
4704 PtrInfo = InferPointerInfo(Ptr);
4705
4706 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004707 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004708 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4709 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004710
4711 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4712}
4713
Andrew Trickef9de2a2013-05-25 02:42:55 +00004714SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004715 SDValue Ptr, EVT SVT,
4716 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004717 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004718
Michael Ilsemand5f91512012-09-10 16:56:31 +00004719 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004720 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004721 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004722 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004723
Dan Gohmancecad352009-12-14 23:40:38 +00004724 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4725 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004726 assert(VT.isInteger() == SVT.isInteger() &&
4727 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004728 assert(VT.isVector() == SVT.isVector() &&
4729 "Cannot use trunc store to convert to or from a vector!");
4730 assert((!VT.isVector() ||
4731 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4732 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004733
Owen Anderson9f944592009-08-11 20:47:22 +00004734 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004735 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004736 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4737 FoldingSetNodeID ID;
4738 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004739 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004740 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004741 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004742 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004743 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004744 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4745 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004746 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004747 }
Jack Carter170a5f22013-09-09 22:02:08 +00004748 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4749 dl.getDebugLoc(), VTs,
4750 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004751 CSEMap.InsertNode(N, IP);
4752 AllNodes.push_back(N);
4753 return SDValue(N, 0);
4754}
4755
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004756SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004757SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004758 SDValue Offset, ISD::MemIndexedMode AM) {
4759 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4760 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4761 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004762 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004763 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4764 FoldingSetNodeID ID;
4765 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004766 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004767 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004768 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004769 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004770 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004771 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004772
Jack Carter170a5f22013-09-09 22:02:08 +00004773 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4774 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004775 ST->isTruncatingStore(),
4776 ST->getMemoryVT(),
4777 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004778 CSEMap.InsertNode(N, IP);
4779 AllNodes.push_back(N);
4780 return SDValue(N, 0);
4781}
4782
Andrew Trickef9de2a2013-05-25 02:42:55 +00004783SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004784 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004785 SDValue SV,
4786 unsigned Align) {
4787 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4788 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004789}
4790
Andrew Trickef9de2a2013-05-25 02:42:55 +00004791SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004792 const SDUse *Ops, unsigned NumOps) {
Dan Gohman768f2c92008-07-07 18:26:29 +00004793 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004794 case 0: return getNode(Opcode, DL, VT);
4795 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4796 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4797 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004798 default: break;
4799 }
4800
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004801 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004802 // the regular getNode logic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004803 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004804 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004805}
4806
Andrew Trickef9de2a2013-05-25 02:42:55 +00004807SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004808 const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00004809 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004810 case 0: return getNode(Opcode, DL, VT);
4811 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4812 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4813 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004814 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004815 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004816
Chris Lattnerb0713c72005-04-09 03:27:28 +00004817 switch (Opcode) {
4818 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004819 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004820 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004821 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4822 "LHS and RHS of condition must have same type!");
4823 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4824 "True and False arms of SelectCC must have same type!");
4825 assert(Ops[2].getValueType() == VT &&
4826 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004827 break;
4828 }
4829 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004830 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004831 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4832 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004833 break;
4834 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004835 }
4836
Chris Lattner566307f2005-05-14 07:42:29 +00004837 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004838 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004839 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004840
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004841 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004842 FoldingSetNodeID ID;
4843 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004844 void *IP = 0;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004845
Bill Wendling022d18f2009-12-18 23:32:53 +00004846 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004847 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004848
Jack Carter170a5f22013-09-09 22:02:08 +00004849 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4850 VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004851 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004852 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004853 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4854 VTs, Ops, NumOps);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004855 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004856
Chris Lattnerb0713c72005-04-09 03:27:28 +00004857 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004858#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004859 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004860#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004861 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004862}
4863
Andrew Trickef9de2a2013-05-25 02:42:55 +00004864SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00004865 ArrayRef<EVT> ResultTys,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004866 const SDValue *Ops, unsigned NumOps) {
Dan Gohmande912e22009-04-09 23:54:40 +00004867 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner3bf4be42006-08-14 23:31:51 +00004868 Ops, NumOps);
4869}
4870
Andrew Trickef9de2a2013-05-25 02:42:55 +00004871SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004872 const EVT *VTs, unsigned NumVTs,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004873 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3bf4be42006-08-14 23:31:51 +00004874 if (NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004875 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4876 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004877}
4878
Andrew Trickef9de2a2013-05-25 02:42:55 +00004879SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004880 const SDValue *Ops, unsigned NumOps) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004881 if (VTList.NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004882 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattnerd5531332005-05-14 06:20:26 +00004883
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004884#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004885 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004886 // FIXME: figure out how to safely handle things like
4887 // int foo(int x) { return 1 << (x & 255); }
4888 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004889 case ISD::SRA_PARTS:
4890 case ISD::SRL_PARTS:
4891 case ISD::SHL_PARTS:
4892 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004893 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004894 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004895 else if (N3.getOpcode() == ISD::AND)
4896 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4897 // If the and is only masking out bits that cannot effect the shift,
4898 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004899 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004900 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004901 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004902 }
4903 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004904 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004905#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004906
Chris Lattnerf9c19152005-08-25 19:12:10 +00004907 // Memoize the node unless it returns a flag.
4908 SDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004909 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004910 FoldingSetNodeID ID;
4911 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004912 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004913 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004914 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004915
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004916 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004917 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4918 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004919 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004920 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4921 DL.getDebugLoc(), VTList, Ops[0],
4922 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004923 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004924 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4925 DL.getDebugLoc(), VTList, Ops[0],
4926 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004927 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004928 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4929 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004930 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004931 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004932 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004933 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004934 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4935 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004936 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004937 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4938 DL.getDebugLoc(), VTList, Ops[0],
4939 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004940 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004941 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4942 DL.getDebugLoc(), VTList, Ops[0],
4943 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004944 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004945 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4946 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004947 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004948 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004949 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004950#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004951 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004952#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004953 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004954}
4955
Andrew Trickef9de2a2013-05-25 02:42:55 +00004956SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004957 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004958}
4959
Andrew Trickef9de2a2013-05-25 02:42:55 +00004960SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004961 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004962 SDValue Ops[] = { N1 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004963 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004964}
4965
Andrew Trickef9de2a2013-05-25 02:42:55 +00004966SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004967 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004968 SDValue Ops[] = { N1, N2 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004969 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004970}
4971
Andrew Trickef9de2a2013-05-25 02:42:55 +00004972SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004973 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004974 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004975 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004976}
4977
Andrew Trickef9de2a2013-05-25 02:42:55 +00004978SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004979 SDValue N1, SDValue N2, SDValue N3,
4980 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004981 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004982 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004983}
4984
Andrew Trickef9de2a2013-05-25 02:42:55 +00004985SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004986 SDValue N1, SDValue N2, SDValue N3,
4987 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004988 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004989 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004990}
4991
Owen Anderson53aa7a92009-08-10 22:56:29 +00004992SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00004993 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00004994}
4995
Owen Anderson53aa7a92009-08-10 22:56:29 +00004996SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00004997 FoldingSetNodeID ID;
4998 ID.AddInteger(2U);
4999 ID.AddInteger(VT1.getRawBits());
5000 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005001
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005002 void *IP = 0;
5003 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5004 if (Result == NULL) {
5005 EVT *Array = Allocator.Allocate<EVT>(2);
5006 Array[0] = VT1;
5007 Array[1] = VT2;
5008 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5009 VTListMap.InsertNode(Result, IP);
5010 }
5011 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005012}
Dan Gohman17059682008-07-17 19:10:17 +00005013
Owen Anderson53aa7a92009-08-10 22:56:29 +00005014SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005015 FoldingSetNodeID ID;
5016 ID.AddInteger(3U);
5017 ID.AddInteger(VT1.getRawBits());
5018 ID.AddInteger(VT2.getRawBits());
5019 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005020
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005021 void *IP = 0;
5022 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5023 if (Result == NULL) {
5024 EVT *Array = Allocator.Allocate<EVT>(3);
5025 Array[0] = VT1;
5026 Array[1] = VT2;
5027 Array[2] = VT3;
5028 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5029 VTListMap.InsertNode(Result, IP);
5030 }
5031 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005032}
5033
Owen Anderson53aa7a92009-08-10 22:56:29 +00005034SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005035 FoldingSetNodeID ID;
5036 ID.AddInteger(4U);
5037 ID.AddInteger(VT1.getRawBits());
5038 ID.AddInteger(VT2.getRawBits());
5039 ID.AddInteger(VT3.getRawBits());
5040 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005041
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005042 void *IP = 0;
5043 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5044 if (Result == NULL) {
5045 EVT *Array = Allocator.Allocate<EVT>(4);
5046 Array[0] = VT1;
5047 Array[1] = VT2;
5048 Array[2] = VT3;
5049 Array[3] = VT4;
5050 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5051 VTListMap.InsertNode(Result, IP);
5052 }
5053 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005054}
5055
Owen Anderson53aa7a92009-08-10 22:56:29 +00005056SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005057 FoldingSetNodeID ID;
5058 ID.AddInteger(NumVTs);
5059 for (unsigned index = 0; index < NumVTs; index++) {
5060 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005061 }
5062
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005063 void *IP = 0;
5064 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5065 if (Result == NULL) {
5066 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5067 std::copy(VTs, VTs + NumVTs, Array);
5068 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5069 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005070 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005071 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005072}
5073
5074
Chris Lattnerf34156e2006-01-28 09:32:45 +00005075/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5076/// specified operands. If the resultant node already exists in the DAG,
5077/// this does not modify the specified node, instead it returns the node that
5078/// already exists. If the resultant node does not exist in the DAG, the
5079/// input node is returned. As a degenerate case, if you specify the same
5080/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005081SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005082 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005083
Chris Lattnerf34156e2006-01-28 09:32:45 +00005084 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005085 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005086
Chris Lattnerf34156e2006-01-28 09:32:45 +00005087 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005088 void *InsertPos = 0;
5089 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005090 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005091
Dan Gohmanebeccb42008-07-21 22:38:59 +00005092 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005093 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005094 if (!RemoveNodeFromCSEMaps(N))
5095 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005096
Chris Lattnerf34156e2006-01-28 09:32:45 +00005097 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005098 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005099
Chris Lattnerf34156e2006-01-28 09:32:45 +00005100 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005101 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005102 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005103}
5104
Dan Gohman92c11ac2010-06-18 15:30:29 +00005105SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005106 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005107
Chris Lattnerf34156e2006-01-28 09:32:45 +00005108 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005109 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005110 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005111
Chris Lattnerf34156e2006-01-28 09:32:45 +00005112 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005113 void *InsertPos = 0;
5114 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005115 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005116
Dan Gohmanebeccb42008-07-21 22:38:59 +00005117 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005118 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005119 if (!RemoveNodeFromCSEMaps(N))
5120 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005121
Chris Lattnerf34156e2006-01-28 09:32:45 +00005122 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005123 if (N->OperandList[0] != Op1)
5124 N->OperandList[0].set(Op1);
5125 if (N->OperandList[1] != Op2)
5126 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005127
Chris Lattnerf34156e2006-01-28 09:32:45 +00005128 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005129 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005130 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005131}
5132
Dan Gohman92c11ac2010-06-18 15:30:29 +00005133SDNode *SelectionDAG::
5134UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005135 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005136 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005137}
5138
Dan Gohman92c11ac2010-06-18 15:30:29 +00005139SDNode *SelectionDAG::
5140UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005141 SDValue Op3, SDValue Op4) {
5142 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005143 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005144}
5145
Dan Gohman92c11ac2010-06-18 15:30:29 +00005146SDNode *SelectionDAG::
5147UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005148 SDValue Op3, SDValue Op4, SDValue Op5) {
5149 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005150 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner580b12a2006-01-28 10:09:25 +00005151}
5152
Dan Gohman92c11ac2010-06-18 15:30:29 +00005153SDNode *SelectionDAG::
5154UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00005155 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005156 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005157
Chris Lattnerf34156e2006-01-28 09:32:45 +00005158 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005159 bool AnyChange = false;
5160 for (unsigned i = 0; i != NumOps; ++i) {
5161 if (Ops[i] != N->getOperand(i)) {
5162 AnyChange = true;
5163 break;
5164 }
5165 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005166
Chris Lattnerf34156e2006-01-28 09:32:45 +00005167 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005168 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005169
Chris Lattnerf34156e2006-01-28 09:32:45 +00005170 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005171 void *InsertPos = 0;
Chris Lattner97af9d52006-08-08 01:09:31 +00005172 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005173 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005174
Dan Gohman2f83b472008-05-02 00:05:03 +00005175 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005176 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005177 if (!RemoveNodeFromCSEMaps(N))
5178 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005179
Chris Lattnerf34156e2006-01-28 09:32:45 +00005180 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005181 for (unsigned i = 0; i != NumOps; ++i)
5182 if (N->OperandList[i] != Ops[i])
5183 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005184
5185 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005186 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005187 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005188}
5189
Dan Gohman91697632008-07-07 20:57:48 +00005190/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005191/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005192void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005193 // Unlike the code in MorphNodeTo that does this, we don't need to
5194 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005195 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5196 SDUse &Use = *I++;
5197 Use.set(SDValue());
5198 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005199}
Chris Lattner19732782005-08-16 18:17:10 +00005200
Dan Gohman17059682008-07-17 19:10:17 +00005201/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5202/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005203///
Dan Gohman17059682008-07-17 19:10:17 +00005204SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005205 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005206 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005207 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005208}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005209
Dan Gohman17059682008-07-17 19:10:17 +00005210SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005211 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005212 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005213 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005214 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner19732782005-08-16 18:17:10 +00005215}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005216
Dan Gohman17059682008-07-17 19:10:17 +00005217SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005218 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005219 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005220 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005221 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005222 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner19732782005-08-16 18:17:10 +00005223}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005224
Dan Gohman17059682008-07-17 19:10:17 +00005225SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005226 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005227 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005228 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005229 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005230 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner19732782005-08-16 18:17:10 +00005231}
Chris Lattner466fece2005-08-21 22:30:30 +00005232
Dan Gohman17059682008-07-17 19:10:17 +00005233SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005234 EVT VT, const SDValue *Ops,
Evan Cheng849f4bf2006-08-27 08:08:54 +00005235 unsigned NumOps) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005236 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005237 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005238}
5239
Dan Gohman17059682008-07-17 19:10:17 +00005240SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005241 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005242 unsigned NumOps) {
5243 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman17059682008-07-17 19:10:17 +00005244 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005245}
5246
Dan Gohman17059682008-07-17 19:10:17 +00005247SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005248 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005249 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005250 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman22e97072008-07-02 23:23:19 +00005251}
5252
Dan Gohman17059682008-07-17 19:10:17 +00005253SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005254 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005255 const SDValue *Ops, unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005256 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohman17059682008-07-17 19:10:17 +00005257 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005258}
5259
Bill Wendling2d598632008-12-01 23:28:22 +00005260SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005261 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling2d598632008-12-01 23:28:22 +00005262 const SDValue *Ops, unsigned NumOps) {
5263 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5264 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5265}
5266
Scott Michelcf0da6c2009-02-17 22:15:04 +00005267SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005268 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005269 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005270 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005271 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005272 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth683352382006-01-23 21:51:14 +00005273}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005274
Scott Michelcf0da6c2009-02-17 22:15:04 +00005275SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005276 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005277 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005278 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005279 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005280 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005281}
5282
Dan Gohman17059682008-07-17 19:10:17 +00005283SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005284 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005285 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005286 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005287 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005288 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005289 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman22e97072008-07-02 23:23:19 +00005290}
5291
Dan Gohman17059682008-07-17 19:10:17 +00005292SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005293 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005294 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005295 SDValue Op3) {
5296 SDVTList VTs = getVTList(VT1, VT2, VT3);
5297 SDValue Ops[] = { Op1, Op2, Op3 };
5298 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5299}
5300
5301SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005302 SDVTList VTs, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005303 unsigned NumOps) {
Chris Lattner625916d2010-02-23 23:01:35 +00005304 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5305 // Reset the NodeID to -1.
5306 N->setNodeId(-1);
5307 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005308}
5309
Andrew Trickef9de2a2013-05-25 02:42:55 +00005310/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005311/// the line number information on the merged node since it is not possible to
5312/// preserve the information that operation is associated with multiple lines.
5313/// This will make the debugger working better at -O0, were there is a higher
5314/// probability having other instructions associated with that line.
5315///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005316/// For IROrder, we keep the smaller of the two
5317SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005318 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005319 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5320 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005321 N->setDebugLoc(DebugLoc());
5322 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005323 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5324 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005325 return N;
5326}
5327
Chris Lattneraf197502010-02-28 21:36:14 +00005328/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005329/// return type, opcode, and operands.
5330///
5331/// Note that MorphNodeTo returns the resultant node. If there is already a
5332/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005333/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005334///
5335/// Using MorphNodeTo is faster than creating a new node and swapping it in
5336/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005337/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005338/// the node's users.
5339///
5340SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005341 SDVTList VTs, const SDValue *Ops,
Dan Gohman17059682008-07-17 19:10:17 +00005342 unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005343 // If an identical node already exists, use it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005344 void *IP = 0;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005345 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005346 FoldingSetNodeID ID;
5347 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendling022d18f2009-12-18 23:32:53 +00005348 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005349 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005350 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005351
Dan Gohmand3fe1742008-09-13 01:54:27 +00005352 if (!RemoveNodeFromCSEMaps(N))
5353 IP = 0;
Chris Lattner486edfb2007-02-04 02:32:44 +00005354
Dan Gohman17059682008-07-17 19:10:17 +00005355 // Start the morphing.
5356 N->NodeType = Opc;
5357 N->ValueList = VTs.VTs;
5358 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005359
Dan Gohman17059682008-07-17 19:10:17 +00005360 // Clear the operands list, updating used nodes to remove this from their
5361 // use list. Keep track of any operands that become dead as a result.
5362 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005363 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5364 SDUse &Use = *I++;
5365 SDNode *Used = Use.getNode();
5366 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005367 if (Used->use_empty())
5368 DeadNodeSet.insert(Used);
5369 }
5370
Dan Gohman48b185d2009-09-25 20:36:54 +00005371 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5372 // Initialize the memory references information.
5373 MN->setMemRefs(0, 0);
5374 // If NumOps is larger than the # of operands we can have in a
5375 // MachineSDNode, reallocate the operand list.
5376 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5377 if (MN->OperandsNeedDelete)
5378 delete[] MN->OperandList;
5379 if (NumOps > array_lengthof(MN->LocalOperands))
5380 // We're creating a final node that will live unmorphed for the
5381 // remainder of the current SelectionDAG iteration, so we can allocate
5382 // the operands directly out of a pool with no recycling metadata.
5383 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman01c65a22010-03-18 18:49:47 +00005384 Ops, NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005385 else
5386 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5387 MN->OperandsNeedDelete = false;
5388 } else
5389 MN->InitOperands(MN->OperandList, Ops, NumOps);
5390 } else {
5391 // If NumOps is larger than the # of operands we currently have, reallocate
5392 // the operand list.
5393 if (NumOps > N->NumOperands) {
5394 if (N->OperandsNeedDelete)
5395 delete[] N->OperandList;
5396 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005397 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005398 } else
Anton Korobeynikov86263672009-10-22 00:15:17 +00005399 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005400 }
5401
5402 // Delete any nodes that are still dead after adding the uses for the
5403 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005404 if (!DeadNodeSet.empty()) {
5405 SmallVector<SDNode *, 16> DeadNodes;
5406 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5407 E = DeadNodeSet.end(); I != E; ++I)
5408 if ((*I)->use_empty())
5409 DeadNodes.push_back(*I);
5410 RemoveDeadNodes(DeadNodes);
5411 }
Dan Gohman91697632008-07-07 20:57:48 +00005412
Dan Gohman17059682008-07-17 19:10:17 +00005413 if (IP)
5414 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005415 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005416}
5417
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005418
Dan Gohman32f71d72009-09-25 18:54:59 +00005419/// getMachineNode - These are used for target selectors to create a new node
5420/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005421///
Dan Gohman32f71d72009-09-25 18:54:59 +00005422/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005423/// node of the specified opcode and operands, it returns that node instead of
5424/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005425MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005426SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005427 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005428 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005429}
Bill Wendlinga434d932009-01-29 09:01:55 +00005430
Dan Gohmana22f2d82009-10-10 01:29:16 +00005431MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005432SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005433 SDVTList VTs = getVTList(VT);
5434 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005435 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005436}
Bill Wendlinga434d932009-01-29 09:01:55 +00005437
Dan Gohmana22f2d82009-10-10 01:29:16 +00005438MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005439SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005440 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005441 SDVTList VTs = getVTList(VT);
5442 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005443 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005444}
Bill Wendlinga434d932009-01-29 09:01:55 +00005445
Dan Gohmana22f2d82009-10-10 01:29:16 +00005446MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005447SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005448 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005449 SDVTList VTs = getVTList(VT);
5450 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005451 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005452}
Bill Wendlinga434d932009-01-29 09:01:55 +00005453
Dan Gohmana22f2d82009-10-10 01:29:16 +00005454MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005455SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005456 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005457 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005458 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005459}
Bill Wendlinga434d932009-01-29 09:01:55 +00005460
Dan Gohmana22f2d82009-10-10 01:29:16 +00005461MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005462SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005463 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005464 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005465}
Bill Wendlinga434d932009-01-29 09:01:55 +00005466
Dan Gohmana22f2d82009-10-10 01:29:16 +00005467MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005468SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005469 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005470 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005471 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005472 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005473}
Bill Wendlinga434d932009-01-29 09:01:55 +00005474
Dan Gohmana22f2d82009-10-10 01:29:16 +00005475MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005476SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005477 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005478 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005479 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005480 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005481}
5482
Dan Gohmana22f2d82009-10-10 01:29:16 +00005483MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005484SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005485 EVT VT1, EVT VT2, SDValue Op1,
5486 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005487 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005488 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005489 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005490}
5491
Dan Gohmana22f2d82009-10-10 01:29:16 +00005492MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005493SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005494 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005495 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005496 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005497 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005498}
Bill Wendlinga434d932009-01-29 09:01:55 +00005499
Dan Gohmana22f2d82009-10-10 01:29:16 +00005500MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005501SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005502 EVT VT1, EVT VT2, EVT VT3,
5503 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005504 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005505 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005506 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005507}
Bill Wendlinga434d932009-01-29 09:01:55 +00005508
Dan Gohmana22f2d82009-10-10 01:29:16 +00005509MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005510SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005511 EVT VT1, EVT VT2, EVT VT3,
5512 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005513 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005514 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005515 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005516}
Bill Wendlinga434d932009-01-29 09:01:55 +00005517
Dan Gohmana22f2d82009-10-10 01:29:16 +00005518MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005519SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005520 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005521 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005522 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005523 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005524}
Bill Wendlinga434d932009-01-29 09:01:55 +00005525
Dan Gohmana22f2d82009-10-10 01:29:16 +00005526MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005527SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005528 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005529 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005530 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005531 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005532}
5533
Dan Gohmana22f2d82009-10-10 01:29:16 +00005534MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005535SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005536 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005537 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005538 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liaob53d8962013-04-19 22:22:57 +00005539 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005540}
5541
Dan Gohmana22f2d82009-10-10 01:29:16 +00005542MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005543SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005544 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005545 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005546 MachineSDNode *N;
Ted Kremenek3c4408c2011-01-23 17:05:06 +00005547 void *IP = 0;
Michael Liaob53d8962013-04-19 22:22:57 +00005548 const SDValue *Ops = OpsArray.data();
5549 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005550
5551 if (DoCSE) {
5552 FoldingSetNodeID ID;
5553 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5554 IP = 0;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005555 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005556 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005557 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005558 }
5559
5560 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005561 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5562 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005563
5564 // Initialize the operands list.
5565 if (NumOps > array_lengthof(N->LocalOperands))
5566 // We're creating a final node that will live unmorphed for the
5567 // remainder of the current SelectionDAG iteration, so we can allocate
5568 // the operands directly out of a pool with no recycling metadata.
5569 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5570 Ops, NumOps);
5571 else
5572 N->InitOperands(N->LocalOperands, Ops, NumOps);
5573 N->OperandsNeedDelete = false;
5574
5575 if (DoCSE)
5576 CSEMap.InsertNode(N, IP);
5577
5578 AllNodes.push_back(N);
5579#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005580 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005581#endif
5582 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005583}
Evan Chengd3f1db92006-02-09 07:15:23 +00005584
Dan Gohmanac33a902009-08-19 18:16:17 +00005585/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005586/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005587SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005588SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005589 SDValue Operand) {
5590 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005591 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005592 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005593 return SDValue(Subreg, 0);
5594}
5595
Bob Wilson2a45a652009-10-08 18:49:46 +00005596/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005597/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005598SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005599SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005600 SDValue Operand, SDValue Subreg) {
5601 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005602 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005603 VT, Operand, Subreg, SRIdxVal);
5604 return SDValue(Result, 0);
5605}
5606
Evan Cheng31604a62008-03-22 01:55:50 +00005607/// getNodeIfExists - Get the specified node if it's already available, or
5608/// else return NULL.
5609SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005610 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005611 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005612 FoldingSetNodeID ID;
5613 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5614 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00005615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005616 return E;
5617 }
5618 return NULL;
5619}
5620
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005621/// getDbgValue - Creates a SDDbgValue node.
5622///
5623SDDbgValue *
5624SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5625 DebugLoc DL, unsigned O) {
5626 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5627}
5628
5629SDDbgValue *
Dan Gohmanbcaf6812010-04-15 01:51:59 +00005630SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005631 DebugLoc DL, unsigned O) {
5632 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5633}
5634
5635SDDbgValue *
5636SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5637 DebugLoc DL, unsigned O) {
5638 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5639}
5640
Dan Gohman7d099f72010-03-03 21:33:37 +00005641namespace {
5642
Dan Gohman9cc886b2010-03-04 19:11:28 +00005643/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005644/// pointed to by a use iterator is deleted, increment the use iterator
5645/// so that it doesn't dangle.
5646///
Dan Gohman7d099f72010-03-03 21:33:37 +00005647class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005648 SDNode::use_iterator &UI;
5649 SDNode::use_iterator &UE;
5650
5651 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5652 // Increment the iterator as needed.
5653 while (UI != UE && N == *UI)
5654 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005655 }
5656
5657public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005658 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005659 SDNode::use_iterator &ui,
5660 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005661 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005662};
5663
5664}
5665
Evan Cheng445b91a2006-08-07 22:13:29 +00005666/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005667/// This can cause recursive merging of nodes in the DAG.
5668///
Chris Lattner76858912008-02-03 03:35:22 +00005669/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005670///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005671void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005672 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005673 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005674 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005675 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005676
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005677 // Iterate over all the existing uses of From. New uses will be added
5678 // to the beginning of the use list, which we avoid visiting.
5679 // This specifically avoids visiting uses of From that arise while the
5680 // replacement is happening, because any such uses would be the result
5681 // of CSE: If an existing node looks like From after one of its operands
5682 // is replaced by To, we don't want to replace of all its users with To
5683 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005684 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005685 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005686 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005687 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005688
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005689 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005690 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005691
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005692 // A user can appear in a use list multiple times, and when this
5693 // happens the uses are usually next to each other in the list.
5694 // To help reduce the number of CSE recomputations, process all
5695 // the uses of this user that we can find this way.
5696 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005697 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005698 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005699 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005700 } while (UI != UE && *UI == User);
5701
5702 // Now that we have modified User, add it back to the CSE maps. If it
5703 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005704 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005705 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005706
5707 // If we just RAUW'd the root, take note.
5708 if (FromN == getRoot())
5709 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005710}
5711
5712/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5713/// This can cause recursive merging of nodes in the DAG.
5714///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005715/// This version assumes that for each value of From, there is a
5716/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005717///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005718void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005719#ifndef NDEBUG
5720 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5721 assert((!From->hasAnyUseOfValue(i) ||
5722 From->getValueType(i) == To->getValueType(i)) &&
5723 "Cannot use this version of ReplaceAllUsesWith!");
5724#endif
Dan Gohman17059682008-07-17 19:10:17 +00005725
5726 // Handle the trivial case.
5727 if (From == To)
5728 return;
5729
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005730 // Iterate over just the existing users of From. See the comments in
5731 // the ReplaceAllUsesWith above.
5732 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005733 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005734 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005735 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005736
Chris Lattner373f0482005-08-26 18:36:28 +00005737 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005738 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005739
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005740 // A user can appear in a use list multiple times, and when this
5741 // happens the uses are usually next to each other in the list.
5742 // To help reduce the number of CSE recomputations, process all
5743 // the uses of this user that we can find this way.
5744 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005745 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005746 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005747 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005748 } while (UI != UE && *UI == User);
5749
5750 // Now that we have modified User, add it back to the CSE maps. If it
5751 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005752 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005753 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005754
5755 // If we just RAUW'd the root, take note.
5756 if (From == getRoot().getNode())
5757 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005758}
5759
Chris Lattner373f0482005-08-26 18:36:28 +00005760/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5761/// This can cause recursive merging of nodes in the DAG.
5762///
5763/// This version can replace From with any result values. To must match the
5764/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005765void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005766 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005767 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005768
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005769 // Iterate over just the existing users of From. See the comments in
5770 // the ReplaceAllUsesWith above.
5771 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005772 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005773 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005774 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005775
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005776 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005777 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005778
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005779 // A user can appear in a use list multiple times, and when this
5780 // happens the uses are usually next to each other in the list.
5781 // To help reduce the number of CSE recomputations, process all
5782 // the uses of this user that we can find this way.
5783 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005784 SDUse &Use = UI.getUse();
5785 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005786 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005787 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005788 } while (UI != UE && *UI == User);
5789
5790 // Now that we have modified User, add it back to the CSE maps. If it
5791 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005792 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005793 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005794
5795 // If we just RAUW'd the root, take note.
5796 if (From == getRoot().getNode())
5797 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005798}
5799
Chris Lattner375e1a72006-02-17 21:58:01 +00005800/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005801/// uses of other values produced by From.getNode() alone. The Deleted
5802/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005803void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005804 // Handle the really simple, really trivial case efficiently.
5805 if (From == To) return;
5806
Chris Lattner375e1a72006-02-17 21:58:01 +00005807 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005808 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005809 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005810 return;
5811 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005812
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005813 // Iterate over just the existing users of From. See the comments in
5814 // the ReplaceAllUsesWith above.
5815 SDNode::use_iterator UI = From.getNode()->use_begin(),
5816 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005817 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005818 while (UI != UE) {
5819 SDNode *User = *UI;
5820 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005821
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005822 // A user can appear in a use list multiple times, and when this
5823 // happens the uses are usually next to each other in the list.
5824 // To help reduce the number of CSE recomputations, process all
5825 // the uses of this user that we can find this way.
5826 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005827 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005828
5829 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005830 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005831 ++UI;
5832 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005833 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005834
5835 // If this node hasn't been modified yet, it's still in the CSE maps,
5836 // so remove its old self from the CSE maps.
5837 if (!UserRemovedFromCSEMaps) {
5838 RemoveNodeFromCSEMaps(User);
5839 UserRemovedFromCSEMaps = true;
5840 }
5841
5842 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005843 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005844 } while (UI != UE && *UI == User);
5845
5846 // We are iterating over all uses of the From node, so if a use
5847 // doesn't use the specific value, no changes are made.
5848 if (!UserRemovedFromCSEMaps)
5849 continue;
5850
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005851 // Now that we have modified User, add it back to the CSE maps. If it
5852 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005853 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005854 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005855
5856 // If we just RAUW'd the root, take note.
5857 if (From == getRoot())
5858 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005859}
5860
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005861namespace {
5862 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5863 /// to record information about a use.
5864 struct UseMemo {
5865 SDNode *User;
5866 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005867 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005868 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005869
5870 /// operator< - Sort Memos by User.
5871 bool operator<(const UseMemo &L, const UseMemo &R) {
5872 return (intptr_t)L.User < (intptr_t)R.User;
5873 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005874}
5875
Dan Gohman17059682008-07-17 19:10:17 +00005876/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005877/// uses of other values produced by From.getNode() alone. The same value
5878/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005879/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005880void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5881 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005882 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005883 // Handle the simple, trivial case efficiently.
5884 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005885 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005886
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005887 // Read up all the uses and make records of them. This helps
5888 // processing new uses that are introduced during the
5889 // replacement process.
5890 SmallVector<UseMemo, 4> Uses;
5891 for (unsigned i = 0; i != Num; ++i) {
5892 unsigned FromResNo = From[i].getResNo();
5893 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005894 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005895 E = FromNode->use_end(); UI != E; ++UI) {
5896 SDUse &Use = UI.getUse();
5897 if (Use.getResNo() == FromResNo) {
5898 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005899 Uses.push_back(Memo);
5900 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005901 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005902 }
Dan Gohman17059682008-07-17 19:10:17 +00005903
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005904 // Sort the uses, so that all the uses from a given User are together.
5905 std::sort(Uses.begin(), Uses.end());
5906
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005907 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5908 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005909 // We know that this user uses some value of From. If it is the right
5910 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005911 SDNode *User = Uses[UseIndex].User;
5912
5913 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005914 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005915
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005916 // The Uses array is sorted, so all the uses for a given User
5917 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005918 // To help reduce the number of CSE recomputations, process all
5919 // the uses of this user that we can find this way.
5920 do {
5921 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005922 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005923 ++UseIndex;
5924
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005925 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005926 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5927
Dan Gohman17059682008-07-17 19:10:17 +00005928 // Now that we have modified User, add it back to the CSE maps. If it
5929 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005930 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005931 }
5932}
5933
Evan Cheng9631a602006-08-01 08:20:41 +00005934/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005935/// based on their topological order. It returns the maximum id and a vector
5936/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005937unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005938
Dan Gohman86aa16a2008-09-30 18:30:35 +00005939 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005940
Dan Gohman86aa16a2008-09-30 18:30:35 +00005941 // SortedPos tracks the progress of the algorithm. Nodes before it are
5942 // sorted, nodes after it are unsorted. When the algorithm completes
5943 // it is at the end of the list.
5944 allnodes_iterator SortedPos = allnodes_begin();
5945
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005946 // Visit all the nodes. Move nodes with no operands to the front of
5947 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005948 // operand count. Before we do this, the Node Id fields of the nodes
5949 // may contain arbitrary values. After, the Node Id fields for nodes
5950 // before SortedPos will contain the topological sort index, and the
5951 // Node Id fields for nodes At SortedPos and after will contain the
5952 // count of outstanding operands.
5953 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5954 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005955 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005956 unsigned Degree = N->getNumOperands();
5957 if (Degree == 0) {
5958 // A node with no uses, add it to the result array immediately.
5959 N->setNodeId(DAGSize++);
5960 allnodes_iterator Q = N;
5961 if (Q != SortedPos)
5962 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005963 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005964 ++SortedPos;
5965 } else {
5966 // Temporarily use the Node Id as scratch space for the degree count.
5967 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005968 }
5969 }
5970
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005971 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005972 // such that by the time the end is reached all nodes will be sorted.
5973 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5974 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00005975 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00005976 // N is in sorted position, so all its uses have one less operand
5977 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005978 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5979 UI != UE; ++UI) {
5980 SDNode *P = *UI;
5981 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00005982 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005983 --Degree;
5984 if (Degree == 0) {
5985 // All of P's operands are sorted, so P may sorted now.
5986 P->setNodeId(DAGSize++);
5987 if (P != SortedPos)
5988 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00005989 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005990 ++SortedPos;
5991 } else {
5992 // Update P's outstanding operand count.
5993 P->setNodeId(Degree);
5994 }
5995 }
David Greene3b2a68c2010-01-20 00:59:23 +00005996 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00005997#ifndef NDEBUG
5998 SDNode *S = ++I;
5999 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00006000 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00006001#endif
6002 llvm_unreachable(0);
David Greene3b2a68c2010-01-20 00:59:23 +00006003 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006004 }
6005
6006 assert(SortedPos == AllNodes.end() &&
6007 "Topological sort incomplete!");
6008 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6009 "First node in topological sort is not the entry token!");
6010 assert(AllNodes.front().getNodeId() == 0 &&
6011 "First node in topological sort has non-zero id!");
6012 assert(AllNodes.front().getNumOperands() == 0 &&
6013 "First node in topological sort has operands!");
6014 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6015 "Last node in topologic sort has unexpected id!");
6016 assert(AllNodes.back().use_empty() &&
6017 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006018 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006019 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006020}
6021
Evan Cheng563fe3c2010-03-25 01:38:16 +00006022/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6023/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006024void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6025 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006026 if (SD)
6027 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006028}
Evan Cheng29eefc12006-07-27 06:39:06 +00006029
Devang Patelefc6b162011-01-25 23:27:42 +00006030/// TransferDbgValues - Transfer SDDbgValues.
6031void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6032 if (From == To || !From.getNode()->getHasDebugValue())
6033 return;
6034 SDNode *FromNode = From.getNode();
6035 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006036 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006037 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006038 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006039 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006040 SDDbgValue *Dbg = *I;
6041 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6042 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
6043 Dbg->getOffset(), Dbg->getDebugLoc(),
6044 Dbg->getOrder());
6045 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006046 }
6047 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006048 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006049 E = ClonedDVs.end(); I != E; ++I)
6050 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006051}
6052
Jim Laskeyd66e6162005-08-17 20:08:02 +00006053//===----------------------------------------------------------------------===//
6054// SDNode Class
6055//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006056
Chris Lattner3bf17b62007-02-04 02:41:42 +00006057HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006058 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006059}
6060
Andrew Trickef9de2a2013-05-25 02:42:55 +00006061GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6062 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006063 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006064 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006065 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006066}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006067
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006068AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6069 SDValue X, unsigned SrcAS,
6070 unsigned DestAS)
6071 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6072 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6073
Andrew Trickef9de2a2013-05-25 02:42:55 +00006074MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6075 EVT memvt, MachineMemOperand *mmo)
6076 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006077 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006078 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006079 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006080 assert(isNonTemporal() == MMO->isNonTemporal() &&
6081 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006082 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006083}
6084
Andrew Trickef9de2a2013-05-25 02:42:55 +00006085MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peck527da1b2010-11-23 03:31:01 +00006086 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohman48b185d2009-09-25 20:36:54 +00006087 MachineMemOperand *mmo)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006088 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohman48b185d2009-09-25 20:36:54 +00006089 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006090 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006091 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006092 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6093 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006094}
6095
Jim Laskeyf576b422006-10-27 23:46:08 +00006096/// Profile - Gather unique data for the node.
6097///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006098void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006099 AddNodeIDNode(ID, this);
6100}
6101
Owen Anderson3b1665e2009-08-25 22:27:22 +00006102namespace {
6103 struct EVTArray {
6104 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006105
Owen Anderson3b1665e2009-08-25 22:27:22 +00006106 EVTArray() {
6107 VTs.reserve(MVT::LAST_VALUETYPE);
6108 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6109 VTs.push_back(MVT((MVT::SimpleValueType)i));
6110 }
6111 };
6112}
6113
Owen Anderson53aa7a92009-08-10 22:56:29 +00006114static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006115static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006116static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006117
Chris Lattner88fa11c2005-11-08 23:30:28 +00006118/// getValueTypeList - Return a pointer to the specified value type.
6119///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006120const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006121 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006122 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006123 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006124 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006125 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006126 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006127 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006128 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006129}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006130
Chris Lattner40e79822005-01-12 18:37:47 +00006131/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6132/// indicated value. This method ignores uses of other values defined by this
6133/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006134bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006135 assert(Value < getNumValues() && "Bad value!");
6136
Roman Levenstein51f532f2008-04-07 10:06:32 +00006137 // TODO: Only iterate over uses of a given value of the node
6138 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006139 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006140 if (NUses == 0)
6141 return false;
6142 --NUses;
6143 }
Chris Lattner40e79822005-01-12 18:37:47 +00006144 }
6145
6146 // Found exactly the right number of uses?
6147 return NUses == 0;
6148}
6149
6150
Evan Cheng358c3d12007-08-02 05:29:38 +00006151/// hasAnyUseOfValue - Return true if there are any use of the indicated
6152/// value. This method ignores uses of other values defined by this operation.
6153bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6154 assert(Value < getNumValues() && "Bad value!");
6155
Dan Gohman7a510c22008-07-09 22:39:01 +00006156 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006157 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006158 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006159
6160 return false;
6161}
6162
6163
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006164/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006165///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006166bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006167 bool Seen = false;
6168 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006169 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006170 if (User == this)
6171 Seen = true;
6172 else
6173 return false;
6174 }
6175
6176 return Seen;
6177}
6178
Evan Cheng9456dd82006-11-03 07:31:32 +00006179/// isOperand - Return true if this node is an operand of N.
6180///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006181bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006182 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6183 if (*this == N->getOperand(i))
6184 return true;
6185 return false;
6186}
6187
Evan Cheng567d2e52008-03-04 00:41:45 +00006188bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006189 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006190 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006191 return true;
6192 return false;
6193}
Evan Chengd37645c2006-02-05 06:29:23 +00006194
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006195/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006196/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006197/// side-effecting instructions on any chain path. In practice, this looks
6198/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006199/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006200bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006201 unsigned Depth) const {
6202 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006203
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006204 // Don't search too deeply, we just want to be able to see through
6205 // TokenFactor's etc.
6206 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006207
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006208 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006209 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006210 if (getOpcode() == ISD::TokenFactor) {
6211 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006212 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6213 return false;
6214 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006215 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006216
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006217 // Loads don't have side effects, look through them.
6218 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6219 if (!Ld->isVolatile())
6220 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6221 }
6222 return false;
6223}
6224
Lang Hames5a004992011-07-07 04:31:51 +00006225/// hasPredecessor - Return true if N is a predecessor of this node.
6226/// N is either an operand of this node, or can be reached by recursively
6227/// traversing up the operands.
6228/// NOTE: This is an expensive method. Use it carefully.
6229bool SDNode::hasPredecessor(const SDNode *N) const {
6230 SmallPtrSet<const SDNode *, 32> Visited;
6231 SmallVector<const SDNode *, 16> Worklist;
6232 return hasPredecessorHelper(N, Visited, Worklist);
6233}
Dan Gohmancd139c02009-10-28 03:44:30 +00006234
Craig Topperb94011f2013-07-14 04:42:23 +00006235bool
6236SDNode::hasPredecessorHelper(const SDNode *N,
6237 SmallPtrSet<const SDNode *, 32> &Visited,
6238 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006239 if (Visited.empty()) {
6240 Worklist.push_back(this);
6241 } else {
6242 // Take a look in the visited set. If we've already encountered this node
6243 // we needn't search further.
6244 if (Visited.count(N))
6245 return true;
6246 }
6247
6248 // Haven't visited N yet. Continue the search.
6249 while (!Worklist.empty()) {
6250 const SDNode *M = Worklist.pop_back_val();
6251 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6252 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006253 if (Visited.insert(Op))
6254 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006255 if (Op == N)
6256 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006257 }
Lang Hames5a004992011-07-07 04:31:51 +00006258 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006259
6260 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006261}
6262
Evan Cheng5d9fd972006-10-04 00:56:09 +00006263uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6264 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006265 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006266}
6267
Mon P Wang32f8bb92009-11-30 02:42:02 +00006268SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6269 assert(N->getNumValues() == 1 &&
6270 "Can't unroll a vector with multiple results!");
6271
6272 EVT VT = N->getValueType(0);
6273 unsigned NE = VT.getVectorNumElements();
6274 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006275 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006276
6277 SmallVector<SDValue, 8> Scalars;
6278 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6279
6280 // If ResNE is 0, fully unroll the vector op.
6281 if (ResNE == 0)
6282 ResNE = NE;
6283 else if (NE > ResNE)
6284 NE = ResNE;
6285
6286 unsigned i;
6287 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006288 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006289 SDValue Operand = N->getOperand(j);
6290 EVT OperandVT = Operand.getValueType();
6291 if (OperandVT.isVector()) {
6292 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006293 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006294 EVT OperandEltVT = OperandVT.getVectorElementType();
6295 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6296 OperandEltVT,
6297 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006298 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006299 } else {
6300 // A scalar operand; just use it as is.
6301 Operands[j] = Operand;
6302 }
6303 }
6304
6305 switch (N->getOpcode()) {
6306 default:
6307 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6308 &Operands[0], Operands.size()));
6309 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006310 case ISD::VSELECT:
6311 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6312 &Operands[0], Operands.size()));
6313 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006314 case ISD::SHL:
6315 case ISD::SRA:
6316 case ISD::SRL:
6317 case ISD::ROTL:
6318 case ISD::ROTR:
6319 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006320 getShiftAmountOperand(Operands[0].getValueType(),
6321 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006322 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006323 case ISD::SIGN_EXTEND_INREG:
6324 case ISD::FP_ROUND_INREG: {
6325 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6326 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6327 Operands[0],
6328 getValueType(ExtVT)));
6329 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006330 }
6331 }
6332
6333 for (; i < ResNE; ++i)
6334 Scalars.push_back(getUNDEF(EltVT));
6335
6336 return getNode(ISD::BUILD_VECTOR, dl,
6337 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6338 &Scalars[0], Scalars.size());
6339}
6340
Evan Chengf5938d52009-12-09 01:36:00 +00006341
Wesley Peck527da1b2010-11-23 03:31:01 +00006342/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6343/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006344/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006345bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006346 unsigned Bytes, int Dist) const {
6347 if (LD->getChain() != Base->getChain())
6348 return false;
6349 EVT VT = LD->getValueType(0);
6350 if (VT.getSizeInBits() / 8 != Bytes)
6351 return false;
6352
6353 SDValue Loc = LD->getOperand(1);
6354 SDValue BaseLoc = Base->getOperand(1);
6355 if (Loc.getOpcode() == ISD::FrameIndex) {
6356 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6357 return false;
6358 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6359 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6360 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6361 int FS = MFI->getObjectSize(FI);
6362 int BFS = MFI->getObjectSize(BFI);
6363 if (FS != BFS || FS != (int)Bytes) return false;
6364 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6365 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006366
6367 // Handle X+C
6368 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6369 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6370 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006371
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006372 const GlobalValue *GV1 = NULL;
6373 const GlobalValue *GV2 = NULL;
Evan Chengf5938d52009-12-09 01:36:00 +00006374 int64_t Offset1 = 0;
6375 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006376 const TargetLowering *TLI = TM.getTargetLowering();
6377 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6378 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006379 if (isGA1 && isGA2 && GV1 == GV2)
6380 return Offset1 == (Offset2 + Dist*Bytes);
6381 return false;
6382}
6383
6384
Evan Cheng34a23ea2009-12-09 01:04:59 +00006385/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6386/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006387unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006388 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006389 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006390 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006391 const TargetLowering *TLI = TM.getTargetLowering();
6392 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006393 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006394 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00006395 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006396 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006397 unsigned AlignBits = KnownZero.countTrailingOnes();
6398 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6399 if (Align)
6400 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006401 }
Evan Chengd938faf2009-12-09 01:53:58 +00006402
Evan Cheng34a23ea2009-12-09 01:04:59 +00006403 // If this is a direct reference to a stack slot, use information about the
6404 // stack slot's alignment.
6405 int FrameIdx = 1 << 31;
6406 int64_t FrameOffset = 0;
6407 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6408 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006409 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006410 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006411 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006412 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6413 FrameOffset = Ptr.getConstantOperandVal(1);
6414 }
6415
6416 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006417 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006418 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6419 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006420 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006421 }
6422
6423 return 0;
6424}
6425
Juergen Ributzkab3487102013-11-19 21:20:17 +00006426/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6427/// which is split (or expanded) into two not necessarily identical pieces.
6428std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6429 // Currently all types are split in half.
6430 EVT LoVT, HiVT;
6431 if (!VT.isVector()) {
6432 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6433 } else {
6434 unsigned NumElements = VT.getVectorNumElements();
6435 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6436 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6437 NumElements/2);
6438 }
6439 return std::make_pair(LoVT, HiVT);
6440}
6441
6442/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6443/// low/high part.
6444std::pair<SDValue, SDValue>
6445SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6446 const EVT &HiVT) {
6447 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6448 N.getValueType().getVectorNumElements() &&
6449 "More vector elements requested than available!");
6450 SDValue Lo, Hi;
6451 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6452 getConstant(0, TLI->getVectorIdxTy()));
6453 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6454 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6455 return std::make_pair(Lo, Hi);
6456}
6457
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006458// getAddressSpace - Return the address space this GlobalAddress belongs to.
6459unsigned GlobalAddressSDNode::getAddressSpace() const {
6460 return getGlobal()->getType()->getAddressSpace();
6461}
6462
6463
Chris Lattner229907c2011-07-18 04:54:35 +00006464Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006465 if (isMachineConstantPoolEntry())
6466 return Val.MachineCPVal->getType();
6467 return Val.ConstVal->getType();
6468}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006469
Bob Wilson85cefe82009-03-02 23:24:16 +00006470bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6471 APInt &SplatUndef,
6472 unsigned &SplatBitSize,
6473 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006474 unsigned MinSplatBits,
6475 bool isBigEndian) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006476 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006477 assert(VT.isVector() && "Expected a vector type");
6478 unsigned sz = VT.getSizeInBits();
6479 if (MinSplatBits > sz)
6480 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006481
Bob Wilson85cefe82009-03-02 23:24:16 +00006482 SplatValue = APInt(sz, 0);
6483 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006484
Bob Wilson85cefe82009-03-02 23:24:16 +00006485 // Get the bits. Bits with undefined values (when the corresponding element
6486 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6487 // in SplatValue. If any of the values are not constant, give up and return
6488 // false.
6489 unsigned int nOps = getNumOperands();
6490 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6491 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006492
6493 for (unsigned j = 0; j < nOps; ++j) {
6494 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006495 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006496 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006497
Bob Wilson85cefe82009-03-02 23:24:16 +00006498 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006499 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006500 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006501 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006502 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006503 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006504 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006505 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006506 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006507 }
6508
Bob Wilson85cefe82009-03-02 23:24:16 +00006509 // The build_vector is all constants or undefs. Find the smallest element
6510 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006511
Bob Wilson85cefe82009-03-02 23:24:16 +00006512 HasAnyUndefs = (SplatUndef != 0);
6513 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006514
Bob Wilson85cefe82009-03-02 23:24:16 +00006515 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006516 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6517 APInt LowValue = SplatValue.trunc(HalfSize);
6518 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6519 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006520
Bob Wilson85cefe82009-03-02 23:24:16 +00006521 // If the two halves do not match (ignoring undef bits), stop here.
6522 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6523 MinSplatBits > HalfSize)
6524 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006525
Bob Wilson85cefe82009-03-02 23:24:16 +00006526 SplatValue = HighValue | LowValue;
6527 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006528
Bob Wilson85cefe82009-03-02 23:24:16 +00006529 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006530 }
6531
Bob Wilson85cefe82009-03-02 23:24:16 +00006532 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006533 return true;
6534}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006535
Juergen Ributzka73844052014-01-13 20:51:35 +00006536bool BuildVectorSDNode::isConstant() const {
6537 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6538 unsigned Opc = getOperand(i).getOpcode();
6539 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6540 return false;
6541 }
6542 return true;
6543}
6544
Owen Anderson53aa7a92009-08-10 22:56:29 +00006545bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006546 // Find the first non-undef value in the shuffle mask.
6547 unsigned i, e;
6548 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6549 /* search */;
6550
Nate Begeman39b59db2009-04-29 18:13:31 +00006551 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006552
Nate Begeman5f829d82009-04-29 05:20:52 +00006553 // Make sure all remaining elements are either undef or the same as the first
6554 // non-undef value.
6555 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006556 if (Mask[i] >= 0 && Mask[i] != Idx)
6557 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006558 return true;
6559}
David Greene09851602010-01-20 20:13:31 +00006560
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006561#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006562static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006563 SmallPtrSet<const SDNode*, 32> &Visited,
6564 SmallPtrSet<const SDNode*, 32> &Checked) {
6565 // If this node has already been checked, don't check it again.
6566 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006567 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006568
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006569 // If a node has already been visited on this depth-first walk, reject it as
6570 // a cycle.
6571 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006572 dbgs() << "Offending node:\n";
6573 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006574 errs() << "Detected cycle in SelectionDAG\n";
6575 abort();
David Greene09851602010-01-20 20:13:31 +00006576 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006577
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006578 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6579 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006580
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006581 Checked.insert(N);
6582 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006583}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006584#endif
David Greene09851602010-01-20 20:13:31 +00006585
6586void llvm::checkForCycles(const llvm::SDNode *N) {
6587#ifdef XDEBUG
Alp Toker6a033742013-10-29 02:35:28 +00006588 assert(N && "Checking nonexistent SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006589 SmallPtrSet<const SDNode*, 32> visited;
6590 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006591 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006592#endif
6593}
6594
6595void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6596 checkForCycles(DAG->getRoot().getNode());
6597}