blob: 430736aa46fe5e0778c5caa065ffe5fbe90b4ce4 [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:
Juergen Ributzka38b67d02014-01-24 18:23:08 +0000387 case ISD::Constant: {
388 const ConstantSDNode *C = cast<ConstantSDNode>(N);
389 ID.AddPointer(C->getConstantIntValue());
390 ID.AddBoolean(C->isOpaque());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000391 break;
Juergen Ributzka38b67d02014-01-24 18:23:08 +0000392 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000393 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000394 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000395 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000396 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000397 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000398 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000399 case ISD::GlobalAddress:
400 case ISD::TargetGlobalTLSAddress:
401 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000402 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000403 ID.AddPointer(GA->getGlobal());
404 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000405 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000406 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000407 break;
408 }
409 case ISD::BasicBlock:
410 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
411 break;
412 case ISD::Register:
413 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
414 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000415 case ISD::RegisterMask:
416 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
417 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000418 case ISD::SRCVALUE:
419 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
420 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000421 case ISD::FrameIndex:
422 case ISD::TargetFrameIndex:
423 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
424 break;
425 case ISD::JumpTable:
426 case ISD::TargetJumpTable:
427 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000428 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000429 break;
430 case ISD::ConstantPool:
431 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000432 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000433 ID.AddInteger(CP->getAlignment());
434 ID.AddInteger(CP->getOffset());
435 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000436 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000437 else
438 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000439 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000440 break;
441 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000442 case ISD::TargetIndex: {
443 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
444 ID.AddInteger(TI->getIndex());
445 ID.AddInteger(TI->getOffset());
446 ID.AddInteger(TI->getTargetFlags());
447 break;
448 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000449 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000450 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000451 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000452 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000453 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000454 break;
455 }
456 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000457 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000458 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000459 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000460 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000461 break;
462 }
Dan Gohman12f24902008-12-23 21:37:04 +0000463 case ISD::ATOMIC_CMP_SWAP:
464 case ISD::ATOMIC_SWAP:
465 case ISD::ATOMIC_LOAD_ADD:
466 case ISD::ATOMIC_LOAD_SUB:
467 case ISD::ATOMIC_LOAD_AND:
468 case ISD::ATOMIC_LOAD_OR:
469 case ISD::ATOMIC_LOAD_XOR:
470 case ISD::ATOMIC_LOAD_NAND:
471 case ISD::ATOMIC_LOAD_MIN:
472 case ISD::ATOMIC_LOAD_MAX:
473 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000474 case ISD::ATOMIC_LOAD_UMAX:
475 case ISD::ATOMIC_LOAD:
476 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000477 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000478 ID.AddInteger(AT->getMemoryVT().getRawBits());
479 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000480 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
481 break;
482 }
483 case ISD::PREFETCH: {
484 const MemSDNode *PF = cast<MemSDNode>(N);
485 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000486 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000487 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000488 case ISD::VECTOR_SHUFFLE: {
489 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000490 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000491 i != e; ++i)
492 ID.AddInteger(SVN->getMaskElt(i));
493 break;
494 }
Dan Gohman6c938802009-10-30 01:27:03 +0000495 case ISD::TargetBlockAddress:
496 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000497 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
498 ID.AddPointer(BA->getBlockAddress());
499 ID.AddInteger(BA->getOffset());
500 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000501 break;
502 }
Mon P Wang6a490372008-06-25 08:15:39 +0000503 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000504
505 // Target specific memory nodes could also have address spaces to check.
506 if (N->isTargetMemoryOpcode())
507 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000508}
509
Duncan Sands835bdca2008-10-27 15:30:53 +0000510/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
511/// data.
512static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
513 AddNodeIDOpcode(ID, N->getOpcode());
514 // Add the return value info.
515 AddNodeIDValueTypes(ID, N->getVTList());
516 // Add the operand info.
517 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
518
519 // Handle SDNode leafs with special info.
520 AddNodeIDCustom(ID, N);
521}
522
Dan Gohman2da2bed2008-08-20 15:58:01 +0000523/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000524/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000525/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000526///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000527static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000528encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000529 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000530 assert((ConvType & 3) == ConvType &&
531 "ConvType may not require more than 2 bits!");
532 assert((AM & 7) == AM &&
533 "AM may not require more than 3 bits!");
534 return ConvType |
535 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000536 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000537 (isNonTemporal << 6) |
538 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000539}
540
Jim Laskeyf576b422006-10-27 23:46:08 +0000541//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000542// SelectionDAG Class
543//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000544
Duncan Sands835bdca2008-10-27 15:30:53 +0000545/// doNotCSE - Return true if CSE should not be performed for this node.
546static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000547 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000548 return true; // Never CSE anything that produces a flag.
549
550 switch (N->getOpcode()) {
551 default: break;
552 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000553 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000554 return true; // Never CSE these nodes.
555 }
556
557 // Check that remaining values produced are not flags.
558 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000559 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000560 return true; // Never CSE anything that produces a flag.
561
562 return false;
563}
564
Chris Lattner9c667932005-01-07 21:09:16 +0000565/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000566/// SelectionDAG.
567void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000568 // Create a dummy node (which is not added to allnodes), that adds a reference
569 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000570 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000571
Chris Lattner8927c872006-08-04 17:45:20 +0000572 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000573
Chris Lattner8927c872006-08-04 17:45:20 +0000574 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000575 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000576 if (I->use_empty())
577 DeadNodes.push_back(I);
578
Dan Gohman91697632008-07-07 20:57:48 +0000579 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000580
Dan Gohman91697632008-07-07 20:57:48 +0000581 // If the root changed (e.g. it was a dead load, update the root).
582 setRoot(Dummy.getValue());
583}
584
585/// RemoveDeadNodes - This method deletes the unreachable nodes in the
586/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000587void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000588
Chris Lattner8927c872006-08-04 17:45:20 +0000589 // Process the worklist, deleting the nodes and adding their uses to the
590 // worklist.
591 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000592 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000593
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000594 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
595 DUL->NodeDeleted(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000596
Chris Lattner8927c872006-08-04 17:45:20 +0000597 // Take the node out of the appropriate CSE map.
598 RemoveNodeFromCSEMaps(N);
599
600 // Next, brutally remove the operand list. This is safe to do, as there are
601 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000602 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
603 SDUse &Use = *I++;
604 SDNode *Operand = Use.getNode();
605 Use.set(SDValue());
606
Chris Lattner8927c872006-08-04 17:45:20 +0000607 // Now that we removed this operand, see if there are no uses of it left.
608 if (Operand->use_empty())
609 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000610 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000611
Dan Gohman534c8a22009-01-19 22:39:36 +0000612 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000613 }
Chris Lattner9c667932005-01-07 21:09:16 +0000614}
615
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000616void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000617 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000618
619 // Create a dummy node that adds a reference to the root node, preventing
620 // it from being deleted. (This matters if the root is an operand of the
621 // dead node.)
622 HandleSDNode Dummy(getRoot());
623
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000624 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000625}
626
Chris Lattnerc738d002005-08-29 21:59:31 +0000627void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000628 // First take this out of the appropriate CSE map.
629 RemoveNodeFromCSEMaps(N);
630
Scott Michelcf0da6c2009-02-17 22:15:04 +0000631 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000632 // AllNodes list, and delete the node.
633 DeleteNodeNotInCSEMaps(N);
634}
635
636void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000637 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
638 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000639
Dan Gohman86aa16a2008-09-30 18:30:35 +0000640 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000641 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000642
Dan Gohman534c8a22009-01-19 22:39:36 +0000643 DeallocateNode(N);
644}
645
646void SelectionDAG::DeallocateNode(SDNode *N) {
647 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000648 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000649
Dan Gohman534c8a22009-01-19 22:39:36 +0000650 // Set the opcode to DELETED_NODE to help catch bugs when node
651 // memory is reallocated.
652 N->NodeType = ISD::DELETED_NODE;
653
Dan Gohman2e834902008-08-26 01:44:34 +0000654 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000655
Evan Cheng563fe3c2010-03-25 01:38:16 +0000656 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000657 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000658 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
659 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000660}
661
Chris Lattner19732782005-08-16 18:17:10 +0000662/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
663/// correspond to it. This is useful when we're about to delete or repurpose
664/// the node. We don't want future request for structurally identical nodes
665/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000666bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000667 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000668 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000669 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000670 case ISD::CONDCODE:
671 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
672 "Cond code doesn't exist!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000673 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000674 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
675 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000676 case ISD::ExternalSymbol:
677 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000678 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000679 case ISD::TargetExternalSymbol: {
680 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
681 Erased = TargetExternalSymbols.erase(
682 std::pair<std::string,unsigned char>(ESN->getSymbol(),
683 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000684 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000685 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000686 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000687 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000688 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000689 Erased = ExtendedValueTypeNodes.erase(VT);
690 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000691 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
692 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000693 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000694 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000695 }
Chris Lattner9c667932005-01-07 21:09:16 +0000696 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000697 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000698 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
699 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000700 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000701 break;
702 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000703#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000704 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000705 // flag result (which cannot be CSE'd) or is one of the special cases that are
706 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000707 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000708 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000709 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000710 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000711 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000712 }
713#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000714 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000715}
716
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000717/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
718/// maps and modified in place. Add it back to the CSE maps, unless an identical
719/// node already exists, in which case transfer all its users to the existing
720/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000721///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000722void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000723SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000724 // For node types that aren't CSE'd, just act as if no identical node
725 // already exists.
726 if (!doNotCSE(N)) {
727 SDNode *Existing = CSEMap.GetOrInsertNode(N);
728 if (Existing != N) {
729 // If there was already an existing matching node, use ReplaceAllUsesWith
730 // to replace the dead one with the existing one. This can cause
731 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000732 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000733
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000734 // N is now dead. Inform the listeners and delete it.
735 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
736 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000737 DeleteNodeNotInCSEMaps(N);
738 return;
739 }
740 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000741
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000742 // If the node doesn't already exist, we updated it. Inform listeners.
743 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
744 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000745}
746
Chris Lattnerf34156e2006-01-28 09:32:45 +0000747/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000748/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000749/// return null, otherwise return a pointer to the slot it would take. If a
750/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000751SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000752 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000753 if (doNotCSE(N))
754 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000755
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000756 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000757 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000758 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands835bdca2008-10-27 15:30:53 +0000759 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000760 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000761 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000762}
763
764/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000765/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000766/// return null, otherwise return a pointer to the slot it would take. If a
767/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000768SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000769 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000770 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000771 if (doNotCSE(N))
772 return 0;
773
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000774 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000775 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000776 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands835bdca2008-10-27 15:30:53 +0000777 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000778 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000779 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000780}
781
782
783/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000784/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000785/// return null, otherwise return a pointer to the slot it would take. If a
786/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000787SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000788 const SDValue *Ops,unsigned NumOps,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000789 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000790 if (doNotCSE(N))
791 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000792
Jim Laskeyf576b422006-10-27 23:46:08 +0000793 FoldingSetNodeID ID;
Dan Gohman92a7f3a2007-06-04 15:49:41 +0000794 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands835bdca2008-10-27 15:30:53 +0000795 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000796 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000797 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000798}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000799
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000800#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000801/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
802static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsb0e39382008-07-21 10:20:31 +0000803 switch (N->getOpcode()) {
804 default:
805 break;
Duncan Sands17e678b2008-10-29 14:22:20 +0000806 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000807 EVT VT = N->getValueType(0);
Duncan Sands17e678b2008-10-29 14:22:20 +0000808 assert(N->getNumValues() == 1 && "Too many results!");
809 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
810 "Wrong return type!");
811 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
812 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
813 "Mismatched operand types!");
814 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
815 "Wrong operand type!");
816 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
817 "Wrong return type size");
818 break;
819 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000820 case ISD::BUILD_VECTOR: {
Duncan Sands17e678b2008-10-29 14:22:20 +0000821 assert(N->getNumValues() == 1 && "Too many results!");
822 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsb0e39382008-07-21 10:20:31 +0000823 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands17e678b2008-10-29 14:22:20 +0000824 "Wrong number of operands!");
Owen Anderson53aa7a92009-08-10 22:56:29 +0000825 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedmanb7910b72011-09-09 21:04:06 +0000826 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindolab93db662009-04-24 12:40:33 +0000827 assert((I->getValueType() == EltVT ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000828 (EltVT.isInteger() && I->getValueType().isInteger() &&
829 EltVT.bitsLE(I->getValueType()))) &&
830 "Wrong operand type!");
Eli Friedmanb7910b72011-09-09 21:04:06 +0000831 assert(I->getValueType() == N->getOperand(0).getValueType() &&
832 "Operands must all have the same type");
833 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000834 break;
835 }
836 }
837}
838
Duncan Sands7c601de2010-11-20 11:25:00 +0000839/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
840static void VerifySDNode(SDNode *N) {
841 // The SDNode allocators cannot be used to allocate nodes with fields that are
842 // not present in an SDNode!
843 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
844 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
845 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
846 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
847 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
848 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
849 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
850 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
851 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
852 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
853 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
854 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
855 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
856 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
857 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
858 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
859 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
860 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
861 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
862
863 VerifyNodeCommon(N);
864}
865
866/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
867/// invalid.
868static void VerifyMachineNode(SDNode *N) {
869 // The MachineNode allocators cannot be used to allocate nodes with fields
870 // that are not present in a MachineNode!
871 // Currently there are no such nodes.
872
873 VerifyNodeCommon(N);
874}
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000875#endif // NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000876
Owen Anderson53aa7a92009-08-10 22:56:29 +0000877/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000878/// given type.
879///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000880unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000881 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000882 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000883 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000884
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000885 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000886}
Chris Lattner9c667932005-01-07 21:09:16 +0000887
Dale Johannesen8ba713212009-02-07 02:15:05 +0000888// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000889SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Juergen Ributzkab3487102013-11-19 21:20:17 +0000890 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TTI(0), TLI(0), OptLevel(OL),
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000891 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sanders50b80412013-11-15 12:56:49 +0000892 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
893 UpdateListeners(0) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000894 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000895 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000896}
897
Juergen Ributzkab3487102013-11-19 21:20:17 +0000898void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti,
899 const TargetLowering *tli) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000900 MF = &mf;
Chandler Carruth42e96112013-01-05 12:32:17 +0000901 TTI = tti;
Juergen Ributzkab3487102013-11-19 21:20:17 +0000902 TLI = tli;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000903 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000904}
905
Chris Lattner600d3082003-08-11 14:57:33 +0000906SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000907 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000908 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000909 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000910}
911
912void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000913 assert(&*AllNodes.begin() == &EntryNode);
914 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000915 while (!AllNodes.empty())
916 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000917}
918
Dan Gohmane1a9a782008-08-27 23:52:12 +0000919void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000920 allnodes_clear();
921 OperandAllocator.Reset();
922 CSEMap.clear();
923
924 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000925 ExternalSymbols.clear();
926 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000927 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
928 static_cast<CondCodeSDNode*>(0));
929 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
930 static_cast<SDNode*>(0));
931
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000932 EntryNode.UseList = 0;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000933 AllNodes.push_back(&EntryNode);
934 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000935 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000936}
937
Andrew Trickef9de2a2013-05-25 02:42:55 +0000938SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000939 return VT.bitsGT(Op.getValueType()) ?
940 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
941 getNode(ISD::TRUNCATE, DL, VT, Op);
942}
943
Andrew Trickef9de2a2013-05-25 02:42:55 +0000944SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000945 return VT.bitsGT(Op.getValueType()) ?
946 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
947 getNode(ISD::TRUNCATE, DL, VT, Op);
948}
949
Andrew Trickef9de2a2013-05-25 02:42:55 +0000950SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000951 return VT.bitsGT(Op.getValueType()) ?
952 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
953 getNode(ISD::TRUNCATE, DL, VT, Op);
954}
955
Andrew Trickef9de2a2013-05-25 02:42:55 +0000956SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000957 assert(!VT.isVector() &&
958 "getZeroExtendInReg should use the vector element type instead of "
959 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000960 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000961 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
962 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000963 VT.getSizeInBits());
964 return getNode(ISD::AND, DL, Op.getValueType(), Op,
965 getConstant(Imm, Op.getValueType()));
966}
967
Bob Wilsonc5890052009-01-22 17:39:32 +0000968/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
969///
Andrew Trickef9de2a2013-05-25 02:42:55 +0000970SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000971 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +0000972 SDValue NegOne =
973 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +0000974 return getNode(ISD::XOR, DL, VT, Val, NegOne);
975}
976
Juergen Ributzka38b67d02014-01-24 18:23:08 +0000977SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000978 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +0000979 assert((EltVT.getSizeInBits() >= 64 ||
980 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
981 "getConstant with a uint64_t value that doesn't fit in the type!");
Juergen Ributzka38b67d02014-01-24 18:23:08 +0000982 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman65f63eb2008-02-08 22:59:30 +0000983}
984
Juergen Ributzka38b67d02014-01-24 18:23:08 +0000985SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
986{
987 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000988}
989
Juergen Ributzka38b67d02014-01-24 18:23:08 +0000990SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
991 bool isO) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000992 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +0000993
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000994 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +0000995 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +0000996
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000997 const TargetLowering *TLI = TM.getTargetLowering();
998
Duncan Sandsf2641e12011-09-06 19:07:46 +0000999 // In some cases the vector type is legal but the element type is illegal and
1000 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1001 // inserted value (the type does not need to match the vector element type).
1002 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001003 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001004 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001005 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001006 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1007 Elt = ConstantInt::get(*getContext(), NewVal);
1008 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001009 // In other cases the element type is illegal and needs to be expanded, for
1010 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1011 // the value into n parts and use a vector type with n-times the elements.
1012 // Then bitcast to the type requested.
1013 // Legalizing constants too early makes the DAGCombiner's job harder so we
1014 // only legalize if the DAG tells us we must produce legal types.
1015 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1016 TLI->getTypeAction(*getContext(), EltVT) ==
1017 TargetLowering::TypeExpandInteger) {
1018 APInt NewVal = Elt->getValue();
1019 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1020 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1021 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1022 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1023
1024 // Check the temporary vector is the correct size. If this fails then
1025 // getTypeToTransformTo() probably returned a type whose size (in bits)
1026 // isn't a power-of-2 factor of the requested type size.
1027 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1028
1029 SmallVector<SDValue, 2> EltParts;
1030 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1031 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1032 .trunc(ViaEltSizeInBits),
Juergen Ributzka38b67d02014-01-24 18:23:08 +00001033 ViaEltVT, isT, isO));
Daniel Sanders50b80412013-11-15 12:56:49 +00001034 }
1035
1036 // EltParts is currently in little endian order. If we actually want
1037 // big-endian order then reverse it now.
1038 if (TLI->isBigEndian())
1039 std::reverse(EltParts.begin(), EltParts.end());
1040
1041 // The elements must be reversed when the element order is different
1042 // to the endianness of the elements (because the BITCAST is itself a
1043 // vector shuffle in this situation). However, we do not need any code to
1044 // perform this reversal because getConstant() is producing a vector
1045 // splat.
1046 // This situation occurs in MIPS MSA.
1047
1048 SmallVector<SDValue, 8> Ops;
1049 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1050 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1051
1052 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1053 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
1054 &Ops[0], Ops.size()));
1055 return Result;
1056 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001057
1058 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1059 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001060 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001061 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001062 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001063 ID.AddPointer(Elt);
Juergen Ributzka38b67d02014-01-24 18:23:08 +00001064 ID.AddBoolean(isO);
Chris Lattner3f16b202006-08-11 21:01:22 +00001065 void *IP = 0;
Dan Gohman7a7742c2007-12-12 22:21:26 +00001066 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +00001067 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001068 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001069 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001070
Dan Gohman7a7742c2007-12-12 22:21:26 +00001071 if (!N) {
Juergen Ributzka38b67d02014-01-24 18:23:08 +00001072 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001073 CSEMap.InsertNode(N, IP);
1074 AllNodes.push_back(N);
1075 }
1076
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001077 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001078 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001079 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001080 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001081 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7a7742c2007-12-12 22:21:26 +00001082 }
1083 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001084}
1085
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001086SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001087 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001088}
1089
1090
Owen Anderson53aa7a92009-08-10 22:56:29 +00001091SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001092 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001093}
1094
Owen Anderson53aa7a92009-08-10 22:56:29 +00001095SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001096 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001097
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001098 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001099
Chris Lattner381dddc2005-02-17 20:17:32 +00001100 // Do the map lookup using the actual bit pattern for the floating point
1101 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1102 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001103 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001104 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001105 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001106 ID.AddPointer(&V);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001107 void *IP = 0;
Evan Cheng9458e6a2007-06-29 21:36:04 +00001108 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +00001109 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001110 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001111 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001112
Evan Cheng9458e6a2007-06-29 21:36:04 +00001113 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001114 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001115 CSEMap.InsertNode(N, IP);
1116 AllNodes.push_back(N);
1117 }
1118
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001119 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001120 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001121 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001122 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001123 // FIXME SDLoc info might be appropriate here
1124 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohmana8665142007-06-25 16:23:39 +00001125 }
1126 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001127}
1128
Owen Anderson53aa7a92009-08-10 22:56:29 +00001129SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001130 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001131 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001132 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001133 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001134 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001135 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1136 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001137 bool ignored;
1138 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001139 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001140 &ignored);
1141 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001142 } else
1143 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001144}
1145
Andrew Trickef9de2a2013-05-25 02:42:55 +00001146SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001147 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001148 bool isTargetGA,
1149 unsigned char TargetFlags) {
1150 assert((TargetFlags == 0 || isTargetGA) &&
1151 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001152 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001153
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001154 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001155 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001156 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001157 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001158
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001159 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1160 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001161 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001162 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov1a114042008-09-09 20:05:04 +00001163 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001164 }
1165
Chris Lattner8e34f982009-06-25 21:21:14 +00001166 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001167 if (GVar && GVar->isThreadLocal())
1168 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1169 else
1170 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001171
Jim Laskeyf576b422006-10-27 23:46:08 +00001172 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001173 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001174 ID.AddPointer(GV);
1175 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001176 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001177 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner3f16b202006-08-11 21:01:22 +00001178 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001179 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001180 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001181
Andrew Trickef9de2a2013-05-25 02:42:55 +00001182 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1183 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001184 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001185 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001186 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001187 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001188}
1189
Owen Anderson53aa7a92009-08-10 22:56:29 +00001190SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001191 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001192 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001193 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001194 ID.AddInteger(FI);
1195 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001196 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001197 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001198
Dan Gohman01c65a22010-03-18 18:49:47 +00001199 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001200 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001201 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001202 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001203}
1204
Owen Anderson53aa7a92009-08-10 22:56:29 +00001205SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001206 unsigned char TargetFlags) {
1207 assert((TargetFlags == 0 || isTarget) &&
1208 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001209 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001210 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001211 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001212 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001213 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001214 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001215 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001216 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001217
Dan Gohman01c65a22010-03-18 18:49:47 +00001218 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1219 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001220 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001221 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001222 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001223}
1224
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001225SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001226 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001227 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001228 unsigned char TargetFlags) {
1229 assert((TargetFlags == 0 || isTarget) &&
1230 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001231 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001232 Alignment =
1233 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001234 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001235 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001236 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001237 ID.AddInteger(Alignment);
1238 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001239 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001240 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001241 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001242 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001243 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001244
Dan Gohman01c65a22010-03-18 18:49:47 +00001245 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1246 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001247 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001248 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001249 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001250}
1251
Chris Lattner061a1ea2005-01-07 07:46:32 +00001252
Owen Anderson53aa7a92009-08-10 22:56:29 +00001253SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001254 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001255 bool isTarget,
1256 unsigned char TargetFlags) {
1257 assert((TargetFlags == 0 || isTarget) &&
1258 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001259 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001260 Alignment =
1261 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001262 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001263 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001264 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001265 ID.AddInteger(Alignment);
1266 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001267 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001268 ID.AddInteger(TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001269 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001270 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001271 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001272
Dan Gohman01c65a22010-03-18 18:49:47 +00001273 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1274 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001275 CSEMap.InsertNode(N, IP);
1276 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001277 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001278}
1279
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001280SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1281 unsigned char TargetFlags) {
1282 FoldingSetNodeID ID;
1283 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1284 ID.AddInteger(Index);
1285 ID.AddInteger(Offset);
1286 ID.AddInteger(TargetFlags);
1287 void *IP = 0;
1288 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1289 return SDValue(E, 0);
1290
1291 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1292 TargetFlags);
1293 CSEMap.InsertNode(N, IP);
1294 AllNodes.push_back(N);
1295 return SDValue(N, 0);
1296}
1297
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001298SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001299 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001300 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001301 ID.AddPointer(MBB);
1302 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001304 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001305
Dan Gohman01c65a22010-03-18 18:49:47 +00001306 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001307 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001308 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001309 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001310}
1311
Owen Anderson53aa7a92009-08-10 22:56:29 +00001312SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001313 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1314 ValueTypeNodes.size())
1315 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001316
Duncan Sands13237ac2008-06-06 12:08:01 +00001317 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001318 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001319
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001320 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001321 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001322 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001323 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001324}
1325
Owen Anderson53aa7a92009-08-10 22:56:29 +00001326SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001327 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001328 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001329 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001330 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001331 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001332}
1333
Owen Anderson53aa7a92009-08-10 22:56:29 +00001334SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001335 unsigned char TargetFlags) {
1336 SDNode *&N =
1337 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1338 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001339 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001340 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001341 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001342 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001343}
1344
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001345SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001346 if ((unsigned)Cond >= CondCodeNodes.size())
1347 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001348
Chris Lattner14e060f2005-08-09 20:40:02 +00001349 if (CondCodeNodes[Cond] == 0) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001350 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001351 CondCodeNodes[Cond] = N;
1352 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001353 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001354
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001355 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001356}
1357
Nate Begeman5f829d82009-04-29 05:20:52 +00001358// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1359// the shuffle mask M that point at N1 to point at N2, and indices that point
1360// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001361static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1362 std::swap(N1, N2);
1363 int NElts = M.size();
1364 for (int i = 0; i != NElts; ++i) {
1365 if (M[i] >= NElts)
1366 M[i] -= NElts;
1367 else if (M[i] >= 0)
1368 M[i] += NElts;
1369 }
1370}
1371
Andrew Trickef9de2a2013-05-25 02:42:55 +00001372SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001373 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001374 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1375 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001376
1377 // Canonicalize shuffle undef, undef -> undef
1378 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001379 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001380
Eric Christopherdfda92b2009-08-22 00:40:45 +00001381 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001382 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001383 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001384 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001385 for (unsigned i = 0; i != NElts; ++i) {
1386 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001387 MaskVec.push_back(Mask[i]);
1388 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001389
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001390 // Canonicalize shuffle v, v -> v, undef
1391 if (N1 == N2) {
1392 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001393 for (unsigned i = 0; i != NElts; ++i)
1394 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001395 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001396
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001397 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1398 if (N1.getOpcode() == ISD::UNDEF)
1399 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001400
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001401 // Canonicalize all index into lhs, -> shuffle lhs, undef
1402 // Canonicalize all index into rhs, -> shuffle rhs, undef
1403 bool AllLHS = true, AllRHS = true;
1404 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001405 for (unsigned i = 0; i != NElts; ++i) {
1406 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001407 if (N2Undef)
1408 MaskVec[i] = -1;
1409 else
1410 AllLHS = false;
1411 } else if (MaskVec[i] >= 0) {
1412 AllRHS = false;
1413 }
1414 }
1415 if (AllLHS && AllRHS)
1416 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001417 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001418 N2 = getUNDEF(VT);
1419 if (AllRHS) {
1420 N1 = getUNDEF(VT);
1421 commuteShuffle(N1, N2, MaskVec);
1422 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001423
Craig Topper9a39b072013-08-08 08:03:12 +00001424 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001425 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001426 for (unsigned i = 0; i != NElts; ++i) {
1427 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001428 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001429 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001430 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001431
1432 FoldingSetNodeID ID;
1433 SDValue Ops[2] = { N1, N2 };
1434 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5f829d82009-04-29 05:20:52 +00001435 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001436 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001437
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001438 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001439 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001440 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001441
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001442 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1443 // SDNode doesn't have access to it. This memory will be "leaked" when
1444 // the node is deallocated, but recovered when the NodeAllocator is released.
1445 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1446 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001447
Dan Gohman01c65a22010-03-18 18:49:47 +00001448 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001449 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1450 dl.getDebugLoc(), N1, N2,
1451 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001452 CSEMap.InsertNode(N, IP);
1453 AllNodes.push_back(N);
1454 return SDValue(N, 0);
1455}
1456
Andrew Trickef9de2a2013-05-25 02:42:55 +00001457SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001458 SDValue Val, SDValue DTy,
1459 SDValue STy, SDValue Rnd, SDValue Sat,
1460 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001461 // If the src and dest types are the same and the conversion is between
1462 // integer types of the same sign or two floats, no conversion is necessary.
1463 if (DTy == STy &&
1464 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001465 return Val;
1466
1467 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001468 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1469 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001470 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001471 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001472 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001473
Jack Carter170a5f22013-09-09 22:02:08 +00001474 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1475 dl.getDebugLoc(),
1476 Ops, 5, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001477 CSEMap.InsertNode(N, IP);
1478 AllNodes.push_back(N);
1479 return SDValue(N, 0);
1480}
1481
Owen Anderson53aa7a92009-08-10 22:56:29 +00001482SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001483 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001484 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001485 ID.AddInteger(RegNo);
1486 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001487 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001488 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001489
Dan Gohman01c65a22010-03-18 18:49:47 +00001490 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001491 CSEMap.InsertNode(N, IP);
1492 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001493 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001494}
1495
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001496SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1497 FoldingSetNodeID ID;
1498 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1499 ID.AddPointer(RegMask);
1500 void *IP = 0;
1501 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1502 return SDValue(E, 0);
1503
1504 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1505 CSEMap.InsertNode(N, IP);
1506 AllNodes.push_back(N);
1507 return SDValue(N, 0);
1508}
1509
Andrew Trickef9de2a2013-05-25 02:42:55 +00001510SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001511 FoldingSetNodeID ID;
1512 SDValue Ops[] = { Root };
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001513 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1514 ID.AddPointer(Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001515 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001516 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001517 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001518
Jack Carter170a5f22013-09-09 22:02:08 +00001519 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1520 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001521 CSEMap.InsertNode(N, IP);
1522 AllNodes.push_back(N);
1523 return SDValue(N, 0);
1524}
1525
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001526
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001527SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001528 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001529 bool isTarget,
1530 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001531 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1532
1533 FoldingSetNodeID ID;
Dan Gohman7a6611792009-11-20 23:18:13 +00001534 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman6c938802009-10-30 01:27:03 +00001535 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001536 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001537 ID.AddInteger(TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001538 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001539 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001540 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001541
Michael Liaoabb87d42012-09-12 21:43:09 +00001542 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1543 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001544 CSEMap.InsertNode(N, IP);
1545 AllNodes.push_back(N);
1546 return SDValue(N, 0);
1547}
1548
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001549SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001550 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001551 "SrcValue is not a pointer?");
1552
Jim Laskeyf576b422006-10-27 23:46:08 +00001553 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001554 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001555 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001556
Chris Lattner3f16b202006-08-11 21:01:22 +00001557 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001559 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001560
Dan Gohman01c65a22010-03-18 18:49:47 +00001561 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001562 CSEMap.InsertNode(N, IP);
1563 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001564 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001565}
1566
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001567/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1568SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1569 FoldingSetNodeID ID;
1570 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1571 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001572
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001573 void *IP = 0;
1574 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1575 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001576
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001577 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1578 CSEMap.InsertNode(N, IP);
1579 AllNodes.push_back(N);
1580 return SDValue(N, 0);
1581}
1582
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001583/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1584SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1585 unsigned SrcAS, unsigned DestAS) {
1586 SDValue Ops[] = {Ptr};
1587 FoldingSetNodeID ID;
1588 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), &Ops[0], 1);
1589 ID.AddInteger(SrcAS);
1590 ID.AddInteger(DestAS);
1591
1592 void *IP = 0;
1593 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1594 return SDValue(E, 0);
1595
1596 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1597 dl.getDebugLoc(),
1598 VT, Ptr, SrcAS, DestAS);
1599 CSEMap.InsertNode(N, IP);
1600 AllNodes.push_back(N);
1601 return SDValue(N, 0);
1602}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001603
Duncan Sands41826032009-01-31 15:50:11 +00001604/// getShiftAmountOperand - Return the specified value casted to
1605/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001606SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001607 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001608 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001609 if (OpTy == ShTy || OpTy.isVector()) return Op;
1610
1611 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001612 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001613}
1614
Chris Lattner9eb7a822007-10-15 17:47:20 +00001615/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1616/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001617SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001618 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001619 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001620 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001621 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001622 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001623 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001624
David Greene1fbe0542009-11-12 20:49:22 +00001625 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001626 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001627}
1628
Duncan Sands445071c2008-12-09 21:33:20 +00001629/// CreateStackTemporary - Create a stack temporary suitable for holding
1630/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001631SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001632 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1633 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001634 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1635 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001636 const TargetLowering *TLI = TM.getTargetLowering();
1637 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001638 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1639 TD->getPrefTypeAlignment(Ty2));
1640
1641 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001642 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001643 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001644}
1645
Owen Anderson53aa7a92009-08-10 22:56:29 +00001646SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001647 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001648 // These setcc operations always fold.
1649 switch (Cond) {
1650 default: break;
1651 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001652 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001653 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001654 case ISD::SETTRUE2: {
1655 const TargetLowering *TLI = TM.getTargetLowering();
1656 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1657 return getConstant(
1658 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1659 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001660
Chris Lattner393d96a2006-04-27 05:01:07 +00001661 case ISD::SETOEQ:
1662 case ISD::SETOGT:
1663 case ISD::SETOGE:
1664 case ISD::SETOLT:
1665 case ISD::SETOLE:
1666 case ISD::SETONE:
1667 case ISD::SETO:
1668 case ISD::SETUO:
1669 case ISD::SETUEQ:
1670 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001671 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001672 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001673 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001674
Gabor Greiff304a7a2008-08-28 21:40:38 +00001675 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001676 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001677 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001678 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001679
Chris Lattner061a1ea2005-01-07 07:46:32 +00001680 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001681 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001682 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1683 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001684 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1685 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1686 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1687 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1688 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1689 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1690 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1691 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001692 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001693 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001694 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001695 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1696 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001697 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001698 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001699 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001700 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001701 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001702 // fall through
1703 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001704 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001705 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001706 // fall through
1707 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001708 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001709 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001710 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001711 // fall through
1712 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001713 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001714 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001715 // fall through
1716 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001717 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001718 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001719 // fall through
1720 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001721 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001722 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001723 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001724 // fall through
1725 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001726 R==APFloat::cmpEqual, VT);
1727 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1728 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1729 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1730 R==APFloat::cmpEqual, VT);
1731 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1732 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1733 R==APFloat::cmpLessThan, VT);
1734 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1735 R==APFloat::cmpUnordered, VT);
1736 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1737 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001738 }
1739 } else {
1740 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001741 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1742 MVT CompVT = N1.getValueType().getSimpleVT();
1743 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1744 return SDValue();
1745
1746 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001747 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001748 }
1749
Chris Lattnerd47675e2005-08-09 20:20:18 +00001750 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001751 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001752}
1753
Dan Gohman1f372ed2008-02-25 21:11:39 +00001754/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1755/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001756bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001757 // This predicate is not safe for vector operations.
1758 if (Op.getValueType().isVector())
1759 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001760
Dan Gohman1d459e42009-12-11 21:31:27 +00001761 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001762 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1763}
1764
Dan Gohman309d3d52007-06-22 14:59:07 +00001765/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1766/// this predicate to simplify operations downstream. Mask is known to be zero
1767/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001768bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001769 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001770 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001771 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001772 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001773 return (KnownZero & Mask) == Mask;
1774}
1775
1776/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1777/// known to be either zero or one and return them in the KnownZero/KnownOne
1778/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1779/// processing.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001780void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1781 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001782 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001783 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001784
Dan Gohmanf990faf2008-02-13 00:35:47 +00001785 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001786 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001787 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001788
Dan Gohmanf990faf2008-02-13 00:35:47 +00001789 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001790
1791 switch (Op.getOpcode()) {
1792 case ISD::Constant:
1793 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001794 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1795 KnownZero = ~KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00001796 return;
1797 case ISD::AND:
1798 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001799 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1800 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001801 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1802 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001803
1804 // Output known-1 bits are only known if set in both the LHS & RHS.
1805 KnownOne &= KnownOne2;
1806 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1807 KnownZero |= KnownZero2;
1808 return;
1809 case ISD::OR:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001810 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1811 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001812 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1813 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1814
Dan Gohman309d3d52007-06-22 14:59:07 +00001815 // Output known-0 bits are only known if clear in both the LHS & RHS.
1816 KnownZero &= KnownZero2;
1817 // Output known-1 are known to be set if set in either the LHS | RHS.
1818 KnownOne |= KnownOne2;
1819 return;
1820 case ISD::XOR: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001821 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1822 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001823 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1824 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1825
Dan Gohman309d3d52007-06-22 14:59:07 +00001826 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001827 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001828 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1829 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1830 KnownZero = KnownZeroOut;
1831 return;
1832 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001833 case ISD::MUL: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001834 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1835 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001836 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1837 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1838
1839 // If low bits are zero in either operand, output low known-0 bits.
1840 // Also compute a conserative estimate for high known-0 bits.
1841 // More trickiness is possible, but this is sufficient for the
1842 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001843 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001844 unsigned TrailZ = KnownZero.countTrailingOnes() +
1845 KnownZero2.countTrailingOnes();
1846 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001847 KnownZero2.countLeadingOnes(),
1848 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001849
1850 TrailZ = std::min(TrailZ, BitWidth);
1851 LeadZ = std::min(LeadZ, BitWidth);
1852 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1853 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001854 return;
1855 }
1856 case ISD::UDIV: {
1857 // For the purposes of computing leading zeros we can conservatively
1858 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001859 // be less than the denominator.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001860 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001861 unsigned LeadZ = KnownZero2.countLeadingOnes();
1862
Jay Foad25a5e4c2010-12-01 08:53:58 +00001863 KnownOne2.clearAllBits();
1864 KnownZero2.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001865 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001866 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1867 if (RHSUnknownLeadingOnes != BitWidth)
1868 LeadZ = std::min(BitWidth,
1869 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001870
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001871 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001872 return;
1873 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001874 case ISD::SELECT:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001875 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1876 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001877 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1878 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1879
Dan Gohman309d3d52007-06-22 14:59:07 +00001880 // Only known if known in both the LHS and RHS.
1881 KnownOne &= KnownOne2;
1882 KnownZero &= KnownZero2;
1883 return;
1884 case ISD::SELECT_CC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001885 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1886 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001887 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1888 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1889
Dan Gohman309d3d52007-06-22 14:59:07 +00001890 // Only known if known in both the LHS and RHS.
1891 KnownOne &= KnownOne2;
1892 KnownZero &= KnownZero2;
1893 return;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001894 case ISD::SADDO:
1895 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001896 case ISD::SSUBO:
1897 case ISD::USUBO:
1898 case ISD::SMULO:
1899 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001900 if (Op.getResNo() != 1)
1901 return;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001902 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001903 case ISD::SETCC:
1904 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001905 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001906 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001907 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001908 return;
1909 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001910 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001911 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001912 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001913
1914 // If the shift count is an invalid immediate, don't do anything.
1915 if (ShAmt >= BitWidth)
1916 return;
1917
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001918 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001919 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman9db0aa82008-02-26 18:50:50 +00001920 KnownZero <<= ShAmt;
1921 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00001922 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00001923 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001924 }
1925 return;
1926 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001927 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001928 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001929 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001930
Dan Gohman9db0aa82008-02-26 18:50:50 +00001931 // If the shift count is an invalid immediate, don't do anything.
1932 if (ShAmt >= BitWidth)
1933 return;
1934
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001935 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001936 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001937 KnownZero = KnownZero.lshr(ShAmt);
1938 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001939
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001940 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001941 KnownZero |= HighBits; // High bits known zero.
1942 }
1943 return;
1944 case ISD::SRA:
1945 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001946 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001947
Dan Gohman9db0aa82008-02-26 18:50:50 +00001948 // If the shift count is an invalid immediate, don't do anything.
1949 if (ShAmt >= BitWidth)
1950 return;
1951
Dan Gohman309d3d52007-06-22 14:59:07 +00001952 // If any of the demanded bits are produced by the sign extension, we also
1953 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001954 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001955
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001956 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001957 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001958 KnownZero = KnownZero.lshr(ShAmt);
1959 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001960
Dan Gohman309d3d52007-06-22 14:59:07 +00001961 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001962 APInt SignBit = APInt::getSignBit(BitWidth);
1963 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001964
Dan Gohmanb717fda2008-02-20 16:30:17 +00001965 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001966 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001967 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001968 KnownOne |= HighBits; // New bits are known one.
1969 }
1970 }
1971 return;
1972 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001973 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001974 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001975
1976 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00001977 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001978 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00001979
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001980 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001981 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001982
Dan Gohman309d3d52007-06-22 14:59:07 +00001983 // If the sign extended bits are demanded, we know that the sign
1984 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00001985 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001986 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00001987 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001988
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001989 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1990 KnownOne &= InputDemandedBits;
1991 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001992 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1993
Dan Gohman309d3d52007-06-22 14:59:07 +00001994 // If the sign bit of the input is known set or clear, then we know the
1995 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001996 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00001997 KnownZero |= NewBits;
1998 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00001999 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00002000 KnownOne |= NewBits;
2001 KnownZero &= ~NewBits;
2002 } else { // Input sign bit unknown
2003 KnownZero &= ~NewBits;
2004 KnownOne &= ~NewBits;
2005 }
2006 return;
2007 }
2008 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002009 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002010 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002011 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002012 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002013 unsigned LowBits = Log2_32(BitWidth)+1;
2014 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002015 KnownOne.clearAllBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002016 return;
2017 }
2018 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002019 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002020 // If this is a ZEXTLoad and we are looking at the loaded value.
2021 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002022 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002023 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002024 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002025 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002026 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002027 }
2028 return;
2029 }
2030 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002031 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002032 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002033 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002034 KnownZero = KnownZero.trunc(InBits);
2035 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002036 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002037 KnownZero = KnownZero.zext(BitWidth);
2038 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002039 KnownZero |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002040 return;
2041 }
2042 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002043 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002044 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002045 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002046
Jay Foad583abbc2010-12-07 08:25:19 +00002047 KnownZero = KnownZero.trunc(InBits);
2048 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002049 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002050
2051 // Note if the sign bit is known to be zero or one.
2052 bool SignBitKnownZero = KnownZero.isNegative();
2053 bool SignBitKnownOne = KnownOne.isNegative();
2054 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2055 "Sign bit can't be known to be both zero and one!");
2056
Jay Foad583abbc2010-12-07 08:25:19 +00002057 KnownZero = KnownZero.zext(BitWidth);
2058 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002059
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002060 // If the sign bit is known zero or one, the top bits match.
2061 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002062 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002063 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002064 KnownOne |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002065 return;
2066 }
2067 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002068 EVT InVT = Op.getOperand(0).getValueType();
2069 unsigned InBits = InVT.getScalarType().getSizeInBits();
2070 KnownZero = KnownZero.trunc(InBits);
2071 KnownOne = KnownOne.trunc(InBits);
2072 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2073 KnownZero = KnownZero.zext(BitWidth);
2074 KnownOne = KnownOne.zext(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002075 return;
2076 }
2077 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002078 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002079 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002080 KnownZero = KnownZero.zext(InBits);
2081 KnownOne = KnownOne.zext(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002082 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002083 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +00002084 KnownZero = KnownZero.trunc(BitWidth);
2085 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002086 break;
2087 }
2088 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002089 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002090 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002091 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2092 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002093 KnownOne &= (~KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002094 return;
2095 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002096 case ISD::FGETSIGN:
2097 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002098 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerafc8f132007-12-22 21:26:52 +00002099 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002100
Dan Gohman72ec3f42008-04-28 17:02:21 +00002101 case ISD::SUB: {
2102 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2103 // We know that the top bits of C-X are clear if X contains less bits
2104 // than C (i.e. no wrap-around can happen). For example, 20-X is
2105 // positive if we can prove that X is >= 0 and < 16.
2106 if (CLHS->getAPIntValue().isNonNegative()) {
2107 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2108 // NLZ can't be BitWidth with no sign bit
2109 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002110 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002111
2112 // If all of the MaskV bits are known to be zero, then we know the
2113 // output top bits are zero, because we now know that the output is
2114 // from [0-C].
2115 if ((KnownZero2 & MaskV) == MaskV) {
2116 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2117 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002118 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002119 }
2120 }
2121 }
2122 }
2123 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002124 case ISD::ADD:
2125 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002126 // Output known-0 bits are known if clear or set in both the low clear bits
2127 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2128 // low 3 bits clear.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002129 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002130 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002131 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2132
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002133 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002134 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002135 KnownZeroOut = std::min(KnownZeroOut,
2136 KnownZero2.countTrailingOnes());
2137
Chris Lattner440b2802010-12-19 20:38:28 +00002138 if (Op.getOpcode() == ISD::ADD) {
2139 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2140 return;
2141 }
2142
2143 // With ADDE, a carry bit may be added in, so we can only use this
2144 // information if we know (at least) that the low two bits are clear. We
2145 // then return to the caller that the low bit is unknown but that other bits
2146 // are known zero.
2147 if (KnownZeroOut >= 2) // ADDE
2148 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohman309d3d52007-06-22 14:59:07 +00002149 return;
2150 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002151 case ISD::SREM:
2152 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002153 const APInt &RA = Rem->getAPIntValue().abs();
2154 if (RA.isPowerOf2()) {
2155 APInt LowBits = RA - 1;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002156 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002157
Duncan Sands33274982010-01-29 09:45:26 +00002158 // The low bits of the first operand are unchanged by the srem.
2159 KnownZero = KnownZero2 & LowBits;
2160 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002161
Duncan Sands33274982010-01-29 09:45:26 +00002162 // If the first operand is non-negative or has all low bits zero, then
2163 // the upper bits are all zero.
2164 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2165 KnownZero |= ~LowBits;
2166
2167 // If the first operand is negative and not all low bits are zero, then
2168 // the upper bits are all one.
2169 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2170 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002171 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002172 }
2173 }
2174 return;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002175 case ISD::UREM: {
2176 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002177 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002178 if (RA.isPowerOf2()) {
2179 APInt LowBits = (RA - 1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002180 KnownZero |= ~LowBits;
2181 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002182 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2183 break;
2184 }
2185 }
2186
2187 // Since the result is less than or equal to either operand, any leading
2188 // zero bits in either operand must also exist in the result.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002189 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2190 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002191
2192 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2193 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002194 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002195 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002196 return;
Dan Gohman309d3d52007-06-22 14:59:07 +00002197 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002198 case ISD::FrameIndex:
2199 case ISD::TargetFrameIndex:
2200 if (unsigned Align = InferPtrAlignment(Op)) {
2201 // The low bits are known zero if the pointer is aligned.
2202 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2203 return;
2204 }
2205 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002206
Dan Gohman309d3d52007-06-22 14:59:07 +00002207 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002208 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2209 break;
2210 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002211 case ISD::INTRINSIC_WO_CHAIN:
2212 case ISD::INTRINSIC_W_CHAIN:
2213 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002214 // Allow the target to implement this method for its nodes.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002215 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002216 return;
2217 }
2218}
2219
2220/// ComputeNumSignBits - Return the number of times the sign bit of the
2221/// register is replicated into the other bits. We know that at least 1 bit
2222/// is always equal to the sign bit (itself), but other cases can give us
2223/// information. For example, immediately after an "SRA X, 2", we know that
2224/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002225unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002226 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002227 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002228 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002229 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002230 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002231 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002232
Dan Gohman309d3d52007-06-22 14:59:07 +00002233 if (Depth == 6)
2234 return 1; // Limit search depth.
2235
2236 switch (Op.getOpcode()) {
2237 default: break;
2238 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002239 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002240 return VTBits-Tmp+1;
2241 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002242 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002243 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002244
Dan Gohman309d3d52007-06-22 14:59:07 +00002245 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002246 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002247 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002248 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002249
Dan Gohman309d3d52007-06-22 14:59:07 +00002250 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002251 Tmp =
2252 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002253 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002254
Dan Gohman309d3d52007-06-22 14:59:07 +00002255 case ISD::SIGN_EXTEND_INREG:
2256 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002257 Tmp =
2258 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002259 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002260
Dan Gohman309d3d52007-06-22 14:59:07 +00002261 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2262 return std::max(Tmp, Tmp2);
2263
2264 case ISD::SRA:
2265 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2266 // SRA X, C -> adds C sign bits.
2267 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002268 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002269 if (Tmp > VTBits) Tmp = VTBits;
2270 }
2271 return Tmp;
2272 case ISD::SHL:
2273 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2274 // shl destroys sign bits.
2275 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002276 if (C->getZExtValue() >= VTBits || // Bad shift.
2277 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2278 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002279 }
2280 break;
2281 case ISD::AND:
2282 case ISD::OR:
2283 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002284 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002285 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002286 if (Tmp != 1) {
2287 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2288 FirstAnswer = std::min(Tmp, Tmp2);
2289 // We computed what we know about the sign bits as our first
2290 // answer. Now proceed to the generic code that uses
2291 // ComputeMaskedBits, and pick whichever answer is better.
2292 }
2293 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002294
2295 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002296 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002297 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002298 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002299 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002300
2301 case ISD::SADDO:
2302 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002303 case ISD::SSUBO:
2304 case ISD::USUBO:
2305 case ISD::SMULO:
2306 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002307 if (Op.getResNo() != 1)
2308 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002309 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002310 case ISD::SETCC:
2311 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002312 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002313 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002314 return VTBits;
2315 break;
2316 case ISD::ROTL:
2317 case ISD::ROTR:
2318 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002319 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002320
Dan Gohman309d3d52007-06-22 14:59:07 +00002321 // Handle rotate right by N like a rotate left by 32-N.
2322 if (Op.getOpcode() == ISD::ROTR)
2323 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2324
2325 // If we aren't rotating out all of the known-in sign bits, return the
2326 // number that are left. This handles rotl(sext(x), 1) for example.
2327 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2328 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2329 }
2330 break;
2331 case ISD::ADD:
2332 // Add can have at most one carry bit. Thus we know that the output
2333 // is, at worst, one more bit than the inputs.
2334 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2335 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002336
Dan Gohman309d3d52007-06-22 14:59:07 +00002337 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002338 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002339 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002340 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002341 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002342
Dan Gohman309d3d52007-06-22 14:59:07 +00002343 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2344 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002345 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002346 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002347
Dan Gohman309d3d52007-06-22 14:59:07 +00002348 // If we are subtracting one from a positive number, there is no carry
2349 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002350 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002351 return Tmp;
2352 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002353
Dan Gohman309d3d52007-06-22 14:59:07 +00002354 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2355 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002356 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002357
Dan Gohman309d3d52007-06-22 14:59:07 +00002358 case ISD::SUB:
2359 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2360 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002361
Dan Gohman309d3d52007-06-22 14:59:07 +00002362 // Handle NEG.
2363 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002364 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002365 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002366 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002367 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2368 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002369 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002370 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002371
Dan Gohman309d3d52007-06-22 14:59:07 +00002372 // If the input is known to be positive (the sign bit is known clear),
2373 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002374 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002375 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002376
Dan Gohman309d3d52007-06-22 14:59:07 +00002377 // Otherwise, we treat this like a SUB.
2378 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002379
Dan Gohman309d3d52007-06-22 14:59:07 +00002380 // Sub can have at most one carry bit. Thus we know that the output
2381 // is, at worst, one more bit than the inputs.
2382 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2383 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002384 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002385 case ISD::TRUNCATE:
2386 // FIXME: it's tricky to do anything useful for this, but it is an important
2387 // case for targets like X86.
2388 break;
2389 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002390
Nadav Rotem4536d582013-03-20 22:53:44 +00002391 // If we are looking at the loaded value of the SDNode.
2392 if (Op.getResNo() == 0) {
2393 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2394 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2395 unsigned ExtType = LD->getExtensionType();
2396 switch (ExtType) {
2397 default: break;
2398 case ISD::SEXTLOAD: // '17' bits known
2399 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2400 return VTBits-Tmp+1;
2401 case ISD::ZEXTLOAD: // '16' bits known
2402 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2403 return VTBits-Tmp;
2404 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002405 }
2406 }
2407
2408 // Allow the target to implement this method for its nodes.
2409 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002410 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002411 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2412 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002413 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002414 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002415 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002416
Dan Gohman309d3d52007-06-22 14:59:07 +00002417 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2418 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002419 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002420 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002421
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002422 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002423 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002424 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002425 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002426 Mask = KnownOne;
2427 } else {
2428 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002429 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002430 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002431
Dan Gohman309d3d52007-06-22 14:59:07 +00002432 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2433 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002434 Mask = ~Mask;
2435 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002436 // Return # leading zeros. We use 'min' here in case Val was zero before
2437 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002438 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002439}
2440
Chris Lattner46c01a32011-02-13 22:25:43 +00002441/// isBaseWithConstantOffset - Return true if the specified operand is an
2442/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2443/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2444/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002445/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002446bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2447 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2448 !isa<ConstantSDNode>(Op.getOperand(1)))
2449 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002450
2451 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002452 !MaskedValueIsZero(Op.getOperand(0),
2453 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2454 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002455
Chris Lattner46c01a32011-02-13 22:25:43 +00002456 return true;
2457}
2458
2459
Dan Gohmand0d5e682009-09-03 20:34:31 +00002460bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2461 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002462 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002463 return true;
2464
2465 // If the value is a constant, we can obviously see if it is a NaN or not.
2466 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2467 return !C->getValueAPF().isNaN();
2468
2469 // TODO: Recognize more cases here.
2470
2471 return false;
2472}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002473
Dan Gohman38605212010-02-24 06:52:40 +00002474bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2475 // If the value is a constant, we can obviously see if it is a zero or not.
2476 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2477 return !C->isZero();
2478
2479 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002480 switch (Op.getOpcode()) {
2481 default: break;
2482 case ISD::OR:
2483 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2484 return !C->isNullValue();
2485 break;
2486 }
Dan Gohman38605212010-02-24 06:52:40 +00002487
2488 return false;
2489}
2490
2491bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2492 // Check the obvious case.
2493 if (A == B) return true;
2494
2495 // For for negative and positive zero.
2496 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2497 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2498 if (CA->isZero() && CB->isZero()) return true;
2499
2500 // Otherwise they may not be equal.
2501 return false;
2502}
2503
Chris Lattner061a1ea2005-01-07 07:46:32 +00002504/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002505///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002506SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002507 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00002508 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattnerfcb16472006-08-11 18:38:11 +00002509 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002511 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002512
Jack Carter170a5f22013-09-09 22:02:08 +00002513 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2514 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002515 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002516
Chris Lattnerfcb16472006-08-11 18:38:11 +00002517 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002518#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002519 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002520#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002521 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002522}
2523
Andrew Trickef9de2a2013-05-25 02:42:55 +00002524SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002525 EVT VT, SDValue Operand) {
Chris Lattnera1874602005-12-23 05:30:37 +00002526 // Constant fold unary operations with an integer constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002527 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002528 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002529 switch (Opcode) {
2530 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002531 case ISD::SIGN_EXTEND:
Jay Foad583abbc2010-12-07 08:25:19 +00002532 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner8c393c22005-09-02 00:17:32 +00002533 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002534 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002535 case ISD::TRUNCATE:
Jay Foad583abbc2010-12-07 08:25:19 +00002536 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002537 case ISD::UINT_TO_FP:
2538 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002539 APFloat apf(EVTToAPFloatSemantics(VT),
2540 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002541 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002542 Opcode==ISD::SINT_TO_FP,
2543 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002544 return getConstantFP(apf, VT);
2545 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002546 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002547 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002548 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002549 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002550 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002551 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002552 case ISD::BSWAP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002553 return getConstant(Val.byteSwap(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002554 case ISD::CTPOP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002555 return getConstant(Val.countPopulation(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002556 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002557 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002558 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002559 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002560 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002561 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002562 }
2563 }
2564
Dale Johannesen446b9002007-08-31 23:34:27 +00002565 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002566 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002567 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002568 switch (Opcode) {
2569 case ISD::FNEG:
2570 V.changeSign();
2571 return getConstantFP(V, VT);
2572 case ISD::FABS:
2573 V.clearSign();
2574 return getConstantFP(V, VT);
2575 case ISD::FCEIL: {
2576 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
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::FTRUNC: {
2582 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
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::FFLOOR: {
2588 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2589 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002590 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002591 break;
2592 }
2593 case ISD::FP_EXTEND: {
2594 bool ignored;
2595 // This can return overflow, underflow, or inexact; we don't care.
2596 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002597 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002598 APFloat::rmNearestTiesToEven, &ignored);
2599 return getConstantFP(V, VT);
2600 }
2601 case ISD::FP_TO_SINT:
2602 case ISD::FP_TO_UINT: {
2603 integerPart x[2];
2604 bool ignored;
2605 assert(integerPartWidth >= 64);
2606 // FIXME need to be more flexible about rounding mode.
2607 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2608 Opcode==ISD::FP_TO_SINT,
2609 APFloat::rmTowardZero, &ignored);
2610 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002611 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002612 APInt api(VT.getSizeInBits(), x);
2613 return getConstant(api, VT);
2614 }
2615 case ISD::BITCAST:
2616 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2617 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2618 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2619 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2620 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002621 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002622 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002623
Gabor Greiff304a7a2008-08-28 21:40:38 +00002624 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002625 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002626 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002627 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002628 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002629 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002630 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002631 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002632 assert(VT.isFloatingPoint() &&
2633 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002634 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002635 assert((!VT.isVector() ||
2636 VT.getVectorNumElements() ==
2637 Operand.getValueType().getVectorNumElements()) &&
2638 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002639 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002640 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002641 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002642 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002643 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002644 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002645 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002646 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2647 "Invalid sext node, dst < src!");
2648 assert((!VT.isVector() ||
2649 VT.getVectorNumElements() ==
2650 Operand.getValueType().getVectorNumElements()) &&
2651 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002652 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2653 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2654 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002655 // sext(undef) = 0, because the top bits will all be the same.
2656 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002657 break;
2658 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002659 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002660 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002661 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002662 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2663 "Invalid zext node, dst < src!");
2664 assert((!VT.isVector() ||
2665 VT.getVectorNumElements() ==
2666 Operand.getValueType().getVectorNumElements()) &&
2667 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002668 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002669 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002670 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002671 else if (OpOpcode == ISD::UNDEF)
2672 // zext(undef) = 0, because the top bits will be zero.
2673 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002674 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002675 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002676 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002677 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002678 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002679 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2680 "Invalid anyext node, dst < src!");
2681 assert((!VT.isVector() ||
2682 VT.getVectorNumElements() ==
2683 Operand.getValueType().getVectorNumElements()) &&
2684 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002685
Dan Gohman08837892010-06-18 00:08:30 +00002686 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2687 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002688 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002689 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002690 else if (OpOpcode == ISD::UNDEF)
2691 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002692
2693 // (ext (trunx x)) -> x
2694 if (OpOpcode == ISD::TRUNCATE) {
2695 SDValue OpOp = Operand.getNode()->getOperand(0);
2696 if (OpOp.getValueType() == VT)
2697 return OpOp;
2698 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002699 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002700 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002701 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002702 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002703 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002704 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2705 "Invalid truncate node, src < dst!");
2706 assert((!VT.isVector() ||
2707 VT.getVectorNumElements() ==
2708 Operand.getValueType().getVectorNumElements()) &&
2709 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002710 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002711 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002712 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2713 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002714 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002715 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2716 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002717 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002718 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002719 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002720 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002721 }
Craig Topper201c1a32012-01-15 01:05:11 +00002722 if (OpOpcode == ISD::UNDEF)
2723 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002724 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002725 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002726 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002727 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002728 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002729 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002730 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2731 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002732 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002733 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002734 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002735 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002736 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002737 (VT.getVectorElementType() == Operand.getValueType() ||
2738 (VT.getVectorElementType().isInteger() &&
2739 Operand.getValueType().isInteger() &&
2740 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002741 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002742 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002743 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002744 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2745 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2746 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2747 Operand.getConstantOperandVal(1) == 0 &&
2748 Operand.getOperand(0).getValueType() == VT)
2749 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002750 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002751 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002752 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002753 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002754 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002755 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002756 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002757 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002758 break;
2759 case ISD::FABS:
2760 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002761 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002762 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002763 }
2764
Chris Lattnerf9c19152005-08-25 19:12:10 +00002765 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002766 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002767 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002768 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002769 SDValue Ops[1] = { Operand };
Chris Lattnerf17b4222007-02-04 07:28:00 +00002770 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002771 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002772 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002773 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002774
Jack Carter170a5f22013-09-09 22:02:08 +00002775 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2776 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002777 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002778 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002779 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2780 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002781 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002782
Chris Lattner061a1ea2005-01-07 07:46:32 +00002783 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002784#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002785 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002786#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002787 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002788}
2789
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002790SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2791 SDNode *Cst1, SDNode *Cst2) {
2792 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2793 SmallVector<SDValue, 4> Outputs;
2794 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002795
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002796 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2797 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Juergen Ributzka38b67d02014-01-24 18:23:08 +00002798 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2799 return SDValue();
2800
2801 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002802 // Scalar instruction.
2803 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Juergen Ributzka38b67d02014-01-24 18:23:08 +00002804 else {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002805 // For vectors extract each constant element into Inputs so we can constant
2806 // fold them individually.
2807 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2808 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2809 if (!BV1 || !BV2)
2810 return SDValue();
2811
2812 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2813
2814 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2815 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2816 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2817 if (!V1 || !V2) // Not a constant, bail.
2818 return SDValue();
2819
Juergen Ributzka38b67d02014-01-24 18:23:08 +00002820 if (V1->isOpaque() || V2->isOpaque())
2821 return SDValue();
2822
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002823 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2824 // FIXME: This is valid and could be handled by truncating the APInts.
2825 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2826 return SDValue();
2827
2828 Inputs.push_back(std::make_pair(V1, V2));
2829 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002830 }
2831
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002832 // We have a number of constant values, constant fold them element by element.
2833 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2834 const APInt &C1 = Inputs[I].first->getAPIntValue();
2835 const APInt &C2 = Inputs[I].second->getAPIntValue();
2836
2837 switch (Opcode) {
2838 case ISD::ADD:
2839 Outputs.push_back(getConstant(C1 + C2, SVT));
2840 break;
2841 case ISD::SUB:
2842 Outputs.push_back(getConstant(C1 - C2, SVT));
2843 break;
2844 case ISD::MUL:
2845 Outputs.push_back(getConstant(C1 * C2, SVT));
2846 break;
2847 case ISD::UDIV:
2848 if (!C2.getBoolValue())
2849 return SDValue();
2850 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2851 break;
2852 case ISD::UREM:
2853 if (!C2.getBoolValue())
2854 return SDValue();
2855 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2856 break;
2857 case ISD::SDIV:
2858 if (!C2.getBoolValue())
2859 return SDValue();
2860 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2861 break;
2862 case ISD::SREM:
2863 if (!C2.getBoolValue())
2864 return SDValue();
2865 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2866 break;
2867 case ISD::AND:
2868 Outputs.push_back(getConstant(C1 & C2, SVT));
2869 break;
2870 case ISD::OR:
2871 Outputs.push_back(getConstant(C1 | C2, SVT));
2872 break;
2873 case ISD::XOR:
2874 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2875 break;
2876 case ISD::SHL:
2877 Outputs.push_back(getConstant(C1 << C2, SVT));
2878 break;
2879 case ISD::SRL:
2880 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2881 break;
2882 case ISD::SRA:
2883 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2884 break;
2885 case ISD::ROTL:
2886 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2887 break;
2888 case ISD::ROTR:
2889 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2890 break;
2891 default:
2892 return SDValue();
2893 }
2894 }
2895
2896 // Handle the scalar case first.
Silviu Baranga4ad2bc52013-04-25 09:32:33 +00002897 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002898 return Outputs.back();
2899
2900 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002901 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002902 Outputs.size());
Bill Wendling162c26d2008-09-24 10:16:24 +00002903}
2904
Andrew Trickef9de2a2013-05-25 02:42:55 +00002905SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002906 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002907 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2908 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002909 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002910 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002911 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002912 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2913 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002914 // Fold trivial token factors.
2915 if (N1.getOpcode() == ISD::EntryToken) return N2;
2916 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002917 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002918 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002919 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002920 // Concat of UNDEFs is UNDEF.
2921 if (N1.getOpcode() == ISD::UNDEF &&
2922 N2.getOpcode() == ISD::UNDEF)
2923 return getUNDEF(VT);
2924
Dan Gohman550c9af2008-08-14 20:04:46 +00002925 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2926 // one big BUILD_VECTOR.
2927 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2928 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002929 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2930 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002931 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00002932 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00002933 }
2934 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002935 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002936 assert(VT.isInteger() && "This operator does not apply to FP types!");
2937 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002938 N1.getValueType() == VT && "Binary operator types must match!");
2939 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2940 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002941 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002942 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002943 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2944 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002945 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002946 case ISD::OR:
2947 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002948 case ISD::ADD:
2949 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002950 assert(VT.isInteger() && "This operator does not apply to FP types!");
2951 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002952 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002953 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2954 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002955 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002956 return N1;
2957 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002958 case ISD::UDIV:
2959 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00002960 case ISD::MULHU:
2961 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002962 case ISD::MUL:
2963 case ISD::SDIV:
2964 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00002965 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002966 assert(N1.getValueType() == N2.getValueType() &&
2967 N1.getValueType() == VT && "Binary operator types must match!");
2968 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002969 case ISD::FADD:
2970 case ISD::FSUB:
2971 case ISD::FMUL:
2972 case ISD::FDIV:
2973 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002974 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00002975 if (Opcode == ISD::FADD) {
2976 // 0+x --> x
2977 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2978 if (CFP->getValueAPF().isZero())
2979 return N2;
2980 // x+0 --> x
2981 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2982 if (CFP->getValueAPF().isZero())
2983 return N1;
2984 } else if (Opcode == ISD::FSUB) {
2985 // x-0 --> x
2986 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2987 if (CFP->getValueAPF().isZero())
2988 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00002989 } else if (Opcode == ISD::FMUL) {
2990 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2991 SDValue V = N2;
2992
2993 // If the first operand isn't the constant, try the second
2994 if (!CFP) {
2995 CFP = dyn_cast<ConstantFPSDNode>(N2);
2996 V = N1;
2997 }
2998
2999 if (CFP) {
3000 // 0*x --> 0
3001 if (CFP->isZero())
3002 return SDValue(CFP,0);
3003 // 1*x --> x
3004 if (CFP->isExactlyValue(1.0))
3005 return V;
3006 }
Dan Gohman1275e282009-01-23 19:10:37 +00003007 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00003008 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00003009 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00003010 assert(N1.getValueType() == N2.getValueType() &&
3011 N1.getValueType() == VT && "Binary operator types must match!");
3012 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003013 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3014 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003015 N1.getValueType().isFloatingPoint() &&
3016 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003017 "Invalid FCOPYSIGN!");
3018 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003019 case ISD::SHL:
3020 case ISD::SRA:
3021 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003022 case ISD::ROTL:
3023 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003024 assert(VT == N1.getValueType() &&
3025 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003026 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003027 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003028 assert((!VT.isVector() || VT == N2.getValueType()) &&
3029 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003030 // Verify that the shift amount VT is bit enough to hold valid shift
3031 // amounts. This catches things like trying to shift an i1024 value by an
3032 // i8, which is easy to fall into in generic code that uses
3033 // TLI.getShiftAmount().
3034 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003035 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003036 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003037
3038 // Always fold shifts of i1 values so the code generator doesn't need to
3039 // handle them. Since we know the size of the shift has to be less than the
3040 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003041 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003042 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003043 if (N2C && N2C->isNullValue())
3044 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003045 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003046 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003047 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003048 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003049 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003050 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003051 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003052 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003053 "type is vector!");
3054 assert((!EVT.isVector() ||
3055 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3056 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003057 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003058 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003059 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003060 break;
3061 }
Chris Lattner72733e52008-01-17 07:00:52 +00003062 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003063 assert(VT.isFloatingPoint() &&
3064 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003065 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003066 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003067 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003068 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003069 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003070 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003071 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003072 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003073 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003074 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003075 assert(!EVT.isVector() &&
3076 "AssertSExt/AssertZExt type should be the vector element type "
3077 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003078 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003079 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003080 break;
3081 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003082 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003083 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003084 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003085 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003086 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003087 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003088 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003089 "type is vector!");
3090 assert((!EVT.isVector() ||
3091 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3092 "Vector element counts must match in SIGN_EXTEND_INREG");
3093 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003094 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003095
Chris Lattner16713612008-01-22 19:09:33 +00003096 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003097 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003098 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003099 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003100 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003101 return getConstant(Val, VT);
3102 }
Chris Lattner16713612008-01-22 19:09:33 +00003103 break;
3104 }
3105 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003106 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3107 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003108 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003109
Chris Lattner16713612008-01-22 19:09:33 +00003110 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3111 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003112 if (N2C &&
3113 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003114 N1.getNumOperands() > 0) {
3115 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003116 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003117 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003118 N1.getOperand(N2C->getZExtValue() / Factor),
3119 getConstant(N2C->getZExtValue() % Factor,
3120 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003121 }
3122
3123 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3124 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003125 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3126 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003127
3128 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003129 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003130 // are promoted and implicitly truncated, and the result implicitly
3131 // extended. Make that explicit here.
3132 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003133
Bob Wilson59dbbb22009-04-13 22:05:19 +00003134 return Elt;
3135 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003136
Chris Lattner16713612008-01-22 19:09:33 +00003137 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3138 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003139 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003140 // If the indices are the same, return the inserted element else
3141 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003142 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003143 SDValue N1Op2 = N1.getOperand(2);
3144 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3145
3146 if (N1Op2C && N2C) {
3147 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3148 if (VT == N1.getOperand(1).getValueType())
3149 return N1.getOperand(1);
3150 else
3151 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3152 }
3153
Dale Johannesendb393622009-02-03 01:55:44 +00003154 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003155 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003156 }
Chris Lattner16713612008-01-22 19:09:33 +00003157 break;
3158 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003159 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003160 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3161 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003162 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003163 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003164
Chris Lattner16713612008-01-22 19:09:33 +00003165 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3166 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003167 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003168 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003169 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003170
Chris Lattner16713612008-01-22 19:09:33 +00003171 // EXTRACT_ELEMENT of a constant int is also very common.
3172 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003173 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003174 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003175 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3176 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003177 }
3178 break;
David Greeneb6f16112011-01-26 15:38:49 +00003179 case ISD::EXTRACT_SUBVECTOR: {
3180 SDValue Index = N2;
3181 if (VT.isSimple() && N1.getValueType().isSimple()) {
3182 assert(VT.isVector() && N1.getValueType().isVector() &&
3183 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003184 assert(VT.getVectorElementType() ==
3185 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003186 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003187 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003188 "Extract subvector must be from larger vector to smaller vector!");
3189
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003190 if (isa<ConstantSDNode>(Index.getNode())) {
3191 assert((VT.getVectorNumElements() +
3192 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003193 <= N1.getValueType().getVectorNumElements())
3194 && "Extract subvector overflow!");
3195 }
3196
3197 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003198 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003199 return N1;
3200 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003201 break;
Chris Lattner16713612008-01-22 19:09:33 +00003202 }
David Greeneb6f16112011-01-26 15:38:49 +00003203 }
Chris Lattner16713612008-01-22 19:09:33 +00003204
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003205 // Perform trivial constant folding.
3206 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3207 if (SV.getNode()) return SV;
3208
3209 // Canonicalize constant to RHS if commutative.
3210 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3211 std::swap(N1C, N2C);
3212 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003213 }
3214
Chris Lattner16713612008-01-22 19:09:33 +00003215 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003216 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3217 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003218 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003219 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003220 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003221 std::swap(N1CFP, N2CFP);
3222 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003223 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003224 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3225 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003226 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003227 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003228 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003229 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003230 return getConstantFP(V1, VT);
3231 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003232 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003233 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3234 if (s!=APFloat::opInvalidOp)
3235 return getConstantFP(V1, VT);
3236 break;
3237 case ISD::FMUL:
3238 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3239 if (s!=APFloat::opInvalidOp)
3240 return getConstantFP(V1, VT);
3241 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003242 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003243 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3244 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3245 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003246 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003247 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003248 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3249 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3250 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003251 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003252 case ISD::FCOPYSIGN:
3253 V1.copySign(V2);
3254 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003255 default: break;
3256 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003257 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003258
3259 if (Opcode == ISD::FP_ROUND) {
3260 APFloat V = N1CFP->getValueAPF(); // make copy
3261 bool ignored;
3262 // This can return overflow, underflow, or inexact; we don't care.
3263 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003264 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003265 APFloat::rmNearestTiesToEven, &ignored);
3266 return getConstantFP(V, VT);
3267 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003268 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003269
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003270 // Canonicalize an UNDEF to the RHS, even over a constant.
3271 if (N1.getOpcode() == ISD::UNDEF) {
3272 if (isCommutativeBinOp(Opcode)) {
3273 std::swap(N1, N2);
3274 } else {
3275 switch (Opcode) {
3276 case ISD::FP_ROUND_INREG:
3277 case ISD::SIGN_EXTEND_INREG:
3278 case ISD::SUB:
3279 case ISD::FSUB:
3280 case ISD::FDIV:
3281 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003282 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003283 return N1; // fold op(undef, arg2) -> undef
3284 case ISD::UDIV:
3285 case ISD::SDIV:
3286 case ISD::UREM:
3287 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003288 case ISD::SRL:
3289 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003290 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003291 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3292 // For vectors, we can't easily build an all zero vector, just return
3293 // the LHS.
3294 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003295 }
3296 }
3297 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003298
3299 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003300 if (N2.getOpcode() == ISD::UNDEF) {
3301 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003302 case ISD::XOR:
3303 if (N1.getOpcode() == ISD::UNDEF)
3304 // Handle undef ^ undef -> 0 special case. This is a common
3305 // idiom (misuse).
3306 return getConstant(0, VT);
3307 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003308 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003309 case ISD::ADDC:
3310 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003311 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003312 case ISD::UDIV:
3313 case ISD::SDIV:
3314 case ISD::UREM:
3315 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003316 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003317 case ISD::FADD:
3318 case ISD::FSUB:
3319 case ISD::FMUL:
3320 case ISD::FDIV:
3321 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003322 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003323 return N2;
3324 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003325 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003326 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003327 case ISD::SRL:
3328 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003329 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003330 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3331 // For vectors, we can't easily build an all zero vector, just return
3332 // the LHS.
3333 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003334 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003335 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003336 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003337 // For vectors, we can't easily build an all one vector, just return
3338 // the LHS.
3339 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003340 case ISD::SRA:
3341 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003342 }
3343 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003344
Chris Lattner724f7ee2005-05-11 18:57:39 +00003345 // Memoize this node if possible.
3346 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003347 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003348 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003349 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003350 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003351 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003352 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003353 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003354 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003355
Jack Carter170a5f22013-09-09 22:02:08 +00003356 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3357 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003358 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003359 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003360 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3361 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003362 }
3363
Chris Lattner061a1ea2005-01-07 07:46:32 +00003364 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003365#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003366 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003367#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003368 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003369}
3370
Andrew Trickef9de2a2013-05-25 02:42:55 +00003371SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003372 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003373 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003374 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003375 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003376 case ISD::FMA: {
3377 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3378 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3379 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3380 if (N1CFP && N2CFP && N3CFP) {
3381 APFloat V1 = N1CFP->getValueAPF();
3382 const APFloat &V2 = N2CFP->getValueAPF();
3383 const APFloat &V3 = N3CFP->getValueAPF();
3384 APFloat::opStatus s =
3385 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3386 if (s != APFloat::opInvalidOp)
3387 return getConstantFP(V1, VT);
3388 }
3389 break;
3390 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003391 case ISD::CONCAT_VECTORS:
3392 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3393 // one big BUILD_VECTOR.
3394 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3395 N2.getOpcode() == ISD::BUILD_VECTOR &&
3396 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003397 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3398 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003399 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3400 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00003401 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00003402 }
3403 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003404 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003405 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003406 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003407 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003408 break;
3409 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003410 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003411 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003412 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003413 return N2; // select true, X, Y -> X
3414 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003415 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003416
3417 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003418 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003419 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003420 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003421 case ISD::INSERT_SUBVECTOR: {
3422 SDValue Index = N3;
3423 if (VT.isSimple() && N1.getValueType().isSimple()
3424 && N2.getValueType().isSimple()) {
3425 assert(VT.isVector() && N1.getValueType().isVector() &&
3426 N2.getValueType().isVector() &&
3427 "Insert subvector VTs must be a vectors");
3428 assert(VT == N1.getValueType() &&
3429 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003430 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003431 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003432 if (isa<ConstantSDNode>(Index.getNode())) {
3433 assert((N2.getValueType().getVectorNumElements() +
3434 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003435 <= VT.getVectorNumElements())
3436 && "Insert subvector overflow!");
3437 }
3438
3439 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003440 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003441 return N2;
3442 }
3443 break;
3444 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003445 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003446 // Fold bit_convert nodes from a type to themselves.
3447 if (N1.getValueType() == VT)
3448 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003449 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003450 }
3451
Chris Lattnerf9c19152005-08-25 19:12:10 +00003452 // Memoize node if it doesn't produce a flag.
3453 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003454 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003455 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003456 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003457 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003458 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003459 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003460 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003461 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003462
Jack Carter170a5f22013-09-09 22:02:08 +00003463 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3464 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003465 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003466 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003467 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3468 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003469 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003470
Chris Lattner061a1ea2005-01-07 07:46:32 +00003471 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003472#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003473 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003474#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003475 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003476}
3477
Andrew Trickef9de2a2013-05-25 02:42:55 +00003478SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003479 SDValue N1, SDValue N2, SDValue N3,
3480 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003481 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003482 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003483}
3484
Andrew Trickef9de2a2013-05-25 02:42:55 +00003485SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003486 SDValue N1, SDValue N2, SDValue N3,
3487 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003488 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003489 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003490}
3491
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003492/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3493/// the incoming stack arguments to be loaded from the stack.
3494SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3495 SmallVector<SDValue, 8> ArgChains;
3496
3497 // Include the original chain at the beginning of the list. When this is
3498 // used by target LowerCall hooks, this helps legalize find the
3499 // CALLSEQ_BEGIN node.
3500 ArgChains.push_back(Chain);
3501
3502 // Add a chain value for each stack argument.
3503 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3504 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3505 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3506 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3507 if (FI->getIndex() < 0)
3508 ArgChains.push_back(SDValue(L, 1));
3509
3510 // Build a tokenfactor for all the chains.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003511 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003512 &ArgChains[0], ArgChains.size());
3513}
3514
Dan Gohman544ab2c2008-04-12 04:36:06 +00003515/// getMemsetValue - Vectorized representation of the memset value
3516/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003517static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003518 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003519 assert(Value.getOpcode() != ISD::UNDEF);
3520
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003521 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003523 assert(C->getAPIntValue().getBitWidth() == 8);
3524 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003525 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003526 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003527 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003528 }
Evan Chengef377ad2008-05-15 08:39:06 +00003529
Dale Johannesenabf66b82009-02-03 22:26:09 +00003530 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003531 if (NumBits > 8) {
3532 // Use a multiplication with 0x010101... to extend the input to the
3533 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003534 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003535 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003536 }
3537
3538 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003539}
3540
Dan Gohman544ab2c2008-04-12 04:36:06 +00003541/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3542/// used when a memcpy is turned into a memset when the source is a constant
3543/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003544static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003545 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003546 // Handle vector with all elements zero.
3547 if (Str.empty()) {
3548 if (VT.isInteger())
3549 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003550 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003551 return DAG.getConstantFP(0.0, VT);
3552 else if (VT.isVector()) {
3553 unsigned NumElts = VT.getVectorNumElements();
3554 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003555 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003556 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3557 EltVT, NumElts)));
3558 } else
3559 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003560 }
3561
Duncan Sands13237ac2008-06-06 12:08:01 +00003562 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003563 unsigned NumVTBits = VT.getSizeInBits();
3564 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003565 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3566
Evan Chengc8444b12013-01-10 22:13:27 +00003567 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003568 if (TLI.isLittleEndian()) {
3569 for (unsigned i = 0; i != NumBytes; ++i)
3570 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3571 } else {
3572 for (unsigned i = 0; i != NumBytes; ++i)
3573 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003574 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003575
Juergen Ributzka38b67d02014-01-24 18:23:08 +00003576 // If the "cost" of materializing the integer immediate is less than the cost
3577 // of a load, then it is cost effective to turn the load into the immediate.
Chandler Carruth42e96112013-01-05 12:32:17 +00003578 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
Juergen Ributzka38b67d02014-01-24 18:23:08 +00003579 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) <
3580 TargetTransformInfo::TCC_Load)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003581 return DAG.getConstant(Val, VT);
3582 return SDValue(0, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003583}
3584
Scott Michelcf0da6c2009-02-17 22:15:04 +00003585/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003586///
Andrew Tricke2431c62013-05-25 03:08:10 +00003587static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003588 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003589 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003590 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003591 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003592}
3593
Evan Chengef377ad2008-05-15 08:39:06 +00003594/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3595///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003596static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003597 unsigned SrcDelta = 0;
3598 GlobalAddressSDNode *G = NULL;
3599 if (Src.getOpcode() == ISD::GlobalAddress)
3600 G = cast<GlobalAddressSDNode>(Src);
3601 else if (Src.getOpcode() == ISD::ADD &&
3602 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3603 Src.getOperand(1).getOpcode() == ISD::Constant) {
3604 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003605 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003606 }
3607 if (!G)
3608 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003609
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003610 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003611}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003612
Evan Cheng43cd9e32010-04-01 06:04:33 +00003613/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3614/// to replace the memset / memcpy. Return true if the number of memory ops
3615/// is below the threshold. It returns the types of the sequence of
3616/// memory ops to perform memset / memcpy by reference.
3617static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003618 unsigned Limit, uint64_t Size,
3619 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003620 bool IsMemset,
3621 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003622 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003623 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003624 SelectionDAG &DAG,
3625 const TargetLowering &TLI) {
3626 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3627 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003628 // If 'SrcAlign' is zero, that means the memory operation does not need to
3629 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3630 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3631 // is the specified alignment of the memory operation. If it is zero, that
3632 // means it's possible to change the alignment of the destination.
3633 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3634 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003635 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003636 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003637 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003638
Chris Lattner28dc6c12010-03-07 07:45:08 +00003639 if (VT == MVT::Other) {
Chandler Carruth5da3f052012-11-01 09:14:31 +00003640 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng43cd9e32010-04-01 06:04:33 +00003641 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003642 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003643 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003644 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003645 case 0: VT = MVT::i64; break;
3646 case 4: VT = MVT::i32; break;
3647 case 2: VT = MVT::i16; break;
3648 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003649 }
3650 }
3651
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003652 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003653 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003654 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003655 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003656
Duncan Sands11dd4242008-06-08 20:54:56 +00003657 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003658 VT = LVT;
3659 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003660
Dan Gohman544ab2c2008-04-12 04:36:06 +00003661 unsigned NumMemOps = 0;
3662 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003663 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003664 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003665 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003666 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003667 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003668
3669 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003670 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003671 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003672 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003673 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003674 Found = true;
3675 else if (NewVT == MVT::i64 &&
3676 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003677 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003678 // i64 is usually not legal on 32-bit targets, but f64 may be.
3679 NewVT = MVT::f64;
3680 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003681 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003682 }
3683
Evan Cheng04e55182012-12-12 00:42:09 +00003684 if (!Found) {
3685 do {
3686 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3687 if (NewVT == MVT::i8)
3688 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003689 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003690 }
3691 NewVTSize = NewVT.getSizeInBits() / 8;
3692
Evan Cheng79e2ca92012-12-10 23:21:26 +00003693 // If the new VT cannot cover all of the remaining bits, then consider
3694 // issuing a (or a pair of) unaligned and overlapping load / store.
3695 // FIXME: Only does this for 64-bit or more since we don't have proper
3696 // cost model for unaligned load / store.
3697 bool Fast;
Evan Chengb7d3d032012-12-12 20:43:23 +00003698 if (NumMemOps && AllowOverlap &&
3699 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00003700 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3701 VTSize = Size;
3702 else {
3703 VT = NewVT;
3704 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003705 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003706 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003707
Evan Chengb7d3d032012-12-12 20:43:23 +00003708 if (++NumMemOps > Limit)
3709 return false;
3710
Dan Gohman544ab2c2008-04-12 04:36:06 +00003711 MemOps.push_back(VT);
3712 Size -= VTSize;
3713 }
3714
3715 return true;
3716}
3717
Andrew Trickef9de2a2013-05-25 02:42:55 +00003718static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003719 SDValue Chain, SDValue Dst,
3720 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003721 unsigned Align, bool isVol,
3722 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003723 MachinePointerInfo DstPtrInfo,
3724 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003725 // Turn a memcpy of undef to nop.
3726 if (Src.getOpcode() == ISD::UNDEF)
3727 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003728
Dan Gohman714663a2008-05-29 19:42:22 +00003729 // Expand memcpy to a series of load and store ops if the size operand falls
3730 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003731 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3732 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003733 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003734 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003735 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003736 MachineFunction &MF = DAG.getMachineFunction();
3737 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003738 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003739 MF.getFunction()->getAttributes().
3740 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003741 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3742 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3743 DstAlignCanChange = true;
3744 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3745 if (Align > SrcAlign)
3746 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003747 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003748 bool CopyFromStr = isMemSrcFromString(Src, Str);
3749 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003750 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003751
Evan Cheng4c014c82010-04-01 18:19:11 +00003752 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003753 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003754 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003755 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003756 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003757
Evan Cheng43cd9e32010-04-01 06:04:33 +00003758 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003759 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003760 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003761
3762 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003763 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003764 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3765 if (!TRI->needsStackRealignment(MF))
3766 while (NewAlign > Align &&
3767 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3768 NewAlign /= 2;
3769
Evan Cheng43cd9e32010-04-01 06:04:33 +00003770 if (NewAlign > Align) {
3771 // Give the stack frame object a larger alignment if needed.
3772 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3773 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3774 Align = NewAlign;
3775 }
3776 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003777
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003778 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003779 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003780 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003781 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003782 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003783 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003784 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003785
Evan Cheng79e2ca92012-12-10 23:21:26 +00003786 if (VTSize > Size) {
3787 // Issuing an unaligned load / store pair that overlaps with the previous
3788 // pair. Adjust the offset accordingly.
3789 assert(i == NumMemOps-1 && i != 0);
3790 SrcOff -= VTSize - Size;
3791 DstOff -= VTSize - Size;
3792 }
3793
Evan Cheng43cd9e32010-04-01 06:04:33 +00003794 if (CopyFromStr &&
3795 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003796 // It's unlikely a store of a vector immediate can be done in a single
3797 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003798 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003799 // FIXME: Handle other cases where store of vector immediate is done in
3800 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003801 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003802 if (Value.getNode())
3803 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003804 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003805 DstPtrInfo.getWithOffset(DstOff), isVol,
3806 false, Align);
3807 }
3808
3809 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003810 // The type might not be legal for the target. This should only happen
3811 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003812 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3813 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003814 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003815 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003816 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003817 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003818 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003819 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003820 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003821 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003822 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003823 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3824 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003825 }
3826 OutChains.push_back(Store);
3827 SrcOff += VTSize;
3828 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003829 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003830 }
3831
Owen Anderson9f944592009-08-11 20:47:22 +00003832 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003833 &OutChains[0], OutChains.size());
3834}
3835
Andrew Trickef9de2a2013-05-25 02:42:55 +00003836static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003837 SDValue Chain, SDValue Dst,
3838 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003839 unsigned Align, bool isVol,
3840 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003841 MachinePointerInfo DstPtrInfo,
3842 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003843 // Turn a memmove of undef to nop.
3844 if (Src.getOpcode() == ISD::UNDEF)
3845 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003846
3847 // Expand memmove to a series of load and store ops if the size operand falls
3848 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003849 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003850 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003851 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003852 MachineFunction &MF = DAG.getMachineFunction();
3853 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003854 bool OptSize = MF.getFunction()->getAttributes().
3855 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003856 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3857 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3858 DstAlignCanChange = true;
3859 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3860 if (Align > SrcAlign)
3861 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003862 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003863
Evan Cheng4c014c82010-04-01 18:19:11 +00003864 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003865 (DstAlignCanChange ? 0 : Align), SrcAlign,
3866 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003867 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003868
Evan Cheng43cd9e32010-04-01 06:04:33 +00003869 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003870 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003871 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003872 if (NewAlign > Align) {
3873 // Give the stack frame object a larger alignment if needed.
3874 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3875 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3876 Align = NewAlign;
3877 }
3878 }
Dan Gohman714663a2008-05-29 19:42:22 +00003879
Evan Cheng43cd9e32010-04-01 06:04:33 +00003880 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003881 SmallVector<SDValue, 8> LoadValues;
3882 SmallVector<SDValue, 8> LoadChains;
3883 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003884 unsigned NumMemOps = MemOps.size();
3885 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003886 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003887 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003888 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00003889
Dale Johannesenabf66b82009-02-03 22:26:09 +00003890 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003891 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003892 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003893 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003894 LoadValues.push_back(Value);
3895 LoadChains.push_back(Value.getValue(1));
3896 SrcOff += VTSize;
3897 }
Owen Anderson9f944592009-08-11 20:47:22 +00003898 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003899 &LoadChains[0], LoadChains.size());
3900 OutChains.clear();
3901 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003902 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003903 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003904 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003905
Dale Johannesenabf66b82009-02-03 22:26:09 +00003906 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003907 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003908 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003909 OutChains.push_back(Store);
3910 DstOff += VTSize;
3911 }
3912
Owen Anderson9f944592009-08-11 20:47:22 +00003913 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003914 &OutChains[0], OutChains.size());
3915}
3916
Serge Pavlov8ec39992013-09-17 16:24:42 +00003917/// \brief Lower the call to 'memset' intrinsic function into a series of store
3918/// operations.
3919///
3920/// \param DAG Selection DAG where lowered code is placed.
3921/// \param dl Link to corresponding IR location.
3922/// \param Chain Control flow dependency.
3923/// \param Dst Pointer to destination memory location.
3924/// \param Src Value of byte to write into the memory.
3925/// \param Size Number of bytes to write.
3926/// \param Align Alignment of the destination in bytes.
3927/// \param isVol True if destination is volatile.
3928/// \param DstPtrInfo IR information on the memory pointer.
3929/// \returns New head in the control flow, if lowering was successful, empty
3930/// SDValue otherwise.
3931///
3932/// The function tries to replace 'llvm.memset' intrinsic with several store
3933/// operations and value calculation code. This is usually profitable for small
3934/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003935static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003936 SDValue Chain, SDValue Dst,
3937 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003938 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003939 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003940 // Turn a memset of undef to nop.
3941 if (Src.getOpcode() == ISD::UNDEF)
3942 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003943
3944 // Expand memset to a series of load/store ops if the size operand
3945 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003946 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003947 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003948 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003949 MachineFunction &MF = DAG.getMachineFunction();
3950 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003951 bool OptSize = MF.getFunction()->getAttributes().
3952 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003953 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3954 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3955 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003956 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003957 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003958 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00003959 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00003960 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003961 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003962
Evan Cheng43cd9e32010-04-01 06:04:33 +00003963 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003964 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003965 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003966 if (NewAlign > Align) {
3967 // Give the stack frame object a larger alignment if needed.
3968 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3969 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3970 Align = NewAlign;
3971 }
3972 }
3973
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003974 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00003975 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003976 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003977
3978 // Find the largest store and generate the bit pattern for it.
3979 EVT LargestVT = MemOps[0];
3980 for (unsigned i = 1; i < NumMemOps; i++)
3981 if (MemOps[i].bitsGT(LargestVT))
3982 LargestVT = MemOps[i];
3983 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3984
Dan Gohman544ab2c2008-04-12 04:36:06 +00003985 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003986 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00003987 unsigned VTSize = VT.getSizeInBits() / 8;
3988 if (VTSize > Size) {
3989 // Issuing an unaligned load / store pair that overlaps with the previous
3990 // pair. Adjust the offset accordingly.
3991 assert(i == NumMemOps-1 && i != 0);
3992 DstOff -= VTSize - Size;
3993 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003994
3995 // If this store is smaller than the largest store see whether we can get
3996 // the smaller value for free with a truncate.
3997 SDValue Value = MemSetValue;
3998 if (VT.bitsLT(LargestVT)) {
3999 if (!LargestVT.isVector() && !VT.isVector() &&
4000 TLI.isTruncateFree(LargestVT, VT))
4001 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4002 else
4003 Value = getMemsetValue(Src, VT, DAG, dl);
4004 }
4005 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004006 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00004007 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00004008 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00004009 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004010 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00004011 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004012 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004013 }
4014
Owen Anderson9f944592009-08-11 20:47:22 +00004015 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00004016 &OutChains[0], OutChains.size());
4017}
4018
Andrew Trickef9de2a2013-05-25 02:42:55 +00004019SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004020 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004021 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004022 MachinePointerInfo DstPtrInfo,
4023 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004024 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004025
4026 // Check to see if we should lower the memcpy to loads and stores first.
4027 // For cases within the target-specified limits, this is the best choice.
4028 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4029 if (ConstantSize) {
4030 // Memcpy with size zero? Just return the original chain.
4031 if (ConstantSize->isNullValue())
4032 return Chain;
4033
Evan Cheng43cd9e32010-04-01 06:04:33 +00004034 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4035 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004036 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004037 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004038 return Result;
4039 }
4040
4041 // Then check to see if we should lower the memcpy with target-specific
4042 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004043 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004044 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004045 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004046 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004047 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004048 return Result;
4049
4050 // If we really need inline code and the target declined to provide it,
4051 // use a (potentially long) sequence of loads and stores.
4052 if (AlwaysInline) {
4053 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004054 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004055 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004056 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004057 }
4058
Dan Gohmanf38547c2010-04-05 20:24:08 +00004059 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4060 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4061 // respect volatile, so they may do things like read or write memory
4062 // beyond the given memory regions. But fixing this isn't easy, and most
4063 // people don't care.
4064
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004065 const TargetLowering *TLI = TM.getTargetLowering();
4066
Dan Gohman544ab2c2008-04-12 04:36:06 +00004067 // Emit a library call.
4068 TargetLowering::ArgListTy Args;
4069 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004070 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004071 Entry.Node = Dst; Args.push_back(Entry);
4072 Entry.Node = Src; Args.push_back(Entry);
4073 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004074 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004075 TargetLowering::
4076 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004077 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004078 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00004079 /*isTailCall=*/false,
4080 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004081 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4082 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004083 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004084 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004085
Dan Gohman544ab2c2008-04-12 04:36:06 +00004086 return CallResult.second;
4087}
4088
Andrew Trickef9de2a2013-05-25 02:42:55 +00004089SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004090 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004091 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004092 MachinePointerInfo DstPtrInfo,
4093 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004094 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004095
Dan Gohman714663a2008-05-29 19:42:22 +00004096 // Check to see if we should lower the memmove to loads and stores first.
4097 // For cases within the target-specified limits, this is the best choice.
4098 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4099 if (ConstantSize) {
4100 // Memmove with size zero? Just return the original chain.
4101 if (ConstantSize->isNullValue())
4102 return Chain;
4103
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004104 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004105 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004106 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004107 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004108 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004109 return Result;
4110 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004111
4112 // Then check to see if we should lower the memmove with target-specific
4113 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004114 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004115 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004116 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004117 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004118 return Result;
4119
Mon P Wangbf862242010-04-06 08:27:51 +00004120 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4121 // not be safe. See memcpy above for more details.
4122
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004123 const TargetLowering *TLI = TM.getTargetLowering();
4124
Dan Gohman544ab2c2008-04-12 04:36:06 +00004125 // Emit a library call.
4126 TargetLowering::ArgListTy Args;
4127 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004128 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004129 Entry.Node = Dst; Args.push_back(Entry);
4130 Entry.Node = Src; Args.push_back(Entry);
4131 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004132 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004133 TargetLowering::
4134 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004135 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004136 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004137 /*isTailCall=*/false,
4138 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004139 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4140 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004141 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004142 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004143
Dan Gohman544ab2c2008-04-12 04:36:06 +00004144 return CallResult.second;
4145}
4146
Andrew Trickef9de2a2013-05-25 02:42:55 +00004147SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004148 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004149 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004150 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004151 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004152
4153 // Check to see if we should lower the memset to stores first.
4154 // For cases within the target-specified limits, this is the best choice.
4155 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4156 if (ConstantSize) {
4157 // Memset with size zero? Just return the original chain.
4158 if (ConstantSize->isNullValue())
4159 return Chain;
4160
Mon P Wangc576ee92010-04-04 03:10:48 +00004161 SDValue Result =
4162 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004163 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004164
Gabor Greiff304a7a2008-08-28 21:40:38 +00004165 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004166 return Result;
4167 }
4168
4169 // Then check to see if we should lower the memset with target-specific
4170 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004171 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004172 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004173 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004174 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004175 return Result;
4176
Wesley Peck527da1b2010-11-23 03:31:01 +00004177 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004178 const TargetLowering *TLI = TM.getTargetLowering();
4179 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004180 TargetLowering::ArgListTy Args;
4181 TargetLowering::ArgListEntry Entry;
4182 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4183 Args.push_back(Entry);
4184 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004185 if (Src.getValueType().bitsGT(MVT::i32))
4186 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004187 else
Owen Anderson9f944592009-08-11 20:47:22 +00004188 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004189 Entry.Node = Src;
4190 Entry.Ty = Type::getInt32Ty(*getContext());
4191 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004192 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004193 Entry.Node = Size;
4194 Entry.Ty = IntPtrTy;
4195 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004196 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004197 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004198 TargetLowering::
4199 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004200 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004201 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004202 /*isTailCall=*/false,
4203 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004204 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4205 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004206 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004207 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004208
Dan Gohman544ab2c2008-04-12 04:36:06 +00004209 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004210}
4211
Andrew Trickef9de2a2013-05-25 02:42:55 +00004212SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004213 SDVTList VTList, SDValue* Ops, unsigned NumOps,
4214 MachineMemOperand *MMO,
4215 AtomicOrdering Ordering,
4216 SynchronizationScope SynchScope) {
4217 FoldingSetNodeID ID;
4218 ID.AddInteger(MemVT.getRawBits());
4219 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4220 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4221 void* IP = 0;
4222 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4223 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4224 return SDValue(E, 0);
4225 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004226
4227 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4228 // SDNode doesn't have access to it. This memory will be "leaked" when
4229 // the node is deallocated, but recovered when the allocator is released.
4230 // If the number of operands is less than 5 we use AtomicSDNode's internal
4231 // storage.
4232 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : 0;
4233
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004234 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4235 dl.getDebugLoc(), VTList, MemVT,
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004236 Ops, DynOps, NumOps, MMO,
4237 Ordering, SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004238 CSEMap.InsertNode(N, IP);
4239 AllNodes.push_back(N);
4240 return SDValue(N, 0);
4241}
4242
4243SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004244 SDValue Chain, SDValue Ptr, SDValue Cmp,
4245 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004246 unsigned Alignment,
4247 AtomicOrdering Ordering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004248 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004249 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4250 Alignment = getEVTAlignment(MemVT);
4251
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004252 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004253
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004254 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004255 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004256 // FIXME: Volatile isn't really correct; we should keep track of atomic
4257 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004258 unsigned Flags = MachineMemOperand::MOVolatile;
4259 if (Opcode != ISD::ATOMIC_STORE)
4260 Flags |= MachineMemOperand::MOLoad;
4261 if (Opcode != ISD::ATOMIC_LOAD)
4262 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004263
4264 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004265 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004266
Eli Friedmanadec5872011-07-29 03:05:32 +00004267 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4268 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004269}
4270
Andrew Trickef9de2a2013-05-25 02:42:55 +00004271SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004272 SDValue Chain,
4273 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004274 SDValue Swp, MachineMemOperand *MMO,
4275 AtomicOrdering Ordering,
4276 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004277 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4278 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4279
Owen Anderson53aa7a92009-08-10 22:56:29 +00004280 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004281
Owen Anderson9f944592009-08-11 20:47:22 +00004282 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004283 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004284 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004285}
4286
Andrew Trickef9de2a2013-05-25 02:42:55 +00004287SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004288 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004289 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004290 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004291 unsigned Alignment,
4292 AtomicOrdering Ordering,
4293 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004294 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4295 Alignment = getEVTAlignment(MemVT);
4296
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004297 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004298 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004299 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004300 // For now, atomics are considered to be volatile always, and they are
4301 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004302 // FIXME: Volatile isn't really correct; we should keep track of atomic
4303 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004304 unsigned Flags = MachineMemOperand::MOVolatile;
4305 if (Opcode != ISD::ATOMIC_STORE)
4306 Flags |= MachineMemOperand::MOLoad;
4307 if (Opcode != ISD::ATOMIC_LOAD)
4308 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004309
4310 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004311 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004312 MemVT.getStoreSize(), Alignment);
4313
Eli Friedmanadec5872011-07-29 03:05:32 +00004314 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4315 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004316}
4317
Andrew Trickef9de2a2013-05-25 02:42:55 +00004318SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004319 SDValue Chain,
4320 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004321 MachineMemOperand *MMO,
4322 AtomicOrdering Ordering,
4323 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004324 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4325 Opcode == ISD::ATOMIC_LOAD_SUB ||
4326 Opcode == ISD::ATOMIC_LOAD_AND ||
4327 Opcode == ISD::ATOMIC_LOAD_OR ||
4328 Opcode == ISD::ATOMIC_LOAD_XOR ||
4329 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004330 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004331 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004332 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004333 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004334 Opcode == ISD::ATOMIC_SWAP ||
4335 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004336 "Invalid Atomic Op");
4337
Owen Anderson53aa7a92009-08-10 22:56:29 +00004338 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004339
Eli Friedman342e8df2011-08-24 20:50:09 +00004340 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4341 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004342 SDValue Ops[] = {Chain, Ptr, Val};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004343 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004344}
4345
Andrew Trickef9de2a2013-05-25 02:42:55 +00004346SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004347 EVT VT, SDValue Chain,
4348 SDValue Ptr,
4349 const Value* PtrVal,
4350 unsigned Alignment,
4351 AtomicOrdering Ordering,
4352 SynchronizationScope SynchScope) {
4353 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4354 Alignment = getEVTAlignment(MemVT);
4355
4356 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004357 // An atomic store does not load. An atomic load does not store.
4358 // (An atomicrmw obviously both loads and stores.)
4359 // For now, atomics are considered to be volatile always, and they are
4360 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004361 // FIXME: Volatile isn't really correct; we should keep track of atomic
4362 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004363 unsigned Flags = MachineMemOperand::MOVolatile;
4364 if (Opcode != ISD::ATOMIC_STORE)
4365 Flags |= MachineMemOperand::MOLoad;
4366 if (Opcode != ISD::ATOMIC_LOAD)
4367 Flags |= MachineMemOperand::MOStore;
Eli Friedman342e8df2011-08-24 20:50:09 +00004368
4369 MachineMemOperand *MMO =
4370 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4371 MemVT.getStoreSize(), Alignment);
4372
4373 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4374 Ordering, SynchScope);
4375}
4376
Andrew Trickef9de2a2013-05-25 02:42:55 +00004377SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004378 EVT VT, SDValue Chain,
4379 SDValue Ptr,
4380 MachineMemOperand *MMO,
4381 AtomicOrdering Ordering,
4382 SynchronizationScope SynchScope) {
4383 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4384
4385 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004386 SDValue Ops[] = {Chain, Ptr};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004387 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004388}
4389
Duncan Sands739a0542008-07-02 17:40:58 +00004390/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesenae7992a2009-02-02 20:47:48 +00004391SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004392 SDLoc dl) {
Dale Johannesenae7992a2009-02-02 20:47:48 +00004393 if (NumOps == 1)
4394 return Ops[0];
4395
Owen Anderson53aa7a92009-08-10 22:56:29 +00004396 SmallVector<EVT, 4> VTs;
Dale Johannesenae7992a2009-02-02 20:47:48 +00004397 VTs.reserve(NumOps);
4398 for (unsigned i = 0; i < NumOps; ++i)
4399 VTs.push_back(Ops[i].getValueType());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004400 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesenae7992a2009-02-02 20:47:48 +00004401 Ops, NumOps);
4402}
4403
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004404SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004405SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004406 const EVT *VTs, unsigned NumVTs,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004407 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004408 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004409 unsigned Align, bool Vol,
4410 bool ReadMem, bool WriteMem) {
4411 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004412 MemVT, PtrInfo, Align, Vol,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004413 ReadMem, WriteMem);
4414}
4415
4416SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004417SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004418 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004419 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004420 unsigned Align, bool Vol,
4421 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004422 if (Align == 0) // Ensure that codegen never sees alignment 0
4423 Align = getEVTAlignment(MemVT);
4424
4425 MachineFunction &MF = getMachineFunction();
4426 unsigned Flags = 0;
4427 if (WriteMem)
4428 Flags |= MachineMemOperand::MOStore;
4429 if (ReadMem)
4430 Flags |= MachineMemOperand::MOLoad;
4431 if (Vol)
4432 Flags |= MachineMemOperand::MOVolatile;
4433 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004434 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004435
4436 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4437}
4438
4439SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004440SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohman48b185d2009-09-25 20:36:54 +00004441 const SDValue *Ops, unsigned NumOps,
4442 EVT MemVT, MachineMemOperand *MMO) {
4443 assert((Opcode == ISD::INTRINSIC_VOID ||
4444 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004445 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004446 Opcode == ISD::LIFETIME_START ||
4447 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004448 (Opcode <= INT_MAX &&
4449 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4450 "Opcode is not a memory-accessing opcode!");
4451
Dale Johannesen839acbb2009-01-29 00:47:48 +00004452 // Memoize the node unless it returns a flag.
4453 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004454 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004455 FoldingSetNodeID ID;
4456 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper91244262012-07-30 20:23:19 +00004457 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004458 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004459 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4460 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004461 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004462 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004463
Jack Carter170a5f22013-09-09 22:02:08 +00004464 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4465 dl.getDebugLoc(), VTList, Ops,
4466 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004467 CSEMap.InsertNode(N, IP);
4468 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004469 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4470 dl.getDebugLoc(), VTList, Ops,
4471 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004472 }
4473 AllNodes.push_back(N);
4474 return SDValue(N, 0);
4475}
4476
Chris Lattnerea952f02010-09-21 17:24:05 +00004477/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4478/// MachinePointerInfo record from it. This is particularly useful because the
4479/// code generator has many cases where it doesn't bother passing in a
4480/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4481static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4482 // If this is FI+Offset, we can model it.
4483 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4484 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4485
4486 // If this is (FI+Offset1)+Offset2, we can model it.
4487 if (Ptr.getOpcode() != ISD::ADD ||
4488 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4489 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4490 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004491
Chris Lattnerea952f02010-09-21 17:24:05 +00004492 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4493 return MachinePointerInfo::getFixedStack(FI, Offset+
4494 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4495}
4496
4497/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4498/// MachinePointerInfo record from it. This is particularly useful because the
4499/// code generator has many cases where it doesn't bother passing in a
4500/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4501static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4502 // If the 'Offset' value isn't a constant, we can't handle this.
4503 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4504 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4505 if (OffsetOp.getOpcode() == ISD::UNDEF)
4506 return InferPointerInfo(Ptr);
4507 return MachinePointerInfo();
4508}
Wesley Peck527da1b2010-11-23 03:31:01 +00004509
Chris Lattnerea952f02010-09-21 17:24:05 +00004510
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004511SDValue
4512SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004513 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004514 SDValue Ptr, SDValue Offset,
4515 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004516 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004517 unsigned Alignment, const MDNode *TBAAInfo,
4518 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004519 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004520 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004521 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4522 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004523
Dan Gohman48b185d2009-09-25 20:36:54 +00004524 unsigned Flags = MachineMemOperand::MOLoad;
4525 if (isVolatile)
4526 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004527 if (isNonTemporal)
4528 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004529 if (isInvariant)
4530 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004531
Chris Lattnerea952f02010-09-21 17:24:05 +00004532 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4533 // clients.
4534 if (PtrInfo.V == 0)
4535 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004536
Chris Lattnerea952f02010-09-21 17:24:05 +00004537 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004538 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004539 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004540 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004541 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004542}
4543
4544SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004545SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004546 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004547 SDValue Ptr, SDValue Offset, EVT MemVT,
4548 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004549 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004550 ExtType = ISD::NON_EXTLOAD;
4551 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004552 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004553 } else {
4554 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004555 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4556 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004557 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004558 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004559 assert(VT.isVector() == MemVT.isVector() &&
4560 "Cannot use trunc store to convert to or from a vector!");
4561 assert((!VT.isVector() ||
4562 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4563 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004564 }
4565
4566 bool Indexed = AM != ISD::UNINDEXED;
4567 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4568 "Unindexed load with an offset!");
4569
4570 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004571 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004572 SDValue Ops[] = { Chain, Ptr, Offset };
4573 FoldingSetNodeID ID;
4574 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman08c0a952009-09-23 21:02:20 +00004575 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004576 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004577 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004578 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004579 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004580 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004581 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4582 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004583 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004584 }
Jack Carter170a5f22013-09-09 22:02:08 +00004585 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4586 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004587 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004588 CSEMap.InsertNode(N, IP);
4589 AllNodes.push_back(N);
4590 return SDValue(N, 0);
4591}
4592
Andrew Trickef9de2a2013-05-25 02:42:55 +00004593SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004594 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004595 MachinePointerInfo PtrInfo,
4596 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004597 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004598 const MDNode *TBAAInfo,
4599 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004600 SDValue Undef = getUNDEF(Ptr.getValueType());
4601 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004602 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4603 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004604}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004605
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004606SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4607 SDValue Chain, SDValue Ptr,
4608 MachineMemOperand *MMO) {
4609 SDValue Undef = getUNDEF(Ptr.getValueType());
4610 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4611 VT, MMO);
4612}
4613
Andrew Trickef9de2a2013-05-25 02:42:55 +00004614SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004615 SDValue Chain, SDValue Ptr,
4616 MachinePointerInfo PtrInfo, EVT MemVT,
4617 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004618 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004619 SDValue Undef = getUNDEF(Ptr.getValueType());
4620 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004621 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004622 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004623}
4624
4625
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004626SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4627 SDValue Chain, SDValue Ptr, EVT MemVT,
4628 MachineMemOperand *MMO) {
4629 SDValue Undef = getUNDEF(Ptr.getValueType());
4630 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4631 MemVT, MMO);
4632}
4633
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004634SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004635SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004636 SDValue Offset, ISD::MemIndexedMode AM) {
4637 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4638 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4639 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004640 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004641 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004642 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004643 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004644}
4645
Andrew Trickef9de2a2013-05-25 02:42:55 +00004646SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004647 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004648 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004649 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004650 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004651 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004652 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004653 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004654
Dan Gohman48b185d2009-09-25 20:36:54 +00004655 unsigned Flags = MachineMemOperand::MOStore;
4656 if (isVolatile)
4657 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004658 if (isNonTemporal)
4659 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004660
Chris Lattnerea952f02010-09-21 17:24:05 +00004661 if (PtrInfo.V == 0)
4662 PtrInfo = InferPointerInfo(Ptr);
4663
4664 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004665 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004666 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004667 Val.getValueType().getStoreSize(), Alignment,
4668 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004669
4670 return getStore(Chain, dl, Val, Ptr, MMO);
4671}
4672
Andrew Trickef9de2a2013-05-25 02:42:55 +00004673SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004674 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004675 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004676 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004677 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004678 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004679 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004680 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4681 FoldingSetNodeID ID;
4682 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004683 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004684 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004685 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004686 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004687 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004688 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4689 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004690 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004691 }
Jack Carter170a5f22013-09-09 22:02:08 +00004692 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4693 dl.getDebugLoc(), VTs,
4694 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004695 CSEMap.InsertNode(N, IP);
4696 AllNodes.push_back(N);
4697 return SDValue(N, 0);
4698}
4699
Andrew Trickef9de2a2013-05-25 02:42:55 +00004700SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004701 SDValue Ptr, MachinePointerInfo PtrInfo,
4702 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004703 unsigned Alignment,
4704 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004705 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004706 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004707 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4708 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004709
Dan Gohman48b185d2009-09-25 20:36:54 +00004710 unsigned Flags = MachineMemOperand::MOStore;
4711 if (isVolatile)
4712 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004713 if (isNonTemporal)
4714 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004715
Chris Lattnerea952f02010-09-21 17:24:05 +00004716 if (PtrInfo.V == 0)
4717 PtrInfo = InferPointerInfo(Ptr);
4718
4719 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004720 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004721 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4722 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004723
4724 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4725}
4726
Andrew Trickef9de2a2013-05-25 02:42:55 +00004727SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004728 SDValue Ptr, EVT SVT,
4729 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004730 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004731
Michael Ilsemand5f91512012-09-10 16:56:31 +00004732 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004733 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004734 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004735 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004736
Dan Gohmancecad352009-12-14 23:40:38 +00004737 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4738 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004739 assert(VT.isInteger() == SVT.isInteger() &&
4740 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004741 assert(VT.isVector() == SVT.isVector() &&
4742 "Cannot use trunc store to convert to or from a vector!");
4743 assert((!VT.isVector() ||
4744 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4745 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004746
Owen Anderson9f944592009-08-11 20:47:22 +00004747 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004748 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004749 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4750 FoldingSetNodeID ID;
4751 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004752 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004753 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004754 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004755 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004756 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004757 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4758 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004759 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004760 }
Jack Carter170a5f22013-09-09 22:02:08 +00004761 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4762 dl.getDebugLoc(), VTs,
4763 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004764 CSEMap.InsertNode(N, IP);
4765 AllNodes.push_back(N);
4766 return SDValue(N, 0);
4767}
4768
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004769SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004770SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004771 SDValue Offset, ISD::MemIndexedMode AM) {
4772 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4773 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4774 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004775 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004776 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4777 FoldingSetNodeID ID;
4778 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004779 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004780 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004781 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004782 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004784 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004785
Jack Carter170a5f22013-09-09 22:02:08 +00004786 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4787 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004788 ST->isTruncatingStore(),
4789 ST->getMemoryVT(),
4790 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004791 CSEMap.InsertNode(N, IP);
4792 AllNodes.push_back(N);
4793 return SDValue(N, 0);
4794}
4795
Andrew Trickef9de2a2013-05-25 02:42:55 +00004796SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004797 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004798 SDValue SV,
4799 unsigned Align) {
4800 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4801 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004802}
4803
Andrew Trickef9de2a2013-05-25 02:42:55 +00004804SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004805 const SDUse *Ops, unsigned NumOps) {
Dan Gohman768f2c92008-07-07 18:26:29 +00004806 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004807 case 0: return getNode(Opcode, DL, VT);
4808 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4809 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4810 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004811 default: break;
4812 }
4813
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004814 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004815 // the regular getNode logic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004816 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004817 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004818}
4819
Andrew Trickef9de2a2013-05-25 02:42:55 +00004820SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004821 const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00004822 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004823 case 0: return getNode(Opcode, DL, VT);
4824 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4825 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4826 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004827 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004828 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004829
Chris Lattnerb0713c72005-04-09 03:27:28 +00004830 switch (Opcode) {
4831 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004832 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004833 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004834 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4835 "LHS and RHS of condition must have same type!");
4836 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4837 "True and False arms of SelectCC must have same type!");
4838 assert(Ops[2].getValueType() == VT &&
4839 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004840 break;
4841 }
4842 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004843 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004844 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4845 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004846 break;
4847 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004848 }
4849
Chris Lattner566307f2005-05-14 07:42:29 +00004850 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004851 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004852 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004853
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004854 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004855 FoldingSetNodeID ID;
4856 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004857 void *IP = 0;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004858
Bill Wendling022d18f2009-12-18 23:32:53 +00004859 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004860 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004861
Jack Carter170a5f22013-09-09 22:02:08 +00004862 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4863 VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004864 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004865 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004866 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4867 VTs, Ops, NumOps);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004868 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004869
Chris Lattnerb0713c72005-04-09 03:27:28 +00004870 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004871#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004872 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004873#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004874 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004875}
4876
Andrew Trickef9de2a2013-05-25 02:42:55 +00004877SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00004878 ArrayRef<EVT> ResultTys,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004879 const SDValue *Ops, unsigned NumOps) {
Dan Gohmande912e22009-04-09 23:54:40 +00004880 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner3bf4be42006-08-14 23:31:51 +00004881 Ops, NumOps);
4882}
4883
Andrew Trickef9de2a2013-05-25 02:42:55 +00004884SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004885 const EVT *VTs, unsigned NumVTs,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004886 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3bf4be42006-08-14 23:31:51 +00004887 if (NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004888 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4889 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004890}
4891
Andrew Trickef9de2a2013-05-25 02:42:55 +00004892SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004893 const SDValue *Ops, unsigned NumOps) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004894 if (VTList.NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004895 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattnerd5531332005-05-14 06:20:26 +00004896
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004897#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004898 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004899 // FIXME: figure out how to safely handle things like
4900 // int foo(int x) { return 1 << (x & 255); }
4901 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004902 case ISD::SRA_PARTS:
4903 case ISD::SRL_PARTS:
4904 case ISD::SHL_PARTS:
4905 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004906 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004907 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004908 else if (N3.getOpcode() == ISD::AND)
4909 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4910 // If the and is only masking out bits that cannot effect the shift,
4911 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004912 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004913 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004914 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004915 }
4916 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004917 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004918#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004919
Chris Lattnerf9c19152005-08-25 19:12:10 +00004920 // Memoize the node unless it returns a flag.
4921 SDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004922 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004923 FoldingSetNodeID ID;
4924 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004925 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004926 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004927 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004928
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004929 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004930 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4931 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004932 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004933 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4934 DL.getDebugLoc(), VTList, Ops[0],
4935 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004936 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004937 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4938 DL.getDebugLoc(), VTList, Ops[0],
4939 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004940 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004941 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4942 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004943 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004944 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004945 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004946 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004947 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4948 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004949 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004950 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4951 DL.getDebugLoc(), VTList, Ops[0],
4952 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004953 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004954 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4955 DL.getDebugLoc(), VTList, Ops[0],
4956 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004957 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004958 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4959 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004960 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004961 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004962 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004963#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004964 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004965#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004966 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004967}
4968
Andrew Trickef9de2a2013-05-25 02:42:55 +00004969SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004970 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004971}
4972
Andrew Trickef9de2a2013-05-25 02:42:55 +00004973SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004974 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004975 SDValue Ops[] = { N1 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004976 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004977}
4978
Andrew Trickef9de2a2013-05-25 02:42:55 +00004979SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004980 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004981 SDValue Ops[] = { N1, N2 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004982 return getNode(Opcode, DL, VTList, Ops, 2);
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) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004987 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004988 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004989}
4990
Andrew Trickef9de2a2013-05-25 02:42:55 +00004991SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004992 SDValue N1, SDValue N2, SDValue N3,
4993 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004994 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004995 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004996}
4997
Andrew Trickef9de2a2013-05-25 02:42:55 +00004998SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004999 SDValue N1, SDValue N2, SDValue N3,
5000 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005001 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00005002 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00005003}
5004
Owen Anderson53aa7a92009-08-10 22:56:29 +00005005SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005006 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00005007}
5008
Owen Anderson53aa7a92009-08-10 22:56:29 +00005009SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005010 FoldingSetNodeID ID;
5011 ID.AddInteger(2U);
5012 ID.AddInteger(VT1.getRawBits());
5013 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005014
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005015 void *IP = 0;
5016 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5017 if (Result == NULL) {
5018 EVT *Array = Allocator.Allocate<EVT>(2);
5019 Array[0] = VT1;
5020 Array[1] = VT2;
5021 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5022 VTListMap.InsertNode(Result, IP);
5023 }
5024 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005025}
Dan Gohman17059682008-07-17 19:10:17 +00005026
Owen Anderson53aa7a92009-08-10 22:56:29 +00005027SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005028 FoldingSetNodeID ID;
5029 ID.AddInteger(3U);
5030 ID.AddInteger(VT1.getRawBits());
5031 ID.AddInteger(VT2.getRawBits());
5032 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005033
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005034 void *IP = 0;
5035 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5036 if (Result == NULL) {
5037 EVT *Array = Allocator.Allocate<EVT>(3);
5038 Array[0] = VT1;
5039 Array[1] = VT2;
5040 Array[2] = VT3;
5041 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5042 VTListMap.InsertNode(Result, IP);
5043 }
5044 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005045}
5046
Owen Anderson53aa7a92009-08-10 22:56:29 +00005047SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005048 FoldingSetNodeID ID;
5049 ID.AddInteger(4U);
5050 ID.AddInteger(VT1.getRawBits());
5051 ID.AddInteger(VT2.getRawBits());
5052 ID.AddInteger(VT3.getRawBits());
5053 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005054
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005055 void *IP = 0;
5056 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5057 if (Result == NULL) {
5058 EVT *Array = Allocator.Allocate<EVT>(4);
5059 Array[0] = VT1;
5060 Array[1] = VT2;
5061 Array[2] = VT3;
5062 Array[3] = VT4;
5063 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5064 VTListMap.InsertNode(Result, IP);
5065 }
5066 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005067}
5068
Owen Anderson53aa7a92009-08-10 22:56:29 +00005069SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005070 FoldingSetNodeID ID;
5071 ID.AddInteger(NumVTs);
5072 for (unsigned index = 0; index < NumVTs; index++) {
5073 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005074 }
5075
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005076 void *IP = 0;
5077 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5078 if (Result == NULL) {
5079 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5080 std::copy(VTs, VTs + NumVTs, Array);
5081 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5082 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005083 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005084 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005085}
5086
5087
Chris Lattnerf34156e2006-01-28 09:32:45 +00005088/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5089/// specified operands. If the resultant node already exists in the DAG,
5090/// this does not modify the specified node, instead it returns the node that
5091/// already exists. If the resultant node does not exist in the DAG, the
5092/// input node is returned. As a degenerate case, if you specify the same
5093/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005094SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005095 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005096
Chris Lattnerf34156e2006-01-28 09:32:45 +00005097 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005098 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005099
Chris Lattnerf34156e2006-01-28 09:32:45 +00005100 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005101 void *InsertPos = 0;
5102 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005103 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005104
Dan Gohmanebeccb42008-07-21 22:38:59 +00005105 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005106 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005107 if (!RemoveNodeFromCSEMaps(N))
5108 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005109
Chris Lattnerf34156e2006-01-28 09:32:45 +00005110 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005111 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005112
Chris Lattnerf34156e2006-01-28 09:32:45 +00005113 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005114 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005115 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005116}
5117
Dan Gohman92c11ac2010-06-18 15:30:29 +00005118SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005119 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005120
Chris Lattnerf34156e2006-01-28 09:32:45 +00005121 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005122 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005123 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005124
Chris Lattnerf34156e2006-01-28 09:32:45 +00005125 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005126 void *InsertPos = 0;
5127 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005128 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005129
Dan Gohmanebeccb42008-07-21 22:38:59 +00005130 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005131 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005132 if (!RemoveNodeFromCSEMaps(N))
5133 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005134
Chris Lattnerf34156e2006-01-28 09:32:45 +00005135 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005136 if (N->OperandList[0] != Op1)
5137 N->OperandList[0].set(Op1);
5138 if (N->OperandList[1] != Op2)
5139 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005140
Chris Lattnerf34156e2006-01-28 09:32:45 +00005141 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005142 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005143 return N;
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, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005148 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005149 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005150}
5151
Dan Gohman92c11ac2010-06-18 15:30:29 +00005152SDNode *SelectionDAG::
5153UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005154 SDValue Op3, SDValue Op4) {
5155 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005156 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005157}
5158
Dan Gohman92c11ac2010-06-18 15:30:29 +00005159SDNode *SelectionDAG::
5160UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005161 SDValue Op3, SDValue Op4, SDValue Op5) {
5162 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005163 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner580b12a2006-01-28 10:09:25 +00005164}
5165
Dan Gohman92c11ac2010-06-18 15:30:29 +00005166SDNode *SelectionDAG::
5167UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00005168 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005169 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005170
Chris Lattnerf34156e2006-01-28 09:32:45 +00005171 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005172 bool AnyChange = false;
5173 for (unsigned i = 0; i != NumOps; ++i) {
5174 if (Ops[i] != N->getOperand(i)) {
5175 AnyChange = true;
5176 break;
5177 }
5178 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005179
Chris Lattnerf34156e2006-01-28 09:32:45 +00005180 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005181 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005182
Chris Lattnerf34156e2006-01-28 09:32:45 +00005183 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005184 void *InsertPos = 0;
Chris Lattner97af9d52006-08-08 01:09:31 +00005185 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005186 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005187
Dan Gohman2f83b472008-05-02 00:05:03 +00005188 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005189 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005190 if (!RemoveNodeFromCSEMaps(N))
5191 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005192
Chris Lattnerf34156e2006-01-28 09:32:45 +00005193 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005194 for (unsigned i = 0; i != NumOps; ++i)
5195 if (N->OperandList[i] != Ops[i])
5196 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005197
5198 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005199 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005200 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005201}
5202
Dan Gohman91697632008-07-07 20:57:48 +00005203/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005204/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005205void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005206 // Unlike the code in MorphNodeTo that does this, we don't need to
5207 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005208 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5209 SDUse &Use = *I++;
5210 Use.set(SDValue());
5211 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005212}
Chris Lattner19732782005-08-16 18:17:10 +00005213
Dan Gohman17059682008-07-17 19:10:17 +00005214/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5215/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +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) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005219 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005220 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005221}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005222
Dan Gohman17059682008-07-17 19:10:17 +00005223SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005224 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005225 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005226 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005227 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner19732782005-08-16 18:17:10 +00005228}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005229
Dan Gohman17059682008-07-17 19:10:17 +00005230SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005231 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005232 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005233 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005234 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005235 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner19732782005-08-16 18:17:10 +00005236}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005237
Dan Gohman17059682008-07-17 19:10:17 +00005238SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005239 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005240 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005241 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005242 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005243 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner19732782005-08-16 18:17:10 +00005244}
Chris Lattner466fece2005-08-21 22:30:30 +00005245
Dan Gohman17059682008-07-17 19:10:17 +00005246SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005247 EVT VT, const SDValue *Ops,
Evan Cheng849f4bf2006-08-27 08:08:54 +00005248 unsigned NumOps) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005249 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005250 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
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, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005255 unsigned NumOps) {
5256 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman17059682008-07-17 19:10:17 +00005257 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005258}
5259
Dan Gohman17059682008-07-17 19:10:17 +00005260SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005261 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005262 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005263 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman22e97072008-07-02 23:23:19 +00005264}
5265
Dan Gohman17059682008-07-17 19:10:17 +00005266SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005267 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005268 const SDValue *Ops, unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005269 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohman17059682008-07-17 19:10:17 +00005270 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005271}
5272
Bill Wendling2d598632008-12-01 23:28:22 +00005273SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005274 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling2d598632008-12-01 23:28:22 +00005275 const SDValue *Ops, unsigned NumOps) {
5276 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5277 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5278}
5279
Scott Michelcf0da6c2009-02-17 22:15:04 +00005280SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005281 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005282 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005283 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005284 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005285 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth683352382006-01-23 21:51:14 +00005286}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005287
Scott Michelcf0da6c2009-02-17 22:15:04 +00005288SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005289 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005290 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005291 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005292 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005293 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005294}
5295
Dan Gohman17059682008-07-17 19:10:17 +00005296SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005297 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005298 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005299 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005300 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005301 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005302 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman22e97072008-07-02 23:23:19 +00005303}
5304
Dan Gohman17059682008-07-17 19:10:17 +00005305SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005306 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005307 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005308 SDValue Op3) {
5309 SDVTList VTs = getVTList(VT1, VT2, VT3);
5310 SDValue Ops[] = { Op1, Op2, Op3 };
5311 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5312}
5313
5314SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005315 SDVTList VTs, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005316 unsigned NumOps) {
Chris Lattner625916d2010-02-23 23:01:35 +00005317 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5318 // Reset the NodeID to -1.
5319 N->setNodeId(-1);
5320 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005321}
5322
Andrew Trickef9de2a2013-05-25 02:42:55 +00005323/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005324/// the line number information on the merged node since it is not possible to
5325/// preserve the information that operation is associated with multiple lines.
5326/// This will make the debugger working better at -O0, were there is a higher
5327/// probability having other instructions associated with that line.
5328///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005329/// For IROrder, we keep the smaller of the two
5330SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005331 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005332 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5333 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005334 N->setDebugLoc(DebugLoc());
5335 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005336 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5337 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005338 return N;
5339}
5340
Chris Lattneraf197502010-02-28 21:36:14 +00005341/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005342/// return type, opcode, and operands.
5343///
5344/// Note that MorphNodeTo returns the resultant node. If there is already a
5345/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005346/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005347///
5348/// Using MorphNodeTo is faster than creating a new node and swapping it in
5349/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005350/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005351/// the node's users.
5352///
5353SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005354 SDVTList VTs, const SDValue *Ops,
Dan Gohman17059682008-07-17 19:10:17 +00005355 unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005356 // If an identical node already exists, use it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005357 void *IP = 0;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005358 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005359 FoldingSetNodeID ID;
5360 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendling022d18f2009-12-18 23:32:53 +00005361 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005362 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005363 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005364
Dan Gohmand3fe1742008-09-13 01:54:27 +00005365 if (!RemoveNodeFromCSEMaps(N))
5366 IP = 0;
Chris Lattner486edfb2007-02-04 02:32:44 +00005367
Dan Gohman17059682008-07-17 19:10:17 +00005368 // Start the morphing.
5369 N->NodeType = Opc;
5370 N->ValueList = VTs.VTs;
5371 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005372
Dan Gohman17059682008-07-17 19:10:17 +00005373 // Clear the operands list, updating used nodes to remove this from their
5374 // use list. Keep track of any operands that become dead as a result.
5375 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005376 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5377 SDUse &Use = *I++;
5378 SDNode *Used = Use.getNode();
5379 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005380 if (Used->use_empty())
5381 DeadNodeSet.insert(Used);
5382 }
5383
Dan Gohman48b185d2009-09-25 20:36:54 +00005384 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5385 // Initialize the memory references information.
5386 MN->setMemRefs(0, 0);
5387 // If NumOps is larger than the # of operands we can have in a
5388 // MachineSDNode, reallocate the operand list.
5389 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5390 if (MN->OperandsNeedDelete)
5391 delete[] MN->OperandList;
5392 if (NumOps > array_lengthof(MN->LocalOperands))
5393 // We're creating a final node that will live unmorphed for the
5394 // remainder of the current SelectionDAG iteration, so we can allocate
5395 // the operands directly out of a pool with no recycling metadata.
5396 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman01c65a22010-03-18 18:49:47 +00005397 Ops, NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005398 else
5399 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5400 MN->OperandsNeedDelete = false;
5401 } else
5402 MN->InitOperands(MN->OperandList, Ops, NumOps);
5403 } else {
5404 // If NumOps is larger than the # of operands we currently have, reallocate
5405 // the operand list.
5406 if (NumOps > N->NumOperands) {
5407 if (N->OperandsNeedDelete)
5408 delete[] N->OperandList;
5409 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005410 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005411 } else
Anton Korobeynikov86263672009-10-22 00:15:17 +00005412 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005413 }
5414
5415 // Delete any nodes that are still dead after adding the uses for the
5416 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005417 if (!DeadNodeSet.empty()) {
5418 SmallVector<SDNode *, 16> DeadNodes;
5419 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5420 E = DeadNodeSet.end(); I != E; ++I)
5421 if ((*I)->use_empty())
5422 DeadNodes.push_back(*I);
5423 RemoveDeadNodes(DeadNodes);
5424 }
Dan Gohman91697632008-07-07 20:57:48 +00005425
Dan Gohman17059682008-07-17 19:10:17 +00005426 if (IP)
5427 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005428 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005429}
5430
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005431
Dan Gohman32f71d72009-09-25 18:54:59 +00005432/// getMachineNode - These are used for target selectors to create a new node
5433/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005434///
Dan Gohman32f71d72009-09-25 18:54:59 +00005435/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005436/// node of the specified opcode and operands, it returns that node instead of
5437/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005438MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005439SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005440 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005441 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005442}
Bill Wendlinga434d932009-01-29 09:01:55 +00005443
Dan Gohmana22f2d82009-10-10 01:29:16 +00005444MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005445SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005446 SDVTList VTs = getVTList(VT);
5447 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005448 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005449}
Bill Wendlinga434d932009-01-29 09:01:55 +00005450
Dan Gohmana22f2d82009-10-10 01:29:16 +00005451MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005452SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005453 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005454 SDVTList VTs = getVTList(VT);
5455 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005456 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005457}
Bill Wendlinga434d932009-01-29 09:01:55 +00005458
Dan Gohmana22f2d82009-10-10 01:29:16 +00005459MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005460SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005461 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005462 SDVTList VTs = getVTList(VT);
5463 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005464 return getMachineNode(Opcode, dl, VTs, Ops);
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, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005469 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005470 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005471 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005472}
Bill Wendlinga434d932009-01-29 09:01:55 +00005473
Dan Gohmana22f2d82009-10-10 01:29:16 +00005474MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005475SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005476 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005477 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005478}
Bill Wendlinga434d932009-01-29 09:01:55 +00005479
Dan Gohmana22f2d82009-10-10 01:29:16 +00005480MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005481SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005482 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005483 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005484 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005485 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005486}
Bill Wendlinga434d932009-01-29 09:01:55 +00005487
Dan Gohmana22f2d82009-10-10 01:29:16 +00005488MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005489SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005490 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005491 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005492 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005493 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005494}
5495
Dan Gohmana22f2d82009-10-10 01:29:16 +00005496MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005497SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005498 EVT VT1, EVT VT2, SDValue Op1,
5499 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005500 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005501 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005502 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005503}
5504
Dan Gohmana22f2d82009-10-10 01:29:16 +00005505MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005506SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005507 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005508 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005509 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005510 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005511}
Bill Wendlinga434d932009-01-29 09:01:55 +00005512
Dan Gohmana22f2d82009-10-10 01:29:16 +00005513MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005514SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005515 EVT VT1, EVT VT2, EVT VT3,
5516 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005517 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005518 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005519 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005520}
Bill Wendlinga434d932009-01-29 09:01:55 +00005521
Dan Gohmana22f2d82009-10-10 01:29:16 +00005522MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005523SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005524 EVT VT1, EVT VT2, EVT VT3,
5525 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005526 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005527 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005528 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005529}
Bill Wendlinga434d932009-01-29 09:01:55 +00005530
Dan Gohmana22f2d82009-10-10 01:29:16 +00005531MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005532SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005533 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005534 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005535 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005536 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005537}
Bill Wendlinga434d932009-01-29 09:01:55 +00005538
Dan Gohmana22f2d82009-10-10 01:29:16 +00005539MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005540SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005541 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005542 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005543 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005544 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005545}
5546
Dan Gohmana22f2d82009-10-10 01:29:16 +00005547MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005548SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005549 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005550 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005551 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liaob53d8962013-04-19 22:22:57 +00005552 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005553}
5554
Dan Gohmana22f2d82009-10-10 01:29:16 +00005555MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005556SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005557 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005558 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005559 MachineSDNode *N;
Ted Kremenek3c4408c2011-01-23 17:05:06 +00005560 void *IP = 0;
Michael Liaob53d8962013-04-19 22:22:57 +00005561 const SDValue *Ops = OpsArray.data();
5562 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005563
5564 if (DoCSE) {
5565 FoldingSetNodeID ID;
5566 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5567 IP = 0;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005568 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005569 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005570 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005571 }
5572
5573 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005574 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5575 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005576
5577 // Initialize the operands list.
5578 if (NumOps > array_lengthof(N->LocalOperands))
5579 // We're creating a final node that will live unmorphed for the
5580 // remainder of the current SelectionDAG iteration, so we can allocate
5581 // the operands directly out of a pool with no recycling metadata.
5582 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5583 Ops, NumOps);
5584 else
5585 N->InitOperands(N->LocalOperands, Ops, NumOps);
5586 N->OperandsNeedDelete = false;
5587
5588 if (DoCSE)
5589 CSEMap.InsertNode(N, IP);
5590
5591 AllNodes.push_back(N);
5592#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005593 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005594#endif
5595 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005596}
Evan Chengd3f1db92006-02-09 07:15:23 +00005597
Dan Gohmanac33a902009-08-19 18:16:17 +00005598/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005599/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005600SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005601SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005602 SDValue Operand) {
5603 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005604 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005605 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005606 return SDValue(Subreg, 0);
5607}
5608
Bob Wilson2a45a652009-10-08 18:49:46 +00005609/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005610/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005611SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005612SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005613 SDValue Operand, SDValue Subreg) {
5614 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005615 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005616 VT, Operand, Subreg, SRIdxVal);
5617 return SDValue(Result, 0);
5618}
5619
Evan Cheng31604a62008-03-22 01:55:50 +00005620/// getNodeIfExists - Get the specified node if it's already available, or
5621/// else return NULL.
5622SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005623 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005624 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005625 FoldingSetNodeID ID;
5626 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5627 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00005628 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005629 return E;
5630 }
5631 return NULL;
5632}
5633
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005634/// getDbgValue - Creates a SDDbgValue node.
5635///
5636SDDbgValue *
5637SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5638 DebugLoc DL, unsigned O) {
5639 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5640}
5641
5642SDDbgValue *
Dan Gohmanbcaf6812010-04-15 01:51:59 +00005643SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005644 DebugLoc DL, unsigned O) {
5645 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5646}
5647
5648SDDbgValue *
5649SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5650 DebugLoc DL, unsigned O) {
5651 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5652}
5653
Dan Gohman7d099f72010-03-03 21:33:37 +00005654namespace {
5655
Dan Gohman9cc886b2010-03-04 19:11:28 +00005656/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005657/// pointed to by a use iterator is deleted, increment the use iterator
5658/// so that it doesn't dangle.
5659///
Dan Gohman7d099f72010-03-03 21:33:37 +00005660class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005661 SDNode::use_iterator &UI;
5662 SDNode::use_iterator &UE;
5663
5664 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5665 // Increment the iterator as needed.
5666 while (UI != UE && N == *UI)
5667 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005668 }
5669
5670public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005671 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005672 SDNode::use_iterator &ui,
5673 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005674 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005675};
5676
5677}
5678
Evan Cheng445b91a2006-08-07 22:13:29 +00005679/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005680/// This can cause recursive merging of nodes in the DAG.
5681///
Chris Lattner76858912008-02-03 03:35:22 +00005682/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005683///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005684void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005685 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005686 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005687 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005688 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005689
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005690 // Iterate over all the existing uses of From. New uses will be added
5691 // to the beginning of the use list, which we avoid visiting.
5692 // This specifically avoids visiting uses of From that arise while the
5693 // replacement is happening, because any such uses would be the result
5694 // of CSE: If an existing node looks like From after one of its operands
5695 // is replaced by To, we don't want to replace of all its users with To
5696 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005697 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005698 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005699 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005700 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005701
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005702 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005703 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005704
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005705 // A user can appear in a use list multiple times, and when this
5706 // happens the uses are usually next to each other in the list.
5707 // To help reduce the number of CSE recomputations, process all
5708 // the uses of this user that we can find this way.
5709 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005710 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005711 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005712 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005713 } while (UI != UE && *UI == User);
5714
5715 // Now that we have modified User, add it back to the CSE maps. If it
5716 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005717 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005718 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005719
5720 // If we just RAUW'd the root, take note.
5721 if (FromN == getRoot())
5722 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005723}
5724
5725/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5726/// This can cause recursive merging of nodes in the DAG.
5727///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005728/// This version assumes that for each value of From, there is a
5729/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005730///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005731void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005732#ifndef NDEBUG
5733 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5734 assert((!From->hasAnyUseOfValue(i) ||
5735 From->getValueType(i) == To->getValueType(i)) &&
5736 "Cannot use this version of ReplaceAllUsesWith!");
5737#endif
Dan Gohman17059682008-07-17 19:10:17 +00005738
5739 // Handle the trivial case.
5740 if (From == To)
5741 return;
5742
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005743 // Iterate over just the existing users of From. See the comments in
5744 // the ReplaceAllUsesWith above.
5745 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005746 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005747 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005748 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005749
Chris Lattner373f0482005-08-26 18:36:28 +00005750 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005751 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005752
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005753 // A user can appear in a use list multiple times, and when this
5754 // happens the uses are usually next to each other in the list.
5755 // To help reduce the number of CSE recomputations, process all
5756 // the uses of this user that we can find this way.
5757 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005758 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005759 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005760 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005761 } while (UI != UE && *UI == User);
5762
5763 // Now that we have modified User, add it back to the CSE maps. If it
5764 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005765 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005766 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005767
5768 // If we just RAUW'd the root, take note.
5769 if (From == getRoot().getNode())
5770 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005771}
5772
Chris Lattner373f0482005-08-26 18:36:28 +00005773/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5774/// This can cause recursive merging of nodes in the DAG.
5775///
5776/// This version can replace From with any result values. To must match the
5777/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005778void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005779 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005780 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005781
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005782 // Iterate over just the existing users of From. See the comments in
5783 // the ReplaceAllUsesWith above.
5784 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005785 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005786 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005787 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005788
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005789 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005790 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005791
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005792 // A user can appear in a use list multiple times, and when this
5793 // happens the uses are usually next to each other in the list.
5794 // To help reduce the number of CSE recomputations, process all
5795 // the uses of this user that we can find this way.
5796 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005797 SDUse &Use = UI.getUse();
5798 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005799 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005800 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005801 } while (UI != UE && *UI == User);
5802
5803 // Now that we have modified User, add it back to the CSE maps. If it
5804 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005805 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005806 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005807
5808 // If we just RAUW'd the root, take note.
5809 if (From == getRoot().getNode())
5810 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005811}
5812
Chris Lattner375e1a72006-02-17 21:58:01 +00005813/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005814/// uses of other values produced by From.getNode() alone. The Deleted
5815/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005816void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005817 // Handle the really simple, really trivial case efficiently.
5818 if (From == To) return;
5819
Chris Lattner375e1a72006-02-17 21:58:01 +00005820 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005821 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005822 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005823 return;
5824 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005825
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005826 // Iterate over just the existing users of From. See the comments in
5827 // the ReplaceAllUsesWith above.
5828 SDNode::use_iterator UI = From.getNode()->use_begin(),
5829 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005830 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005831 while (UI != UE) {
5832 SDNode *User = *UI;
5833 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005834
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005835 // A user can appear in a use list multiple times, and when this
5836 // happens the uses are usually next to each other in the list.
5837 // To help reduce the number of CSE recomputations, process all
5838 // the uses of this user that we can find this way.
5839 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005840 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005841
5842 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005843 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005844 ++UI;
5845 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005846 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005847
5848 // If this node hasn't been modified yet, it's still in the CSE maps,
5849 // so remove its old self from the CSE maps.
5850 if (!UserRemovedFromCSEMaps) {
5851 RemoveNodeFromCSEMaps(User);
5852 UserRemovedFromCSEMaps = true;
5853 }
5854
5855 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005856 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005857 } while (UI != UE && *UI == User);
5858
5859 // We are iterating over all uses of the From node, so if a use
5860 // doesn't use the specific value, no changes are made.
5861 if (!UserRemovedFromCSEMaps)
5862 continue;
5863
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005864 // Now that we have modified User, add it back to the CSE maps. If it
5865 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005866 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005867 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005868
5869 // If we just RAUW'd the root, take note.
5870 if (From == getRoot())
5871 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005872}
5873
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005874namespace {
5875 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5876 /// to record information about a use.
5877 struct UseMemo {
5878 SDNode *User;
5879 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005880 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005881 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005882
5883 /// operator< - Sort Memos by User.
5884 bool operator<(const UseMemo &L, const UseMemo &R) {
5885 return (intptr_t)L.User < (intptr_t)R.User;
5886 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005887}
5888
Dan Gohman17059682008-07-17 19:10:17 +00005889/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005890/// uses of other values produced by From.getNode() alone. The same value
5891/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005892/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005893void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5894 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005895 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005896 // Handle the simple, trivial case efficiently.
5897 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005898 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005899
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005900 // Read up all the uses and make records of them. This helps
5901 // processing new uses that are introduced during the
5902 // replacement process.
5903 SmallVector<UseMemo, 4> Uses;
5904 for (unsigned i = 0; i != Num; ++i) {
5905 unsigned FromResNo = From[i].getResNo();
5906 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005907 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005908 E = FromNode->use_end(); UI != E; ++UI) {
5909 SDUse &Use = UI.getUse();
5910 if (Use.getResNo() == FromResNo) {
5911 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005912 Uses.push_back(Memo);
5913 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005914 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005915 }
Dan Gohman17059682008-07-17 19:10:17 +00005916
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005917 // Sort the uses, so that all the uses from a given User are together.
5918 std::sort(Uses.begin(), Uses.end());
5919
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005920 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5921 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005922 // We know that this user uses some value of From. If it is the right
5923 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005924 SDNode *User = Uses[UseIndex].User;
5925
5926 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005927 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005928
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005929 // The Uses array is sorted, so all the uses for a given User
5930 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005931 // To help reduce the number of CSE recomputations, process all
5932 // the uses of this user that we can find this way.
5933 do {
5934 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005935 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005936 ++UseIndex;
5937
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005938 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005939 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5940
Dan Gohman17059682008-07-17 19:10:17 +00005941 // Now that we have modified User, add it back to the CSE maps. If it
5942 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005943 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005944 }
5945}
5946
Evan Cheng9631a602006-08-01 08:20:41 +00005947/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005948/// based on their topological order. It returns the maximum id and a vector
5949/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005950unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005951
Dan Gohman86aa16a2008-09-30 18:30:35 +00005952 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005953
Dan Gohman86aa16a2008-09-30 18:30:35 +00005954 // SortedPos tracks the progress of the algorithm. Nodes before it are
5955 // sorted, nodes after it are unsorted. When the algorithm completes
5956 // it is at the end of the list.
5957 allnodes_iterator SortedPos = allnodes_begin();
5958
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005959 // Visit all the nodes. Move nodes with no operands to the front of
5960 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005961 // operand count. Before we do this, the Node Id fields of the nodes
5962 // may contain arbitrary values. After, the Node Id fields for nodes
5963 // before SortedPos will contain the topological sort index, and the
5964 // Node Id fields for nodes At SortedPos and after will contain the
5965 // count of outstanding operands.
5966 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5967 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005968 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005969 unsigned Degree = N->getNumOperands();
5970 if (Degree == 0) {
5971 // A node with no uses, add it to the result array immediately.
5972 N->setNodeId(DAGSize++);
5973 allnodes_iterator Q = N;
5974 if (Q != SortedPos)
5975 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005976 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005977 ++SortedPos;
5978 } else {
5979 // Temporarily use the Node Id as scratch space for the degree count.
5980 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005981 }
5982 }
5983
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005984 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005985 // such that by the time the end is reached all nodes will be sorted.
5986 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5987 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00005988 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00005989 // N is in sorted position, so all its uses have one less operand
5990 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005991 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5992 UI != UE; ++UI) {
5993 SDNode *P = *UI;
5994 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00005995 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005996 --Degree;
5997 if (Degree == 0) {
5998 // All of P's operands are sorted, so P may sorted now.
5999 P->setNodeId(DAGSize++);
6000 if (P != SortedPos)
6001 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00006002 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006003 ++SortedPos;
6004 } else {
6005 // Update P's outstanding operand count.
6006 P->setNodeId(Degree);
6007 }
6008 }
David Greene3b2a68c2010-01-20 00:59:23 +00006009 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00006010#ifndef NDEBUG
6011 SDNode *S = ++I;
6012 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00006013 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00006014#endif
6015 llvm_unreachable(0);
David Greene3b2a68c2010-01-20 00:59:23 +00006016 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006017 }
6018
6019 assert(SortedPos == AllNodes.end() &&
6020 "Topological sort incomplete!");
6021 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6022 "First node in topological sort is not the entry token!");
6023 assert(AllNodes.front().getNodeId() == 0 &&
6024 "First node in topological sort has non-zero id!");
6025 assert(AllNodes.front().getNumOperands() == 0 &&
6026 "First node in topological sort has operands!");
6027 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6028 "Last node in topologic sort has unexpected id!");
6029 assert(AllNodes.back().use_empty() &&
6030 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006031 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006032 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006033}
6034
Evan Cheng563fe3c2010-03-25 01:38:16 +00006035/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6036/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006037void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6038 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006039 if (SD)
6040 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006041}
Evan Cheng29eefc12006-07-27 06:39:06 +00006042
Devang Patelefc6b162011-01-25 23:27:42 +00006043/// TransferDbgValues - Transfer SDDbgValues.
6044void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6045 if (From == To || !From.getNode()->getHasDebugValue())
6046 return;
6047 SDNode *FromNode = From.getNode();
6048 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006049 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006050 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006051 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006052 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006053 SDDbgValue *Dbg = *I;
6054 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6055 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
6056 Dbg->getOffset(), Dbg->getDebugLoc(),
6057 Dbg->getOrder());
6058 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006059 }
6060 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006061 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006062 E = ClonedDVs.end(); I != E; ++I)
6063 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006064}
6065
Jim Laskeyd66e6162005-08-17 20:08:02 +00006066//===----------------------------------------------------------------------===//
6067// SDNode Class
6068//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006069
Chris Lattner3bf17b62007-02-04 02:41:42 +00006070HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006071 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006072}
6073
Andrew Trickef9de2a2013-05-25 02:42:55 +00006074GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6075 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006076 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006077 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006078 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006079}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006080
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006081AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6082 SDValue X, unsigned SrcAS,
6083 unsigned DestAS)
6084 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6085 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6086
Andrew Trickef9de2a2013-05-25 02:42:55 +00006087MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6088 EVT memvt, MachineMemOperand *mmo)
6089 : SDNode(Opc, Order, dl, VTs), 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!");
David Greeneb7941b02010-02-17 20:21:42 +00006093 assert(isNonTemporal() == MMO->isNonTemporal() &&
6094 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006095 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006096}
6097
Andrew Trickef9de2a2013-05-25 02:42:55 +00006098MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peck527da1b2010-11-23 03:31:01 +00006099 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohman48b185d2009-09-25 20:36:54 +00006100 MachineMemOperand *mmo)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006101 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohman48b185d2009-09-25 20:36:54 +00006102 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006103 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006104 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006105 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6106 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006107}
6108
Jim Laskeyf576b422006-10-27 23:46:08 +00006109/// Profile - Gather unique data for the node.
6110///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006111void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006112 AddNodeIDNode(ID, this);
6113}
6114
Owen Anderson3b1665e2009-08-25 22:27:22 +00006115namespace {
6116 struct EVTArray {
6117 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006118
Owen Anderson3b1665e2009-08-25 22:27:22 +00006119 EVTArray() {
6120 VTs.reserve(MVT::LAST_VALUETYPE);
6121 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6122 VTs.push_back(MVT((MVT::SimpleValueType)i));
6123 }
6124 };
6125}
6126
Owen Anderson53aa7a92009-08-10 22:56:29 +00006127static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006128static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006129static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006130
Chris Lattner88fa11c2005-11-08 23:30:28 +00006131/// getValueTypeList - Return a pointer to the specified value type.
6132///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006133const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006134 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006135 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006136 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006137 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006138 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006139 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006140 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006141 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006142}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006143
Chris Lattner40e79822005-01-12 18:37:47 +00006144/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6145/// indicated value. This method ignores uses of other values defined by this
6146/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006147bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006148 assert(Value < getNumValues() && "Bad value!");
6149
Roman Levenstein51f532f2008-04-07 10:06:32 +00006150 // TODO: Only iterate over uses of a given value of the node
6151 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006152 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006153 if (NUses == 0)
6154 return false;
6155 --NUses;
6156 }
Chris Lattner40e79822005-01-12 18:37:47 +00006157 }
6158
6159 // Found exactly the right number of uses?
6160 return NUses == 0;
6161}
6162
6163
Evan Cheng358c3d12007-08-02 05:29:38 +00006164/// hasAnyUseOfValue - Return true if there are any use of the indicated
6165/// value. This method ignores uses of other values defined by this operation.
6166bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6167 assert(Value < getNumValues() && "Bad value!");
6168
Dan Gohman7a510c22008-07-09 22:39:01 +00006169 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006170 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006171 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006172
6173 return false;
6174}
6175
6176
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006177/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006178///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006179bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006180 bool Seen = false;
6181 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006182 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006183 if (User == this)
6184 Seen = true;
6185 else
6186 return false;
6187 }
6188
6189 return Seen;
6190}
6191
Evan Cheng9456dd82006-11-03 07:31:32 +00006192/// isOperand - Return true if this node is an operand of N.
6193///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006194bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006195 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6196 if (*this == N->getOperand(i))
6197 return true;
6198 return false;
6199}
6200
Evan Cheng567d2e52008-03-04 00:41:45 +00006201bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006202 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006203 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006204 return true;
6205 return false;
6206}
Evan Chengd37645c2006-02-05 06:29:23 +00006207
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006208/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006209/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006210/// side-effecting instructions on any chain path. In practice, this looks
6211/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006212/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006213bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006214 unsigned Depth) const {
6215 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006216
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006217 // Don't search too deeply, we just want to be able to see through
6218 // TokenFactor's etc.
6219 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006220
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006221 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006222 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006223 if (getOpcode() == ISD::TokenFactor) {
6224 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006225 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6226 return false;
6227 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006228 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006229
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006230 // Loads don't have side effects, look through them.
6231 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6232 if (!Ld->isVolatile())
6233 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6234 }
6235 return false;
6236}
6237
Lang Hames5a004992011-07-07 04:31:51 +00006238/// hasPredecessor - Return true if N is a predecessor of this node.
6239/// N is either an operand of this node, or can be reached by recursively
6240/// traversing up the operands.
6241/// NOTE: This is an expensive method. Use it carefully.
6242bool SDNode::hasPredecessor(const SDNode *N) const {
6243 SmallPtrSet<const SDNode *, 32> Visited;
6244 SmallVector<const SDNode *, 16> Worklist;
6245 return hasPredecessorHelper(N, Visited, Worklist);
6246}
Dan Gohmancd139c02009-10-28 03:44:30 +00006247
Craig Topperb94011f2013-07-14 04:42:23 +00006248bool
6249SDNode::hasPredecessorHelper(const SDNode *N,
6250 SmallPtrSet<const SDNode *, 32> &Visited,
6251 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006252 if (Visited.empty()) {
6253 Worklist.push_back(this);
6254 } else {
6255 // Take a look in the visited set. If we've already encountered this node
6256 // we needn't search further.
6257 if (Visited.count(N))
6258 return true;
6259 }
6260
6261 // Haven't visited N yet. Continue the search.
6262 while (!Worklist.empty()) {
6263 const SDNode *M = Worklist.pop_back_val();
6264 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6265 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006266 if (Visited.insert(Op))
6267 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006268 if (Op == N)
6269 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006270 }
Lang Hames5a004992011-07-07 04:31:51 +00006271 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006272
6273 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006274}
6275
Evan Cheng5d9fd972006-10-04 00:56:09 +00006276uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6277 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006278 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006279}
6280
Mon P Wang32f8bb92009-11-30 02:42:02 +00006281SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6282 assert(N->getNumValues() == 1 &&
6283 "Can't unroll a vector with multiple results!");
6284
6285 EVT VT = N->getValueType(0);
6286 unsigned NE = VT.getVectorNumElements();
6287 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006288 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006289
6290 SmallVector<SDValue, 8> Scalars;
6291 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6292
6293 // If ResNE is 0, fully unroll the vector op.
6294 if (ResNE == 0)
6295 ResNE = NE;
6296 else if (NE > ResNE)
6297 NE = ResNE;
6298
6299 unsigned i;
6300 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006301 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006302 SDValue Operand = N->getOperand(j);
6303 EVT OperandVT = Operand.getValueType();
6304 if (OperandVT.isVector()) {
6305 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006306 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006307 EVT OperandEltVT = OperandVT.getVectorElementType();
6308 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6309 OperandEltVT,
6310 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006311 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006312 } else {
6313 // A scalar operand; just use it as is.
6314 Operands[j] = Operand;
6315 }
6316 }
6317
6318 switch (N->getOpcode()) {
6319 default:
6320 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6321 &Operands[0], Operands.size()));
6322 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006323 case ISD::VSELECT:
6324 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6325 &Operands[0], Operands.size()));
6326 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006327 case ISD::SHL:
6328 case ISD::SRA:
6329 case ISD::SRL:
6330 case ISD::ROTL:
6331 case ISD::ROTR:
6332 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006333 getShiftAmountOperand(Operands[0].getValueType(),
6334 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006335 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006336 case ISD::SIGN_EXTEND_INREG:
6337 case ISD::FP_ROUND_INREG: {
6338 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6339 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6340 Operands[0],
6341 getValueType(ExtVT)));
6342 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006343 }
6344 }
6345
6346 for (; i < ResNE; ++i)
6347 Scalars.push_back(getUNDEF(EltVT));
6348
6349 return getNode(ISD::BUILD_VECTOR, dl,
6350 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6351 &Scalars[0], Scalars.size());
6352}
6353
Evan Chengf5938d52009-12-09 01:36:00 +00006354
Wesley Peck527da1b2010-11-23 03:31:01 +00006355/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6356/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006357/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006358bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006359 unsigned Bytes, int Dist) const {
6360 if (LD->getChain() != Base->getChain())
6361 return false;
6362 EVT VT = LD->getValueType(0);
6363 if (VT.getSizeInBits() / 8 != Bytes)
6364 return false;
6365
6366 SDValue Loc = LD->getOperand(1);
6367 SDValue BaseLoc = Base->getOperand(1);
6368 if (Loc.getOpcode() == ISD::FrameIndex) {
6369 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6370 return false;
6371 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6372 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6373 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6374 int FS = MFI->getObjectSize(FI);
6375 int BFS = MFI->getObjectSize(BFI);
6376 if (FS != BFS || FS != (int)Bytes) return false;
6377 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6378 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006379
6380 // Handle X+C
6381 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6382 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6383 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006384
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006385 const GlobalValue *GV1 = NULL;
6386 const GlobalValue *GV2 = NULL;
Evan Chengf5938d52009-12-09 01:36:00 +00006387 int64_t Offset1 = 0;
6388 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006389 const TargetLowering *TLI = TM.getTargetLowering();
6390 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6391 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006392 if (isGA1 && isGA2 && GV1 == GV2)
6393 return Offset1 == (Offset2 + Dist*Bytes);
6394 return false;
6395}
6396
6397
Evan Cheng34a23ea2009-12-09 01:04:59 +00006398/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6399/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006400unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006401 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006402 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006403 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006404 const TargetLowering *TLI = TM.getTargetLowering();
6405 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006406 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006407 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00006408 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006409 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006410 unsigned AlignBits = KnownZero.countTrailingOnes();
6411 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6412 if (Align)
6413 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006414 }
Evan Chengd938faf2009-12-09 01:53:58 +00006415
Evan Cheng34a23ea2009-12-09 01:04:59 +00006416 // If this is a direct reference to a stack slot, use information about the
6417 // stack slot's alignment.
6418 int FrameIdx = 1 << 31;
6419 int64_t FrameOffset = 0;
6420 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6421 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006422 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006423 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006424 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006425 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6426 FrameOffset = Ptr.getConstantOperandVal(1);
6427 }
6428
6429 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006430 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006431 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6432 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006433 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006434 }
6435
6436 return 0;
6437}
6438
Juergen Ributzkab3487102013-11-19 21:20:17 +00006439/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6440/// which is split (or expanded) into two not necessarily identical pieces.
6441std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6442 // Currently all types are split in half.
6443 EVT LoVT, HiVT;
6444 if (!VT.isVector()) {
6445 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6446 } else {
6447 unsigned NumElements = VT.getVectorNumElements();
6448 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6449 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6450 NumElements/2);
6451 }
6452 return std::make_pair(LoVT, HiVT);
6453}
6454
6455/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6456/// low/high part.
6457std::pair<SDValue, SDValue>
6458SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6459 const EVT &HiVT) {
6460 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6461 N.getValueType().getVectorNumElements() &&
6462 "More vector elements requested than available!");
6463 SDValue Lo, Hi;
6464 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6465 getConstant(0, TLI->getVectorIdxTy()));
6466 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6467 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6468 return std::make_pair(Lo, Hi);
6469}
6470
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006471// getAddressSpace - Return the address space this GlobalAddress belongs to.
6472unsigned GlobalAddressSDNode::getAddressSpace() const {
6473 return getGlobal()->getType()->getAddressSpace();
6474}
6475
6476
Chris Lattner229907c2011-07-18 04:54:35 +00006477Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006478 if (isMachineConstantPoolEntry())
6479 return Val.MachineCPVal->getType();
6480 return Val.ConstVal->getType();
6481}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006482
Bob Wilson85cefe82009-03-02 23:24:16 +00006483bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6484 APInt &SplatUndef,
6485 unsigned &SplatBitSize,
6486 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006487 unsigned MinSplatBits,
6488 bool isBigEndian) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006489 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006490 assert(VT.isVector() && "Expected a vector type");
6491 unsigned sz = VT.getSizeInBits();
6492 if (MinSplatBits > sz)
6493 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006494
Bob Wilson85cefe82009-03-02 23:24:16 +00006495 SplatValue = APInt(sz, 0);
6496 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006497
Bob Wilson85cefe82009-03-02 23:24:16 +00006498 // Get the bits. Bits with undefined values (when the corresponding element
6499 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6500 // in SplatValue. If any of the values are not constant, give up and return
6501 // false.
6502 unsigned int nOps = getNumOperands();
6503 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6504 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006505
6506 for (unsigned j = 0; j < nOps; ++j) {
6507 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006508 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006509 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006510
Bob Wilson85cefe82009-03-02 23:24:16 +00006511 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006512 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006513 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006514 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006515 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006516 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006517 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006518 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006519 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006520 }
6521
Bob Wilson85cefe82009-03-02 23:24:16 +00006522 // The build_vector is all constants or undefs. Find the smallest element
6523 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006524
Bob Wilson85cefe82009-03-02 23:24:16 +00006525 HasAnyUndefs = (SplatUndef != 0);
6526 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006527
Bob Wilson85cefe82009-03-02 23:24:16 +00006528 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006529 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6530 APInt LowValue = SplatValue.trunc(HalfSize);
6531 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6532 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006533
Bob Wilson85cefe82009-03-02 23:24:16 +00006534 // If the two halves do not match (ignoring undef bits), stop here.
6535 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6536 MinSplatBits > HalfSize)
6537 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006538
Bob Wilson85cefe82009-03-02 23:24:16 +00006539 SplatValue = HighValue | LowValue;
6540 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006541
Bob Wilson85cefe82009-03-02 23:24:16 +00006542 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006543 }
6544
Bob Wilson85cefe82009-03-02 23:24:16 +00006545 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006546 return true;
6547}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006548
Juergen Ributzka73844052014-01-13 20:51:35 +00006549bool BuildVectorSDNode::isConstant() const {
6550 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6551 unsigned Opc = getOperand(i).getOpcode();
6552 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6553 return false;
6554 }
6555 return true;
6556}
6557
Owen Anderson53aa7a92009-08-10 22:56:29 +00006558bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006559 // Find the first non-undef value in the shuffle mask.
6560 unsigned i, e;
6561 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6562 /* search */;
6563
Nate Begeman39b59db2009-04-29 18:13:31 +00006564 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006565
Nate Begeman5f829d82009-04-29 05:20:52 +00006566 // Make sure all remaining elements are either undef or the same as the first
6567 // non-undef value.
6568 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006569 if (Mask[i] >= 0 && Mask[i] != Idx)
6570 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006571 return true;
6572}
David Greene09851602010-01-20 20:13:31 +00006573
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006574#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006575static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006576 SmallPtrSet<const SDNode*, 32> &Visited,
6577 SmallPtrSet<const SDNode*, 32> &Checked) {
6578 // If this node has already been checked, don't check it again.
6579 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006580 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006581
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006582 // If a node has already been visited on this depth-first walk, reject it as
6583 // a cycle.
6584 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006585 dbgs() << "Offending node:\n";
6586 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006587 errs() << "Detected cycle in SelectionDAG\n";
6588 abort();
David Greene09851602010-01-20 20:13:31 +00006589 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006590
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006591 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6592 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006593
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006594 Checked.insert(N);
6595 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006596}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006597#endif
David Greene09851602010-01-20 20:13:31 +00006598
6599void llvm::checkForCycles(const llvm::SDNode *N) {
6600#ifdef XDEBUG
Alp Toker6a033742013-10-29 02:35:28 +00006601 assert(N && "Checking nonexistent SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006602 SmallPtrSet<const SDNode*, 32> visited;
6603 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006604 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006605#endif
6606}
6607
6608void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6609 checkForCycles(DAG->getRoot().getNode());
6610}