blob: dacfa1ecd4b76c17509913ce268dbaa0a38db8da [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"
Chandler Carruth9aca9182014-01-07 12:34:26 +000036#include "llvm/IR/Writer.h"
Bill Wendlingbd092622008-09-30 21:22:07 +000037#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000040#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000041#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000042#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/Support/raw_ostream.h"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetIntrinsicInfo.h"
46#include "llvm/Target/TargetLowering.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetOptions.h"
49#include "llvm/Target/TargetRegisterInfo.h"
50#include "llvm/Target/TargetSelectionDAGInfo.h"
Jeff Cohen7d1670da2005-01-09 20:41:56 +000051#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000052#include <cmath>
Chris Lattner560b5e42004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000054
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000065
Jim Laskeyd66e6162005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
Dale Johannesenb6d2bec2007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000076}
77
Owen Anderson53aa7a92009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Dale Johannesend246b2c2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000089}
90
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000094
Evan Chengc70e33c2006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000098 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +000099 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Evan Chengc70e33c2006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000105
Chris Lattnerc2d28112006-03-25 22:57:01 +0000106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000109
Chris Lattnerc2d28112006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000112
Chris Lattnerc2d28112006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-03-21 17:48:04 +0000114 // elements. We have to be a bit careful here, as the type of the constant
115 // may not be the same as the type of the vector elements due to type
116 // legalization (the elements are promoted to a legal type for the target and
117 // a vector of a type may be legal when the base element type is not).
118 // We only want to check enough bits to cover the vector elements, because
119 // we care if the resultant vector is all ones, not whether the individual
120 // constants are.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000125 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000128 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000129 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Chris Lattnerc2d28112006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-03-21 17:48:04 +0000133 // undefs. Even with the above element type twiddling, this should be OK, as
134 // the same type legalization should have applied to all the elements.
Chris Lattnerc2d28112006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Chenga6789912006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +0000146 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +0000147 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chenga6789912006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Evan Chengc70e33c2006-03-27 06:58:47 +0000152 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000153
Evan Chengc70e33c2006-03-27 06:58:47 +0000154 // Skip over all of the undef values.
155 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
156 ++i;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000157
Evan Chengc70e33c2006-03-27 06:58:47 +0000158 // Do not accept an all-undef vector.
159 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000161 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chengc70e33c2006-03-27 06:58:47 +0000162 // elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000163 SDValue Zero = N->getOperand(i);
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000164 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
165 if (!CN->isNullValue())
Evan Chengc70e33c2006-03-27 06:58:47 +0000166 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000167 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
168 if (!CFPN->getValueAPF().isPosZero())
Evan Chengc70e33c2006-03-27 06:58:47 +0000169 return false;
170 } else
171 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000172
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000173 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chengc70e33c2006-03-27 06:58:47 +0000174 // undefs.
175 for (++i; i != e; ++i)
176 if (N->getOperand(i) != Zero &&
177 N->getOperand(i).getOpcode() != ISD::UNDEF)
178 return false;
179 return true;
Evan Chenga6789912006-03-26 09:50:58 +0000180}
181
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +0000182/// \brief Return true if the specified node is a BUILD_VECTOR node of
183/// all ConstantSDNode or undef.
184bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
185 if (N->getOpcode() != ISD::BUILD_VECTOR)
186 return false;
187
188 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
189 SDValue Op = N->getOperand(i);
190 if (Op.getOpcode() == ISD::UNDEF)
191 continue;
192 if (!isa<ConstantSDNode>(Op))
193 return false;
194 }
195 return true;
196}
197
Evan Cheng6200c222008-02-18 23:04:32 +0000198/// isScalarToVector - Return true if the specified node is a
199/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
200/// element is not an undef.
201bool ISD::isScalarToVector(const SDNode *N) {
202 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
203 return true;
204
205 if (N->getOpcode() != ISD::BUILD_VECTOR)
206 return false;
207 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
208 return false;
209 unsigned NumElems = N->getNumOperands();
Mon P Wang88ff56c2010-11-19 19:08:12 +0000210 if (NumElems == 1)
211 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000212 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000213 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000214 if (V.getOpcode() != ISD::UNDEF)
215 return false;
216 }
217 return true;
218}
219
Nadav Rotema62368c2012-07-15 08:38:23 +0000220/// allOperandsUndef - Return true if the node has at least one operand
221/// and all operands of the specified node are ISD::UNDEF.
222bool ISD::allOperandsUndef(const SDNode *N) {
223 // Return false if the node has no operands.
224 // This is "logically inconsistent" with the definition of "all" but
225 // is probably the desired behavior.
226 if (N->getNumOperands() == 0)
227 return false;
228
229 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
230 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
231 return false;
232
233 return true;
234}
235
Chris Lattner061a1ea2005-01-07 07:46:32 +0000236/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
237/// when given the operation for (X op Y).
238ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
239 // To perform this operation, we just need to swap the L and G bits of the
240 // operation.
241 unsigned OldL = (Operation >> 2) & 1;
242 unsigned OldG = (Operation >> 1) & 1;
243 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
244 (OldL << 1) | // New G bit
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000245 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000246}
247
248/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
249/// 'op' is a valid SetCC operation.
250ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
251 unsigned Operation = Op;
252 if (isInteger)
253 Operation ^= 7; // Flip L, G, E bits, but not U.
254 else
255 Operation ^= 15; // Flip all of the condition bits.
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000256
Chris Lattner061a1ea2005-01-07 07:46:32 +0000257 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000258 Operation &= ~8; // Don't let N and U bits get set.
259
Chris Lattner061a1ea2005-01-07 07:46:32 +0000260 return ISD::CondCode(Operation);
261}
262
263
264/// isSignedOp - For an integer comparison, return 1 if the comparison is a
265/// signed operation and 2 if the result is an unsigned comparison. Return zero
266/// if the operation does not depend on the sign of the input (setne and seteq).
267static int isSignedOp(ISD::CondCode Opcode) {
268 switch (Opcode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000269 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-01-07 07:46:32 +0000270 case ISD::SETEQ:
271 case ISD::SETNE: return 0;
272 case ISD::SETLT:
273 case ISD::SETLE:
274 case ISD::SETGT:
275 case ISD::SETGE: return 1;
276 case ISD::SETULT:
277 case ISD::SETULE:
278 case ISD::SETUGT:
279 case ISD::SETUGE: return 2;
280 }
281}
282
283/// getSetCCOrOperation - Return the result of a logical OR between different
284/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
285/// returns SETCC_INVALID if it is not possible to represent the resultant
286/// comparison.
287ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
288 bool isInteger) {
289 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
290 // Cannot fold a signed integer setcc with an unsigned integer setcc.
291 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000292
Chris Lattner061a1ea2005-01-07 07:46:32 +0000293 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000294
Chris Lattner061a1ea2005-01-07 07:46:32 +0000295 // If the N and U bits get set then the resultant comparison DOES suddenly
296 // care about orderedness, and is true when ordered.
297 if (Op > ISD::SETTRUE2)
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000298 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000299
Chris Lattner8c02c3f2006-05-12 17:03:46 +0000300 // Canonicalize illegal integer setcc's.
301 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
302 Op = ISD::SETNE;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000303
Chris Lattner061a1ea2005-01-07 07:46:32 +0000304 return ISD::CondCode(Op);
305}
306
307/// getSetCCAndOperation - Return the result of a logical AND between different
308/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
309/// function returns zero if it is not possible to represent the resultant
310/// comparison.
311ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
312 bool isInteger) {
313 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
314 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000315 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000316
317 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000318 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Chris Lattner393d96a2006-04-27 05:01:07 +0000320 // Canonicalize illegal integer setcc's.
321 if (isInteger) {
322 switch (Result) {
323 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000324 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000325 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-06-28 18:29:47 +0000326 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
327 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
328 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattner393d96a2006-04-27 05:01:07 +0000329 }
330 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000331
Chris Lattner393d96a2006-04-27 05:01:07 +0000332 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000333}
334
Jim Laskeyd66e6162005-08-17 20:08:02 +0000335//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000336// SDNode Profile Support
337//===----------------------------------------------------------------------===//
338
Jim Laskeybd0f0882006-10-27 23:52:51 +0000339/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
340///
Jim Laskeyf576b422006-10-27 23:46:08 +0000341static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
342 ID.AddInteger(OpC);
343}
344
345/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
346/// solely with their pointer.
Dan Gohmand78c4002008-05-13 00:00:25 +0000347static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000348 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000349}
350
Jim Laskeybd0f0882006-10-27 23:52:51 +0000351/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
352///
Jim Laskeyf576b422006-10-27 23:46:08 +0000353static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000354 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf17b4222007-02-04 07:28:00 +0000355 for (; NumOps; --NumOps, ++Ops) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000356 ID.AddPointer(Ops->getNode());
Gabor Greifabfdf922008-08-26 22:36:50 +0000357 ID.AddInteger(Ops->getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000358 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000359}
360
Dan Gohman768f2c92008-07-07 18:26:29 +0000361/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
362///
363static void AddNodeIDOperands(FoldingSetNodeID &ID,
364 const SDUse *Ops, unsigned NumOps) {
365 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000366 ID.AddPointer(Ops->getNode());
367 ID.AddInteger(Ops->getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000368 }
369}
370
Jim Laskeyf576b422006-10-27 23:46:08 +0000371static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelcf0da6c2009-02-17 22:15:04 +0000372 unsigned short OpC, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000373 const SDValue *OpList, unsigned N) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000374 AddNodeIDOpcode(ID, OpC);
375 AddNodeIDValueTypes(ID, VTList);
376 AddNodeIDOperands(ID, OpList, N);
377}
378
Duncan Sands835bdca2008-10-27 15:30:53 +0000379/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
380/// the NodeID data.
381static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000382 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000383 case ISD::TargetExternalSymbol:
384 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000385 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000386 default: break; // Normal nodes don't need extra info.
387 case ISD::TargetConstant:
388 case ISD::Constant:
Dan Gohmanec270fb2008-09-12 18:08:03 +0000389 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000390 break;
391 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000392 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000393 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000394 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000395 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000396 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000397 case ISD::GlobalAddress:
398 case ISD::TargetGlobalTLSAddress:
399 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000400 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000401 ID.AddPointer(GA->getGlobal());
402 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000403 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000404 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000405 break;
406 }
407 case ISD::BasicBlock:
408 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
409 break;
410 case ISD::Register:
411 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
412 break;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +0000413 case ISD::RegisterMask:
414 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
415 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000416 case ISD::SRCVALUE:
417 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
418 break;
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000419 case ISD::FrameIndex:
420 case ISD::TargetFrameIndex:
421 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
422 break;
423 case ISD::JumpTable:
424 case ISD::TargetJumpTable:
425 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000426 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000427 break;
428 case ISD::ConstantPool:
429 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000430 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000431 ID.AddInteger(CP->getAlignment());
432 ID.AddInteger(CP->getOffset());
433 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000434 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000435 else
436 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000437 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000438 break;
439 }
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +0000440 case ISD::TargetIndex: {
441 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
442 ID.AddInteger(TI->getIndex());
443 ID.AddInteger(TI->getOffset());
444 ID.AddInteger(TI->getTargetFlags());
445 break;
446 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000447 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000448 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000449 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000450 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000451 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000452 break;
453 }
454 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000455 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000456 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000457 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000458 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000459 break;
460 }
Dan Gohman12f24902008-12-23 21:37:04 +0000461 case ISD::ATOMIC_CMP_SWAP:
462 case ISD::ATOMIC_SWAP:
463 case ISD::ATOMIC_LOAD_ADD:
464 case ISD::ATOMIC_LOAD_SUB:
465 case ISD::ATOMIC_LOAD_AND:
466 case ISD::ATOMIC_LOAD_OR:
467 case ISD::ATOMIC_LOAD_XOR:
468 case ISD::ATOMIC_LOAD_NAND:
469 case ISD::ATOMIC_LOAD_MIN:
470 case ISD::ATOMIC_LOAD_MAX:
471 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman342e8df2011-08-24 20:50:09 +0000472 case ISD::ATOMIC_LOAD_UMAX:
473 case ISD::ATOMIC_LOAD:
474 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000475 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000476 ID.AddInteger(AT->getMemoryVT().getRawBits());
477 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000478 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
479 break;
480 }
481 case ISD::PREFETCH: {
482 const MemSDNode *PF = cast<MemSDNode>(N);
483 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang6a490372008-06-25 08:15:39 +0000484 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000485 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000486 case ISD::VECTOR_SHUFFLE: {
487 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000488 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000489 i != e; ++i)
490 ID.AddInteger(SVN->getMaskElt(i));
491 break;
492 }
Dan Gohman6c938802009-10-30 01:27:03 +0000493 case ISD::TargetBlockAddress:
494 case ISD::BlockAddress: {
Michael Liaoabb87d42012-09-12 21:43:09 +0000495 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
496 ID.AddPointer(BA->getBlockAddress());
497 ID.AddInteger(BA->getOffset());
498 ID.AddInteger(BA->getTargetFlags());
Dan Gohman6c938802009-10-30 01:27:03 +0000499 break;
500 }
Mon P Wang6a490372008-06-25 08:15:39 +0000501 } // end switch (N->getOpcode())
Pete Cooper91244262012-07-30 20:23:19 +0000502
503 // Target specific memory nodes could also have address spaces to check.
504 if (N->isTargetMemoryOpcode())
505 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskeyf576b422006-10-27 23:46:08 +0000506}
507
Duncan Sands835bdca2008-10-27 15:30:53 +0000508/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
509/// data.
510static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
511 AddNodeIDOpcode(ID, N->getOpcode());
512 // Add the return value info.
513 AddNodeIDValueTypes(ID, N->getVTList());
514 // Add the operand info.
515 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
516
517 // Handle SDNode leafs with special info.
518 AddNodeIDCustom(ID, N);
519}
520
Dan Gohman2da2bed2008-08-20 15:58:01 +0000521/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000522/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000523/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000524///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000525static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000526encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000527 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-02-03 00:08:45 +0000528 assert((ConvType & 3) == ConvType &&
529 "ConvType may not require more than 2 bits!");
530 assert((AM & 7) == AM &&
531 "AM may not require more than 3 bits!");
532 return ConvType |
533 (AM << 2) |
David Greeneb7941b02010-02-17 20:21:42 +0000534 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000535 (isNonTemporal << 6) |
536 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000537}
538
Jim Laskeyf576b422006-10-27 23:46:08 +0000539//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000540// SelectionDAG Class
541//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000542
Duncan Sands835bdca2008-10-27 15:30:53 +0000543/// doNotCSE - Return true if CSE should not be performed for this node.
544static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000545 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000546 return true; // Never CSE anything that produces a flag.
547
548 switch (N->getOpcode()) {
549 default: break;
550 case ISD::HANDLENODE:
Duncan Sands835bdca2008-10-27 15:30:53 +0000551 case ISD::EH_LABEL:
Duncan Sands835bdca2008-10-27 15:30:53 +0000552 return true; // Never CSE these nodes.
553 }
554
555 // Check that remaining values produced are not flags.
556 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000557 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000558 return true; // Never CSE anything that produces a flag.
559
560 return false;
561}
562
Chris Lattner9c667932005-01-07 21:09:16 +0000563/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000564/// SelectionDAG.
565void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-01-07 21:09:16 +0000566 // Create a dummy node (which is not added to allnodes), that adds a reference
567 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000568 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000569
Chris Lattner8927c872006-08-04 17:45:20 +0000570 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000571
Chris Lattner8927c872006-08-04 17:45:20 +0000572 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000573 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000574 if (I->use_empty())
575 DeadNodes.push_back(I);
576
Dan Gohman91697632008-07-07 20:57:48 +0000577 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000578
Dan Gohman91697632008-07-07 20:57:48 +0000579 // If the root changed (e.g. it was a dead load, update the root).
580 setRoot(Dummy.getValue());
581}
582
583/// RemoveDeadNodes - This method deletes the unreachable nodes in the
584/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000585void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000586
Chris Lattner8927c872006-08-04 17:45:20 +0000587 // Process the worklist, deleting the nodes and adding their uses to the
588 // worklist.
589 while (!DeadNodes.empty()) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000590 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000591
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000592 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
593 DUL->NodeDeleted(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000594
Chris Lattner8927c872006-08-04 17:45:20 +0000595 // Take the node out of the appropriate CSE map.
596 RemoveNodeFromCSEMaps(N);
597
598 // Next, brutally remove the operand list. This is safe to do, as there are
599 // no cycles in the graph.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000600 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
601 SDUse &Use = *I++;
602 SDNode *Operand = Use.getNode();
603 Use.set(SDValue());
604
Chris Lattner8927c872006-08-04 17:45:20 +0000605 // Now that we removed this operand, see if there are no uses of it left.
606 if (Operand->use_empty())
607 DeadNodes.push_back(Operand);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000608 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000609
Dan Gohman534c8a22009-01-19 22:39:36 +0000610 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000611 }
Chris Lattner9c667932005-01-07 21:09:16 +0000612}
613
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000614void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000615 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-11-08 01:25:24 +0000616
617 // Create a dummy node that adds a reference to the root node, preventing
618 // it from being deleted. (This matters if the root is an operand of the
619 // dead node.)
620 HandleSDNode Dummy(getRoot());
621
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000622 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000623}
624
Chris Lattnerc738d002005-08-29 21:59:31 +0000625void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000626 // First take this out of the appropriate CSE map.
627 RemoveNodeFromCSEMaps(N);
628
Scott Michelcf0da6c2009-02-17 22:15:04 +0000629 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000630 // AllNodes list, and delete the node.
631 DeleteNodeNotInCSEMaps(N);
632}
633
634void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-01-25 16:20:37 +0000635 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
636 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman534c8a22009-01-19 22:39:36 +0000637
Dan Gohman86aa16a2008-09-30 18:30:35 +0000638 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000639 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000640
Dan Gohman534c8a22009-01-19 22:39:36 +0000641 DeallocateNode(N);
642}
643
644void SelectionDAG::DeallocateNode(SDNode *N) {
645 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000646 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000647
Dan Gohman534c8a22009-01-19 22:39:36 +0000648 // Set the opcode to DELETED_NODE to help catch bugs when node
649 // memory is reallocated.
650 N->NodeType = ISD::DELETED_NODE;
651
Dan Gohman2e834902008-08-26 01:44:34 +0000652 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000653
Evan Cheng563fe3c2010-03-25 01:38:16 +0000654 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000655 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000656 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
657 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000658}
659
Chris Lattner19732782005-08-16 18:17:10 +0000660/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
661/// correspond to it. This is useful when we're about to delete or repurpose
662/// the node. We don't want future request for structurally identical nodes
663/// to return N anymore.
Dan Gohmand3fe1742008-09-13 01:54:27 +0000664bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000665 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000666 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000667 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000668 case ISD::CONDCODE:
669 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
670 "Cond code doesn't exist!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000671 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000672 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
673 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000674 case ISD::ExternalSymbol:
675 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000676 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000677 case ISD::TargetExternalSymbol: {
678 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
679 Erased = TargetExternalSymbols.erase(
680 std::pair<std::string,unsigned char>(ESN->getSymbol(),
681 ESN->getTargetFlags()));
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000682 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000683 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000684 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000685 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000686 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000687 Erased = ExtendedValueTypeNodes.erase(VT);
688 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000689 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
690 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000691 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000692 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000693 }
Chris Lattner9c667932005-01-07 21:09:16 +0000694 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000695 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-12-12 13:22:50 +0000696 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
697 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattnerfcb16472006-08-11 18:38:11 +0000698 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000699 break;
700 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000701#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000702 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-09-02 19:15:44 +0000703 // flag result (which cannot be CSE'd) or is one of the special cases that are
704 // not subject to CSE.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000705 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000706 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000707 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000708 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000709 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000710 }
711#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000712 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000713}
714
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000715/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
716/// maps and modified in place. Add it back to the CSE maps, unless an identical
717/// node already exists, in which case transfer all its users to the existing
718/// node. This transfer can potentially trigger recursive merging.
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000719///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000720void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000721SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000722 // For node types that aren't CSE'd, just act as if no identical node
723 // already exists.
724 if (!doNotCSE(N)) {
725 SDNode *Existing = CSEMap.GetOrInsertNode(N);
726 if (Existing != N) {
727 // If there was already an existing matching node, use ReplaceAllUsesWith
728 // to replace the dead one with the existing one. This can cause
729 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000730 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000731
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000732 // N is now dead. Inform the listeners and delete it.
733 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
734 DUL->NodeDeleted(N, Existing);
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000735 DeleteNodeNotInCSEMaps(N);
736 return;
737 }
738 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000739
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000740 // If the node doesn't already exist, we updated it. Inform listeners.
741 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
742 DUL->NodeUpdated(N);
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000743}
744
Chris Lattnerf34156e2006-01-28 09:32:45 +0000745/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000746/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000747/// return null, otherwise return a pointer to the slot it would take. If a
748/// node already exists with these operands, the slot will be non-null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000749SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000750 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000751 if (doNotCSE(N))
752 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000753
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000754 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000755 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000756 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands835bdca2008-10-27 15:30:53 +0000757 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000758 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000759 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000760}
761
762/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000763/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-01-28 09:32:45 +0000764/// return null, otherwise return a pointer to the slot it would take. If a
765/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000766SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000767 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000768 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000769 if (doNotCSE(N))
770 return 0;
771
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000772 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000773 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000774 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands835bdca2008-10-27 15:30:53 +0000775 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000776 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000777 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000778}
779
780
781/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000782/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000783/// return null, otherwise return a pointer to the slot it would take. If a
784/// node already exists with these operands, the slot will be non-null.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000785SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000786 const SDValue *Ops,unsigned NumOps,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000787 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000788 if (doNotCSE(N))
789 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000790
Jim Laskeyf576b422006-10-27 23:46:08 +0000791 FoldingSetNodeID ID;
Dan Gohman92a7f3a2007-06-04 15:49:41 +0000792 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands835bdca2008-10-27 15:30:53 +0000793 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000794 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000795 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000796}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000797
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000798#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000799/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
800static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsb0e39382008-07-21 10:20:31 +0000801 switch (N->getOpcode()) {
802 default:
803 break;
Duncan Sands17e678b2008-10-29 14:22:20 +0000804 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000805 EVT VT = N->getValueType(0);
Duncan Sands17e678b2008-10-29 14:22:20 +0000806 assert(N->getNumValues() == 1 && "Too many results!");
807 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
808 "Wrong return type!");
809 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
810 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
811 "Mismatched operand types!");
812 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
813 "Wrong operand type!");
814 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
815 "Wrong return type size");
816 break;
817 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000818 case ISD::BUILD_VECTOR: {
Duncan Sands17e678b2008-10-29 14:22:20 +0000819 assert(N->getNumValues() == 1 && "Too many results!");
820 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsb0e39382008-07-21 10:20:31 +0000821 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands17e678b2008-10-29 14:22:20 +0000822 "Wrong number of operands!");
Owen Anderson53aa7a92009-08-10 22:56:29 +0000823 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedmanb7910b72011-09-09 21:04:06 +0000824 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindolab93db662009-04-24 12:40:33 +0000825 assert((I->getValueType() == EltVT ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000826 (EltVT.isInteger() && I->getValueType().isInteger() &&
827 EltVT.bitsLE(I->getValueType()))) &&
828 "Wrong operand type!");
Eli Friedmanb7910b72011-09-09 21:04:06 +0000829 assert(I->getValueType() == N->getOperand(0).getValueType() &&
830 "Operands must all have the same type");
831 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000832 break;
833 }
834 }
835}
836
Duncan Sands7c601de2010-11-20 11:25:00 +0000837/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
838static void VerifySDNode(SDNode *N) {
839 // The SDNode allocators cannot be used to allocate nodes with fields that are
840 // not present in an SDNode!
841 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
842 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
843 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
844 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
845 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
846 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
847 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
848 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
849 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
850 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
851 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
852 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
853 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
854 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
855 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
856 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
857 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
858 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
859 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
860
861 VerifyNodeCommon(N);
862}
863
864/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
865/// invalid.
866static void VerifyMachineNode(SDNode *N) {
867 // The MachineNode allocators cannot be used to allocate nodes with fields
868 // that are not present in a MachineNode!
869 // Currently there are no such nodes.
870
871 VerifyNodeCommon(N);
872}
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000873#endif // NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000874
Owen Anderson53aa7a92009-08-10 22:56:29 +0000875/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000876/// given type.
877///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000878unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000879 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000880 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000881 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000882
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000883 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000884}
Chris Lattner9c667932005-01-07 21:09:16 +0000885
Dale Johannesen8ba713212009-02-07 02:15:05 +0000886// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000887SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Juergen Ributzkab3487102013-11-19 21:20:17 +0000888 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TTI(0), TLI(0), OptLevel(OL),
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000889 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sanders50b80412013-11-15 12:56:49 +0000890 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
891 UpdateListeners(0) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000892 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000893 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000894}
895
Juergen Ributzkab3487102013-11-19 21:20:17 +0000896void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti,
897 const TargetLowering *tli) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000898 MF = &mf;
Chandler Carruth42e96112013-01-05 12:32:17 +0000899 TTI = tti;
Juergen Ributzkab3487102013-11-19 21:20:17 +0000900 TLI = tli;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000901 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000902}
903
Chris Lattner600d3082003-08-11 14:57:33 +0000904SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000905 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000906 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000907 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000908}
909
910void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000911 assert(&*AllNodes.begin() == &EntryNode);
912 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000913 while (!AllNodes.empty())
914 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000915}
916
Dan Gohmane1a9a782008-08-27 23:52:12 +0000917void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000918 allnodes_clear();
919 OperandAllocator.Reset();
920 CSEMap.clear();
921
922 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000923 ExternalSymbols.clear();
924 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000925 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
926 static_cast<CondCodeSDNode*>(0));
927 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
928 static_cast<SDNode*>(0));
929
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000930 EntryNode.UseList = 0;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000931 AllNodes.push_back(&EntryNode);
932 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000933 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000934}
935
Andrew Trickef9de2a2013-05-25 02:42:55 +0000936SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000937 return VT.bitsGT(Op.getValueType()) ?
938 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
939 getNode(ISD::TRUNCATE, DL, VT, Op);
940}
941
Andrew Trickef9de2a2013-05-25 02:42:55 +0000942SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000943 return VT.bitsGT(Op.getValueType()) ?
944 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
945 getNode(ISD::TRUNCATE, DL, VT, Op);
946}
947
Andrew Trickef9de2a2013-05-25 02:42:55 +0000948SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000949 return VT.bitsGT(Op.getValueType()) ?
950 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
951 getNode(ISD::TRUNCATE, DL, VT, Op);
952}
953
Andrew Trickef9de2a2013-05-25 02:42:55 +0000954SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000955 assert(!VT.isVector() &&
956 "getZeroExtendInReg should use the vector element type instead of "
957 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000958 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000959 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
960 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000961 VT.getSizeInBits());
962 return getNode(ISD::AND, DL, Op.getValueType(), Op,
963 getConstant(Imm, Op.getValueType()));
964}
965
Bob Wilsonc5890052009-01-22 17:39:32 +0000966/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
967///
Andrew Trickef9de2a2013-05-25 02:42:55 +0000968SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000969 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +0000970 SDValue NegOne =
971 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +0000972 return getNode(ISD::XOR, DL, VT, Val, NegOne);
973}
974
Owen Anderson53aa7a92009-08-10 22:56:29 +0000975SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000976 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +0000977 assert((EltVT.getSizeInBits() >= 64 ||
978 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
979 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands13237ac2008-06-06 12:08:01 +0000980 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman65f63eb2008-02-08 22:59:30 +0000981}
982
Owen Anderson53aa7a92009-08-10 22:56:29 +0000983SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000984 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000985}
986
Owen Anderson53aa7a92009-08-10 22:56:29 +0000987SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000988 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +0000989
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000990 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +0000991 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +0000992
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000993 const TargetLowering *TLI = TM.getTargetLowering();
994
Duncan Sandsf2641e12011-09-06 19:07:46 +0000995 // In some cases the vector type is legal but the element type is illegal and
996 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
997 // inserted value (the type does not need to match the vector element type).
998 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000999 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001000 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001001 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001002 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1003 Elt = ConstantInt::get(*getContext(), NewVal);
1004 }
Daniel Sanders50b80412013-11-15 12:56:49 +00001005 // In other cases the element type is illegal and needs to be expanded, for
1006 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1007 // the value into n parts and use a vector type with n-times the elements.
1008 // Then bitcast to the type requested.
1009 // Legalizing constants too early makes the DAGCombiner's job harder so we
1010 // only legalize if the DAG tells us we must produce legal types.
1011 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1012 TLI->getTypeAction(*getContext(), EltVT) ==
1013 TargetLowering::TypeExpandInteger) {
1014 APInt NewVal = Elt->getValue();
1015 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1016 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1017 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1018 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1019
1020 // Check the temporary vector is the correct size. If this fails then
1021 // getTypeToTransformTo() probably returned a type whose size (in bits)
1022 // isn't a power-of-2 factor of the requested type size.
1023 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1024
1025 SmallVector<SDValue, 2> EltParts;
1026 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1027 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1028 .trunc(ViaEltSizeInBits),
1029 ViaEltVT, isT));
1030 }
1031
1032 // EltParts is currently in little endian order. If we actually want
1033 // big-endian order then reverse it now.
1034 if (TLI->isBigEndian())
1035 std::reverse(EltParts.begin(), EltParts.end());
1036
1037 // The elements must be reversed when the element order is different
1038 // to the endianness of the elements (because the BITCAST is itself a
1039 // vector shuffle in this situation). However, we do not need any code to
1040 // perform this reversal because getConstant() is producing a vector
1041 // splat.
1042 // This situation occurs in MIPS MSA.
1043
1044 SmallVector<SDValue, 8> Ops;
1045 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1046 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1047
1048 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1049 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
1050 &Ops[0], Ops.size()));
1051 return Result;
1052 }
Duncan Sandsf2641e12011-09-06 19:07:46 +00001053
1054 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1055 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +00001056 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +00001057 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001058 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001059 ID.AddPointer(Elt);
Chris Lattner3f16b202006-08-11 21:01:22 +00001060 void *IP = 0;
Dan Gohman7a7742c2007-12-12 22:21:26 +00001061 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +00001062 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001063 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001064 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001065
Dan Gohman7a7742c2007-12-12 22:21:26 +00001066 if (!N) {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001067 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001068 CSEMap.InsertNode(N, IP);
1069 AllNodes.push_back(N);
1070 }
1071
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001072 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001073 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001074 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001075 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001076 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7a7742c2007-12-12 22:21:26 +00001077 }
1078 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001079}
1080
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001081SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001082 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001083}
1084
1085
Owen Anderson53aa7a92009-08-10 22:56:29 +00001086SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001087 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001088}
1089
Owen Anderson53aa7a92009-08-10 22:56:29 +00001090SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001091 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001092
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001093 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001094
Chris Lattner381dddc2005-02-17 20:17:32 +00001095 // Do the map lookup using the actual bit pattern for the floating point
1096 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1097 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001098 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001099 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001100 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001101 ID.AddPointer(&V);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001102 void *IP = 0;
Evan Cheng9458e6a2007-06-29 21:36:04 +00001103 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +00001104 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001105 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001106 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001107
Evan Cheng9458e6a2007-06-29 21:36:04 +00001108 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001109 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001110 CSEMap.InsertNode(N, IP);
1111 AllNodes.push_back(N);
1112 }
1113
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001114 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001115 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001116 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001117 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001118 // FIXME SDLoc info might be appropriate here
1119 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohmana8665142007-06-25 16:23:39 +00001120 }
1121 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001122}
1123
Owen Anderson53aa7a92009-08-10 22:56:29 +00001124SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001125 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001126 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001127 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001128 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001129 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001130 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1131 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001132 bool ignored;
1133 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001134 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001135 &ignored);
1136 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001137 } else
1138 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001139}
1140
Andrew Trickef9de2a2013-05-25 02:42:55 +00001141SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001142 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001143 bool isTargetGA,
1144 unsigned char TargetFlags) {
1145 assert((TargetFlags == 0 || isTargetGA) &&
1146 "Cannot set target flags on target-independent globals");
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001147 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopherdfda92b2009-08-22 00:40:45 +00001148
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001149 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault36f5eb52013-11-17 00:06:39 +00001150 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001151 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001152 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001153
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001154 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1155 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001156 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001157 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov1a114042008-09-09 20:05:04 +00001158 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001159 }
1160
Chris Lattner8e34f982009-06-25 21:21:14 +00001161 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001162 if (GVar && GVar->isThreadLocal())
1163 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1164 else
1165 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001166
Jim Laskeyf576b422006-10-27 23:46:08 +00001167 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001168 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001169 ID.AddPointer(GV);
1170 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001171 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001172 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner3f16b202006-08-11 21:01:22 +00001173 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001174 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001175 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001176
Andrew Trickef9de2a2013-05-25 02:42:55 +00001177 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1178 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001179 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001180 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001181 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001182 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001183}
1184
Owen Anderson53aa7a92009-08-10 22:56:29 +00001185SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001186 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001187 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001188 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001189 ID.AddInteger(FI);
1190 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001191 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001192 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001193
Dan Gohman01c65a22010-03-18 18:49:47 +00001194 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001195 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001196 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001197 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001198}
1199
Owen Anderson53aa7a92009-08-10 22:56:29 +00001200SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001201 unsigned char TargetFlags) {
1202 assert((TargetFlags == 0 || isTarget) &&
1203 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001204 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001205 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001206 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001207 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001208 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001209 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001210 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001211 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001212
Dan Gohman01c65a22010-03-18 18:49:47 +00001213 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1214 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001215 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001216 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001217 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001218}
1219
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001220SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001221 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001222 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001223 unsigned char TargetFlags) {
1224 assert((TargetFlags == 0 || isTarget) &&
1225 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001226 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001227 Alignment =
1228 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001229 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001230 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001231 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001232 ID.AddInteger(Alignment);
1233 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001234 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001235 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001236 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001237 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001239
Dan Gohman01c65a22010-03-18 18:49:47 +00001240 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1241 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001242 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001243 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001244 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001245}
1246
Chris Lattner061a1ea2005-01-07 07:46:32 +00001247
Owen Anderson53aa7a92009-08-10 22:56:29 +00001248SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001249 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001250 bool isTarget,
1251 unsigned char TargetFlags) {
1252 assert((TargetFlags == 0 || isTarget) &&
1253 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001254 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001255 Alignment =
1256 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001257 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001258 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001259 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001260 ID.AddInteger(Alignment);
1261 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001262 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001263 ID.AddInteger(TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001264 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001265 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001266 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001267
Dan Gohman01c65a22010-03-18 18:49:47 +00001268 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1269 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001270 CSEMap.InsertNode(N, IP);
1271 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001272 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001273}
1274
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001275SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1276 unsigned char TargetFlags) {
1277 FoldingSetNodeID ID;
1278 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1279 ID.AddInteger(Index);
1280 ID.AddInteger(Offset);
1281 ID.AddInteger(TargetFlags);
1282 void *IP = 0;
1283 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1284 return SDValue(E, 0);
1285
1286 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1287 TargetFlags);
1288 CSEMap.InsertNode(N, IP);
1289 AllNodes.push_back(N);
1290 return SDValue(N, 0);
1291}
1292
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001293SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001294 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001295 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001296 ID.AddPointer(MBB);
1297 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001298 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001299 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001300
Dan Gohman01c65a22010-03-18 18:49:47 +00001301 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001302 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001303 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001304 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001305}
1306
Owen Anderson53aa7a92009-08-10 22:56:29 +00001307SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001308 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1309 ValueTypeNodes.size())
1310 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001311
Duncan Sands13237ac2008-06-06 12:08:01 +00001312 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001313 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001314
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001315 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001316 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001317 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001318 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001319}
1320
Owen Anderson53aa7a92009-08-10 22:56:29 +00001321SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001322 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001323 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001324 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001325 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001326 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001327}
1328
Owen Anderson53aa7a92009-08-10 22:56:29 +00001329SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001330 unsigned char TargetFlags) {
1331 SDNode *&N =
1332 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1333 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001334 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001335 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001336 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001337 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001338}
1339
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001340SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001341 if ((unsigned)Cond >= CondCodeNodes.size())
1342 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001343
Chris Lattner14e060f2005-08-09 20:40:02 +00001344 if (CondCodeNodes[Cond] == 0) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001345 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001346 CondCodeNodes[Cond] = N;
1347 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001348 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001349
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001350 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001351}
1352
Nate Begeman5f829d82009-04-29 05:20:52 +00001353// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1354// the shuffle mask M that point at N1 to point at N2, and indices that point
1355// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001356static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1357 std::swap(N1, N2);
1358 int NElts = M.size();
1359 for (int i = 0; i != NElts; ++i) {
1360 if (M[i] >= NElts)
1361 M[i] -= NElts;
1362 else if (M[i] >= 0)
1363 M[i] += NElts;
1364 }
1365}
1366
Andrew Trickef9de2a2013-05-25 02:42:55 +00001367SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001368 SDValue N2, const int *Mask) {
Craig Topper0ecb26a2013-08-09 04:37:24 +00001369 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1370 "Invalid VECTOR_SHUFFLE");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001371
1372 // Canonicalize shuffle undef, undef -> undef
1373 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001374 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001375
Eric Christopherdfda92b2009-08-22 00:40:45 +00001376 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001377 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001378 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001379 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001380 for (unsigned i = 0; i != NElts; ++i) {
1381 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001382 MaskVec.push_back(Mask[i]);
1383 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001384
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001385 // Canonicalize shuffle v, v -> v, undef
1386 if (N1 == N2) {
1387 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001388 for (unsigned i = 0; i != NElts; ++i)
1389 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001390 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001391
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001392 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1393 if (N1.getOpcode() == ISD::UNDEF)
1394 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001395
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001396 // Canonicalize all index into lhs, -> shuffle lhs, undef
1397 // Canonicalize all index into rhs, -> shuffle rhs, undef
1398 bool AllLHS = true, AllRHS = true;
1399 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001400 for (unsigned i = 0; i != NElts; ++i) {
1401 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001402 if (N2Undef)
1403 MaskVec[i] = -1;
1404 else
1405 AllLHS = false;
1406 } else if (MaskVec[i] >= 0) {
1407 AllRHS = false;
1408 }
1409 }
1410 if (AllLHS && AllRHS)
1411 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001412 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001413 N2 = getUNDEF(VT);
1414 if (AllRHS) {
1415 N1 = getUNDEF(VT);
1416 commuteShuffle(N1, N2, MaskVec);
1417 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001418
Craig Topper9a39b072013-08-08 08:03:12 +00001419 // If Identity shuffle return that node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001420 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001421 for (unsigned i = 0; i != NElts; ++i) {
1422 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001423 }
Craig Topper0ecb26a2013-08-09 04:37:24 +00001424 if (Identity && NElts)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001425 return N1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001426
1427 FoldingSetNodeID ID;
1428 SDValue Ops[2] = { N1, N2 };
1429 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5f829d82009-04-29 05:20:52 +00001430 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001431 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001432
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001433 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001435 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001436
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001437 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1438 // SDNode doesn't have access to it. This memory will be "leaked" when
1439 // the node is deallocated, but recovered when the NodeAllocator is released.
1440 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1441 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001442
Dan Gohman01c65a22010-03-18 18:49:47 +00001443 ShuffleVectorSDNode *N =
Jack Carter170a5f22013-09-09 22:02:08 +00001444 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1445 dl.getDebugLoc(), N1, N2,
1446 MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001447 CSEMap.InsertNode(N, IP);
1448 AllNodes.push_back(N);
1449 return SDValue(N, 0);
1450}
1451
Andrew Trickef9de2a2013-05-25 02:42:55 +00001452SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001453 SDValue Val, SDValue DTy,
1454 SDValue STy, SDValue Rnd, SDValue Sat,
1455 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001456 // If the src and dest types are the same and the conversion is between
1457 // integer types of the same sign or two floats, no conversion is necessary.
1458 if (DTy == STy &&
1459 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001460 return Val;
1461
1462 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001463 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1464 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001465 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001466 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001467 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001468
Jack Carter170a5f22013-09-09 22:02:08 +00001469 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1470 dl.getDebugLoc(),
1471 Ops, 5, Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001472 CSEMap.InsertNode(N, IP);
1473 AllNodes.push_back(N);
1474 return SDValue(N, 0);
1475}
1476
Owen Anderson53aa7a92009-08-10 22:56:29 +00001477SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001478 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001479 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001480 ID.AddInteger(RegNo);
1481 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001482 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001483 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001484
Dan Gohman01c65a22010-03-18 18:49:47 +00001485 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001486 CSEMap.InsertNode(N, IP);
1487 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001488 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001489}
1490
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001491SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1492 FoldingSetNodeID ID;
1493 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1494 ID.AddPointer(RegMask);
1495 void *IP = 0;
1496 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1497 return SDValue(E, 0);
1498
1499 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1500 CSEMap.InsertNode(N, IP);
1501 AllNodes.push_back(N);
1502 return SDValue(N, 0);
1503}
1504
Andrew Trickef9de2a2013-05-25 02:42:55 +00001505SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001506 FoldingSetNodeID ID;
1507 SDValue Ops[] = { Root };
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001508 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1509 ID.AddPointer(Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001510 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001511 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001512 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001513
Jack Carter170a5f22013-09-09 22:02:08 +00001514 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1515 dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001516 CSEMap.InsertNode(N, IP);
1517 AllNodes.push_back(N);
1518 return SDValue(N, 0);
1519}
1520
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001521
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001522SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001523 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001524 bool isTarget,
1525 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001526 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1527
1528 FoldingSetNodeID ID;
Dan Gohman7a6611792009-11-20 23:18:13 +00001529 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman6c938802009-10-30 01:27:03 +00001530 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001531 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001532 ID.AddInteger(TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001533 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001534 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001535 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001536
Michael Liaoabb87d42012-09-12 21:43:09 +00001537 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1538 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001539 CSEMap.InsertNode(N, IP);
1540 AllNodes.push_back(N);
1541 return SDValue(N, 0);
1542}
1543
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001544SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001545 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001546 "SrcValue is not a pointer?");
1547
Jim Laskeyf576b422006-10-27 23:46:08 +00001548 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001549 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001550 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001551
Chris Lattner3f16b202006-08-11 21:01:22 +00001552 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001553 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001554 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001555
Dan Gohman01c65a22010-03-18 18:49:47 +00001556 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001557 CSEMap.InsertNode(N, IP);
1558 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001559 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001560}
1561
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001562/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1563SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1564 FoldingSetNodeID ID;
1565 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1566 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001567
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001568 void *IP = 0;
1569 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1570 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001571
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001572 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1573 CSEMap.InsertNode(N, IP);
1574 AllNodes.push_back(N);
1575 return SDValue(N, 0);
1576}
1577
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001578/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1579SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1580 unsigned SrcAS, unsigned DestAS) {
1581 SDValue Ops[] = {Ptr};
1582 FoldingSetNodeID ID;
1583 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), &Ops[0], 1);
1584 ID.AddInteger(SrcAS);
1585 ID.AddInteger(DestAS);
1586
1587 void *IP = 0;
1588 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1589 return SDValue(E, 0);
1590
1591 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1592 dl.getDebugLoc(),
1593 VT, Ptr, SrcAS, DestAS);
1594 CSEMap.InsertNode(N, IP);
1595 AllNodes.push_back(N);
1596 return SDValue(N, 0);
1597}
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001598
Duncan Sands41826032009-01-31 15:50:11 +00001599/// getShiftAmountOperand - Return the specified value casted to
1600/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001601SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001602 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001603 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001604 if (OpTy == ShTy || OpTy.isVector()) return Op;
1605
1606 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001607 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001608}
1609
Chris Lattner9eb7a822007-10-15 17:47:20 +00001610/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1611/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001612SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001613 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001614 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001615 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001616 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001617 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001618 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001619
David Greene1fbe0542009-11-12 20:49:22 +00001620 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001621 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001622}
1623
Duncan Sands445071c2008-12-09 21:33:20 +00001624/// CreateStackTemporary - Create a stack temporary suitable for holding
1625/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001626SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001627 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1628 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001629 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1630 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001631 const TargetLowering *TLI = TM.getTargetLowering();
1632 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001633 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1634 TD->getPrefTypeAlignment(Ty2));
1635
1636 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001637 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001638 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001639}
1640
Owen Anderson53aa7a92009-08-10 22:56:29 +00001641SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001642 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001643 // These setcc operations always fold.
1644 switch (Cond) {
1645 default: break;
1646 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001647 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001648 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001649 case ISD::SETTRUE2: {
1650 const TargetLowering *TLI = TM.getTargetLowering();
1651 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1652 return getConstant(
1653 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1654 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001655
Chris Lattner393d96a2006-04-27 05:01:07 +00001656 case ISD::SETOEQ:
1657 case ISD::SETOGT:
1658 case ISD::SETOGE:
1659 case ISD::SETOLT:
1660 case ISD::SETOLE:
1661 case ISD::SETONE:
1662 case ISD::SETO:
1663 case ISD::SETUO:
1664 case ISD::SETUEQ:
1665 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001666 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001667 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001668 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001669
Gabor Greiff304a7a2008-08-28 21:40:38 +00001670 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001671 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001672 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001673 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001674
Chris Lattner061a1ea2005-01-07 07:46:32 +00001675 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001676 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001677 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1678 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001679 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1680 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1681 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1682 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1683 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1684 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1685 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1686 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001687 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001688 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001689 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001690 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1691 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001692 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001693 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001694 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001695 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001696 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001697 // fall through
1698 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001699 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001700 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001701 // fall through
1702 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001703 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001704 case ISD::SETLT: 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::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001708 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001709 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001710 // fall through
1711 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001712 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001713 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001714 // fall through
1715 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001716 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001717 case ISD::SETGE: 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::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001721 R==APFloat::cmpEqual, VT);
1722 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1723 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1724 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1725 R==APFloat::cmpEqual, VT);
1726 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1727 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1728 R==APFloat::cmpLessThan, VT);
1729 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1730 R==APFloat::cmpUnordered, VT);
1731 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1732 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001733 }
1734 } else {
1735 // Ensure that the constant occurs on the RHS.
Tom Stellardcd428182013-09-28 02:50:38 +00001736 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1737 MVT CompVT = N1.getValueType().getSimpleVT();
1738 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1739 return SDValue();
1740
1741 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001742 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001743 }
1744
Chris Lattnerd47675e2005-08-09 20:20:18 +00001745 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001746 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001747}
1748
Dan Gohman1f372ed2008-02-25 21:11:39 +00001749/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1750/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001751bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001752 // This predicate is not safe for vector operations.
1753 if (Op.getValueType().isVector())
1754 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001755
Dan Gohman1d459e42009-12-11 21:31:27 +00001756 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001757 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1758}
1759
Dan Gohman309d3d52007-06-22 14:59:07 +00001760/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1761/// this predicate to simplify operations downstream. Mask is known to be zero
1762/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001763bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001764 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001765 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001766 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001767 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001768 return (KnownZero & Mask) == Mask;
1769}
1770
1771/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1772/// known to be either zero or one and return them in the KnownZero/KnownOne
1773/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1774/// processing.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001775void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1776 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001777 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001778 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001779
Dan Gohmanf990faf2008-02-13 00:35:47 +00001780 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001781 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001782 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001783
Dan Gohmanf990faf2008-02-13 00:35:47 +00001784 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001785
1786 switch (Op.getOpcode()) {
1787 case ISD::Constant:
1788 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001789 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1790 KnownZero = ~KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00001791 return;
1792 case ISD::AND:
1793 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001794 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1795 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001796 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1797 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001798
1799 // Output known-1 bits are only known if set in both the LHS & RHS.
1800 KnownOne &= KnownOne2;
1801 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1802 KnownZero |= KnownZero2;
1803 return;
1804 case ISD::OR:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001805 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1806 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001807 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1808 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1809
Dan Gohman309d3d52007-06-22 14:59:07 +00001810 // Output known-0 bits are only known if clear in both the LHS & RHS.
1811 KnownZero &= KnownZero2;
1812 // Output known-1 are known to be set if set in either the LHS | RHS.
1813 KnownOne |= KnownOne2;
1814 return;
1815 case ISD::XOR: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001816 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1817 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001818 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1819 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1820
Dan Gohman309d3d52007-06-22 14:59:07 +00001821 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001822 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001823 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1824 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1825 KnownZero = KnownZeroOut;
1826 return;
1827 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001828 case ISD::MUL: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001829 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1830 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001831 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1832 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1833
1834 // If low bits are zero in either operand, output low known-0 bits.
1835 // Also compute a conserative estimate for high known-0 bits.
1836 // More trickiness is possible, but this is sufficient for the
1837 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001838 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001839 unsigned TrailZ = KnownZero.countTrailingOnes() +
1840 KnownZero2.countTrailingOnes();
1841 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001842 KnownZero2.countLeadingOnes(),
1843 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001844
1845 TrailZ = std::min(TrailZ, BitWidth);
1846 LeadZ = std::min(LeadZ, BitWidth);
1847 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1848 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001849 return;
1850 }
1851 case ISD::UDIV: {
1852 // For the purposes of computing leading zeros we can conservatively
1853 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001854 // be less than the denominator.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001855 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001856 unsigned LeadZ = KnownZero2.countLeadingOnes();
1857
Jay Foad25a5e4c2010-12-01 08:53:58 +00001858 KnownOne2.clearAllBits();
1859 KnownZero2.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001860 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001861 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1862 if (RHSUnknownLeadingOnes != BitWidth)
1863 LeadZ = std::min(BitWidth,
1864 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001865
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001866 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001867 return;
1868 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001869 case ISD::SELECT:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001870 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1871 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001872 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1873 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1874
Dan Gohman309d3d52007-06-22 14:59:07 +00001875 // Only known if known in both the LHS and RHS.
1876 KnownOne &= KnownOne2;
1877 KnownZero &= KnownZero2;
1878 return;
1879 case ISD::SELECT_CC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001880 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1881 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001882 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1883 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1884
Dan Gohman309d3d52007-06-22 14:59:07 +00001885 // Only known if known in both the LHS and RHS.
1886 KnownOne &= KnownOne2;
1887 KnownZero &= KnownZero2;
1888 return;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001889 case ISD::SADDO:
1890 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001891 case ISD::SSUBO:
1892 case ISD::USUBO:
1893 case ISD::SMULO:
1894 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001895 if (Op.getResNo() != 1)
1896 return;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001897 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001898 case ISD::SETCC:
1899 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001900 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001901 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001902 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001903 return;
1904 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001905 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001906 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001907 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001908
1909 // If the shift count is an invalid immediate, don't do anything.
1910 if (ShAmt >= BitWidth)
1911 return;
1912
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001913 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001914 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman9db0aa82008-02-26 18:50:50 +00001915 KnownZero <<= ShAmt;
1916 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00001917 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00001918 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001919 }
1920 return;
1921 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001922 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001923 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001924 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001925
Dan Gohman9db0aa82008-02-26 18:50:50 +00001926 // If the shift count is an invalid immediate, don't do anything.
1927 if (ShAmt >= BitWidth)
1928 return;
1929
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001930 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001931 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001932 KnownZero = KnownZero.lshr(ShAmt);
1933 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001934
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001935 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001936 KnownZero |= HighBits; // High bits known zero.
1937 }
1938 return;
1939 case ISD::SRA:
1940 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001941 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001942
Dan Gohman9db0aa82008-02-26 18:50:50 +00001943 // If the shift count is an invalid immediate, don't do anything.
1944 if (ShAmt >= BitWidth)
1945 return;
1946
Dan Gohman309d3d52007-06-22 14:59:07 +00001947 // If any of the demanded bits are produced by the sign extension, we also
1948 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001949 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001950
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001951 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001952 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001953 KnownZero = KnownZero.lshr(ShAmt);
1954 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001955
Dan Gohman309d3d52007-06-22 14:59:07 +00001956 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001957 APInt SignBit = APInt::getSignBit(BitWidth);
1958 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001959
Dan Gohmanb717fda2008-02-20 16:30:17 +00001960 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001961 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001962 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001963 KnownOne |= HighBits; // New bits are known one.
1964 }
1965 }
1966 return;
1967 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001968 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001969 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001970
1971 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00001972 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001973 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00001974
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001975 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001976 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001977
Dan Gohman309d3d52007-06-22 14:59:07 +00001978 // If the sign extended bits are demanded, we know that the sign
1979 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00001980 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001981 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00001982 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001983
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001984 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1985 KnownOne &= InputDemandedBits;
1986 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001987 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1988
Dan Gohman309d3d52007-06-22 14:59:07 +00001989 // If the sign bit of the input is known set or clear, then we know the
1990 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001991 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00001992 KnownZero |= NewBits;
1993 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00001994 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00001995 KnownOne |= NewBits;
1996 KnownZero &= ~NewBits;
1997 } else { // Input sign bit unknown
1998 KnownZero &= ~NewBits;
1999 KnownOne &= ~NewBits;
2000 }
2001 return;
2002 }
2003 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002004 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002005 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002006 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00002007 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00002008 unsigned LowBits = Log2_32(BitWidth)+1;
2009 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002010 KnownOne.clearAllBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002011 return;
2012 }
2013 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00002014 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00002015 // If this is a ZEXTLoad and we are looking at the loaded value.
2016 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002017 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002018 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002019 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00002020 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002021 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002022 }
2023 return;
2024 }
2025 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002026 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002027 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002028 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00002029 KnownZero = KnownZero.trunc(InBits);
2030 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002031 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00002032 KnownZero = KnownZero.zext(BitWidth);
2033 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002034 KnownZero |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002035 return;
2036 }
2037 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002038 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002039 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002040 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002041
Jay Foad583abbc2010-12-07 08:25:19 +00002042 KnownZero = KnownZero.trunc(InBits);
2043 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002044 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002045
2046 // Note if the sign bit is known to be zero or one.
2047 bool SignBitKnownZero = KnownZero.isNegative();
2048 bool SignBitKnownOne = KnownOne.isNegative();
2049 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2050 "Sign bit can't be known to be both zero and one!");
2051
Jay Foad583abbc2010-12-07 08:25:19 +00002052 KnownZero = KnownZero.zext(BitWidth);
2053 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00002054
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002055 // If the sign bit is known zero or one, the top bits match.
2056 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00002057 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002058 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00002059 KnownOne |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002060 return;
2061 }
2062 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00002063 EVT InVT = Op.getOperand(0).getValueType();
2064 unsigned InBits = InVT.getScalarType().getSizeInBits();
2065 KnownZero = KnownZero.trunc(InBits);
2066 KnownOne = KnownOne.trunc(InBits);
2067 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2068 KnownZero = KnownZero.zext(BitWidth);
2069 KnownOne = KnownOne.zext(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002070 return;
2071 }
2072 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002073 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00002074 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00002075 KnownZero = KnownZero.zext(InBits);
2076 KnownOne = KnownOne.zext(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002077 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002078 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +00002079 KnownZero = KnownZero.trunc(BitWidth);
2080 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002081 break;
2082 }
2083 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002084 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00002085 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002086 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2087 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00002088 KnownOne &= (~KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00002089 return;
2090 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00002091 case ISD::FGETSIGN:
2092 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00002093 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerafc8f132007-12-22 21:26:52 +00002094 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002095
Dan Gohman72ec3f42008-04-28 17:02:21 +00002096 case ISD::SUB: {
2097 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2098 // We know that the top bits of C-X are clear if X contains less bits
2099 // than C (i.e. no wrap-around can happen). For example, 20-X is
2100 // positive if we can prove that X is >= 0 and < 16.
2101 if (CLHS->getAPIntValue().isNonNegative()) {
2102 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2103 // NLZ can't be BitWidth with no sign bit
2104 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002105 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002106
2107 // If all of the MaskV bits are known to be zero, then we know the
2108 // output top bits are zero, because we now know that the output is
2109 // from [0-C].
2110 if ((KnownZero2 & MaskV) == MaskV) {
2111 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2112 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002113 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002114 }
2115 }
2116 }
2117 }
2118 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002119 case ISD::ADD:
2120 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002121 // Output known-0 bits are known if clear or set in both the low clear bits
2122 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2123 // low 3 bits clear.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002124 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002125 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002126 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2127
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002128 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002129 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002130 KnownZeroOut = std::min(KnownZeroOut,
2131 KnownZero2.countTrailingOnes());
2132
Chris Lattner440b2802010-12-19 20:38:28 +00002133 if (Op.getOpcode() == ISD::ADD) {
2134 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2135 return;
2136 }
2137
2138 // With ADDE, a carry bit may be added in, so we can only use this
2139 // information if we know (at least) that the low two bits are clear. We
2140 // then return to the caller that the low bit is unknown but that other bits
2141 // are known zero.
2142 if (KnownZeroOut >= 2) // ADDE
2143 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohman309d3d52007-06-22 14:59:07 +00002144 return;
2145 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002146 case ISD::SREM:
2147 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002148 const APInt &RA = Rem->getAPIntValue().abs();
2149 if (RA.isPowerOf2()) {
2150 APInt LowBits = RA - 1;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002151 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002152
Duncan Sands33274982010-01-29 09:45:26 +00002153 // The low bits of the first operand are unchanged by the srem.
2154 KnownZero = KnownZero2 & LowBits;
2155 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002156
Duncan Sands33274982010-01-29 09:45:26 +00002157 // If the first operand is non-negative or has all low bits zero, then
2158 // the upper bits are all zero.
2159 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2160 KnownZero |= ~LowBits;
2161
2162 // If the first operand is negative and not all low bits are zero, then
2163 // the upper bits are all one.
2164 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2165 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002166 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002167 }
2168 }
2169 return;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002170 case ISD::UREM: {
2171 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002172 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002173 if (RA.isPowerOf2()) {
2174 APInt LowBits = (RA - 1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002175 KnownZero |= ~LowBits;
2176 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002177 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2178 break;
2179 }
2180 }
2181
2182 // Since the result is less than or equal to either operand, any leading
2183 // zero bits in either operand must also exist in the result.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002184 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2185 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002186
2187 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2188 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002189 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002190 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002191 return;
Dan Gohman309d3d52007-06-22 14:59:07 +00002192 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002193 case ISD::FrameIndex:
2194 case ISD::TargetFrameIndex:
2195 if (unsigned Align = InferPtrAlignment(Op)) {
2196 // The low bits are known zero if the pointer is aligned.
2197 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2198 return;
2199 }
2200 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002201
Dan Gohman309d3d52007-06-22 14:59:07 +00002202 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002203 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2204 break;
2205 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002206 case ISD::INTRINSIC_WO_CHAIN:
2207 case ISD::INTRINSIC_W_CHAIN:
2208 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002209 // Allow the target to implement this method for its nodes.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002210 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002211 return;
2212 }
2213}
2214
2215/// ComputeNumSignBits - Return the number of times the sign bit of the
2216/// register is replicated into the other bits. We know that at least 1 bit
2217/// is always equal to the sign bit (itself), but other cases can give us
2218/// information. For example, immediately after an "SRA X, 2", we know that
2219/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002220unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002221 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002222 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002223 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002224 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002225 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002226 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002227
Dan Gohman309d3d52007-06-22 14:59:07 +00002228 if (Depth == 6)
2229 return 1; // Limit search depth.
2230
2231 switch (Op.getOpcode()) {
2232 default: break;
2233 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002234 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002235 return VTBits-Tmp+1;
2236 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002237 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002238 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002239
Dan Gohman309d3d52007-06-22 14:59:07 +00002240 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002241 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002242 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002243 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002244
Dan Gohman309d3d52007-06-22 14:59:07 +00002245 case ISD::SIGN_EXTEND:
Jack Carter170a5f22013-09-09 22:02:08 +00002246 Tmp =
2247 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002248 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002249
Dan Gohman309d3d52007-06-22 14:59:07 +00002250 case ISD::SIGN_EXTEND_INREG:
2251 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002252 Tmp =
2253 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002254 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002255
Dan Gohman309d3d52007-06-22 14:59:07 +00002256 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2257 return std::max(Tmp, Tmp2);
2258
2259 case ISD::SRA:
2260 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2261 // SRA X, C -> adds C sign bits.
2262 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002263 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002264 if (Tmp > VTBits) Tmp = VTBits;
2265 }
2266 return Tmp;
2267 case ISD::SHL:
2268 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2269 // shl destroys sign bits.
2270 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002271 if (C->getZExtValue() >= VTBits || // Bad shift.
2272 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2273 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002274 }
2275 break;
2276 case ISD::AND:
2277 case ISD::OR:
2278 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002279 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002280 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002281 if (Tmp != 1) {
2282 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2283 FirstAnswer = std::min(Tmp, Tmp2);
2284 // We computed what we know about the sign bits as our first
2285 // answer. Now proceed to the generic code that uses
2286 // ComputeMaskedBits, and pick whichever answer is better.
2287 }
2288 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002289
2290 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002291 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002292 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002293 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002294 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002295
2296 case ISD::SADDO:
2297 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002298 case ISD::SSUBO:
2299 case ISD::USUBO:
2300 case ISD::SMULO:
2301 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002302 if (Op.getResNo() != 1)
2303 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002304 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002305 case ISD::SETCC:
2306 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002307 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002308 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002309 return VTBits;
2310 break;
2311 case ISD::ROTL:
2312 case ISD::ROTR:
2313 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002314 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002315
Dan Gohman309d3d52007-06-22 14:59:07 +00002316 // Handle rotate right by N like a rotate left by 32-N.
2317 if (Op.getOpcode() == ISD::ROTR)
2318 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2319
2320 // If we aren't rotating out all of the known-in sign bits, return the
2321 // number that are left. This handles rotl(sext(x), 1) for example.
2322 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2323 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2324 }
2325 break;
2326 case ISD::ADD:
2327 // Add can have at most one carry bit. Thus we know that the output
2328 // is, at worst, one more bit than the inputs.
2329 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2330 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002331
Dan Gohman309d3d52007-06-22 14:59:07 +00002332 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002333 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002334 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002335 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002336 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002337
Dan Gohman309d3d52007-06-22 14:59:07 +00002338 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2339 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002340 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002341 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002342
Dan Gohman309d3d52007-06-22 14:59:07 +00002343 // If we are subtracting one from a positive number, there is no carry
2344 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002345 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002346 return Tmp;
2347 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002348
Dan Gohman309d3d52007-06-22 14:59:07 +00002349 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2350 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002351 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002352
Dan Gohman309d3d52007-06-22 14:59:07 +00002353 case ISD::SUB:
2354 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2355 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002356
Dan Gohman309d3d52007-06-22 14:59:07 +00002357 // Handle NEG.
2358 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002359 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002360 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002361 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002362 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2363 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002364 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002365 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002366
Dan Gohman309d3d52007-06-22 14:59:07 +00002367 // If the input is known to be positive (the sign bit is known clear),
2368 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002369 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002370 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002371
Dan Gohman309d3d52007-06-22 14:59:07 +00002372 // Otherwise, we treat this like a SUB.
2373 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002374
Dan Gohman309d3d52007-06-22 14:59:07 +00002375 // Sub can have at most one carry bit. Thus we know that the output
2376 // is, at worst, one more bit than the inputs.
2377 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2378 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002379 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002380 case ISD::TRUNCATE:
2381 // FIXME: it's tricky to do anything useful for this, but it is an important
2382 // case for targets like X86.
2383 break;
2384 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002385
Nadav Rotem4536d582013-03-20 22:53:44 +00002386 // If we are looking at the loaded value of the SDNode.
2387 if (Op.getResNo() == 0) {
2388 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2389 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2390 unsigned ExtType = LD->getExtensionType();
2391 switch (ExtType) {
2392 default: break;
2393 case ISD::SEXTLOAD: // '17' bits known
2394 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2395 return VTBits-Tmp+1;
2396 case ISD::ZEXTLOAD: // '16' bits known
2397 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2398 return VTBits-Tmp;
2399 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002400 }
2401 }
2402
2403 // Allow the target to implement this method for its nodes.
2404 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002405 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002406 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2407 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002408 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002409 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002410 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002411
Dan Gohman309d3d52007-06-22 14:59:07 +00002412 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2413 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002414 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002415 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002416
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002417 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002418 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002419 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002420 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002421 Mask = KnownOne;
2422 } else {
2423 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002424 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002425 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002426
Dan Gohman309d3d52007-06-22 14:59:07 +00002427 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2428 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002429 Mask = ~Mask;
2430 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002431 // Return # leading zeros. We use 'min' here in case Val was zero before
2432 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002433 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002434}
2435
Chris Lattner46c01a32011-02-13 22:25:43 +00002436/// isBaseWithConstantOffset - Return true if the specified operand is an
2437/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2438/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2439/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002440/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002441bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2442 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2443 !isa<ConstantSDNode>(Op.getOperand(1)))
2444 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002445
2446 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002447 !MaskedValueIsZero(Op.getOperand(0),
2448 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2449 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002450
Chris Lattner46c01a32011-02-13 22:25:43 +00002451 return true;
2452}
2453
2454
Dan Gohmand0d5e682009-09-03 20:34:31 +00002455bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2456 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002457 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002458 return true;
2459
2460 // If the value is a constant, we can obviously see if it is a NaN or not.
2461 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2462 return !C->getValueAPF().isNaN();
2463
2464 // TODO: Recognize more cases here.
2465
2466 return false;
2467}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002468
Dan Gohman38605212010-02-24 06:52:40 +00002469bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2470 // If the value is a constant, we can obviously see if it is a zero or not.
2471 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2472 return !C->isZero();
2473
2474 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002475 switch (Op.getOpcode()) {
2476 default: break;
2477 case ISD::OR:
2478 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2479 return !C->isNullValue();
2480 break;
2481 }
Dan Gohman38605212010-02-24 06:52:40 +00002482
2483 return false;
2484}
2485
2486bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2487 // Check the obvious case.
2488 if (A == B) return true;
2489
2490 // For for negative and positive zero.
2491 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2492 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2493 if (CA->isZero() && CB->isZero()) return true;
2494
2495 // Otherwise they may not be equal.
2496 return false;
2497}
2498
Chris Lattner061a1ea2005-01-07 07:46:32 +00002499/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002500///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002501SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002502 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00002503 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattnerfcb16472006-08-11 18:38:11 +00002504 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002505 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002506 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002507
Jack Carter170a5f22013-09-09 22:02:08 +00002508 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2509 DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002510 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002511
Chris Lattnerfcb16472006-08-11 18:38:11 +00002512 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002513#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002514 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002515#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002516 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002517}
2518
Andrew Trickef9de2a2013-05-25 02:42:55 +00002519SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002520 EVT VT, SDValue Operand) {
Chris Lattnera1874602005-12-23 05:30:37 +00002521 // Constant fold unary operations with an integer constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002523 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002524 switch (Opcode) {
2525 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002526 case ISD::SIGN_EXTEND:
Jay Foad583abbc2010-12-07 08:25:19 +00002527 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner8c393c22005-09-02 00:17:32 +00002528 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002529 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002530 case ISD::TRUNCATE:
Jay Foad583abbc2010-12-07 08:25:19 +00002531 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002532 case ISD::UINT_TO_FP:
2533 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002534 APFloat apf(EVTToAPFloatSemantics(VT),
2535 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002536 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002537 Opcode==ISD::SINT_TO_FP,
2538 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002539 return getConstantFP(apf, VT);
2540 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002541 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002542 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002543 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002544 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002545 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002546 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002547 case ISD::BSWAP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002548 return getConstant(Val.byteSwap(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002549 case ISD::CTPOP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002550 return getConstant(Val.countPopulation(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002551 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002552 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002553 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002554 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002555 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002556 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002557 }
2558 }
2559
Dale Johannesen446b9002007-08-31 23:34:27 +00002560 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002561 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002562 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002563 switch (Opcode) {
2564 case ISD::FNEG:
2565 V.changeSign();
2566 return getConstantFP(V, VT);
2567 case ISD::FABS:
2568 V.clearSign();
2569 return getConstantFP(V, VT);
2570 case ISD::FCEIL: {
2571 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2572 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002573 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002574 break;
2575 }
2576 case ISD::FTRUNC: {
2577 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2578 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002579 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002580 break;
2581 }
2582 case ISD::FFLOOR: {
2583 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2584 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002585 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002586 break;
2587 }
2588 case ISD::FP_EXTEND: {
2589 bool ignored;
2590 // This can return overflow, underflow, or inexact; we don't care.
2591 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002592 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002593 APFloat::rmNearestTiesToEven, &ignored);
2594 return getConstantFP(V, VT);
2595 }
2596 case ISD::FP_TO_SINT:
2597 case ISD::FP_TO_UINT: {
2598 integerPart x[2];
2599 bool ignored;
2600 assert(integerPartWidth >= 64);
2601 // FIXME need to be more flexible about rounding mode.
2602 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2603 Opcode==ISD::FP_TO_SINT,
2604 APFloat::rmTowardZero, &ignored);
2605 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002606 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002607 APInt api(VT.getSizeInBits(), x);
2608 return getConstant(api, VT);
2609 }
2610 case ISD::BITCAST:
2611 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2612 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2613 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2614 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2615 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002616 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002617 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002618
Gabor Greiff304a7a2008-08-28 21:40:38 +00002619 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002620 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002621 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002622 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002623 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002624 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002625 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002626 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002627 assert(VT.isFloatingPoint() &&
2628 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002629 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002630 assert((!VT.isVector() ||
2631 VT.getVectorNumElements() ==
2632 Operand.getValueType().getVectorNumElements()) &&
2633 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002634 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002635 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002636 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002637 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002638 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002639 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002640 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002641 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2642 "Invalid sext node, dst < src!");
2643 assert((!VT.isVector() ||
2644 VT.getVectorNumElements() ==
2645 Operand.getValueType().getVectorNumElements()) &&
2646 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002647 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2648 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2649 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002650 // sext(undef) = 0, because the top bits will all be the same.
2651 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002652 break;
2653 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002654 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002655 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002656 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002657 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2658 "Invalid zext node, dst < src!");
2659 assert((!VT.isVector() ||
2660 VT.getVectorNumElements() ==
2661 Operand.getValueType().getVectorNumElements()) &&
2662 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002663 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002664 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002665 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002666 else if (OpOpcode == ISD::UNDEF)
2667 // zext(undef) = 0, because the top bits will be zero.
2668 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002669 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002670 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002671 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002672 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002673 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002674 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2675 "Invalid anyext node, dst < src!");
2676 assert((!VT.isVector() ||
2677 VT.getVectorNumElements() ==
2678 Operand.getValueType().getVectorNumElements()) &&
2679 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002680
Dan Gohman08837892010-06-18 00:08:30 +00002681 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2682 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002683 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002684 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002685 else if (OpOpcode == ISD::UNDEF)
2686 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002687
2688 // (ext (trunx x)) -> x
2689 if (OpOpcode == ISD::TRUNCATE) {
2690 SDValue OpOp = Operand.getNode()->getOperand(0);
2691 if (OpOp.getValueType() == VT)
2692 return OpOp;
2693 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002694 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002695 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002696 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002697 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002698 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002699 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2700 "Invalid truncate node, src < dst!");
2701 assert((!VT.isVector() ||
2702 VT.getVectorNumElements() ==
2703 Operand.getValueType().getVectorNumElements()) &&
2704 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002705 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002706 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002707 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2708 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002709 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002710 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2711 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002712 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002713 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002714 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002715 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002716 }
Craig Topper201c1a32012-01-15 01:05:11 +00002717 if (OpOpcode == ISD::UNDEF)
2718 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002719 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002720 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002721 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002722 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002723 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002724 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002725 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2726 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002727 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002728 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002729 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002730 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002731 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002732 (VT.getVectorElementType() == Operand.getValueType() ||
2733 (VT.getVectorElementType().isInteger() &&
2734 Operand.getValueType().isInteger() &&
2735 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002736 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002737 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002738 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002739 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2740 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2741 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2742 Operand.getConstantOperandVal(1) == 0 &&
2743 Operand.getOperand(0).getValueType() == VT)
2744 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002745 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002746 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002747 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002748 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002749 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002750 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002751 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002752 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002753 break;
2754 case ISD::FABS:
2755 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002756 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002757 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002758 }
2759
Chris Lattnerf9c19152005-08-25 19:12:10 +00002760 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002761 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002762 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002763 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002764 SDValue Ops[1] = { Operand };
Chris Lattnerf17b4222007-02-04 07:28:00 +00002765 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002766 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002767 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002768 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002769
Jack Carter170a5f22013-09-09 22:02:08 +00002770 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2771 DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002772 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002773 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00002774 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2775 DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002776 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002777
Chris Lattner061a1ea2005-01-07 07:46:32 +00002778 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002779#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002780 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002781#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002782 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002783}
2784
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002785SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2786 SDNode *Cst1, SDNode *Cst2) {
2787 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2788 SmallVector<SDValue, 4> Outputs;
2789 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002790
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002791 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2792 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
2793 if (Scalar1 && Scalar2) {
2794 // Scalar instruction.
2795 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
2796 } else {
2797 // For vectors extract each constant element into Inputs so we can constant
2798 // fold them individually.
2799 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2800 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2801 if (!BV1 || !BV2)
2802 return SDValue();
2803
2804 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2805
2806 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2807 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2808 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2809 if (!V1 || !V2) // Not a constant, bail.
2810 return SDValue();
2811
2812 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2813 // FIXME: This is valid and could be handled by truncating the APInts.
2814 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2815 return SDValue();
2816
2817 Inputs.push_back(std::make_pair(V1, V2));
2818 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002819 }
2820
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002821 // We have a number of constant values, constant fold them element by element.
2822 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2823 const APInt &C1 = Inputs[I].first->getAPIntValue();
2824 const APInt &C2 = Inputs[I].second->getAPIntValue();
2825
2826 switch (Opcode) {
2827 case ISD::ADD:
2828 Outputs.push_back(getConstant(C1 + C2, SVT));
2829 break;
2830 case ISD::SUB:
2831 Outputs.push_back(getConstant(C1 - C2, SVT));
2832 break;
2833 case ISD::MUL:
2834 Outputs.push_back(getConstant(C1 * C2, SVT));
2835 break;
2836 case ISD::UDIV:
2837 if (!C2.getBoolValue())
2838 return SDValue();
2839 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2840 break;
2841 case ISD::UREM:
2842 if (!C2.getBoolValue())
2843 return SDValue();
2844 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2845 break;
2846 case ISD::SDIV:
2847 if (!C2.getBoolValue())
2848 return SDValue();
2849 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2850 break;
2851 case ISD::SREM:
2852 if (!C2.getBoolValue())
2853 return SDValue();
2854 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2855 break;
2856 case ISD::AND:
2857 Outputs.push_back(getConstant(C1 & C2, SVT));
2858 break;
2859 case ISD::OR:
2860 Outputs.push_back(getConstant(C1 | C2, SVT));
2861 break;
2862 case ISD::XOR:
2863 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2864 break;
2865 case ISD::SHL:
2866 Outputs.push_back(getConstant(C1 << C2, SVT));
2867 break;
2868 case ISD::SRL:
2869 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2870 break;
2871 case ISD::SRA:
2872 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2873 break;
2874 case ISD::ROTL:
2875 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2876 break;
2877 case ISD::ROTR:
2878 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2879 break;
2880 default:
2881 return SDValue();
2882 }
2883 }
2884
2885 // Handle the scalar case first.
Silviu Baranga4ad2bc52013-04-25 09:32:33 +00002886 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002887 return Outputs.back();
2888
2889 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002890 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002891 Outputs.size());
Bill Wendling162c26d2008-09-24 10:16:24 +00002892}
2893
Andrew Trickef9de2a2013-05-25 02:42:55 +00002894SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002895 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002896 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2897 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002898 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002899 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002900 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002901 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2902 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002903 // Fold trivial token factors.
2904 if (N1.getOpcode() == ISD::EntryToken) return N2;
2905 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002906 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002907 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002908 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002909 // Concat of UNDEFs is UNDEF.
2910 if (N1.getOpcode() == ISD::UNDEF &&
2911 N2.getOpcode() == ISD::UNDEF)
2912 return getUNDEF(VT);
2913
Dan Gohman550c9af2008-08-14 20:04:46 +00002914 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2915 // one big BUILD_VECTOR.
2916 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2917 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002918 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2919 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002920 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00002921 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00002922 }
2923 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002924 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002925 assert(VT.isInteger() && "This operator does not apply to FP types!");
2926 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002927 N1.getValueType() == VT && "Binary operator types must match!");
2928 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2929 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002930 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002931 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002932 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2933 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002934 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002935 case ISD::OR:
2936 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002937 case ISD::ADD:
2938 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002939 assert(VT.isInteger() && "This operator does not apply to FP types!");
2940 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002941 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002942 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2943 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002944 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002945 return N1;
2946 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002947 case ISD::UDIV:
2948 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00002949 case ISD::MULHU:
2950 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002951 case ISD::MUL:
2952 case ISD::SDIV:
2953 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00002954 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002955 assert(N1.getValueType() == N2.getValueType() &&
2956 N1.getValueType() == VT && "Binary operator types must match!");
2957 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002958 case ISD::FADD:
2959 case ISD::FSUB:
2960 case ISD::FMUL:
2961 case ISD::FDIV:
2962 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002963 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00002964 if (Opcode == ISD::FADD) {
2965 // 0+x --> x
2966 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2967 if (CFP->getValueAPF().isZero())
2968 return N2;
2969 // x+0 --> x
2970 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2971 if (CFP->getValueAPF().isZero())
2972 return N1;
2973 } else if (Opcode == ISD::FSUB) {
2974 // x-0 --> x
2975 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2976 if (CFP->getValueAPF().isZero())
2977 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00002978 } else if (Opcode == ISD::FMUL) {
2979 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2980 SDValue V = N2;
2981
2982 // If the first operand isn't the constant, try the second
2983 if (!CFP) {
2984 CFP = dyn_cast<ConstantFPSDNode>(N2);
2985 V = N1;
2986 }
2987
2988 if (CFP) {
2989 // 0*x --> 0
2990 if (CFP->isZero())
2991 return SDValue(CFP,0);
2992 // 1*x --> x
2993 if (CFP->isExactlyValue(1.0))
2994 return V;
2995 }
Dan Gohman1275e282009-01-23 19:10:37 +00002996 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00002997 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002998 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00002999 assert(N1.getValueType() == N2.getValueType() &&
3000 N1.getValueType() == VT && "Binary operator types must match!");
3001 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003002 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3003 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003004 N1.getValueType().isFloatingPoint() &&
3005 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003006 "Invalid FCOPYSIGN!");
3007 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003008 case ISD::SHL:
3009 case ISD::SRA:
3010 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00003011 case ISD::ROTL:
3012 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00003013 assert(VT == N1.getValueType() &&
3014 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00003015 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003016 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00003017 assert((!VT.isVector() || VT == N2.getValueType()) &&
3018 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00003019 // Verify that the shift amount VT is bit enough to hold valid shift
3020 // amounts. This catches things like trying to shift an i1024 value by an
3021 // i8, which is easy to fall into in generic code that uses
3022 // TLI.getShiftAmount().
3023 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00003024 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00003025 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003026
3027 // Always fold shifts of i1 values so the code generator doesn't need to
3028 // handle them. Since we know the size of the shift has to be less than the
3029 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00003030 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00003031 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00003032 if (N2C && N2C->isNullValue())
3033 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00003034 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00003035 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003036 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003037 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003038 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003039 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003040 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003041 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003042 "type is vector!");
3043 assert((!EVT.isVector() ||
3044 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3045 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00003046 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00003047 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00003048 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00003049 break;
3050 }
Chris Lattner72733e52008-01-17 07:00:52 +00003051 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00003052 assert(VT.isFloatingPoint() &&
3053 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00003054 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00003055 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00003056 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00003057 break;
Nate Begeman43144a22005-08-30 02:44:00 +00003058 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00003059 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003060 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00003061 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003062 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00003063 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00003064 assert(!EVT.isVector() &&
3065 "AssertSExt/AssertZExt type should be the vector element type "
3066 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003067 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00003068 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00003069 break;
3070 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003071 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003072 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00003073 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00003074 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00003075 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003076 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003077 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003078 "type is vector!");
3079 assert((!EVT.isVector() ||
3080 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3081 "Vector element counts must match in SIGN_EXTEND_INREG");
3082 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00003083 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00003084
Chris Lattner16713612008-01-22 19:09:33 +00003085 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003086 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003087 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00003088 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00003089 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00003090 return getConstant(Val, VT);
3091 }
Chris Lattner16713612008-01-22 19:09:33 +00003092 break;
3093 }
3094 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00003095 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3096 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003097 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003098
Chris Lattner16713612008-01-22 19:09:33 +00003099 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3100 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003101 if (N2C &&
3102 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003103 N1.getNumOperands() > 0) {
3104 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003105 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003106 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003107 N1.getOperand(N2C->getZExtValue() / Factor),
3108 getConstant(N2C->getZExtValue() % Factor,
3109 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003110 }
3111
3112 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3113 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003114 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3115 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003116
3117 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003118 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003119 // are promoted and implicitly truncated, and the result implicitly
3120 // extended. Make that explicit here.
3121 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003122
Bob Wilson59dbbb22009-04-13 22:05:19 +00003123 return Elt;
3124 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003125
Chris Lattner16713612008-01-22 19:09:33 +00003126 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3127 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003128 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003129 // If the indices are the same, return the inserted element else
3130 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003131 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003132 SDValue N1Op2 = N1.getOperand(2);
3133 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3134
3135 if (N1Op2C && N2C) {
3136 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3137 if (VT == N1.getOperand(1).getValueType())
3138 return N1.getOperand(1);
3139 else
3140 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3141 }
3142
Dale Johannesendb393622009-02-03 01:55:44 +00003143 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003144 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003145 }
Chris Lattner16713612008-01-22 19:09:33 +00003146 break;
3147 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003148 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003149 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3150 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003151 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003152 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003153
Chris Lattner16713612008-01-22 19:09:33 +00003154 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3155 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003156 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003157 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003158 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003159
Chris Lattner16713612008-01-22 19:09:33 +00003160 // EXTRACT_ELEMENT of a constant int is also very common.
3161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003162 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003163 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003164 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3165 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003166 }
3167 break;
David Greeneb6f16112011-01-26 15:38:49 +00003168 case ISD::EXTRACT_SUBVECTOR: {
3169 SDValue Index = N2;
3170 if (VT.isSimple() && N1.getValueType().isSimple()) {
3171 assert(VT.isVector() && N1.getValueType().isVector() &&
3172 "Extract subvector VTs must be a vectors!");
Jack Carter170a5f22013-09-09 22:02:08 +00003173 assert(VT.getVectorElementType() ==
3174 N1.getValueType().getVectorElementType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003175 "Extract subvector VTs must have the same element type!");
Craig Topperd9c27832013-08-15 02:44:19 +00003176 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greeneb6f16112011-01-26 15:38:49 +00003177 "Extract subvector must be from larger vector to smaller vector!");
3178
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003179 if (isa<ConstantSDNode>(Index.getNode())) {
3180 assert((VT.getVectorNumElements() +
3181 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003182 <= N1.getValueType().getVectorNumElements())
3183 && "Extract subvector overflow!");
3184 }
3185
3186 // Trivial extraction.
Craig Topperd9c27832013-08-15 02:44:19 +00003187 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greeneb6f16112011-01-26 15:38:49 +00003188 return N1;
3189 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003190 break;
Chris Lattner16713612008-01-22 19:09:33 +00003191 }
David Greeneb6f16112011-01-26 15:38:49 +00003192 }
Chris Lattner16713612008-01-22 19:09:33 +00003193
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003194 // Perform trivial constant folding.
3195 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3196 if (SV.getNode()) return SV;
3197
3198 // Canonicalize constant to RHS if commutative.
3199 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3200 std::swap(N1C, N2C);
3201 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003202 }
3203
Chris Lattner16713612008-01-22 19:09:33 +00003204 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003205 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3206 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003207 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003208 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003209 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003210 std::swap(N1CFP, N2CFP);
3211 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003212 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003213 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3214 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003215 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003216 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003217 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003218 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003219 return getConstantFP(V1, VT);
3220 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003221 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003222 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3223 if (s!=APFloat::opInvalidOp)
3224 return getConstantFP(V1, VT);
3225 break;
3226 case ISD::FMUL:
3227 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3228 if (s!=APFloat::opInvalidOp)
3229 return getConstantFP(V1, VT);
3230 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003231 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003232 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3233 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3234 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003235 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003236 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003237 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3238 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3239 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003240 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003241 case ISD::FCOPYSIGN:
3242 V1.copySign(V2);
3243 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003244 default: break;
3245 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003246 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003247
3248 if (Opcode == ISD::FP_ROUND) {
3249 APFloat V = N1CFP->getValueAPF(); // make copy
3250 bool ignored;
3251 // This can return overflow, underflow, or inexact; we don't care.
3252 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003253 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003254 APFloat::rmNearestTiesToEven, &ignored);
3255 return getConstantFP(V, VT);
3256 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003257 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003258
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003259 // Canonicalize an UNDEF to the RHS, even over a constant.
3260 if (N1.getOpcode() == ISD::UNDEF) {
3261 if (isCommutativeBinOp(Opcode)) {
3262 std::swap(N1, N2);
3263 } else {
3264 switch (Opcode) {
3265 case ISD::FP_ROUND_INREG:
3266 case ISD::SIGN_EXTEND_INREG:
3267 case ISD::SUB:
3268 case ISD::FSUB:
3269 case ISD::FDIV:
3270 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003271 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003272 return N1; // fold op(undef, arg2) -> undef
3273 case ISD::UDIV:
3274 case ISD::SDIV:
3275 case ISD::UREM:
3276 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003277 case ISD::SRL:
3278 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003279 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003280 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3281 // For vectors, we can't easily build an all zero vector, just return
3282 // the LHS.
3283 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003284 }
3285 }
3286 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003287
3288 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003289 if (N2.getOpcode() == ISD::UNDEF) {
3290 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003291 case ISD::XOR:
3292 if (N1.getOpcode() == ISD::UNDEF)
3293 // Handle undef ^ undef -> 0 special case. This is a common
3294 // idiom (misuse).
3295 return getConstant(0, VT);
3296 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003297 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003298 case ISD::ADDC:
3299 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003300 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003301 case ISD::UDIV:
3302 case ISD::SDIV:
3303 case ISD::UREM:
3304 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003305 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003306 case ISD::FADD:
3307 case ISD::FSUB:
3308 case ISD::FMUL:
3309 case ISD::FDIV:
3310 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003311 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003312 return N2;
3313 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003314 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003315 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003316 case ISD::SRL:
3317 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003318 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003319 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3320 // For vectors, we can't easily build an all zero vector, just return
3321 // the LHS.
3322 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003323 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003324 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003325 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003326 // For vectors, we can't easily build an all one vector, just return
3327 // the LHS.
3328 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003329 case ISD::SRA:
3330 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003331 }
3332 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003333
Chris Lattner724f7ee2005-05-11 18:57:39 +00003334 // Memoize this node if possible.
3335 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003336 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003337 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003338 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003339 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003340 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003341 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003342 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003343 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003344
Jack Carter170a5f22013-09-09 22:02:08 +00003345 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3346 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003347 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003348 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003349 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3350 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003351 }
3352
Chris Lattner061a1ea2005-01-07 07:46:32 +00003353 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003354#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003355 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003356#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003357 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003358}
3359
Andrew Trickef9de2a2013-05-25 02:42:55 +00003360SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003361 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003362 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003363 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003364 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003365 case ISD::FMA: {
3366 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3367 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3368 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3369 if (N1CFP && N2CFP && N3CFP) {
3370 APFloat V1 = N1CFP->getValueAPF();
3371 const APFloat &V2 = N2CFP->getValueAPF();
3372 const APFloat &V3 = N3CFP->getValueAPF();
3373 APFloat::opStatus s =
3374 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3375 if (s != APFloat::opInvalidOp)
3376 return getConstantFP(V1, VT);
3377 }
3378 break;
3379 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003380 case ISD::CONCAT_VECTORS:
3381 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3382 // one big BUILD_VECTOR.
3383 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3384 N2.getOpcode() == ISD::BUILD_VECTOR &&
3385 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003386 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3387 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003388 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3389 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00003390 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00003391 }
3392 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003393 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003394 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003395 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003396 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003397 break;
3398 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003399 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003400 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003401 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003402 return N2; // select true, X, Y -> X
3403 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003404 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003405
3406 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003407 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003408 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003409 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003410 case ISD::INSERT_SUBVECTOR: {
3411 SDValue Index = N3;
3412 if (VT.isSimple() && N1.getValueType().isSimple()
3413 && N2.getValueType().isSimple()) {
3414 assert(VT.isVector() && N1.getValueType().isVector() &&
3415 N2.getValueType().isVector() &&
3416 "Insert subvector VTs must be a vectors");
3417 assert(VT == N1.getValueType() &&
3418 "Dest and insert subvector source types must match!");
Craig Topperd9c27832013-08-15 02:44:19 +00003419 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenebab5e6e2011-01-26 19:13:22 +00003420 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003421 if (isa<ConstantSDNode>(Index.getNode())) {
3422 assert((N2.getValueType().getVectorNumElements() +
3423 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003424 <= VT.getVectorNumElements())
3425 && "Insert subvector overflow!");
3426 }
3427
3428 // Trivial insertion.
Craig Topperd9c27832013-08-15 02:44:19 +00003429 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenebab5e6e2011-01-26 19:13:22 +00003430 return N2;
3431 }
3432 break;
3433 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003434 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003435 // Fold bit_convert nodes from a type to themselves.
3436 if (N1.getValueType() == VT)
3437 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003438 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003439 }
3440
Chris Lattnerf9c19152005-08-25 19:12:10 +00003441 // Memoize node if it doesn't produce a flag.
3442 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003443 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003444 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003445 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003446 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003447 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003448 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003449 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003450 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003451
Jack Carter170a5f22013-09-09 22:02:08 +00003452 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3453 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003454 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003455 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00003456 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3457 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003458 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003459
Chris Lattner061a1ea2005-01-07 07:46:32 +00003460 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003461#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003462 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003463#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003464 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003465}
3466
Andrew Trickef9de2a2013-05-25 02:42:55 +00003467SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003468 SDValue N1, SDValue N2, SDValue N3,
3469 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003470 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003471 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003472}
3473
Andrew Trickef9de2a2013-05-25 02:42:55 +00003474SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003475 SDValue N1, SDValue N2, SDValue N3,
3476 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003477 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003478 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003479}
3480
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003481/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3482/// the incoming stack arguments to be loaded from the stack.
3483SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3484 SmallVector<SDValue, 8> ArgChains;
3485
3486 // Include the original chain at the beginning of the list. When this is
3487 // used by target LowerCall hooks, this helps legalize find the
3488 // CALLSEQ_BEGIN node.
3489 ArgChains.push_back(Chain);
3490
3491 // Add a chain value for each stack argument.
3492 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3493 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3494 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3495 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3496 if (FI->getIndex() < 0)
3497 ArgChains.push_back(SDValue(L, 1));
3498
3499 // Build a tokenfactor for all the chains.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003500 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003501 &ArgChains[0], ArgChains.size());
3502}
3503
Dan Gohman544ab2c2008-04-12 04:36:06 +00003504/// getMemsetValue - Vectorized representation of the memset value
3505/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003506static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003507 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003508 assert(Value.getOpcode() != ISD::UNDEF);
3509
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003510 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003511 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003512 assert(C->getAPIntValue().getBitWidth() == 8);
3513 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003514 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003515 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003516 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003517 }
Evan Chengef377ad2008-05-15 08:39:06 +00003518
Dale Johannesenabf66b82009-02-03 22:26:09 +00003519 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003520 if (NumBits > 8) {
3521 // Use a multiplication with 0x010101... to extend the input to the
3522 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003523 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003524 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003525 }
3526
3527 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003528}
3529
Dan Gohman544ab2c2008-04-12 04:36:06 +00003530/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3531/// used when a memcpy is turned into a memset when the source is a constant
3532/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003533static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003534 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003535 // Handle vector with all elements zero.
3536 if (Str.empty()) {
3537 if (VT.isInteger())
3538 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003539 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003540 return DAG.getConstantFP(0.0, VT);
3541 else if (VT.isVector()) {
3542 unsigned NumElts = VT.getVectorNumElements();
3543 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003544 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003545 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3546 EltVT, NumElts)));
3547 } else
3548 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003549 }
3550
Duncan Sands13237ac2008-06-06 12:08:01 +00003551 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003552 unsigned NumVTBits = VT.getSizeInBits();
3553 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003554 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3555
Evan Chengc8444b12013-01-10 22:13:27 +00003556 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003557 if (TLI.isLittleEndian()) {
3558 for (unsigned i = 0; i != NumBytes; ++i)
3559 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3560 } else {
3561 for (unsigned i = 0; i != NumBytes; ++i)
3562 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003563 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003564
Evan Chengeb542402012-12-11 23:26:14 +00003565 // If the "cost" of materializing the integer immediate is 1 or free, then
3566 // it is cost effective to turn the load into the immediate.
Chandler Carruth42e96112013-01-05 12:32:17 +00003567 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
3568 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003569 return DAG.getConstant(Val, VT);
3570 return SDValue(0, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003571}
3572
Scott Michelcf0da6c2009-02-17 22:15:04 +00003573/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003574///
Andrew Tricke2431c62013-05-25 03:08:10 +00003575static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003576 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003577 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003578 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003579 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003580}
3581
Evan Chengef377ad2008-05-15 08:39:06 +00003582/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3583///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003584static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003585 unsigned SrcDelta = 0;
3586 GlobalAddressSDNode *G = NULL;
3587 if (Src.getOpcode() == ISD::GlobalAddress)
3588 G = cast<GlobalAddressSDNode>(Src);
3589 else if (Src.getOpcode() == ISD::ADD &&
3590 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3591 Src.getOperand(1).getOpcode() == ISD::Constant) {
3592 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003593 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003594 }
3595 if (!G)
3596 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003597
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003598 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003599}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003600
Evan Cheng43cd9e32010-04-01 06:04:33 +00003601/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3602/// to replace the memset / memcpy. Return true if the number of memory ops
3603/// is below the threshold. It returns the types of the sequence of
3604/// memory ops to perform memset / memcpy by reference.
3605static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003606 unsigned Limit, uint64_t Size,
3607 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003608 bool IsMemset,
3609 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003610 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003611 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003612 SelectionDAG &DAG,
3613 const TargetLowering &TLI) {
3614 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3615 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003616 // If 'SrcAlign' is zero, that means the memory operation does not need to
3617 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3618 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3619 // is the specified alignment of the memory operation. If it is zero, that
3620 // means it's possible to change the alignment of the destination.
3621 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3622 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003623 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003624 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003625 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003626
Chris Lattner28dc6c12010-03-07 07:45:08 +00003627 if (VT == MVT::Other) {
Chandler Carruth5da3f052012-11-01 09:14:31 +00003628 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng43cd9e32010-04-01 06:04:33 +00003629 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003630 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003631 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003632 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003633 case 0: VT = MVT::i64; break;
3634 case 4: VT = MVT::i32; break;
3635 case 2: VT = MVT::i16; break;
3636 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003637 }
3638 }
3639
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003640 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003641 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003642 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003643 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003644
Duncan Sands11dd4242008-06-08 20:54:56 +00003645 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003646 VT = LVT;
3647 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003648
Dan Gohman544ab2c2008-04-12 04:36:06 +00003649 unsigned NumMemOps = 0;
3650 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003651 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003652 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003653 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003654 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003655 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003656
3657 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003658 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003659 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003660 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003661 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003662 Found = true;
3663 else if (NewVT == MVT::i64 &&
3664 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003665 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003666 // i64 is usually not legal on 32-bit targets, but f64 may be.
3667 NewVT = MVT::f64;
3668 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003669 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003670 }
3671
Evan Cheng04e55182012-12-12 00:42:09 +00003672 if (!Found) {
3673 do {
3674 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3675 if (NewVT == MVT::i8)
3676 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003677 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003678 }
3679 NewVTSize = NewVT.getSizeInBits() / 8;
3680
Evan Cheng79e2ca92012-12-10 23:21:26 +00003681 // If the new VT cannot cover all of the remaining bits, then consider
3682 // issuing a (or a pair of) unaligned and overlapping load / store.
3683 // FIXME: Only does this for 64-bit or more since we don't have proper
3684 // cost model for unaligned load / store.
3685 bool Fast;
Evan Chengb7d3d032012-12-12 20:43:23 +00003686 if (NumMemOps && AllowOverlap &&
3687 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00003688 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3689 VTSize = Size;
3690 else {
3691 VT = NewVT;
3692 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003693 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003694 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003695
Evan Chengb7d3d032012-12-12 20:43:23 +00003696 if (++NumMemOps > Limit)
3697 return false;
3698
Dan Gohman544ab2c2008-04-12 04:36:06 +00003699 MemOps.push_back(VT);
3700 Size -= VTSize;
3701 }
3702
3703 return true;
3704}
3705
Andrew Trickef9de2a2013-05-25 02:42:55 +00003706static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003707 SDValue Chain, SDValue Dst,
3708 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003709 unsigned Align, bool isVol,
3710 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003711 MachinePointerInfo DstPtrInfo,
3712 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003713 // Turn a memcpy of undef to nop.
3714 if (Src.getOpcode() == ISD::UNDEF)
3715 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003716
Dan Gohman714663a2008-05-29 19:42:22 +00003717 // Expand memcpy to a series of load and store ops if the size operand falls
3718 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003719 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3720 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003721 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003722 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003723 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003724 MachineFunction &MF = DAG.getMachineFunction();
3725 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003726 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003727 MF.getFunction()->getAttributes().
3728 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003729 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3730 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3731 DstAlignCanChange = true;
3732 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3733 if (Align > SrcAlign)
3734 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003735 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003736 bool CopyFromStr = isMemSrcFromString(Src, Str);
3737 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003738 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003739
Evan Cheng4c014c82010-04-01 18:19:11 +00003740 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003741 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003742 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003743 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003744 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003745
Evan Cheng43cd9e32010-04-01 06:04:33 +00003746 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003747 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003748 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003749
3750 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003751 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003752 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3753 if (!TRI->needsStackRealignment(MF))
3754 while (NewAlign > Align &&
3755 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3756 NewAlign /= 2;
3757
Evan Cheng43cd9e32010-04-01 06:04:33 +00003758 if (NewAlign > Align) {
3759 // Give the stack frame object a larger alignment if needed.
3760 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3761 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3762 Align = NewAlign;
3763 }
3764 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003765
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003766 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003767 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003768 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003769 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003770 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003771 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003772 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003773
Evan Cheng79e2ca92012-12-10 23:21:26 +00003774 if (VTSize > Size) {
3775 // Issuing an unaligned load / store pair that overlaps with the previous
3776 // pair. Adjust the offset accordingly.
3777 assert(i == NumMemOps-1 && i != 0);
3778 SrcOff -= VTSize - Size;
3779 DstOff -= VTSize - Size;
3780 }
3781
Evan Cheng43cd9e32010-04-01 06:04:33 +00003782 if (CopyFromStr &&
3783 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003784 // It's unlikely a store of a vector immediate can be done in a single
3785 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003786 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003787 // FIXME: Handle other cases where store of vector immediate is done in
3788 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003789 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003790 if (Value.getNode())
3791 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003792 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003793 DstPtrInfo.getWithOffset(DstOff), isVol,
3794 false, Align);
3795 }
3796
3797 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003798 // The type might not be legal for the target. This should only happen
3799 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003800 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3801 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003802 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003803 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003804 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003805 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003806 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003807 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003808 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003809 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003810 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003811 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3812 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003813 }
3814 OutChains.push_back(Store);
3815 SrcOff += VTSize;
3816 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003817 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003818 }
3819
Owen Anderson9f944592009-08-11 20:47:22 +00003820 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003821 &OutChains[0], OutChains.size());
3822}
3823
Andrew Trickef9de2a2013-05-25 02:42:55 +00003824static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003825 SDValue Chain, SDValue Dst,
3826 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003827 unsigned Align, bool isVol,
3828 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003829 MachinePointerInfo DstPtrInfo,
3830 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003831 // Turn a memmove of undef to nop.
3832 if (Src.getOpcode() == ISD::UNDEF)
3833 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003834
3835 // Expand memmove to a series of load and store ops if the size operand falls
3836 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003837 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003838 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003839 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003840 MachineFunction &MF = DAG.getMachineFunction();
3841 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003842 bool OptSize = MF.getFunction()->getAttributes().
3843 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003844 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3845 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3846 DstAlignCanChange = true;
3847 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3848 if (Align > SrcAlign)
3849 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003850 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003851
Evan Cheng4c014c82010-04-01 18:19:11 +00003852 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003853 (DstAlignCanChange ? 0 : Align), SrcAlign,
3854 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003855 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003856
Evan Cheng43cd9e32010-04-01 06:04:33 +00003857 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003858 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003859 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003860 if (NewAlign > Align) {
3861 // Give the stack frame object a larger alignment if needed.
3862 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3863 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3864 Align = NewAlign;
3865 }
3866 }
Dan Gohman714663a2008-05-29 19:42:22 +00003867
Evan Cheng43cd9e32010-04-01 06:04:33 +00003868 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003869 SmallVector<SDValue, 8> LoadValues;
3870 SmallVector<SDValue, 8> LoadChains;
3871 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003872 unsigned NumMemOps = MemOps.size();
3873 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003874 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003875 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003876 SDValue Value;
Dan Gohman714663a2008-05-29 19:42:22 +00003877
Dale Johannesenabf66b82009-02-03 22:26:09 +00003878 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003879 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003880 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003881 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003882 LoadValues.push_back(Value);
3883 LoadChains.push_back(Value.getValue(1));
3884 SrcOff += VTSize;
3885 }
Owen Anderson9f944592009-08-11 20:47:22 +00003886 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003887 &LoadChains[0], LoadChains.size());
3888 OutChains.clear();
3889 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003890 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003891 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola44fee4e2013-10-01 13:32:03 +00003892 SDValue Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003893
Dale Johannesenabf66b82009-02-03 22:26:09 +00003894 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003895 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003896 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003897 OutChains.push_back(Store);
3898 DstOff += VTSize;
3899 }
3900
Owen Anderson9f944592009-08-11 20:47:22 +00003901 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003902 &OutChains[0], OutChains.size());
3903}
3904
Serge Pavlov8ec39992013-09-17 16:24:42 +00003905/// \brief Lower the call to 'memset' intrinsic function into a series of store
3906/// operations.
3907///
3908/// \param DAG Selection DAG where lowered code is placed.
3909/// \param dl Link to corresponding IR location.
3910/// \param Chain Control flow dependency.
3911/// \param Dst Pointer to destination memory location.
3912/// \param Src Value of byte to write into the memory.
3913/// \param Size Number of bytes to write.
3914/// \param Align Alignment of the destination in bytes.
3915/// \param isVol True if destination is volatile.
3916/// \param DstPtrInfo IR information on the memory pointer.
3917/// \returns New head in the control flow, if lowering was successful, empty
3918/// SDValue otherwise.
3919///
3920/// The function tries to replace 'llvm.memset' intrinsic with several store
3921/// operations and value calculation code. This is usually profitable for small
3922/// memory size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003923static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003924 SDValue Chain, SDValue Dst,
3925 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003926 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003927 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003928 // Turn a memset of undef to nop.
3929 if (Src.getOpcode() == ISD::UNDEF)
3930 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003931
3932 // Expand memset to a series of load/store ops if the size operand
3933 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003934 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003935 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003936 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003937 MachineFunction &MF = DAG.getMachineFunction();
3938 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003939 bool OptSize = MF.getFunction()->getAttributes().
3940 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003941 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3942 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3943 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003944 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003945 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003946 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00003947 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00003948 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003949 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003950
Evan Cheng43cd9e32010-04-01 06:04:33 +00003951 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003952 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003953 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003954 if (NewAlign > Align) {
3955 // Give the stack frame object a larger alignment if needed.
3956 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3957 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3958 Align = NewAlign;
3959 }
3960 }
3961
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003962 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00003963 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003964 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003965
3966 // Find the largest store and generate the bit pattern for it.
3967 EVT LargestVT = MemOps[0];
3968 for (unsigned i = 1; i < NumMemOps; i++)
3969 if (MemOps[i].bitsGT(LargestVT))
3970 LargestVT = MemOps[i];
3971 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3972
Dan Gohman544ab2c2008-04-12 04:36:06 +00003973 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003974 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00003975 unsigned VTSize = VT.getSizeInBits() / 8;
3976 if (VTSize > Size) {
3977 // Issuing an unaligned load / store pair that overlaps with the previous
3978 // pair. Adjust the offset accordingly.
3979 assert(i == NumMemOps-1 && i != 0);
3980 DstOff -= VTSize - Size;
3981 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003982
3983 // If this store is smaller than the largest store see whether we can get
3984 // the smaller value for free with a truncate.
3985 SDValue Value = MemSetValue;
3986 if (VT.bitsLT(LargestVT)) {
3987 if (!LargestVT.isVector() && !VT.isVector() &&
3988 TLI.isTruncateFree(LargestVT, VT))
3989 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3990 else
3991 Value = getMemsetValue(Src, VT, DAG, dl);
3992 }
3993 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00003994 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003995 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003996 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00003997 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003998 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003999 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00004000 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004001 }
4002
Owen Anderson9f944592009-08-11 20:47:22 +00004003 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00004004 &OutChains[0], OutChains.size());
4005}
4006
Andrew Trickef9de2a2013-05-25 02:42:55 +00004007SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004008 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004009 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004010 MachinePointerInfo DstPtrInfo,
4011 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004012 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004013
4014 // Check to see if we should lower the memcpy to loads and stores first.
4015 // For cases within the target-specified limits, this is the best choice.
4016 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4017 if (ConstantSize) {
4018 // Memcpy with size zero? Just return the original chain.
4019 if (ConstantSize->isNullValue())
4020 return Chain;
4021
Evan Cheng43cd9e32010-04-01 06:04:33 +00004022 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4023 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00004024 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004025 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004026 return Result;
4027 }
4028
4029 // Then check to see if we should lower the memcpy with target-specific
4030 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004031 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004032 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00004033 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00004034 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004035 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004036 return Result;
4037
4038 // If we really need inline code and the target declined to provide it,
4039 // use a (potentially long) sequence of loads and stores.
4040 if (AlwaysInline) {
4041 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00004042 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004043 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004044 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004045 }
4046
Dan Gohmanf38547c2010-04-05 20:24:08 +00004047 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4048 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4049 // respect volatile, so they may do things like read or write memory
4050 // beyond the given memory regions. But fixing this isn't easy, and most
4051 // people don't care.
4052
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004053 const TargetLowering *TLI = TM.getTargetLowering();
4054
Dan Gohman544ab2c2008-04-12 04:36:06 +00004055 // Emit a library call.
4056 TargetLowering::ArgListTy Args;
4057 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004058 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004059 Entry.Node = Dst; Args.push_back(Entry);
4060 Entry.Node = Src; Args.push_back(Entry);
4061 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004062 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004063 TargetLowering::
4064 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004065 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004066 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00004067 /*isTailCall=*/false,
4068 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004069 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4070 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004071 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004072 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004073
Dan Gohman544ab2c2008-04-12 04:36:06 +00004074 return CallResult.second;
4075}
4076
Andrew Trickef9de2a2013-05-25 02:42:55 +00004077SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004078 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004079 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004080 MachinePointerInfo DstPtrInfo,
4081 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004082 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004083
Dan Gohman714663a2008-05-29 19:42:22 +00004084 // Check to see if we should lower the memmove to loads and stores first.
4085 // For cases within the target-specified limits, this is the best choice.
4086 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4087 if (ConstantSize) {
4088 // Memmove with size zero? Just return the original chain.
4089 if (ConstantSize->isNullValue())
4090 return Chain;
4091
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004092 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00004093 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00004094 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004095 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004096 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00004097 return Result;
4098 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00004099
4100 // Then check to see if we should lower the memmove with target-specific
4101 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004102 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004103 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004104 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004105 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004106 return Result;
4107
Mon P Wangbf862242010-04-06 08:27:51 +00004108 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4109 // not be safe. See memcpy above for more details.
4110
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004111 const TargetLowering *TLI = TM.getTargetLowering();
4112
Dan Gohman544ab2c2008-04-12 04:36:06 +00004113 // Emit a library call.
4114 TargetLowering::ArgListTy Args;
4115 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004116 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004117 Entry.Node = Dst; Args.push_back(Entry);
4118 Entry.Node = Src; Args.push_back(Entry);
4119 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004120 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004121 TargetLowering::
4122 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004123 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004124 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004125 /*isTailCall=*/false,
4126 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004127 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4128 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004129 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004130 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004131
Dan Gohman544ab2c2008-04-12 04:36:06 +00004132 return CallResult.second;
4133}
4134
Andrew Trickef9de2a2013-05-25 02:42:55 +00004135SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004136 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004137 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004138 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004139 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004140
4141 // Check to see if we should lower the memset to stores first.
4142 // For cases within the target-specified limits, this is the best choice.
4143 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4144 if (ConstantSize) {
4145 // Memset with size zero? Just return the original chain.
4146 if (ConstantSize->isNullValue())
4147 return Chain;
4148
Mon P Wangc576ee92010-04-04 03:10:48 +00004149 SDValue Result =
4150 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004151 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004152
Gabor Greiff304a7a2008-08-28 21:40:38 +00004153 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004154 return Result;
4155 }
4156
4157 // Then check to see if we should lower the memset with target-specific
4158 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004159 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004160 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004161 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004162 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004163 return Result;
4164
Wesley Peck527da1b2010-11-23 03:31:01 +00004165 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004166 const TargetLowering *TLI = TM.getTargetLowering();
4167 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004168 TargetLowering::ArgListTy Args;
4169 TargetLowering::ArgListEntry Entry;
4170 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4171 Args.push_back(Entry);
4172 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004173 if (Src.getValueType().bitsGT(MVT::i32))
4174 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004175 else
Owen Anderson9f944592009-08-11 20:47:22 +00004176 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004177 Entry.Node = Src;
4178 Entry.Ty = Type::getInt32Ty(*getContext());
4179 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004180 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004181 Entry.Node = Size;
4182 Entry.Ty = IntPtrTy;
4183 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004184 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004185 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004186 TargetLowering::
4187 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004188 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004189 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004190 /*isTailCall=*/false,
4191 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004192 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4193 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004194 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004195 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004196
Dan Gohman544ab2c2008-04-12 04:36:06 +00004197 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004198}
4199
Andrew Trickef9de2a2013-05-25 02:42:55 +00004200SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004201 SDVTList VTList, SDValue* Ops, unsigned NumOps,
4202 MachineMemOperand *MMO,
4203 AtomicOrdering Ordering,
4204 SynchronizationScope SynchScope) {
4205 FoldingSetNodeID ID;
4206 ID.AddInteger(MemVT.getRawBits());
4207 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4208 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4209 void* IP = 0;
4210 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4211 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4212 return SDValue(E, 0);
4213 }
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004214
4215 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4216 // SDNode doesn't have access to it. This memory will be "leaked" when
4217 // the node is deallocated, but recovered when the allocator is released.
4218 // If the number of operands is less than 5 we use AtomicSDNode's internal
4219 // storage.
4220 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : 0;
4221
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004222 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4223 dl.getDebugLoc(), VTList, MemVT,
Benjamin Kramerc3c807b2013-09-29 11:18:56 +00004224 Ops, DynOps, NumOps, MMO,
4225 Ordering, SynchScope);
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004226 CSEMap.InsertNode(N, IP);
4227 AllNodes.push_back(N);
4228 return SDValue(N, 0);
4229}
4230
4231SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004232 SDValue Chain, SDValue Ptr, SDValue Cmp,
4233 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004234 unsigned Alignment,
4235 AtomicOrdering Ordering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004236 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004237 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4238 Alignment = getEVTAlignment(MemVT);
4239
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004240 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004241
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004242 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004243 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004244 // FIXME: Volatile isn't really correct; we should keep track of atomic
4245 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004246 unsigned Flags = MachineMemOperand::MOVolatile;
4247 if (Opcode != ISD::ATOMIC_STORE)
4248 Flags |= MachineMemOperand::MOLoad;
4249 if (Opcode != ISD::ATOMIC_LOAD)
4250 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004251
4252 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004253 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004254
Eli Friedmanadec5872011-07-29 03:05:32 +00004255 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4256 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004257}
4258
Andrew Trickef9de2a2013-05-25 02:42:55 +00004259SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004260 SDValue Chain,
4261 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004262 SDValue Swp, MachineMemOperand *MMO,
4263 AtomicOrdering Ordering,
4264 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004265 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4266 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4267
Owen Anderson53aa7a92009-08-10 22:56:29 +00004268 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004269
Owen Anderson9f944592009-08-11 20:47:22 +00004270 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004271 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004272 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004273}
4274
Andrew Trickef9de2a2013-05-25 02:42:55 +00004275SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004276 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004277 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004278 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004279 unsigned Alignment,
4280 AtomicOrdering Ordering,
4281 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004282 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4283 Alignment = getEVTAlignment(MemVT);
4284
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004285 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004286 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004287 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004288 // For now, atomics are considered to be volatile always, and they are
4289 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004290 // FIXME: Volatile isn't really correct; we should keep track of atomic
4291 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004292 unsigned Flags = MachineMemOperand::MOVolatile;
4293 if (Opcode != ISD::ATOMIC_STORE)
4294 Flags |= MachineMemOperand::MOLoad;
4295 if (Opcode != ISD::ATOMIC_LOAD)
4296 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004297
4298 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004299 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004300 MemVT.getStoreSize(), Alignment);
4301
Eli Friedmanadec5872011-07-29 03:05:32 +00004302 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4303 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004304}
4305
Andrew Trickef9de2a2013-05-25 02:42:55 +00004306SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004307 SDValue Chain,
4308 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004309 MachineMemOperand *MMO,
4310 AtomicOrdering Ordering,
4311 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004312 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4313 Opcode == ISD::ATOMIC_LOAD_SUB ||
4314 Opcode == ISD::ATOMIC_LOAD_AND ||
4315 Opcode == ISD::ATOMIC_LOAD_OR ||
4316 Opcode == ISD::ATOMIC_LOAD_XOR ||
4317 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004318 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004319 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004320 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004321 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004322 Opcode == ISD::ATOMIC_SWAP ||
4323 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004324 "Invalid Atomic Op");
4325
Owen Anderson53aa7a92009-08-10 22:56:29 +00004326 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004327
Eli Friedman342e8df2011-08-24 20:50:09 +00004328 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4329 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004330 SDValue Ops[] = {Chain, Ptr, Val};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004331 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004332}
4333
Andrew Trickef9de2a2013-05-25 02:42:55 +00004334SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004335 EVT VT, SDValue Chain,
4336 SDValue Ptr,
4337 const Value* PtrVal,
4338 unsigned Alignment,
4339 AtomicOrdering Ordering,
4340 SynchronizationScope SynchScope) {
4341 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4342 Alignment = getEVTAlignment(MemVT);
4343
4344 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004345 // An atomic store does not load. An atomic load does not store.
4346 // (An atomicrmw obviously both loads and stores.)
4347 // For now, atomics are considered to be volatile always, and they are
4348 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004349 // FIXME: Volatile isn't really correct; we should keep track of atomic
4350 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004351 unsigned Flags = MachineMemOperand::MOVolatile;
4352 if (Opcode != ISD::ATOMIC_STORE)
4353 Flags |= MachineMemOperand::MOLoad;
4354 if (Opcode != ISD::ATOMIC_LOAD)
4355 Flags |= MachineMemOperand::MOStore;
Eli Friedman342e8df2011-08-24 20:50:09 +00004356
4357 MachineMemOperand *MMO =
4358 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4359 MemVT.getStoreSize(), Alignment);
4360
4361 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4362 Ordering, SynchScope);
4363}
4364
Andrew Trickef9de2a2013-05-25 02:42:55 +00004365SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004366 EVT VT, SDValue Chain,
4367 SDValue Ptr,
4368 MachineMemOperand *MMO,
4369 AtomicOrdering Ordering,
4370 SynchronizationScope SynchScope) {
4371 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4372
4373 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman342e8df2011-08-24 20:50:09 +00004374 SDValue Ops[] = {Chain, Ptr};
Amara Emersonb4ad2f32013-09-26 12:22:36 +00004375 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope);
Eli Friedman342e8df2011-08-24 20:50:09 +00004376}
4377
Duncan Sands739a0542008-07-02 17:40:58 +00004378/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesenae7992a2009-02-02 20:47:48 +00004379SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004380 SDLoc dl) {
Dale Johannesenae7992a2009-02-02 20:47:48 +00004381 if (NumOps == 1)
4382 return Ops[0];
4383
Owen Anderson53aa7a92009-08-10 22:56:29 +00004384 SmallVector<EVT, 4> VTs;
Dale Johannesenae7992a2009-02-02 20:47:48 +00004385 VTs.reserve(NumOps);
4386 for (unsigned i = 0; i < NumOps; ++i)
4387 VTs.push_back(Ops[i].getValueType());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004388 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesenae7992a2009-02-02 20:47:48 +00004389 Ops, NumOps);
4390}
4391
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004392SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004393SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004394 const EVT *VTs, unsigned NumVTs,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004395 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004396 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004397 unsigned Align, bool Vol,
4398 bool ReadMem, bool WriteMem) {
4399 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004400 MemVT, PtrInfo, Align, Vol,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004401 ReadMem, WriteMem);
4402}
4403
4404SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004405SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004406 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004407 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004408 unsigned Align, bool Vol,
4409 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004410 if (Align == 0) // Ensure that codegen never sees alignment 0
4411 Align = getEVTAlignment(MemVT);
4412
4413 MachineFunction &MF = getMachineFunction();
4414 unsigned Flags = 0;
4415 if (WriteMem)
4416 Flags |= MachineMemOperand::MOStore;
4417 if (ReadMem)
4418 Flags |= MachineMemOperand::MOLoad;
4419 if (Vol)
4420 Flags |= MachineMemOperand::MOVolatile;
4421 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004422 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004423
4424 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4425}
4426
4427SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004428SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohman48b185d2009-09-25 20:36:54 +00004429 const SDValue *Ops, unsigned NumOps,
4430 EVT MemVT, MachineMemOperand *MMO) {
4431 assert((Opcode == ISD::INTRINSIC_VOID ||
4432 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004433 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004434 Opcode == ISD::LIFETIME_START ||
4435 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004436 (Opcode <= INT_MAX &&
4437 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4438 "Opcode is not a memory-accessing opcode!");
4439
Dale Johannesen839acbb2009-01-29 00:47:48 +00004440 // Memoize the node unless it returns a flag.
4441 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004442 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004443 FoldingSetNodeID ID;
4444 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper91244262012-07-30 20:23:19 +00004445 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004446 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4448 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004449 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004450 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004451
Jack Carter170a5f22013-09-09 22:02:08 +00004452 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4453 dl.getDebugLoc(), VTList, Ops,
4454 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004455 CSEMap.InsertNode(N, IP);
4456 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004457 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4458 dl.getDebugLoc(), VTList, Ops,
4459 NumOps, MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004460 }
4461 AllNodes.push_back(N);
4462 return SDValue(N, 0);
4463}
4464
Chris Lattnerea952f02010-09-21 17:24:05 +00004465/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4466/// MachinePointerInfo record from it. This is particularly useful because the
4467/// code generator has many cases where it doesn't bother passing in a
4468/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4469static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4470 // If this is FI+Offset, we can model it.
4471 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4472 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4473
4474 // If this is (FI+Offset1)+Offset2, we can model it.
4475 if (Ptr.getOpcode() != ISD::ADD ||
4476 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4477 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4478 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004479
Chris Lattnerea952f02010-09-21 17:24:05 +00004480 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4481 return MachinePointerInfo::getFixedStack(FI, Offset+
4482 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4483}
4484
4485/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4486/// MachinePointerInfo record from it. This is particularly useful because the
4487/// code generator has many cases where it doesn't bother passing in a
4488/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4489static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4490 // If the 'Offset' value isn't a constant, we can't handle this.
4491 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4492 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4493 if (OffsetOp.getOpcode() == ISD::UNDEF)
4494 return InferPointerInfo(Ptr);
4495 return MachinePointerInfo();
4496}
Wesley Peck527da1b2010-11-23 03:31:01 +00004497
Chris Lattnerea952f02010-09-21 17:24:05 +00004498
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004499SDValue
4500SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004501 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004502 SDValue Ptr, SDValue Offset,
4503 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004504 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004505 unsigned Alignment, const MDNode *TBAAInfo,
4506 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004507 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004508 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004509 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4510 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004511
Dan Gohman48b185d2009-09-25 20:36:54 +00004512 unsigned Flags = MachineMemOperand::MOLoad;
4513 if (isVolatile)
4514 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004515 if (isNonTemporal)
4516 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004517 if (isInvariant)
4518 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004519
Chris Lattnerea952f02010-09-21 17:24:05 +00004520 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4521 // clients.
4522 if (PtrInfo.V == 0)
4523 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004524
Chris Lattnerea952f02010-09-21 17:24:05 +00004525 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004526 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004527 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004528 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004529 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004530}
4531
4532SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004533SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004534 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004535 SDValue Ptr, SDValue Offset, EVT MemVT,
4536 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004537 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004538 ExtType = ISD::NON_EXTLOAD;
4539 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004540 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004541 } else {
4542 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004543 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4544 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004545 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004546 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004547 assert(VT.isVector() == MemVT.isVector() &&
4548 "Cannot use trunc store to convert to or from a vector!");
4549 assert((!VT.isVector() ||
4550 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4551 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004552 }
4553
4554 bool Indexed = AM != ISD::UNINDEXED;
4555 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4556 "Unindexed load with an offset!");
4557
4558 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004559 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004560 SDValue Ops[] = { Chain, Ptr, Offset };
4561 FoldingSetNodeID ID;
4562 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman08c0a952009-09-23 21:02:20 +00004563 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004564 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004565 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004566 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004567 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004568 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004569 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4570 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004571 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004572 }
Jack Carter170a5f22013-09-09 22:02:08 +00004573 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4574 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004575 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004576 CSEMap.InsertNode(N, IP);
4577 AllNodes.push_back(N);
4578 return SDValue(N, 0);
4579}
4580
Andrew Trickef9de2a2013-05-25 02:42:55 +00004581SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004582 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004583 MachinePointerInfo PtrInfo,
4584 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004585 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004586 const MDNode *TBAAInfo,
4587 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004588 SDValue Undef = getUNDEF(Ptr.getValueType());
4589 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004590 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4591 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004592}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004593
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004594SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4595 SDValue Chain, SDValue Ptr,
4596 MachineMemOperand *MMO) {
4597 SDValue Undef = getUNDEF(Ptr.getValueType());
4598 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4599 VT, MMO);
4600}
4601
Andrew Trickef9de2a2013-05-25 02:42:55 +00004602SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004603 SDValue Chain, SDValue Ptr,
4604 MachinePointerInfo PtrInfo, EVT MemVT,
4605 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004606 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004607 SDValue Undef = getUNDEF(Ptr.getValueType());
4608 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004609 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004610 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004611}
4612
4613
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004614SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4615 SDValue Chain, SDValue Ptr, EVT MemVT,
4616 MachineMemOperand *MMO) {
4617 SDValue Undef = getUNDEF(Ptr.getValueType());
4618 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4619 MemVT, MMO);
4620}
4621
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004622SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004623SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004624 SDValue Offset, ISD::MemIndexedMode AM) {
4625 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4626 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4627 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004628 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004629 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004630 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004631 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004632}
4633
Andrew Trickef9de2a2013-05-25 02:42:55 +00004634SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004635 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004636 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004637 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004638 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004639 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004640 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004641 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004642
Dan Gohman48b185d2009-09-25 20:36:54 +00004643 unsigned Flags = MachineMemOperand::MOStore;
4644 if (isVolatile)
4645 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004646 if (isNonTemporal)
4647 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004648
Chris Lattnerea952f02010-09-21 17:24:05 +00004649 if (PtrInfo.V == 0)
4650 PtrInfo = InferPointerInfo(Ptr);
4651
4652 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004653 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004654 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004655 Val.getValueType().getStoreSize(), Alignment,
4656 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004657
4658 return getStore(Chain, dl, Val, Ptr, MMO);
4659}
4660
Andrew Trickef9de2a2013-05-25 02:42:55 +00004661SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004662 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004663 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004664 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004665 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004666 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004667 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004668 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4669 FoldingSetNodeID ID;
4670 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004671 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004672 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004673 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004674 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004675 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004676 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4677 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004678 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004679 }
Jack Carter170a5f22013-09-09 22:02:08 +00004680 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4681 dl.getDebugLoc(), VTs,
4682 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004683 CSEMap.InsertNode(N, IP);
4684 AllNodes.push_back(N);
4685 return SDValue(N, 0);
4686}
4687
Andrew Trickef9de2a2013-05-25 02:42:55 +00004688SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004689 SDValue Ptr, MachinePointerInfo PtrInfo,
4690 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004691 unsigned Alignment,
4692 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004693 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004694 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004695 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4696 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004697
Dan Gohman48b185d2009-09-25 20:36:54 +00004698 unsigned Flags = MachineMemOperand::MOStore;
4699 if (isVolatile)
4700 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004701 if (isNonTemporal)
4702 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004703
Chris Lattnerea952f02010-09-21 17:24:05 +00004704 if (PtrInfo.V == 0)
4705 PtrInfo = InferPointerInfo(Ptr);
4706
4707 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004708 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004709 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4710 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004711
4712 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4713}
4714
Andrew Trickef9de2a2013-05-25 02:42:55 +00004715SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004716 SDValue Ptr, EVT SVT,
4717 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004718 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004719
Michael Ilsemand5f91512012-09-10 16:56:31 +00004720 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004721 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004722 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004723 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004724
Dan Gohmancecad352009-12-14 23:40:38 +00004725 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4726 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004727 assert(VT.isInteger() == SVT.isInteger() &&
4728 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004729 assert(VT.isVector() == SVT.isVector() &&
4730 "Cannot use trunc store to convert to or from a vector!");
4731 assert((!VT.isVector() ||
4732 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4733 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004734
Owen Anderson9f944592009-08-11 20:47:22 +00004735 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004736 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004737 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4738 FoldingSetNodeID ID;
4739 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004740 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004741 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004742 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004743 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004744 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004745 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4746 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004747 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004748 }
Jack Carter170a5f22013-09-09 22:02:08 +00004749 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4750 dl.getDebugLoc(), VTs,
4751 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004752 CSEMap.InsertNode(N, IP);
4753 AllNodes.push_back(N);
4754 return SDValue(N, 0);
4755}
4756
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004757SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004758SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004759 SDValue Offset, ISD::MemIndexedMode AM) {
4760 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4761 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4762 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004763 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004764 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4765 FoldingSetNodeID ID;
4766 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004767 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004768 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004769 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004770 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004771 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004772 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004773
Jack Carter170a5f22013-09-09 22:02:08 +00004774 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4775 dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004776 ST->isTruncatingStore(),
4777 ST->getMemoryVT(),
4778 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004779 CSEMap.InsertNode(N, IP);
4780 AllNodes.push_back(N);
4781 return SDValue(N, 0);
4782}
4783
Andrew Trickef9de2a2013-05-25 02:42:55 +00004784SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004785 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004786 SDValue SV,
4787 unsigned Align) {
4788 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4789 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004790}
4791
Andrew Trickef9de2a2013-05-25 02:42:55 +00004792SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004793 const SDUse *Ops, unsigned NumOps) {
Dan Gohman768f2c92008-07-07 18:26:29 +00004794 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004795 case 0: return getNode(Opcode, DL, VT);
4796 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4797 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4798 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004799 default: break;
4800 }
4801
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004802 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004803 // the regular getNode logic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004804 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004805 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004806}
4807
Andrew Trickef9de2a2013-05-25 02:42:55 +00004808SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004809 const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00004810 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004811 case 0: return getNode(Opcode, DL, VT);
4812 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4813 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4814 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004815 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004816 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004817
Chris Lattnerb0713c72005-04-09 03:27:28 +00004818 switch (Opcode) {
4819 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004820 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004821 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004822 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4823 "LHS and RHS of condition must have same type!");
4824 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4825 "True and False arms of SelectCC must have same type!");
4826 assert(Ops[2].getValueType() == VT &&
4827 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004828 break;
4829 }
4830 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004831 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004832 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4833 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004834 break;
4835 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004836 }
4837
Chris Lattner566307f2005-05-14 07:42:29 +00004838 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004839 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004840 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004841
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004842 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004843 FoldingSetNodeID ID;
4844 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004845 void *IP = 0;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004846
Bill Wendling022d18f2009-12-18 23:32:53 +00004847 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004848 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004849
Jack Carter170a5f22013-09-09 22:02:08 +00004850 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4851 VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004852 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004853 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004854 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4855 VTs, Ops, NumOps);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004856 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004857
Chris Lattnerb0713c72005-04-09 03:27:28 +00004858 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004859#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004860 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004861#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004862 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004863}
4864
Andrew Trickef9de2a2013-05-25 02:42:55 +00004865SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00004866 ArrayRef<EVT> ResultTys,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004867 const SDValue *Ops, unsigned NumOps) {
Dan Gohmande912e22009-04-09 23:54:40 +00004868 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner3bf4be42006-08-14 23:31:51 +00004869 Ops, NumOps);
4870}
4871
Andrew Trickef9de2a2013-05-25 02:42:55 +00004872SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004873 const EVT *VTs, unsigned NumVTs,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004874 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3bf4be42006-08-14 23:31:51 +00004875 if (NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004876 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4877 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004878}
4879
Andrew Trickef9de2a2013-05-25 02:42:55 +00004880SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004881 const SDValue *Ops, unsigned NumOps) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004882 if (VTList.NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004883 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattnerd5531332005-05-14 06:20:26 +00004884
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004885#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004886 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004887 // FIXME: figure out how to safely handle things like
4888 // int foo(int x) { return 1 << (x & 255); }
4889 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004890 case ISD::SRA_PARTS:
4891 case ISD::SRL_PARTS:
4892 case ISD::SHL_PARTS:
4893 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004894 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004895 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004896 else if (N3.getOpcode() == ISD::AND)
4897 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4898 // If the and is only masking out bits that cannot effect the shift,
4899 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004900 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004901 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004902 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004903 }
4904 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004905 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004906#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004907
Chris Lattnerf9c19152005-08-25 19:12:10 +00004908 // Memoize the node unless it returns a flag.
4909 SDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004910 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004911 FoldingSetNodeID ID;
4912 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004913 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004914 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004915 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004916
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004917 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004918 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4919 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004920 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004921 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4922 DL.getDebugLoc(), VTList, Ops[0],
4923 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004924 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004925 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4926 DL.getDebugLoc(), VTList, Ops[0],
4927 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004928 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004929 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4930 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004931 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004932 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004933 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004934 if (NumOps == 1) {
Jack Carter170a5f22013-09-09 22:02:08 +00004935 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4936 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004937 } else if (NumOps == 2) {
Jack Carter170a5f22013-09-09 22:02:08 +00004938 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4939 DL.getDebugLoc(), VTList, Ops[0],
4940 Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004941 } else if (NumOps == 3) {
Jack Carter170a5f22013-09-09 22:02:08 +00004942 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4943 DL.getDebugLoc(), VTList, Ops[0],
4944 Ops[1], Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004945 } else {
Jack Carter170a5f22013-09-09 22:02:08 +00004946 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4947 VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004948 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004949 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004950 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004951#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004952 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004953#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004954 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004955}
4956
Andrew Trickef9de2a2013-05-25 02:42:55 +00004957SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004958 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004959}
4960
Andrew Trickef9de2a2013-05-25 02:42:55 +00004961SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004962 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004963 SDValue Ops[] = { N1 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004964 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004965}
4966
Andrew Trickef9de2a2013-05-25 02:42:55 +00004967SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004968 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004969 SDValue Ops[] = { N1, N2 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004970 return getNode(Opcode, DL, VTList, Ops, 2);
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, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004975 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004976 return getNode(Opcode, DL, VTList, Ops, 3);
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, SDValue N3,
4981 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004982 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004983 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004984}
4985
Andrew Trickef9de2a2013-05-25 02:42:55 +00004986SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004987 SDValue N1, SDValue N2, SDValue N3,
4988 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004989 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004990 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004991}
4992
Owen Anderson53aa7a92009-08-10 22:56:29 +00004993SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00004994 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00004995}
4996
Owen Anderson53aa7a92009-08-10 22:56:29 +00004997SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00004998 FoldingSetNodeID ID;
4999 ID.AddInteger(2U);
5000 ID.AddInteger(VT1.getRawBits());
5001 ID.AddInteger(VT2.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005002
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005003 void *IP = 0;
5004 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5005 if (Result == NULL) {
5006 EVT *Array = Allocator.Allocate<EVT>(2);
5007 Array[0] = VT1;
5008 Array[1] = VT2;
5009 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5010 VTListMap.InsertNode(Result, IP);
5011 }
5012 return Result->getSDVTList();
Chris Lattner88fa11c2005-11-08 23:30:28 +00005013}
Dan Gohman17059682008-07-17 19:10:17 +00005014
Owen Anderson53aa7a92009-08-10 22:56:29 +00005015SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005016 FoldingSetNodeID ID;
5017 ID.AddInteger(3U);
5018 ID.AddInteger(VT1.getRawBits());
5019 ID.AddInteger(VT2.getRawBits());
5020 ID.AddInteger(VT3.getRawBits());
Dan Gohman17059682008-07-17 19:10:17 +00005021
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005022 void *IP = 0;
5023 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5024 if (Result == NULL) {
5025 EVT *Array = Allocator.Allocate<EVT>(3);
5026 Array[0] = VT1;
5027 Array[1] = VT2;
5028 Array[2] = VT3;
5029 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5030 VTListMap.InsertNode(Result, IP);
5031 }
5032 return Result->getSDVTList();
Chris Lattnerf98411a2006-08-15 17:46:01 +00005033}
5034
Owen Anderson53aa7a92009-08-10 22:56:29 +00005035SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005036 FoldingSetNodeID ID;
5037 ID.AddInteger(4U);
5038 ID.AddInteger(VT1.getRawBits());
5039 ID.AddInteger(VT2.getRawBits());
5040 ID.AddInteger(VT3.getRawBits());
5041 ID.AddInteger(VT4.getRawBits());
Bill Wendling2d598632008-12-01 23:28:22 +00005042
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005043 void *IP = 0;
5044 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5045 if (Result == NULL) {
5046 EVT *Array = Allocator.Allocate<EVT>(4);
5047 Array[0] = VT1;
5048 Array[1] = VT2;
5049 Array[2] = VT3;
5050 Array[3] = VT4;
5051 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5052 VTListMap.InsertNode(Result, IP);
5053 }
5054 return Result->getSDVTList();
Bill Wendling2d598632008-12-01 23:28:22 +00005055}
5056
Owen Anderson53aa7a92009-08-10 22:56:29 +00005057SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005058 FoldingSetNodeID ID;
5059 ID.AddInteger(NumVTs);
5060 for (unsigned index = 0; index < NumVTs; index++) {
5061 ID.AddInteger(VTs[index].getRawBits());
Chris Lattnerf98411a2006-08-15 17:46:01 +00005062 }
5063
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005064 void *IP = 0;
5065 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5066 if (Result == NULL) {
5067 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5068 std::copy(VTs, VTs + NumVTs, Array);
5069 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5070 VTListMap.InsertNode(Result, IP);
Chris Lattnerf98411a2006-08-15 17:46:01 +00005071 }
Wan Xiaofei2f8dc082013-10-22 08:02:02 +00005072 return Result->getSDVTList();
Chris Lattner3bf4be42006-08-14 23:31:51 +00005073}
5074
5075
Chris Lattnerf34156e2006-01-28 09:32:45 +00005076/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5077/// specified operands. If the resultant node already exists in the DAG,
5078/// this does not modify the specified node, instead it returns the node that
5079/// already exists. If the resultant node does not exist in the DAG, the
5080/// input node is returned. As a degenerate case, if you specify the same
5081/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005082SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005083 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005084
Chris Lattnerf34156e2006-01-28 09:32:45 +00005085 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005086 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005087
Chris Lattnerf34156e2006-01-28 09:32:45 +00005088 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005089 void *InsertPos = 0;
5090 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005091 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005092
Dan Gohmanebeccb42008-07-21 22:38:59 +00005093 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005094 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005095 if (!RemoveNodeFromCSEMaps(N))
5096 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005097
Chris Lattnerf34156e2006-01-28 09:32:45 +00005098 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005099 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005100
Chris Lattnerf34156e2006-01-28 09:32:45 +00005101 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005102 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005103 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005104}
5105
Dan Gohman92c11ac2010-06-18 15:30:29 +00005106SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00005107 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005108
Chris Lattnerf34156e2006-01-28 09:32:45 +00005109 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005110 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005111 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005112
Chris Lattnerf34156e2006-01-28 09:32:45 +00005113 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005114 void *InsertPos = 0;
5115 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005116 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005117
Dan Gohmanebeccb42008-07-21 22:38:59 +00005118 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005119 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005120 if (!RemoveNodeFromCSEMaps(N))
5121 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005122
Chris Lattnerf34156e2006-01-28 09:32:45 +00005123 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005124 if (N->OperandList[0] != Op1)
5125 N->OperandList[0].set(Op1);
5126 if (N->OperandList[1] != Op2)
5127 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005128
Chris Lattnerf34156e2006-01-28 09:32:45 +00005129 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005130 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005131 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005132}
5133
Dan Gohman92c11ac2010-06-18 15:30:29 +00005134SDNode *SelectionDAG::
5135UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005136 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005137 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005138}
5139
Dan Gohman92c11ac2010-06-18 15:30:29 +00005140SDNode *SelectionDAG::
5141UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005142 SDValue Op3, SDValue Op4) {
5143 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005144 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005145}
5146
Dan Gohman92c11ac2010-06-18 15:30:29 +00005147SDNode *SelectionDAG::
5148UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005149 SDValue Op3, SDValue Op4, SDValue Op5) {
5150 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005151 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner580b12a2006-01-28 10:09:25 +00005152}
5153
Dan Gohman92c11ac2010-06-18 15:30:29 +00005154SDNode *SelectionDAG::
5155UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00005156 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005157 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005158
Chris Lattnerf34156e2006-01-28 09:32:45 +00005159 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005160 bool AnyChange = false;
5161 for (unsigned i = 0; i != NumOps; ++i) {
5162 if (Ops[i] != N->getOperand(i)) {
5163 AnyChange = true;
5164 break;
5165 }
5166 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005167
Chris Lattnerf34156e2006-01-28 09:32:45 +00005168 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005169 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005170
Chris Lattnerf34156e2006-01-28 09:32:45 +00005171 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005172 void *InsertPos = 0;
Chris Lattner97af9d52006-08-08 01:09:31 +00005173 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005174 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005175
Dan Gohman2f83b472008-05-02 00:05:03 +00005176 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005177 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005178 if (!RemoveNodeFromCSEMaps(N))
5179 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005180
Chris Lattnerf34156e2006-01-28 09:32:45 +00005181 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005182 for (unsigned i = 0; i != NumOps; ++i)
5183 if (N->OperandList[i] != Ops[i])
5184 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005185
5186 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005187 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005188 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005189}
5190
Dan Gohman91697632008-07-07 20:57:48 +00005191/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005192/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005193void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005194 // Unlike the code in MorphNodeTo that does this, we don't need to
5195 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005196 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5197 SDUse &Use = *I++;
5198 Use.set(SDValue());
5199 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005200}
Chris Lattner19732782005-08-16 18:17:10 +00005201
Dan Gohman17059682008-07-17 19:10:17 +00005202/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5203/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005204///
Dan Gohman17059682008-07-17 19:10:17 +00005205SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005206 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005207 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005208 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005209}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005210
Dan Gohman17059682008-07-17 19:10:17 +00005211SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005212 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005213 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005214 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005215 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner19732782005-08-16 18:17:10 +00005216}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005217
Dan Gohman17059682008-07-17 19:10:17 +00005218SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005219 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005220 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005221 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005222 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005223 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner19732782005-08-16 18:17:10 +00005224}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005225
Dan Gohman17059682008-07-17 19:10:17 +00005226SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005227 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005228 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005229 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005230 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005231 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner19732782005-08-16 18:17:10 +00005232}
Chris Lattner466fece2005-08-21 22:30:30 +00005233
Dan Gohman17059682008-07-17 19:10:17 +00005234SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005235 EVT VT, const SDValue *Ops,
Evan Cheng849f4bf2006-08-27 08:08:54 +00005236 unsigned NumOps) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005237 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005238 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005239}
5240
Dan Gohman17059682008-07-17 19:10:17 +00005241SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005242 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005243 unsigned NumOps) {
5244 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman17059682008-07-17 19:10:17 +00005245 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005246}
5247
Dan Gohman17059682008-07-17 19:10:17 +00005248SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005249 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005250 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005251 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman22e97072008-07-02 23:23:19 +00005252}
5253
Dan Gohman17059682008-07-17 19:10:17 +00005254SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005255 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005256 const SDValue *Ops, unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005257 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohman17059682008-07-17 19:10:17 +00005258 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005259}
5260
Bill Wendling2d598632008-12-01 23:28:22 +00005261SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005262 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling2d598632008-12-01 23:28:22 +00005263 const SDValue *Ops, unsigned NumOps) {
5264 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5265 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5266}
5267
Scott Michelcf0da6c2009-02-17 22:15:04 +00005268SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005269 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005270 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005271 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005272 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005273 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth683352382006-01-23 21:51:14 +00005274}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005275
Scott Michelcf0da6c2009-02-17 22:15:04 +00005276SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005277 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005278 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005279 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005280 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005281 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005282}
5283
Dan Gohman17059682008-07-17 19:10:17 +00005284SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005285 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005286 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005287 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005288 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005289 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005290 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman22e97072008-07-02 23:23:19 +00005291}
5292
Dan Gohman17059682008-07-17 19:10:17 +00005293SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005294 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005295 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005296 SDValue Op3) {
5297 SDVTList VTs = getVTList(VT1, VT2, VT3);
5298 SDValue Ops[] = { Op1, Op2, Op3 };
5299 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5300}
5301
5302SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005303 SDVTList VTs, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005304 unsigned NumOps) {
Chris Lattner625916d2010-02-23 23:01:35 +00005305 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5306 // Reset the NodeID to -1.
5307 N->setNodeId(-1);
5308 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005309}
5310
Andrew Trickef9de2a2013-05-25 02:42:55 +00005311/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005312/// the line number information on the merged node since it is not possible to
5313/// preserve the information that operation is associated with multiple lines.
5314/// This will make the debugger working better at -O0, were there is a higher
5315/// probability having other instructions associated with that line.
5316///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005317/// For IROrder, we keep the smaller of the two
5318SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005319 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005320 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5321 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005322 N->setDebugLoc(DebugLoc());
5323 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005324 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5325 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005326 return N;
5327}
5328
Chris Lattneraf197502010-02-28 21:36:14 +00005329/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005330/// return type, opcode, and operands.
5331///
5332/// Note that MorphNodeTo returns the resultant node. If there is already a
5333/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005334/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005335///
5336/// Using MorphNodeTo is faster than creating a new node and swapping it in
5337/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005338/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005339/// the node's users.
5340///
5341SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005342 SDVTList VTs, const SDValue *Ops,
Dan Gohman17059682008-07-17 19:10:17 +00005343 unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005344 // If an identical node already exists, use it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005345 void *IP = 0;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005346 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005347 FoldingSetNodeID ID;
5348 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendling022d18f2009-12-18 23:32:53 +00005349 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005350 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005351 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005352
Dan Gohmand3fe1742008-09-13 01:54:27 +00005353 if (!RemoveNodeFromCSEMaps(N))
5354 IP = 0;
Chris Lattner486edfb2007-02-04 02:32:44 +00005355
Dan Gohman17059682008-07-17 19:10:17 +00005356 // Start the morphing.
5357 N->NodeType = Opc;
5358 N->ValueList = VTs.VTs;
5359 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005360
Dan Gohman17059682008-07-17 19:10:17 +00005361 // Clear the operands list, updating used nodes to remove this from their
5362 // use list. Keep track of any operands that become dead as a result.
5363 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005364 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5365 SDUse &Use = *I++;
5366 SDNode *Used = Use.getNode();
5367 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005368 if (Used->use_empty())
5369 DeadNodeSet.insert(Used);
5370 }
5371
Dan Gohman48b185d2009-09-25 20:36:54 +00005372 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5373 // Initialize the memory references information.
5374 MN->setMemRefs(0, 0);
5375 // If NumOps is larger than the # of operands we can have in a
5376 // MachineSDNode, reallocate the operand list.
5377 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5378 if (MN->OperandsNeedDelete)
5379 delete[] MN->OperandList;
5380 if (NumOps > array_lengthof(MN->LocalOperands))
5381 // We're creating a final node that will live unmorphed for the
5382 // remainder of the current SelectionDAG iteration, so we can allocate
5383 // the operands directly out of a pool with no recycling metadata.
5384 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman01c65a22010-03-18 18:49:47 +00005385 Ops, NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005386 else
5387 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5388 MN->OperandsNeedDelete = false;
5389 } else
5390 MN->InitOperands(MN->OperandList, Ops, NumOps);
5391 } else {
5392 // If NumOps is larger than the # of operands we currently have, reallocate
5393 // the operand list.
5394 if (NumOps > N->NumOperands) {
5395 if (N->OperandsNeedDelete)
5396 delete[] N->OperandList;
5397 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005398 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005399 } else
Anton Korobeynikov86263672009-10-22 00:15:17 +00005400 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005401 }
5402
5403 // Delete any nodes that are still dead after adding the uses for the
5404 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005405 if (!DeadNodeSet.empty()) {
5406 SmallVector<SDNode *, 16> DeadNodes;
5407 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5408 E = DeadNodeSet.end(); I != E; ++I)
5409 if ((*I)->use_empty())
5410 DeadNodes.push_back(*I);
5411 RemoveDeadNodes(DeadNodes);
5412 }
Dan Gohman91697632008-07-07 20:57:48 +00005413
Dan Gohman17059682008-07-17 19:10:17 +00005414 if (IP)
5415 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005416 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005417}
5418
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005419
Dan Gohman32f71d72009-09-25 18:54:59 +00005420/// getMachineNode - These are used for target selectors to create a new node
5421/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005422///
Dan Gohman32f71d72009-09-25 18:54:59 +00005423/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005424/// node of the specified opcode and operands, it returns that node instead of
5425/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005426MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005427SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005428 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005429 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005430}
Bill Wendlinga434d932009-01-29 09:01:55 +00005431
Dan Gohmana22f2d82009-10-10 01:29:16 +00005432MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005433SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005434 SDVTList VTs = getVTList(VT);
5435 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005436 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005437}
Bill Wendlinga434d932009-01-29 09:01:55 +00005438
Dan Gohmana22f2d82009-10-10 01:29:16 +00005439MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005440SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005441 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005442 SDVTList VTs = getVTList(VT);
5443 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005444 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005445}
Bill Wendlinga434d932009-01-29 09:01:55 +00005446
Dan Gohmana22f2d82009-10-10 01:29:16 +00005447MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005448SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005449 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005450 SDVTList VTs = getVTList(VT);
5451 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005452 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005453}
Bill Wendlinga434d932009-01-29 09:01:55 +00005454
Dan Gohmana22f2d82009-10-10 01:29:16 +00005455MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005456SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005457 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005458 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005459 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005460}
Bill Wendlinga434d932009-01-29 09:01:55 +00005461
Dan Gohmana22f2d82009-10-10 01:29:16 +00005462MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005463SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005464 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005465 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005466}
Bill Wendlinga434d932009-01-29 09:01:55 +00005467
Dan Gohmana22f2d82009-10-10 01:29:16 +00005468MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005469SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005470 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005471 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005472 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005473 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005474}
Bill Wendlinga434d932009-01-29 09:01:55 +00005475
Dan Gohmana22f2d82009-10-10 01:29:16 +00005476MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005477SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005478 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005479 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005480 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005481 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005482}
5483
Dan Gohmana22f2d82009-10-10 01:29:16 +00005484MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005485SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005486 EVT VT1, EVT VT2, SDValue Op1,
5487 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005488 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005489 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005490 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005491}
5492
Dan Gohmana22f2d82009-10-10 01:29:16 +00005493MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005494SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005495 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005496 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005497 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005498 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005499}
Bill Wendlinga434d932009-01-29 09:01:55 +00005500
Dan Gohmana22f2d82009-10-10 01:29:16 +00005501MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005502SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005503 EVT VT1, EVT VT2, EVT VT3,
5504 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005505 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005506 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005507 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005508}
Bill Wendlinga434d932009-01-29 09:01:55 +00005509
Dan Gohmana22f2d82009-10-10 01:29:16 +00005510MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005511SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005512 EVT VT1, EVT VT2, EVT VT3,
5513 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005514 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005515 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005516 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005517}
Bill Wendlinga434d932009-01-29 09:01:55 +00005518
Dan Gohmana22f2d82009-10-10 01:29:16 +00005519MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005520SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005521 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005522 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005523 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005524 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005525}
Bill Wendlinga434d932009-01-29 09:01:55 +00005526
Dan Gohmana22f2d82009-10-10 01:29:16 +00005527MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005528SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005529 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005530 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005531 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005532 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005533}
5534
Dan Gohmana22f2d82009-10-10 01:29:16 +00005535MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005536SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005537 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005538 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005539 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liaob53d8962013-04-19 22:22:57 +00005540 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005541}
5542
Dan Gohmana22f2d82009-10-10 01:29:16 +00005543MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005544SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005545 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005546 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005547 MachineSDNode *N;
Ted Kremenek3c4408c2011-01-23 17:05:06 +00005548 void *IP = 0;
Michael Liaob53d8962013-04-19 22:22:57 +00005549 const SDValue *Ops = OpsArray.data();
5550 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005551
5552 if (DoCSE) {
5553 FoldingSetNodeID ID;
5554 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5555 IP = 0;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005556 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005557 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005558 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005559 }
5560
5561 // Allocate a new MachineSDNode.
Jack Carter170a5f22013-09-09 22:02:08 +00005562 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5563 DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005564
5565 // Initialize the operands list.
5566 if (NumOps > array_lengthof(N->LocalOperands))
5567 // We're creating a final node that will live unmorphed for the
5568 // remainder of the current SelectionDAG iteration, so we can allocate
5569 // the operands directly out of a pool with no recycling metadata.
5570 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5571 Ops, NumOps);
5572 else
5573 N->InitOperands(N->LocalOperands, Ops, NumOps);
5574 N->OperandsNeedDelete = false;
5575
5576 if (DoCSE)
5577 CSEMap.InsertNode(N, IP);
5578
5579 AllNodes.push_back(N);
5580#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005581 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005582#endif
5583 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005584}
Evan Chengd3f1db92006-02-09 07:15:23 +00005585
Dan Gohmanac33a902009-08-19 18:16:17 +00005586/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005587/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005588SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005589SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005590 SDValue Operand) {
5591 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005592 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005593 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005594 return SDValue(Subreg, 0);
5595}
5596
Bob Wilson2a45a652009-10-08 18:49:46 +00005597/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005598/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005599SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005600SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005601 SDValue Operand, SDValue Subreg) {
5602 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005603 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005604 VT, Operand, Subreg, SRIdxVal);
5605 return SDValue(Result, 0);
5606}
5607
Evan Cheng31604a62008-03-22 01:55:50 +00005608/// getNodeIfExists - Get the specified node if it's already available, or
5609/// else return NULL.
5610SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005611 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005612 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005613 FoldingSetNodeID ID;
5614 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5615 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00005616 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005617 return E;
5618 }
5619 return NULL;
5620}
5621
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005622/// getDbgValue - Creates a SDDbgValue node.
5623///
5624SDDbgValue *
5625SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5626 DebugLoc DL, unsigned O) {
5627 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5628}
5629
5630SDDbgValue *
Dan Gohmanbcaf6812010-04-15 01:51:59 +00005631SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005632 DebugLoc DL, unsigned O) {
5633 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5634}
5635
5636SDDbgValue *
5637SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5638 DebugLoc DL, unsigned O) {
5639 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5640}
5641
Dan Gohman7d099f72010-03-03 21:33:37 +00005642namespace {
5643
Dan Gohman9cc886b2010-03-04 19:11:28 +00005644/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005645/// pointed to by a use iterator is deleted, increment the use iterator
5646/// so that it doesn't dangle.
5647///
Dan Gohman7d099f72010-03-03 21:33:37 +00005648class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005649 SDNode::use_iterator &UI;
5650 SDNode::use_iterator &UE;
5651
5652 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5653 // Increment the iterator as needed.
5654 while (UI != UE && N == *UI)
5655 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005656 }
5657
5658public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005659 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005660 SDNode::use_iterator &ui,
5661 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005662 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005663};
5664
5665}
5666
Evan Cheng445b91a2006-08-07 22:13:29 +00005667/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005668/// This can cause recursive merging of nodes in the DAG.
5669///
Chris Lattner76858912008-02-03 03:35:22 +00005670/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005671///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005672void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005673 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005674 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005675 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005676 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005677
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005678 // Iterate over all the existing uses of From. New uses will be added
5679 // to the beginning of the use list, which we avoid visiting.
5680 // This specifically avoids visiting uses of From that arise while the
5681 // replacement is happening, because any such uses would be the result
5682 // of CSE: If an existing node looks like From after one of its operands
5683 // is replaced by To, we don't want to replace of all its users with To
5684 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005685 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005686 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005687 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005688 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005689
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005690 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005691 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005692
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005693 // A user can appear in a use list multiple times, and when this
5694 // happens the uses are usually next to each other in the list.
5695 // To help reduce the number of CSE recomputations, process all
5696 // the uses of this user that we can find this way.
5697 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005698 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005699 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005700 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005701 } while (UI != UE && *UI == User);
5702
5703 // Now that we have modified User, add it back to the CSE maps. If it
5704 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005705 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005706 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005707
5708 // If we just RAUW'd the root, take note.
5709 if (FromN == getRoot())
5710 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005711}
5712
5713/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5714/// This can cause recursive merging of nodes in the DAG.
5715///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005716/// This version assumes that for each value of From, there is a
5717/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005718///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005719void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005720#ifndef NDEBUG
5721 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5722 assert((!From->hasAnyUseOfValue(i) ||
5723 From->getValueType(i) == To->getValueType(i)) &&
5724 "Cannot use this version of ReplaceAllUsesWith!");
5725#endif
Dan Gohman17059682008-07-17 19:10:17 +00005726
5727 // Handle the trivial case.
5728 if (From == To)
5729 return;
5730
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005731 // Iterate over just the existing users of From. See the comments in
5732 // the ReplaceAllUsesWith above.
5733 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005734 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005735 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005736 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005737
Chris Lattner373f0482005-08-26 18:36:28 +00005738 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005739 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005740
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005741 // A user can appear in a use list multiple times, and when this
5742 // happens the uses are usually next to each other in the list.
5743 // To help reduce the number of CSE recomputations, process all
5744 // the uses of this user that we can find this way.
5745 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005746 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005747 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005748 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005749 } while (UI != UE && *UI == User);
5750
5751 // Now that we have modified User, add it back to the CSE maps. If it
5752 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005753 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005754 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005755
5756 // If we just RAUW'd the root, take note.
5757 if (From == getRoot().getNode())
5758 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005759}
5760
Chris Lattner373f0482005-08-26 18:36:28 +00005761/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5762/// This can cause recursive merging of nodes in the DAG.
5763///
5764/// This version can replace From with any result values. To must match the
5765/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005766void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005767 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005768 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005769
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005770 // Iterate over just the existing users of From. See the comments in
5771 // the ReplaceAllUsesWith above.
5772 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005773 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005774 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005775 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005776
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005777 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005778 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005779
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005780 // A user can appear in a use list multiple times, and when this
5781 // happens the uses are usually next to each other in the list.
5782 // To help reduce the number of CSE recomputations, process all
5783 // the uses of this user that we can find this way.
5784 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005785 SDUse &Use = UI.getUse();
5786 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005787 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005788 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005789 } while (UI != UE && *UI == User);
5790
5791 // Now that we have modified User, add it back to the CSE maps. If it
5792 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005793 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005794 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005795
5796 // If we just RAUW'd the root, take note.
5797 if (From == getRoot().getNode())
5798 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005799}
5800
Chris Lattner375e1a72006-02-17 21:58:01 +00005801/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005802/// uses of other values produced by From.getNode() alone. The Deleted
5803/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005804void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005805 // Handle the really simple, really trivial case efficiently.
5806 if (From == To) return;
5807
Chris Lattner375e1a72006-02-17 21:58:01 +00005808 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005809 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005810 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005811 return;
5812 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005813
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005814 // Iterate over just the existing users of From. See the comments in
5815 // the ReplaceAllUsesWith above.
5816 SDNode::use_iterator UI = From.getNode()->use_begin(),
5817 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005818 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005819 while (UI != UE) {
5820 SDNode *User = *UI;
5821 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005822
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005823 // A user can appear in a use list multiple times, and when this
5824 // happens the uses are usually next to each other in the list.
5825 // To help reduce the number of CSE recomputations, process all
5826 // the uses of this user that we can find this way.
5827 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005828 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005829
5830 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005831 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005832 ++UI;
5833 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005834 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005835
5836 // If this node hasn't been modified yet, it's still in the CSE maps,
5837 // so remove its old self from the CSE maps.
5838 if (!UserRemovedFromCSEMaps) {
5839 RemoveNodeFromCSEMaps(User);
5840 UserRemovedFromCSEMaps = true;
5841 }
5842
5843 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005844 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005845 } while (UI != UE && *UI == User);
5846
5847 // We are iterating over all uses of the From node, so if a use
5848 // doesn't use the specific value, no changes are made.
5849 if (!UserRemovedFromCSEMaps)
5850 continue;
5851
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005852 // Now that we have modified User, add it back to the CSE maps. If it
5853 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005854 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005855 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005856
5857 // If we just RAUW'd the root, take note.
5858 if (From == getRoot())
5859 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005860}
5861
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005862namespace {
5863 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5864 /// to record information about a use.
5865 struct UseMemo {
5866 SDNode *User;
5867 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005868 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005869 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005870
5871 /// operator< - Sort Memos by User.
5872 bool operator<(const UseMemo &L, const UseMemo &R) {
5873 return (intptr_t)L.User < (intptr_t)R.User;
5874 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005875}
5876
Dan Gohman17059682008-07-17 19:10:17 +00005877/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005878/// uses of other values produced by From.getNode() alone. The same value
5879/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005880/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005881void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5882 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005883 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005884 // Handle the simple, trivial case efficiently.
5885 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005886 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005887
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005888 // Read up all the uses and make records of them. This helps
5889 // processing new uses that are introduced during the
5890 // replacement process.
5891 SmallVector<UseMemo, 4> Uses;
5892 for (unsigned i = 0; i != Num; ++i) {
5893 unsigned FromResNo = From[i].getResNo();
5894 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005895 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005896 E = FromNode->use_end(); UI != E; ++UI) {
5897 SDUse &Use = UI.getUse();
5898 if (Use.getResNo() == FromResNo) {
5899 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005900 Uses.push_back(Memo);
5901 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005902 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005903 }
Dan Gohman17059682008-07-17 19:10:17 +00005904
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005905 // Sort the uses, so that all the uses from a given User are together.
5906 std::sort(Uses.begin(), Uses.end());
5907
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005908 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5909 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005910 // We know that this user uses some value of From. If it is the right
5911 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005912 SDNode *User = Uses[UseIndex].User;
5913
5914 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005915 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005916
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005917 // The Uses array is sorted, so all the uses for a given User
5918 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005919 // To help reduce the number of CSE recomputations, process all
5920 // the uses of this user that we can find this way.
5921 do {
5922 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005923 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005924 ++UseIndex;
5925
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005926 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005927 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5928
Dan Gohman17059682008-07-17 19:10:17 +00005929 // Now that we have modified User, add it back to the CSE maps. If it
5930 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005931 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005932 }
5933}
5934
Evan Cheng9631a602006-08-01 08:20:41 +00005935/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005936/// based on their topological order. It returns the maximum id and a vector
5937/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005938unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005939
Dan Gohman86aa16a2008-09-30 18:30:35 +00005940 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005941
Dan Gohman86aa16a2008-09-30 18:30:35 +00005942 // SortedPos tracks the progress of the algorithm. Nodes before it are
5943 // sorted, nodes after it are unsorted. When the algorithm completes
5944 // it is at the end of the list.
5945 allnodes_iterator SortedPos = allnodes_begin();
5946
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005947 // Visit all the nodes. Move nodes with no operands to the front of
5948 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005949 // operand count. Before we do this, the Node Id fields of the nodes
5950 // may contain arbitrary values. After, the Node Id fields for nodes
5951 // before SortedPos will contain the topological sort index, and the
5952 // Node Id fields for nodes At SortedPos and after will contain the
5953 // count of outstanding operands.
5954 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5955 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005956 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005957 unsigned Degree = N->getNumOperands();
5958 if (Degree == 0) {
5959 // A node with no uses, add it to the result array immediately.
5960 N->setNodeId(DAGSize++);
5961 allnodes_iterator Q = N;
5962 if (Q != SortedPos)
5963 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005964 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005965 ++SortedPos;
5966 } else {
5967 // Temporarily use the Node Id as scratch space for the degree count.
5968 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005969 }
5970 }
5971
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005972 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005973 // such that by the time the end is reached all nodes will be sorted.
5974 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5975 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00005976 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00005977 // N is in sorted position, so all its uses have one less operand
5978 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005979 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5980 UI != UE; ++UI) {
5981 SDNode *P = *UI;
5982 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00005983 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005984 --Degree;
5985 if (Degree == 0) {
5986 // All of P's operands are sorted, so P may sorted now.
5987 P->setNodeId(DAGSize++);
5988 if (P != SortedPos)
5989 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00005990 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005991 ++SortedPos;
5992 } else {
5993 // Update P's outstanding operand count.
5994 P->setNodeId(Degree);
5995 }
5996 }
David Greene3b2a68c2010-01-20 00:59:23 +00005997 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00005998#ifndef NDEBUG
5999 SDNode *S = ++I;
6000 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00006001 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00006002#endif
6003 llvm_unreachable(0);
David Greene3b2a68c2010-01-20 00:59:23 +00006004 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00006005 }
6006
6007 assert(SortedPos == AllNodes.end() &&
6008 "Topological sort incomplete!");
6009 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6010 "First node in topological sort is not the entry token!");
6011 assert(AllNodes.front().getNodeId() == 0 &&
6012 "First node in topological sort has non-zero id!");
6013 assert(AllNodes.front().getNumOperands() == 0 &&
6014 "First node in topological sort has operands!");
6015 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6016 "Last node in topologic sort has unexpected id!");
6017 assert(AllNodes.back().use_empty() &&
6018 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00006019 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00006020 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00006021}
6022
Evan Cheng563fe3c2010-03-25 01:38:16 +00006023/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6024/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00006025void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6026 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00006027 if (SD)
6028 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00006029}
Evan Cheng29eefc12006-07-27 06:39:06 +00006030
Devang Patelefc6b162011-01-25 23:27:42 +00006031/// TransferDbgValues - Transfer SDDbgValues.
6032void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6033 if (From == To || !From.getNode()->getHasDebugValue())
6034 return;
6035 SDNode *FromNode = From.getNode();
6036 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006037 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006038 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00006039 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00006040 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006041 SDDbgValue *Dbg = *I;
6042 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6043 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
6044 Dbg->getOffset(), Dbg->getDebugLoc(),
6045 Dbg->getOrder());
6046 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00006047 }
6048 }
Craig Toppere1c1d362013-07-03 05:11:49 +00006049 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00006050 E = ClonedDVs.end(); I != E; ++I)
6051 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00006052}
6053
Jim Laskeyd66e6162005-08-17 20:08:02 +00006054//===----------------------------------------------------------------------===//
6055// SDNode Class
6056//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00006057
Chris Lattner3bf17b62007-02-04 02:41:42 +00006058HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00006059 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00006060}
6061
Andrew Trickef9de2a2013-05-25 02:42:55 +00006062GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6063 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00006064 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006065 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00006066 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00006067}
Chris Lattner3bf17b62007-02-04 02:41:42 +00006068
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00006069AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6070 SDValue X, unsigned SrcAS,
6071 unsigned DestAS)
6072 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6073 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6074
Andrew Trickef9de2a2013-05-25 02:42:55 +00006075MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6076 EVT memvt, MachineMemOperand *mmo)
6077 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006078 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006079 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006080 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00006081 assert(isNonTemporal() == MMO->isNonTemporal() &&
6082 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00006083 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00006084}
6085
Andrew Trickef9de2a2013-05-25 02:42:55 +00006086MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peck527da1b2010-11-23 03:31:01 +00006087 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohman48b185d2009-09-25 20:36:54 +00006088 MachineMemOperand *mmo)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006089 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohman48b185d2009-09-25 20:36:54 +00006090 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00006091 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006092 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00006093 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6094 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00006095}
6096
Jim Laskeyf576b422006-10-27 23:46:08 +00006097/// Profile - Gather unique data for the node.
6098///
Dan Gohman2da2bed2008-08-20 15:58:01 +00006099void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00006100 AddNodeIDNode(ID, this);
6101}
6102
Owen Anderson3b1665e2009-08-25 22:27:22 +00006103namespace {
6104 struct EVTArray {
6105 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00006106
Owen Anderson3b1665e2009-08-25 22:27:22 +00006107 EVTArray() {
6108 VTs.reserve(MVT::LAST_VALUETYPE);
6109 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6110 VTs.push_back(MVT((MVT::SimpleValueType)i));
6111 }
6112 };
6113}
6114
Owen Anderson53aa7a92009-08-10 22:56:29 +00006115static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00006116static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00006117static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00006118
Chris Lattner88fa11c2005-11-08 23:30:28 +00006119/// getValueTypeList - Return a pointer to the specified value type.
6120///
Owen Anderson53aa7a92009-08-10 22:56:29 +00006121const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006122 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00006123 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00006124 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006125 } else {
Duncan Sands14627772010-11-03 12:17:33 +00006126 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00006127 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00006128 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006129 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00006130}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00006131
Chris Lattner40e79822005-01-12 18:37:47 +00006132/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6133/// indicated value. This method ignores uses of other values defined by this
6134/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00006135bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00006136 assert(Value < getNumValues() && "Bad value!");
6137
Roman Levenstein51f532f2008-04-07 10:06:32 +00006138 // TODO: Only iterate over uses of a given value of the node
6139 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006140 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00006141 if (NUses == 0)
6142 return false;
6143 --NUses;
6144 }
Chris Lattner40e79822005-01-12 18:37:47 +00006145 }
6146
6147 // Found exactly the right number of uses?
6148 return NUses == 0;
6149}
6150
6151
Evan Cheng358c3d12007-08-02 05:29:38 +00006152/// hasAnyUseOfValue - Return true if there are any use of the indicated
6153/// value. This method ignores uses of other values defined by this operation.
6154bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6155 assert(Value < getNumValues() && "Bad value!");
6156
Dan Gohman7a510c22008-07-09 22:39:01 +00006157 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006158 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006159 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006160
6161 return false;
6162}
6163
6164
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006165/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006166///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006167bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006168 bool Seen = false;
6169 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006170 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006171 if (User == this)
6172 Seen = true;
6173 else
6174 return false;
6175 }
6176
6177 return Seen;
6178}
6179
Evan Cheng9456dd82006-11-03 07:31:32 +00006180/// isOperand - Return true if this node is an operand of N.
6181///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006182bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006183 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6184 if (*this == N->getOperand(i))
6185 return true;
6186 return false;
6187}
6188
Evan Cheng567d2e52008-03-04 00:41:45 +00006189bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006190 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006191 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006192 return true;
6193 return false;
6194}
Evan Chengd37645c2006-02-05 06:29:23 +00006195
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006196/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006197/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006198/// side-effecting instructions on any chain path. In practice, this looks
6199/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006200/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006201bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006202 unsigned Depth) const {
6203 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006204
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006205 // Don't search too deeply, we just want to be able to see through
6206 // TokenFactor's etc.
6207 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006208
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006209 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006210 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006211 if (getOpcode() == ISD::TokenFactor) {
6212 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006213 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6214 return false;
6215 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006216 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006217
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006218 // Loads don't have side effects, look through them.
6219 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6220 if (!Ld->isVolatile())
6221 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6222 }
6223 return false;
6224}
6225
Lang Hames5a004992011-07-07 04:31:51 +00006226/// hasPredecessor - Return true if N is a predecessor of this node.
6227/// N is either an operand of this node, or can be reached by recursively
6228/// traversing up the operands.
6229/// NOTE: This is an expensive method. Use it carefully.
6230bool SDNode::hasPredecessor(const SDNode *N) const {
6231 SmallPtrSet<const SDNode *, 32> Visited;
6232 SmallVector<const SDNode *, 16> Worklist;
6233 return hasPredecessorHelper(N, Visited, Worklist);
6234}
Dan Gohmancd139c02009-10-28 03:44:30 +00006235
Craig Topperb94011f2013-07-14 04:42:23 +00006236bool
6237SDNode::hasPredecessorHelper(const SDNode *N,
6238 SmallPtrSet<const SDNode *, 32> &Visited,
6239 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006240 if (Visited.empty()) {
6241 Worklist.push_back(this);
6242 } else {
6243 // Take a look in the visited set. If we've already encountered this node
6244 // we needn't search further.
6245 if (Visited.count(N))
6246 return true;
6247 }
6248
6249 // Haven't visited N yet. Continue the search.
6250 while (!Worklist.empty()) {
6251 const SDNode *M = Worklist.pop_back_val();
6252 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6253 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006254 if (Visited.insert(Op))
6255 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006256 if (Op == N)
6257 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006258 }
Lang Hames5a004992011-07-07 04:31:51 +00006259 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006260
6261 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006262}
6263
Evan Cheng5d9fd972006-10-04 00:56:09 +00006264uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6265 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006266 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006267}
6268
Mon P Wang32f8bb92009-11-30 02:42:02 +00006269SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6270 assert(N->getNumValues() == 1 &&
6271 "Can't unroll a vector with multiple results!");
6272
6273 EVT VT = N->getValueType(0);
6274 unsigned NE = VT.getVectorNumElements();
6275 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006276 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006277
6278 SmallVector<SDValue, 8> Scalars;
6279 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6280
6281 // If ResNE is 0, fully unroll the vector op.
6282 if (ResNE == 0)
6283 ResNE = NE;
6284 else if (NE > ResNE)
6285 NE = ResNE;
6286
6287 unsigned i;
6288 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006289 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006290 SDValue Operand = N->getOperand(j);
6291 EVT OperandVT = Operand.getValueType();
6292 if (OperandVT.isVector()) {
6293 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006294 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006295 EVT OperandEltVT = OperandVT.getVectorElementType();
6296 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6297 OperandEltVT,
6298 Operand,
Tom Stellardd42c5942013-08-05 22:22:01 +00006299 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006300 } else {
6301 // A scalar operand; just use it as is.
6302 Operands[j] = Operand;
6303 }
6304 }
6305
6306 switch (N->getOpcode()) {
6307 default:
6308 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6309 &Operands[0], Operands.size()));
6310 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006311 case ISD::VSELECT:
6312 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6313 &Operands[0], Operands.size()));
6314 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006315 case ISD::SHL:
6316 case ISD::SRA:
6317 case ISD::SRL:
6318 case ISD::ROTL:
6319 case ISD::ROTR:
6320 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter170a5f22013-09-09 22:02:08 +00006321 getShiftAmountOperand(Operands[0].getValueType(),
6322 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006323 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006324 case ISD::SIGN_EXTEND_INREG:
6325 case ISD::FP_ROUND_INREG: {
6326 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6327 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6328 Operands[0],
6329 getValueType(ExtVT)));
6330 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006331 }
6332 }
6333
6334 for (; i < ResNE; ++i)
6335 Scalars.push_back(getUNDEF(EltVT));
6336
6337 return getNode(ISD::BUILD_VECTOR, dl,
6338 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6339 &Scalars[0], Scalars.size());
6340}
6341
Evan Chengf5938d52009-12-09 01:36:00 +00006342
Wesley Peck527da1b2010-11-23 03:31:01 +00006343/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6344/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006345/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006346bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006347 unsigned Bytes, int Dist) const {
6348 if (LD->getChain() != Base->getChain())
6349 return false;
6350 EVT VT = LD->getValueType(0);
6351 if (VT.getSizeInBits() / 8 != Bytes)
6352 return false;
6353
6354 SDValue Loc = LD->getOperand(1);
6355 SDValue BaseLoc = Base->getOperand(1);
6356 if (Loc.getOpcode() == ISD::FrameIndex) {
6357 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6358 return false;
6359 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6360 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6361 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6362 int FS = MFI->getObjectSize(FI);
6363 int BFS = MFI->getObjectSize(BFI);
6364 if (FS != BFS || FS != (int)Bytes) return false;
6365 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6366 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006367
6368 // Handle X+C
6369 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6370 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6371 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006372
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006373 const GlobalValue *GV1 = NULL;
6374 const GlobalValue *GV2 = NULL;
Evan Chengf5938d52009-12-09 01:36:00 +00006375 int64_t Offset1 = 0;
6376 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006377 const TargetLowering *TLI = TM.getTargetLowering();
6378 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6379 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006380 if (isGA1 && isGA2 && GV1 == GV2)
6381 return Offset1 == (Offset2 + Dist*Bytes);
6382 return false;
6383}
6384
6385
Evan Cheng34a23ea2009-12-09 01:04:59 +00006386/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6387/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006388unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006389 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006390 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006391 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006392 const TargetLowering *TLI = TM.getTargetLowering();
6393 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaultdfb3e702013-11-16 20:50:54 +00006394 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006395 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00006396 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006397 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006398 unsigned AlignBits = KnownZero.countTrailingOnes();
6399 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6400 if (Align)
6401 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006402 }
Evan Chengd938faf2009-12-09 01:53:58 +00006403
Evan Cheng34a23ea2009-12-09 01:04:59 +00006404 // If this is a direct reference to a stack slot, use information about the
6405 // stack slot's alignment.
6406 int FrameIdx = 1 << 31;
6407 int64_t FrameOffset = 0;
6408 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6409 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006410 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006411 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006412 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006413 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6414 FrameOffset = Ptr.getConstantOperandVal(1);
6415 }
6416
6417 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006418 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006419 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6420 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006421 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006422 }
6423
6424 return 0;
6425}
6426
Juergen Ributzkab3487102013-11-19 21:20:17 +00006427/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6428/// which is split (or expanded) into two not necessarily identical pieces.
6429std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6430 // Currently all types are split in half.
6431 EVT LoVT, HiVT;
6432 if (!VT.isVector()) {
6433 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6434 } else {
6435 unsigned NumElements = VT.getVectorNumElements();
6436 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6437 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6438 NumElements/2);
6439 }
6440 return std::make_pair(LoVT, HiVT);
6441}
6442
6443/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6444/// low/high part.
6445std::pair<SDValue, SDValue>
6446SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6447 const EVT &HiVT) {
6448 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6449 N.getValueType().getVectorNumElements() &&
6450 "More vector elements requested than available!");
6451 SDValue Lo, Hi;
6452 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6453 getConstant(0, TLI->getVectorIdxTy()));
6454 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6455 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6456 return std::make_pair(Lo, Hi);
6457}
6458
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006459// getAddressSpace - Return the address space this GlobalAddress belongs to.
6460unsigned GlobalAddressSDNode::getAddressSpace() const {
6461 return getGlobal()->getType()->getAddressSpace();
6462}
6463
6464
Chris Lattner229907c2011-07-18 04:54:35 +00006465Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006466 if (isMachineConstantPoolEntry())
6467 return Val.MachineCPVal->getType();
6468 return Val.ConstVal->getType();
6469}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006470
Bob Wilson85cefe82009-03-02 23:24:16 +00006471bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6472 APInt &SplatUndef,
6473 unsigned &SplatBitSize,
6474 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006475 unsigned MinSplatBits,
6476 bool isBigEndian) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006477 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006478 assert(VT.isVector() && "Expected a vector type");
6479 unsigned sz = VT.getSizeInBits();
6480 if (MinSplatBits > sz)
6481 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006482
Bob Wilson85cefe82009-03-02 23:24:16 +00006483 SplatValue = APInt(sz, 0);
6484 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006485
Bob Wilson85cefe82009-03-02 23:24:16 +00006486 // Get the bits. Bits with undefined values (when the corresponding element
6487 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6488 // in SplatValue. If any of the values are not constant, give up and return
6489 // false.
6490 unsigned int nOps = getNumOperands();
6491 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6492 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006493
6494 for (unsigned j = 0; j < nOps; ++j) {
6495 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006496 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006497 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006498
Bob Wilson85cefe82009-03-02 23:24:16 +00006499 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006500 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006501 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006502 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006503 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006504 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006505 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006506 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006507 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006508 }
6509
Bob Wilson85cefe82009-03-02 23:24:16 +00006510 // The build_vector is all constants or undefs. Find the smallest element
6511 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006512
Bob Wilson85cefe82009-03-02 23:24:16 +00006513 HasAnyUndefs = (SplatUndef != 0);
6514 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006515
Bob Wilson85cefe82009-03-02 23:24:16 +00006516 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006517 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6518 APInt LowValue = SplatValue.trunc(HalfSize);
6519 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6520 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006521
Bob Wilson85cefe82009-03-02 23:24:16 +00006522 // If the two halves do not match (ignoring undef bits), stop here.
6523 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6524 MinSplatBits > HalfSize)
6525 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006526
Bob Wilson85cefe82009-03-02 23:24:16 +00006527 SplatValue = HighValue | LowValue;
6528 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006529
Bob Wilson85cefe82009-03-02 23:24:16 +00006530 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006531 }
6532
Bob Wilson85cefe82009-03-02 23:24:16 +00006533 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006534 return true;
6535}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006536
Owen Anderson53aa7a92009-08-10 22:56:29 +00006537bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006538 // Find the first non-undef value in the shuffle mask.
6539 unsigned i, e;
6540 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6541 /* search */;
6542
Nate Begeman39b59db2009-04-29 18:13:31 +00006543 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006544
Nate Begeman5f829d82009-04-29 05:20:52 +00006545 // Make sure all remaining elements are either undef or the same as the first
6546 // non-undef value.
6547 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006548 if (Mask[i] >= 0 && Mask[i] != Idx)
6549 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006550 return true;
6551}
David Greene09851602010-01-20 20:13:31 +00006552
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006553#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006554static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006555 SmallPtrSet<const SDNode*, 32> &Visited,
6556 SmallPtrSet<const SDNode*, 32> &Checked) {
6557 // If this node has already been checked, don't check it again.
6558 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006559 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006560
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006561 // If a node has already been visited on this depth-first walk, reject it as
6562 // a cycle.
6563 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006564 dbgs() << "Offending node:\n";
6565 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006566 errs() << "Detected cycle in SelectionDAG\n";
6567 abort();
David Greene09851602010-01-20 20:13:31 +00006568 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006569
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006570 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6571 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006572
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006573 Checked.insert(N);
6574 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006575}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006576#endif
David Greene09851602010-01-20 20:13:31 +00006577
6578void llvm::checkForCycles(const llvm::SDNode *N) {
6579#ifdef XDEBUG
Alp Toker6a033742013-10-29 02:35:28 +00006580 assert(N && "Checking nonexistent SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006581 SmallPtrSet<const SDNode*, 32> visited;
6582 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006583 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006584#endif
6585}
6586
6587void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6588 checkForCycles(DAG->getRoot().getNode());
6589}