blob: 12105341e8efef3d730be2a6ad5862f9d11dc06a [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendlinge36025e2009-12-21 19:34:59 +000013
Chris Lattner78ec3112003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Chenga8efe282010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthd04a8d42012-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 Carruthbe049292013-01-07 03:08:10 +000021#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/Analysis/ValueTracking.h"
23#include "llvm/Assembly/Writer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineModuleInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000028#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000029#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalAlias.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/Intrinsics.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000037#include "llvm/Support/CommandLine.h"
David Greene55d146e2010-01-05 01:24:36 +000038#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000040#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000041#include "llvm/Support/MathExtras.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000042#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-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 Cohenfd161e92005-01-09 20:41:56 +000051#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000052#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000054
Chris Lattner0b3e5252006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000065
Jim Laskey58b968b2005-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 Johannesene6c17422007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000076}
77
Owen Andersone50ed302009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000081
Dale Johannesenf04afdb2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover0a29cb02013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000089}
90
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000094
Evan Chenga8df1662006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000098 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000099 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000101
Evan Chenga8df1662006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000103
Chris Lattner61d43992006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000105
Chris Lattner61d43992006-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 Michelfdc40a02009-02-17 22:15:04 +0000109
Chris Lattner61d43992006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattner61d43992006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-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 Gohman475871a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszak554c6762012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000125 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000128 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000129 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000130 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
Chris Lattner61d43992006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-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 Lattner61d43992006-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 Cheng4a147842006-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 Lattner547a16f2006-04-15 23:38:00 +0000146 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000147 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000149
Evan Cheng4a147842006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Evan Chenga8df1662006-03-27 06:58:47 +0000152 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000153
Evan Chenga8df1662006-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 Michelfdc40a02009-02-17 22:15:04 +0000157
Evan Chenga8df1662006-03-27 06:58:47 +0000158 // Do not accept an all-undef vector.
159 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000160
Dan Gohman68f32cb2009-06-04 16:49:15 +0000161 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000162 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000163 SDValue Zero = N->getOperand(i);
Jakub Staszak554c6762012-09-30 21:24:57 +0000164 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
165 if (!CN->isNullValue())
Evan Chenga8df1662006-03-27 06:58:47 +0000166 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000167 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
168 if (!CFPN->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000169 return false;
170 } else
171 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000172
Dan Gohman68f32cb2009-06-04 16:49:15 +0000173 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-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 Cheng4a147842006-03-26 09:50:58 +0000180}
181
Evan Chengefec7512008-02-18 23:04:32 +0000182/// isScalarToVector - Return true if the specified node is a
183/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
184/// element is not an undef.
185bool ISD::isScalarToVector(const SDNode *N) {
186 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
187 return true;
188
189 if (N->getOpcode() != ISD::BUILD_VECTOR)
190 return false;
191 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
192 return false;
193 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000194 if (NumElems == 1)
195 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000196 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000197 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000198 if (V.getOpcode() != ISD::UNDEF)
199 return false;
200 }
201 return true;
202}
203
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000204/// allOperandsUndef - Return true if the node has at least one operand
205/// and all operands of the specified node are ISD::UNDEF.
206bool ISD::allOperandsUndef(const SDNode *N) {
207 // Return false if the node has no operands.
208 // This is "logically inconsistent" with the definition of "all" but
209 // is probably the desired behavior.
210 if (N->getNumOperands() == 0)
211 return false;
212
213 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
214 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
215 return false;
216
217 return true;
218}
219
Chris Lattnerc3aae252005-01-07 07:46:32 +0000220/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
221/// when given the operation for (X op Y).
222ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
223 // To perform this operation, we just need to swap the L and G bits of the
224 // operation.
225 unsigned OldL = (Operation >> 2) & 1;
226 unsigned OldG = (Operation >> 1) & 1;
227 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
228 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000229 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000230}
231
232/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
233/// 'op' is a valid SetCC operation.
234ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
235 unsigned Operation = Op;
236 if (isInteger)
237 Operation ^= 7; // Flip L, G, E bits, but not U.
238 else
239 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000240
Chris Lattnerc3aae252005-01-07 07:46:32 +0000241 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000242 Operation &= ~8; // Don't let N and U bits get set.
243
Chris Lattnerc3aae252005-01-07 07:46:32 +0000244 return ISD::CondCode(Operation);
245}
246
247
248/// isSignedOp - For an integer comparison, return 1 if the comparison is a
249/// signed operation and 2 if the result is an unsigned comparison. Return zero
250/// if the operation does not depend on the sign of the input (setne and seteq).
251static int isSignedOp(ISD::CondCode Opcode) {
252 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000253 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000254 case ISD::SETEQ:
255 case ISD::SETNE: return 0;
256 case ISD::SETLT:
257 case ISD::SETLE:
258 case ISD::SETGT:
259 case ISD::SETGE: return 1;
260 case ISD::SETULT:
261 case ISD::SETULE:
262 case ISD::SETUGT:
263 case ISD::SETUGE: return 2;
264 }
265}
266
267/// getSetCCOrOperation - Return the result of a logical OR between different
268/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
269/// returns SETCC_INVALID if it is not possible to represent the resultant
270/// comparison.
271ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
272 bool isInteger) {
273 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
274 // Cannot fold a signed integer setcc with an unsigned integer setcc.
275 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000276
Chris Lattnerc3aae252005-01-07 07:46:32 +0000277 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000278
Chris Lattnerc3aae252005-01-07 07:46:32 +0000279 // If the N and U bits get set then the resultant comparison DOES suddenly
280 // care about orderedness, and is true when ordered.
281 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000282 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Chris Lattnere41102b2006-05-12 17:03:46 +0000284 // Canonicalize illegal integer setcc's.
285 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
286 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000287
Chris Lattnerc3aae252005-01-07 07:46:32 +0000288 return ISD::CondCode(Op);
289}
290
291/// getSetCCAndOperation - Return the result of a logical AND between different
292/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
293/// function returns zero if it is not possible to represent the resultant
294/// comparison.
295ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
296 bool isInteger) {
297 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
298 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000299 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000300
301 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000302 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000303
Chris Lattnera83385f2006-04-27 05:01:07 +0000304 // Canonicalize illegal integer setcc's.
305 if (isInteger) {
306 switch (Result) {
307 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000308 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000309 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000310 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
311 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
312 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000313 }
314 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Chris Lattnera83385f2006-04-27 05:01:07 +0000316 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000317}
318
Jim Laskey58b968b2005-08-17 20:08:02 +0000319//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000320// SDNode Profile Support
321//===----------------------------------------------------------------------===//
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
326 ID.AddInteger(OpC);
327}
328
329/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
330/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000331static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000332 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000333}
334
Jim Laskeydef69b92006-10-27 23:52:51 +0000335/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
336///
Jim Laskey583bd472006-10-27 23:46:08 +0000337static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000338 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000339 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000340 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000341 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000342 }
Jim Laskey583bd472006-10-27 23:46:08 +0000343}
344
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000345/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
346///
347static void AddNodeIDOperands(FoldingSetNodeID &ID,
348 const SDUse *Ops, unsigned NumOps) {
349 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000350 ID.AddPointer(Ops->getNode());
351 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000352 }
353}
354
Jim Laskey583bd472006-10-27 23:46:08 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000356 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000357 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000358 AddNodeIDOpcode(ID, OpC);
359 AddNodeIDValueTypes(ID, VTList);
360 AddNodeIDOperands(ID, OpList, N);
361}
362
Duncan Sands0dc40452008-10-27 15:30:53 +0000363/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
364/// the NodeID data.
365static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000367 case ISD::TargetExternalSymbol:
368 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000369 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 default: break; // Normal nodes don't need extra info.
371 case ISD::TargetConstant:
372 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000373 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 break;
375 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000376 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000377 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000378 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000379 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000381 case ISD::GlobalAddress:
382 case ISD::TargetGlobalTLSAddress:
383 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000384 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000385 ID.AddPointer(GA->getGlobal());
386 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000387 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000388 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000389 break;
390 }
391 case ISD::BasicBlock:
392 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
393 break;
394 case ISD::Register:
395 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
396 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000397 case ISD::RegisterMask:
398 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
399 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000400 case ISD::SRCVALUE:
401 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
402 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000403 case ISD::FrameIndex:
404 case ISD::TargetFrameIndex:
405 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
406 break;
407 case ISD::JumpTable:
408 case ISD::TargetJumpTable:
409 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000410 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000411 break;
412 case ISD::ConstantPool:
413 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000414 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000415 ID.AddInteger(CP->getAlignment());
416 ID.AddInteger(CP->getOffset());
417 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000418 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000419 else
420 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000421 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000422 break;
423 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000424 case ISD::TargetIndex: {
425 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
426 ID.AddInteger(TI->getIndex());
427 ID.AddInteger(TI->getOffset());
428 ID.AddInteger(TI->getTargetFlags());
429 break;
430 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000431 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000432 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000434 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000435 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 break;
437 }
438 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000440 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000441 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000442 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000443 break;
444 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000445 case ISD::ATOMIC_CMP_SWAP:
446 case ISD::ATOMIC_SWAP:
447 case ISD::ATOMIC_LOAD_ADD:
448 case ISD::ATOMIC_LOAD_SUB:
449 case ISD::ATOMIC_LOAD_AND:
450 case ISD::ATOMIC_LOAD_OR:
451 case ISD::ATOMIC_LOAD_XOR:
452 case ISD::ATOMIC_LOAD_NAND:
453 case ISD::ATOMIC_LOAD_MIN:
454 case ISD::ATOMIC_LOAD_MAX:
455 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000456 case ISD::ATOMIC_LOAD_UMAX:
457 case ISD::ATOMIC_LOAD:
458 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000459 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000460 ID.AddInteger(AT->getMemoryVT().getRawBits());
461 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000462 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
463 break;
464 }
465 case ISD::PREFETCH: {
466 const MemSDNode *PF = cast<MemSDNode>(N);
467 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000468 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000469 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000470 case ISD::VECTOR_SHUFFLE: {
471 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000472 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000473 i != e; ++i)
474 ID.AddInteger(SVN->getMaskElt(i));
475 break;
476 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000477 case ISD::TargetBlockAddress:
478 case ISD::BlockAddress: {
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000479 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
480 ID.AddPointer(BA->getBlockAddress());
481 ID.AddInteger(BA->getOffset());
482 ID.AddInteger(BA->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000483 break;
484 }
Mon P Wang28873102008-06-25 08:15:39 +0000485 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000486
487 // Target specific memory nodes could also have address spaces to check.
488 if (N->isTargetMemoryOpcode())
489 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000490}
491
Duncan Sands0dc40452008-10-27 15:30:53 +0000492/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
493/// data.
494static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
495 AddNodeIDOpcode(ID, N->getOpcode());
496 // Add the return value info.
497 AddNodeIDValueTypes(ID, N->getVTList());
498 // Add the operand info.
499 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
500
501 // Handle SDNode leafs with special info.
502 AddNodeIDCustom(ID, N);
503}
504
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000505/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000506/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000507/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000508///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000509static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000510encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000511 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000512 assert((ConvType & 3) == ConvType &&
513 "ConvType may not require more than 2 bits!");
514 assert((AM & 7) == AM &&
515 "AM may not require more than 3 bits!");
516 return ConvType |
517 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000518 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000519 (isNonTemporal << 6) |
520 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000521}
522
Jim Laskey583bd472006-10-27 23:46:08 +0000523//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000524// SelectionDAG Class
525//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000526
Duncan Sands0dc40452008-10-27 15:30:53 +0000527/// doNotCSE - Return true if CSE should not be performed for this node.
528static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000529 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000530 return true; // Never CSE anything that produces a flag.
531
532 switch (N->getOpcode()) {
533 default: break;
534 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000535 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000536 return true; // Never CSE these nodes.
537 }
538
539 // Check that remaining values produced are not flags.
540 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000541 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000542 return true; // Never CSE anything that produces a flag.
543
544 return false;
545}
546
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000547/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000548/// SelectionDAG.
549void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000550 // Create a dummy node (which is not added to allnodes), that adds a reference
551 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000552 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000553
Chris Lattner190a4182006-08-04 17:45:20 +0000554 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000555
Chris Lattner190a4182006-08-04 17:45:20 +0000556 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000557 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000558 if (I->use_empty())
559 DeadNodes.push_back(I);
560
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000561 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000562
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000563 // If the root changed (e.g. it was a dead load, update the root).
564 setRoot(Dummy.getValue());
565}
566
567/// RemoveDeadNodes - This method deletes the unreachable nodes in the
568/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000569void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000570
Chris Lattner190a4182006-08-04 17:45:20 +0000571 // Process the worklist, deleting the nodes and adding their uses to the
572 // worklist.
573 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000574 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000575
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000576 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
577 DUL->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000578
Chris Lattner190a4182006-08-04 17:45:20 +0000579 // Take the node out of the appropriate CSE map.
580 RemoveNodeFromCSEMaps(N);
581
582 // Next, brutally remove the operand list. This is safe to do, as there are
583 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000584 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
585 SDUse &Use = *I++;
586 SDNode *Operand = Use.getNode();
587 Use.set(SDValue());
588
Chris Lattner190a4182006-08-04 17:45:20 +0000589 // Now that we removed this operand, see if there are no uses of it left.
590 if (Operand->use_empty())
591 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000592 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000593
Dan Gohmanc5336122009-01-19 22:39:36 +0000594 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000595 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000596}
597
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000598void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000599 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000600
601 // Create a dummy node that adds a reference to the root node, preventing
602 // it from being deleted. (This matters if the root is an operand of the
603 // dead node.)
604 HandleSDNode Dummy(getRoot());
605
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000606 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000607}
608
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000609void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000610 // First take this out of the appropriate CSE map.
611 RemoveNodeFromCSEMaps(N);
612
Scott Michelfdc40a02009-02-17 22:15:04 +0000613 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000614 // AllNodes list, and delete the node.
615 DeleteNodeNotInCSEMaps(N);
616}
617
618void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000619 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
620 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000621
Dan Gohmanf06c8352008-09-30 18:30:35 +0000622 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000623 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000624
Dan Gohmanc5336122009-01-19 22:39:36 +0000625 DeallocateNode(N);
626}
627
628void SelectionDAG::DeallocateNode(SDNode *N) {
629 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000630 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000631
Dan Gohmanc5336122009-01-19 22:39:36 +0000632 // Set the opcode to DELETED_NODE to help catch bugs when node
633 // memory is reallocated.
634 N->NodeType = ISD::DELETED_NODE;
635
Dan Gohman11467282008-08-26 01:44:34 +0000636 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000637
Evan Chengbfcb3052010-03-25 01:38:16 +0000638 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000639 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000640 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
641 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000642}
643
Chris Lattner149c58c2005-08-16 18:17:10 +0000644/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
645/// correspond to it. This is useful when we're about to delete or repurpose
646/// the node. We don't want future request for structurally identical nodes
647/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000648bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000649 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000650 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000651 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000652 case ISD::CONDCODE:
653 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
654 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000655 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000656 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
657 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000658 case ISD::ExternalSymbol:
659 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000660 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000661 case ISD::TargetExternalSymbol: {
662 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
663 Erased = TargetExternalSymbols.erase(
664 std::pair<std::string,unsigned char>(ESN->getSymbol(),
665 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000666 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000667 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000668 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000669 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000670 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000671 Erased = ExtendedValueTypeNodes.erase(VT);
672 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
674 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000675 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000676 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000677 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000678 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000679 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000680 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
681 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000682 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000683 break;
684 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000685#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000686 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000687 // flag result (which cannot be CSE'd) or is one of the special cases that are
688 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000689 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000690 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000691 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000692 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000693 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000694 }
695#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000696 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000697}
698
Dan Gohman39946102009-01-25 16:29:12 +0000699/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
700/// maps and modified in place. Add it back to the CSE maps, unless an identical
701/// node already exists, in which case transfer all its users to the existing
702/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000703///
Dan Gohman39946102009-01-25 16:29:12 +0000704void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000705SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000706 // For node types that aren't CSE'd, just act as if no identical node
707 // already exists.
708 if (!doNotCSE(N)) {
709 SDNode *Existing = CSEMap.GetOrInsertNode(N);
710 if (Existing != N) {
711 // If there was already an existing matching node, use ReplaceAllUsesWith
712 // to replace the dead one with the existing one. This can cause
713 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000714 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000715
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000716 // N is now dead. Inform the listeners and delete it.
717 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
718 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000719 DeleteNodeNotInCSEMaps(N);
720 return;
721 }
722 }
Evan Cheng71e86852008-07-08 20:06:39 +0000723
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000724 // If the node doesn't already exist, we updated it. Inform listeners.
725 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
726 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000727}
728
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000729/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000730/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000731/// return null, otherwise return a pointer to the slot it would take. If a
732/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000733SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000734 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000735 if (doNotCSE(N))
736 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000737
Dan Gohman475871a2008-07-27 21:46:04 +0000738 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000739 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000740 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000741 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000742 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000743 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000744}
745
746/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000747/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000748/// return null, otherwise return a pointer to the slot it would take. If a
749/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000750SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000751 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000752 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000753 if (doNotCSE(N))
754 return 0;
755
Dan Gohman475871a2008-07-27 21:46:04 +0000756 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000757 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000758 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000759 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000760 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000761 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000762}
763
764
765/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000766/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000767/// return null, otherwise return a pointer to the slot it would take. If a
768/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000769SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000770 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000771 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000772 if (doNotCSE(N))
773 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000774
Jim Laskey583bd472006-10-27 23:46:08 +0000775 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000776 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000777 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000778 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000779 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000780}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000781
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000782#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000783/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
784static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000785 switch (N->getOpcode()) {
786 default:
787 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000788 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000789 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000790 assert(N->getNumValues() == 1 && "Too many results!");
791 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
792 "Wrong return type!");
793 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
794 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
795 "Mismatched operand types!");
796 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
797 "Wrong operand type!");
798 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
799 "Wrong return type size");
800 break;
801 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000802 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000803 assert(N->getNumValues() == 1 && "Too many results!");
804 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000805 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000806 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000807 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000808 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000809 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000810 (EltVT.isInteger() && I->getValueType().isInteger() &&
811 EltVT.bitsLE(I->getValueType()))) &&
812 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000813 assert(I->getValueType() == N->getOperand(0).getValueType() &&
814 "Operands must all have the same type");
815 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000816 break;
817 }
818 }
819}
820
Duncan Sands59d2dad2010-11-20 11:25:00 +0000821/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
822static void VerifySDNode(SDNode *N) {
823 // The SDNode allocators cannot be used to allocate nodes with fields that are
824 // not present in an SDNode!
825 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
826 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
827 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
828 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
829 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
830 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
831 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
832 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
833 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
834 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
835 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
836 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
837 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
838 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
839 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
840 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
841 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
842 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
843 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
844
845 VerifyNodeCommon(N);
846}
847
848/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
849/// invalid.
850static void VerifyMachineNode(SDNode *N) {
851 // The MachineNode allocators cannot be used to allocate nodes with fields
852 // that are not present in a MachineNode!
853 // Currently there are no such nodes.
854
855 VerifyNodeCommon(N);
856}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000857#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000858
Owen Andersone50ed302009-08-10 22:56:29 +0000859/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000860/// given type.
861///
Owen Andersone50ed302009-08-10 22:56:29 +0000862unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000863 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000864 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000865 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000866
Bill Wendlingba54bca2013-06-19 21:36:55 +0000867 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000868}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000869
Dale Johannesen92570c42009-02-07 02:15:05 +0000870// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000871SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Bill Wendlingba54bca2013-06-19 21:36:55 +0000872 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TTI(0), OptLevel(OL),
873 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sandersea28aaf2013-11-15 12:56:49 +0000874 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
875 UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000876 AllNodes.push_back(&EntryNode);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000877 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000878}
879
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000880void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000881 MF = &mf;
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000882 TTI = tti;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000883 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000884}
885
Chris Lattner78ec3112003-08-11 14:57:33 +0000886SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000887 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000888 allnodes_clear();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000889 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000890}
891
892void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000893 assert(&*AllNodes.begin() == &EntryNode);
894 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000895 while (!AllNodes.empty())
896 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000897}
898
Dan Gohman7c3234c2008-08-27 23:52:12 +0000899void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000900 allnodes_clear();
901 OperandAllocator.Reset();
902 CSEMap.clear();
903
904 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000905 ExternalSymbols.clear();
906 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000907 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
908 static_cast<CondCodeSDNode*>(0));
909 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
910 static_cast<SDNode*>(0));
911
Dan Gohmane7852d02009-01-26 04:35:06 +0000912 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000913 AllNodes.push_back(&EntryNode);
914 Root = getEntryNode();
Evan Cheng31441b72010-03-29 20:48:30 +0000915 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000916}
917
Andrew Trickac6d9be2013-05-25 02:42:55 +0000918SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotema3c42f32011-09-27 11:16:47 +0000919 return VT.bitsGT(Op.getValueType()) ?
920 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
921 getNode(ISD::TRUNCATE, DL, VT, Op);
922}
923
Andrew Trickac6d9be2013-05-25 02:42:55 +0000924SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000925 return VT.bitsGT(Op.getValueType()) ?
926 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
927 getNode(ISD::TRUNCATE, DL, VT, Op);
928}
929
Andrew Trickac6d9be2013-05-25 02:42:55 +0000930SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000931 return VT.bitsGT(Op.getValueType()) ?
932 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
933 getNode(ISD::TRUNCATE, DL, VT, Op);
934}
935
Andrew Trickac6d9be2013-05-25 02:42:55 +0000936SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000937 assert(!VT.isVector() &&
938 "getZeroExtendInReg should use the vector element type instead of "
939 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000940 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000941 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
942 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000943 VT.getSizeInBits());
944 return getNode(ISD::AND, DL, Op.getValueType(), Op,
945 getConstant(Imm, Op.getValueType()));
946}
947
Bob Wilson4c245462009-01-22 17:39:32 +0000948/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
949///
Andrew Trickac6d9be2013-05-25 02:42:55 +0000950SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000951 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000952 SDValue NegOne =
953 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000954 return getNode(ISD::XOR, DL, VT, Val, NegOne);
955}
956
Owen Andersone50ed302009-08-10 22:56:29 +0000957SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000958 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000959 assert((EltVT.getSizeInBits() >= 64 ||
960 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
961 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000962 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000963}
964
Owen Andersone50ed302009-08-10 22:56:29 +0000965SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000966 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000967}
968
Owen Andersone50ed302009-08-10 22:56:29 +0000969SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000970 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000971
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000972 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000973 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000974
Bill Wendlingba54bca2013-06-19 21:36:55 +0000975 const TargetLowering *TLI = TM.getTargetLowering();
976
Duncan Sands28b77e92011-09-06 19:07:46 +0000977 // In some cases the vector type is legal but the element type is illegal and
978 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
979 // inserted value (the type does not need to match the vector element type).
980 // Any extra bits introduced will be truncated away.
Bill Wendlingba54bca2013-06-19 21:36:55 +0000981 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sands28b77e92011-09-06 19:07:46 +0000982 TargetLowering::TypePromoteInteger) {
Bill Wendlingba54bca2013-06-19 21:36:55 +0000983 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sands28b77e92011-09-06 19:07:46 +0000984 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
985 Elt = ConstantInt::get(*getContext(), NewVal);
986 }
Daniel Sandersea28aaf2013-11-15 12:56:49 +0000987 // In other cases the element type is illegal and needs to be expanded, for
988 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
989 // the value into n parts and use a vector type with n-times the elements.
990 // Then bitcast to the type requested.
991 // Legalizing constants too early makes the DAGCombiner's job harder so we
992 // only legalize if the DAG tells us we must produce legal types.
993 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
994 TLI->getTypeAction(*getContext(), EltVT) ==
995 TargetLowering::TypeExpandInteger) {
996 APInt NewVal = Elt->getValue();
997 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
998 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
999 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1000 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1001
1002 // Check the temporary vector is the correct size. If this fails then
1003 // getTypeToTransformTo() probably returned a type whose size (in bits)
1004 // isn't a power-of-2 factor of the requested type size.
1005 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1006
1007 SmallVector<SDValue, 2> EltParts;
1008 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1009 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1010 .trunc(ViaEltSizeInBits),
1011 ViaEltVT, isT));
1012 }
1013
1014 // EltParts is currently in little endian order. If we actually want
1015 // big-endian order then reverse it now.
1016 if (TLI->isBigEndian())
1017 std::reverse(EltParts.begin(), EltParts.end());
1018
1019 // The elements must be reversed when the element order is different
1020 // to the endianness of the elements (because the BITCAST is itself a
1021 // vector shuffle in this situation). However, we do not need any code to
1022 // perform this reversal because getConstant() is producing a vector
1023 // splat.
1024 // This situation occurs in MIPS MSA.
1025
1026 SmallVector<SDValue, 8> Ops;
1027 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1028 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1029
1030 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1031 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
1032 &Ops[0], Ops.size()));
1033 return Result;
1034 }
Duncan Sands28b77e92011-09-06 19:07:46 +00001035
1036 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1037 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001038 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001039 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001040 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +00001041 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +00001042 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +00001043 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001044 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001045 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001046 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001047
Dan Gohman89081322007-12-12 22:21:26 +00001048 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001049 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001050 CSEMap.InsertNode(N, IP);
1051 AllNodes.push_back(N);
1052 }
1053
Dan Gohman475871a2008-07-27 21:46:04 +00001054 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001055 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001056 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001057 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001058 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001059 }
1060 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001061}
1062
Dan Gohman475871a2008-07-27 21:46:04 +00001063SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001064 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner0bd48932008-01-17 07:00:52 +00001065}
1066
1067
Owen Andersone50ed302009-08-10 22:56:29 +00001068SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001069 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001070}
1071
Owen Andersone50ed302009-08-10 22:56:29 +00001072SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001073 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001075 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076
Chris Lattnerd8658612005-02-17 20:17:32 +00001077 // Do the map lookup using the actual bit pattern for the floating point
1078 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1079 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001080 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001081 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001082 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001083 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001084 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001085 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001086 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001087 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001088 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001089
Evan Chengc908dcd2007-06-29 21:36:04 +00001090 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001091 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001092 CSEMap.InsertNode(N, IP);
1093 AllNodes.push_back(N);
1094 }
1095
Dan Gohman475871a2008-07-27 21:46:04 +00001096 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001097 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001098 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001099 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001100 // FIXME SDLoc info might be appropriate here
1101 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001102 }
1103 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104}
1105
Owen Andersone50ed302009-08-10 22:56:29 +00001106SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001107 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001108 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001109 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001110 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001111 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001112 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1113 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001114 bool ignored;
1115 APFloat apf = APFloat(Val);
Tim Northover0a29cb02013-01-22 09:46:31 +00001116 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001117 &ignored);
1118 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001119 } else
1120 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001121}
1122
Andrew Trickac6d9be2013-05-25 02:42:55 +00001123SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001124 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001125 bool isTargetGA,
1126 unsigned char TargetFlags) {
1127 assert((TargetFlags == 0 || isTargetGA) &&
1128 "Cannot set target flags on target-independent globals");
Matt Arsenault94437c92013-11-17 00:06:39 +00001129 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001130
Dan Gohman6520e202008-10-18 02:06:02 +00001131 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault94437c92013-11-17 00:06:39 +00001132 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman6520e202008-10-18 02:06:02 +00001133 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001134 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001135
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001136 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1137 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001138 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001139 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001140 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001141 }
1142
Chris Lattner2a4ed822009-06-25 21:21:14 +00001143 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001144 if (GVar && GVar->isThreadLocal())
1145 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1146 else
1147 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001148
Jim Laskey583bd472006-10-27 23:46:08 +00001149 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001150 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001151 ID.AddPointer(GV);
1152 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001153 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001154 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001155 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001156 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001157 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001158
Andrew Trickac6d9be2013-05-25 02:42:55 +00001159 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1160 DL.getDebugLoc(), GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001161 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001162 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001164 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001165}
1166
Owen Andersone50ed302009-08-10 22:56:29 +00001167SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001168 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001169 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001170 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001171 ID.AddInteger(FI);
1172 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001173 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001174 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001175
Dan Gohman95531882010-03-18 18:49:47 +00001176 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001177 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001178 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001179 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001180}
1181
Owen Andersone50ed302009-08-10 22:56:29 +00001182SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001183 unsigned char TargetFlags) {
1184 assert((TargetFlags == 0 || isTarget) &&
1185 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001186 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001187 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001188 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001189 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001190 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001191 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001192 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001193 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001194
Dan Gohman95531882010-03-18 18:49:47 +00001195 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1196 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001197 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001198 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001199 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001200}
1201
Dan Gohman46510a72010-04-15 01:51:59 +00001202SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001203 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001204 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001205 unsigned char TargetFlags) {
1206 assert((TargetFlags == 0 || isTarget) &&
1207 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001208 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001209 Alignment =
1210 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001211 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001212 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001213 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001214 ID.AddInteger(Alignment);
1215 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001216 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001217 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001218 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001219 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001220 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001221
Dan Gohman95531882010-03-18 18:49:47 +00001222 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1223 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001224 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001225 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001226 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001227}
1228
Chris Lattnerc3aae252005-01-07 07:46:32 +00001229
Owen Andersone50ed302009-08-10 22:56:29 +00001230SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001231 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001232 bool isTarget,
1233 unsigned char TargetFlags) {
1234 assert((TargetFlags == 0 || isTarget) &&
1235 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001236 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001237 Alignment =
1238 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001239 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001240 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001241 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001242 ID.AddInteger(Alignment);
1243 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001244 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001245 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001246 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001247 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001248 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001249
Dan Gohman95531882010-03-18 18:49:47 +00001250 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1251 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001252 CSEMap.InsertNode(N, IP);
1253 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001254 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001255}
1256
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001257SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1258 unsigned char TargetFlags) {
1259 FoldingSetNodeID ID;
1260 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1261 ID.AddInteger(Index);
1262 ID.AddInteger(Offset);
1263 ID.AddInteger(TargetFlags);
1264 void *IP = 0;
1265 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1266 return SDValue(E, 0);
1267
1268 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1269 TargetFlags);
1270 CSEMap.InsertNode(N, IP);
1271 AllNodes.push_back(N);
1272 return SDValue(N, 0);
1273}
1274
Dan Gohman475871a2008-07-27 21:46:04 +00001275SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001276 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001278 ID.AddPointer(MBB);
1279 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001280 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001281 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001282
Dan Gohman95531882010-03-18 18:49:47 +00001283 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001284 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001285 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001286 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001287}
1288
Owen Andersone50ed302009-08-10 22:56:29 +00001289SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001290 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1291 ValueTypeNodes.size())
1292 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001293
Duncan Sands83ec4b62008-06-06 12:08:01 +00001294 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001296
Dan Gohman475871a2008-07-27 21:46:04 +00001297 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001298 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001299 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001300 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001301}
1302
Owen Andersone50ed302009-08-10 22:56:29 +00001303SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001304 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001305 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001306 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001307 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001308 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001309}
1310
Owen Andersone50ed302009-08-10 22:56:29 +00001311SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001312 unsigned char TargetFlags) {
1313 SDNode *&N =
1314 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1315 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001316 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001317 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001318 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001319 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001320}
1321
Dan Gohman475871a2008-07-27 21:46:04 +00001322SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001323 if ((unsigned)Cond >= CondCodeNodes.size())
1324 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001325
Chris Lattner079a27a2005-08-09 20:40:02 +00001326 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001327 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001328 CondCodeNodes[Cond] = N;
1329 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001330 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001331
Dan Gohman475871a2008-07-27 21:46:04 +00001332 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001333}
1334
Nate Begeman5a5ca152009-04-29 05:20:52 +00001335// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1336// the shuffle mask M that point at N1 to point at N2, and indices that point
1337// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001338static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1339 std::swap(N1, N2);
1340 int NElts = M.size();
1341 for (int i = 0; i != NElts; ++i) {
1342 if (M[i] >= NElts)
1343 M[i] -= NElts;
1344 else if (M[i] >= 0)
1345 M[i] += NElts;
1346 }
1347}
1348
Andrew Trickac6d9be2013-05-25 02:42:55 +00001349SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001350 SDValue N2, const int *Mask) {
Craig Topperad445a62013-08-09 04:37:24 +00001351 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1352 "Invalid VECTOR_SHUFFLE");
Nate Begeman9008ca62009-04-27 18:41:29 +00001353
1354 // Canonicalize shuffle undef, undef -> undef
1355 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001356 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001357
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001358 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001359 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001360 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001361 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001362 for (unsigned i = 0; i != NElts; ++i) {
1363 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001364 MaskVec.push_back(Mask[i]);
1365 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001366
Nate Begeman9008ca62009-04-27 18:41:29 +00001367 // Canonicalize shuffle v, v -> v, undef
1368 if (N1 == N2) {
1369 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001370 for (unsigned i = 0; i != NElts; ++i)
1371 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001372 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001373
Nate Begeman9008ca62009-04-27 18:41:29 +00001374 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1375 if (N1.getOpcode() == ISD::UNDEF)
1376 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001377
Nate Begeman9008ca62009-04-27 18:41:29 +00001378 // Canonicalize all index into lhs, -> shuffle lhs, undef
1379 // Canonicalize all index into rhs, -> shuffle rhs, undef
1380 bool AllLHS = true, AllRHS = true;
1381 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001382 for (unsigned i = 0; i != NElts; ++i) {
1383 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001384 if (N2Undef)
1385 MaskVec[i] = -1;
1386 else
1387 AllLHS = false;
1388 } else if (MaskVec[i] >= 0) {
1389 AllRHS = false;
1390 }
1391 }
1392 if (AllLHS && AllRHS)
1393 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001394 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001395 N2 = getUNDEF(VT);
1396 if (AllRHS) {
1397 N1 = getUNDEF(VT);
1398 commuteShuffle(N1, N2, MaskVec);
1399 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001400
Craig Toppereee2a112013-08-08 08:03:12 +00001401 // If Identity shuffle return that node.
Nate Begeman9008ca62009-04-27 18:41:29 +00001402 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001403 for (unsigned i = 0; i != NElts; ++i) {
1404 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001405 }
Craig Topperad445a62013-08-09 04:37:24 +00001406 if (Identity && NElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00001407 return N1;
Nate Begeman9008ca62009-04-27 18:41:29 +00001408
1409 FoldingSetNodeID ID;
1410 SDValue Ops[2] = { N1, N2 };
1411 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001412 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001413 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001414
Nate Begeman9008ca62009-04-27 18:41:29 +00001415 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001416 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001417 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001418
Nate Begeman9008ca62009-04-27 18:41:29 +00001419 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1420 // SDNode doesn't have access to it. This memory will be "leaked" when
1421 // the node is deallocated, but recovered when the NodeAllocator is released.
1422 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1423 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001424
Dan Gohman95531882010-03-18 18:49:47 +00001425 ShuffleVectorSDNode *N =
Jack Carter3af4d252013-09-09 22:02:08 +00001426 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1427 dl.getDebugLoc(), N1, N2,
1428 MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001429 CSEMap.InsertNode(N, IP);
1430 AllNodes.push_back(N);
1431 return SDValue(N, 0);
1432}
1433
Andrew Trickac6d9be2013-05-25 02:42:55 +00001434SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001435 SDValue Val, SDValue DTy,
1436 SDValue STy, SDValue Rnd, SDValue Sat,
1437 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001438 // If the src and dest types are the same and the conversion is between
1439 // integer types of the same sign or two floats, no conversion is necessary.
1440 if (DTy == STy &&
1441 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001442 return Val;
1443
1444 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001445 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1446 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001447 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001448 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001449 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001450
Jack Carter3af4d252013-09-09 22:02:08 +00001451 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1452 dl.getDebugLoc(),
1453 Ops, 5, Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001454 CSEMap.InsertNode(N, IP);
1455 AllNodes.push_back(N);
1456 return SDValue(N, 0);
1457}
1458
Owen Andersone50ed302009-08-10 22:56:29 +00001459SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001460 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001461 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001462 ID.AddInteger(RegNo);
1463 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001464 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001465 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001466
Dan Gohman95531882010-03-18 18:49:47 +00001467 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001468 CSEMap.InsertNode(N, IP);
1469 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001470 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001471}
1472
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001473SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1474 FoldingSetNodeID ID;
1475 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1476 ID.AddPointer(RegMask);
1477 void *IP = 0;
1478 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1479 return SDValue(E, 0);
1480
1481 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1482 CSEMap.InsertNode(N, IP);
1483 AllNodes.push_back(N);
1484 return SDValue(N, 0);
1485}
1486
Andrew Trickac6d9be2013-05-25 02:42:55 +00001487SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001488 FoldingSetNodeID ID;
1489 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001490 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1491 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001492 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001493 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001494 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001495
Jack Carter3af4d252013-09-09 22:02:08 +00001496 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1497 dl.getDebugLoc(), Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001498 CSEMap.InsertNode(N, IP);
1499 AllNodes.push_back(N);
1500 return SDValue(N, 0);
1501}
1502
Chris Lattner7561d482010-03-14 02:33:54 +00001503
Dan Gohman46510a72010-04-15 01:51:59 +00001504SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001505 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001506 bool isTarget,
1507 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001508 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1509
1510 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001511 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001512 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001513 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001514 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001515 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001516 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001517 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001518
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001519 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1520 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001521 CSEMap.InsertNode(N, IP);
1522 AllNodes.push_back(N);
1523 return SDValue(N, 0);
1524}
1525
Dan Gohman475871a2008-07-27 21:46:04 +00001526SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001527 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001528 "SrcValue is not a pointer?");
1529
Jim Laskey583bd472006-10-27 23:46:08 +00001530 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001532 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001533
Chris Lattner61b09412006-08-11 21:01:22 +00001534 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001535 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001536 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001537
Dan Gohman95531882010-03-18 18:49:47 +00001538 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001539 CSEMap.InsertNode(N, IP);
1540 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001541 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001542}
1543
Chris Lattnerdecc2672010-04-07 05:20:54 +00001544/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1545SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1546 FoldingSetNodeID ID;
1547 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1548 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001549
Chris Lattnerdecc2672010-04-07 05:20:54 +00001550 void *IP = 0;
1551 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1552 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001553
Chris Lattnerdecc2672010-04-07 05:20:54 +00001554 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1555 CSEMap.InsertNode(N, IP);
1556 AllNodes.push_back(N);
1557 return SDValue(N, 0);
1558}
1559
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001560/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1561SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1562 unsigned SrcAS, unsigned DestAS) {
1563 SDValue Ops[] = {Ptr};
1564 FoldingSetNodeID ID;
1565 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), &Ops[0], 1);
1566 ID.AddInteger(SrcAS);
1567 ID.AddInteger(DestAS);
1568
1569 void *IP = 0;
1570 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1571 return SDValue(E, 0);
1572
1573 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1574 dl.getDebugLoc(),
1575 VT, Ptr, SrcAS, DestAS);
1576 CSEMap.InsertNode(N, IP);
1577 AllNodes.push_back(N);
1578 return SDValue(N, 0);
1579}
Chris Lattnerdecc2672010-04-07 05:20:54 +00001580
Duncan Sands92abc622009-01-31 15:50:11 +00001581/// getShiftAmountOperand - Return the specified value casted to
1582/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001583SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001584 EVT OpTy = Op.getValueType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001585 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001586 if (OpTy == ShTy || OpTy.isVector()) return Op;
1587
1588 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001589 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001590}
1591
Chris Lattner37ce9df2007-10-15 17:47:20 +00001592/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1593/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001594SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001595 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001596 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001597 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001598 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang364d73d2008-07-05 20:40:31 +00001599 unsigned StackAlign =
Bill Wendlingba54bca2013-06-19 21:36:55 +00001600 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
David Greene3f2bf852009-11-12 20:49:22 +00001602 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001603 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner37ce9df2007-10-15 17:47:20 +00001604}
1605
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001606/// CreateStackTemporary - Create a stack temporary suitable for holding
1607/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001608SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001609 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1610 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001611 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1612 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001613 const TargetLowering *TLI = TM.getTargetLowering();
1614 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001615 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1616 TD->getPrefTypeAlignment(Ty2));
1617
1618 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001619 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001620 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001621}
1622
Owen Andersone50ed302009-08-10 22:56:29 +00001623SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001624 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001625 // These setcc operations always fold.
1626 switch (Cond) {
1627 default: break;
1628 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001629 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001630 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001631 case ISD::SETTRUE2: {
1632 const TargetLowering *TLI = TM.getTargetLowering();
1633 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1634 return getConstant(
1635 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1636 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001637
Chris Lattnera83385f2006-04-27 05:01:07 +00001638 case ISD::SETOEQ:
1639 case ISD::SETOGT:
1640 case ISD::SETOGE:
1641 case ISD::SETOLT:
1642 case ISD::SETOLE:
1643 case ISD::SETONE:
1644 case ISD::SETO:
1645 case ISD::SETUO:
1646 case ISD::SETUEQ:
1647 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001648 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001649 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001650 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001651
Gabor Greifba36cb52008-08-28 21:40:38 +00001652 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001653 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001654 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001655 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001656
Chris Lattnerc3aae252005-01-07 07:46:32 +00001657 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001658 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001659 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1660 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001661 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1662 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1663 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1664 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1665 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1666 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1667 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1668 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001669 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001670 }
Chris Lattner67255a12005-04-07 18:14:58 +00001671 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001672 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1673 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001674 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001675 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001676 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001677 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001678 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001679 // fall through
1680 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001681 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001682 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001683 // fall through
1684 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001685 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001686 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001687 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001688 // fall through
1689 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001690 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001691 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001692 // fall through
1693 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001694 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001695 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001696 // fall through
1697 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001698 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001699 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001700 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001701 // fall through
1702 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001703 R==APFloat::cmpEqual, VT);
1704 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1705 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1706 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1707 R==APFloat::cmpEqual, VT);
1708 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1709 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1710 R==APFloat::cmpLessThan, VT);
1711 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1712 R==APFloat::cmpUnordered, VT);
1713 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1714 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001715 }
1716 } else {
1717 // Ensure that the constant occurs on the RHS.
Tom Stellard12d43f92013-09-28 02:50:38 +00001718 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1719 MVT CompVT = N1.getValueType().getSimpleVT();
1720 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1721 return SDValue();
1722
1723 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001724 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001725 }
1726
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001727 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001728 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001729}
1730
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001731/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1732/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001733bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001734 // This predicate is not safe for vector operations.
1735 if (Op.getValueType().isVector())
1736 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001737
Dan Gohman87862e72009-12-11 21:31:27 +00001738 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001739 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1740}
1741
Dan Gohmanea859be2007-06-22 14:59:07 +00001742/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1743/// this predicate to simplify operations downstream. Mask is known to be zero
1744/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001745bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001746 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001747 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001748 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001749 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001750 return (KnownZero & Mask) == Mask;
1751}
1752
1753/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1754/// known to be either zero or one and return them in the KnownZero/KnownOne
1755/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1756/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001757void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1758 APInt &KnownOne, unsigned Depth) const {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001759 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001760 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001761
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001762 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001763 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001764 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001765
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001766 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001767
1768 switch (Op.getOpcode()) {
1769 case ISD::Constant:
1770 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001771 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1772 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001773 return;
1774 case ISD::AND:
1775 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001776 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1777 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001778 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1779 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001780
1781 // Output known-1 bits are only known if set in both the LHS & RHS.
1782 KnownOne &= KnownOne2;
1783 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1784 KnownZero |= KnownZero2;
1785 return;
1786 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001787 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1788 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001789 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1790 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1791
Dan Gohmanea859be2007-06-22 14:59:07 +00001792 // Output known-0 bits are only known if clear in both the LHS & RHS.
1793 KnownZero &= KnownZero2;
1794 // Output known-1 are known to be set if set in either the LHS | RHS.
1795 KnownOne |= KnownOne2;
1796 return;
1797 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001798 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1799 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001800 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1801 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1802
Dan Gohmanea859be2007-06-22 14:59:07 +00001803 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001804 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001805 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1806 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1807 KnownZero = KnownZeroOut;
1808 return;
1809 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001810 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001811 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1812 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001813 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1814 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1815
1816 // If low bits are zero in either operand, output low known-0 bits.
1817 // Also compute a conserative estimate for high known-0 bits.
1818 // More trickiness is possible, but this is sufficient for the
1819 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001820 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001821 unsigned TrailZ = KnownZero.countTrailingOnes() +
1822 KnownZero2.countTrailingOnes();
1823 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001824 KnownZero2.countLeadingOnes(),
1825 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001826
1827 TrailZ = std::min(TrailZ, BitWidth);
1828 LeadZ = std::min(LeadZ, BitWidth);
1829 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1830 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001831 return;
1832 }
1833 case ISD::UDIV: {
1834 // For the purposes of computing leading zeros we can conservatively
1835 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001836 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001837 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001838 unsigned LeadZ = KnownZero2.countLeadingOnes();
1839
Jay Foad7a874dd2010-12-01 08:53:58 +00001840 KnownOne2.clearAllBits();
1841 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001842 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001843 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1844 if (RHSUnknownLeadingOnes != BitWidth)
1845 LeadZ = std::min(BitWidth,
1846 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001847
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001848 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001849 return;
1850 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001851 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001852 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1853 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001854 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1855 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1856
Dan Gohmanea859be2007-06-22 14:59:07 +00001857 // Only known if known in both the LHS and RHS.
1858 KnownOne &= KnownOne2;
1859 KnownZero &= KnownZero2;
1860 return;
1861 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001862 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1863 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001864 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1865 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1866
Dan Gohmanea859be2007-06-22 14:59:07 +00001867 // Only known if known in both the LHS and RHS.
1868 KnownOne &= KnownOne2;
1869 KnownZero &= KnownZero2;
1870 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001871 case ISD::SADDO:
1872 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001873 case ISD::SSUBO:
1874 case ISD::USUBO:
1875 case ISD::SMULO:
1876 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001877 if (Op.getResNo() != 1)
1878 return;
Duncan Sands03228082008-11-23 15:47:28 +00001879 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001880 case ISD::SETCC:
1881 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001882 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands28b77e92011-09-06 19:07:46 +00001883 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001884 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001885 return;
1886 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001887 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001888 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001889 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001890
1891 // If the shift count is an invalid immediate, don't do anything.
1892 if (ShAmt >= BitWidth)
1893 return;
1894
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001895 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001896 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001897 KnownZero <<= ShAmt;
1898 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001899 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001900 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 }
1902 return;
1903 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001904 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001905 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001906 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001907
Dan Gohmand4cf9922008-02-26 18:50:50 +00001908 // If the shift count is an invalid immediate, don't do anything.
1909 if (ShAmt >= BitWidth)
1910 return;
1911
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001912 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001913 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001914 KnownZero = KnownZero.lshr(ShAmt);
1915 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001916
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001917 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001918 KnownZero |= HighBits; // High bits known zero.
1919 }
1920 return;
1921 case ISD::SRA:
1922 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001923 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001924
Dan Gohmand4cf9922008-02-26 18:50:50 +00001925 // If the shift count is an invalid immediate, don't do anything.
1926 if (ShAmt >= BitWidth)
1927 return;
1928
Dan Gohmanea859be2007-06-22 14:59:07 +00001929 // If any of the demanded bits are produced by the sign extension, we also
1930 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001931 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001932
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001933 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001934 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001935 KnownZero = KnownZero.lshr(ShAmt);
1936 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001937
Dan Gohmanea859be2007-06-22 14:59:07 +00001938 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001939 APInt SignBit = APInt::getSignBit(BitWidth);
1940 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001941
Dan Gohmanca93a432008-02-20 16:30:17 +00001942 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001943 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001944 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001945 KnownOne |= HighBits; // New bits are known one.
1946 }
1947 }
1948 return;
1949 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001950 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001951 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001952
1953 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001954 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001955 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001956
Dan Gohman977a76f2008-02-13 22:28:48 +00001957 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001958 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001959
Dan Gohmanea859be2007-06-22 14:59:07 +00001960 // If the sign extended bits are demanded, we know that the sign
1961 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001962 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001963 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001964 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001965
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001966 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1967 KnownOne &= InputDemandedBits;
1968 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001969 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1970
Dan Gohmanea859be2007-06-22 14:59:07 +00001971 // If the sign bit of the input is known set or clear, then we know the
1972 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001973 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001974 KnownZero |= NewBits;
1975 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001976 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001977 KnownOne |= NewBits;
1978 KnownZero &= ~NewBits;
1979 } else { // Input sign bit unknown
1980 KnownZero &= ~NewBits;
1981 KnownOne &= ~NewBits;
1982 }
1983 return;
1984 }
1985 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001986 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001988 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001989 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001990 unsigned LowBits = Log2_32(BitWidth)+1;
1991 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001992 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001993 return;
1994 }
1995 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001996 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00001997 // If this is a ZEXTLoad and we are looking at the loaded value.
1998 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00001999 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002000 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002001 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00002002 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002003 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002004 }
2005 return;
2006 }
2007 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002008 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002009 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002010 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00002011 KnownZero = KnownZero.trunc(InBits);
2012 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002013 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002014 KnownZero = KnownZero.zext(BitWidth);
2015 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002016 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002017 return;
2018 }
2019 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002020 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002021 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002022 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002023
Jay Foad40f8f622010-12-07 08:25:19 +00002024 KnownZero = KnownZero.trunc(InBits);
2025 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002026 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00002027
2028 // Note if the sign bit is known to be zero or one.
2029 bool SignBitKnownZero = KnownZero.isNegative();
2030 bool SignBitKnownOne = KnownOne.isNegative();
2031 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2032 "Sign bit can't be known to be both zero and one!");
2033
Jay Foad40f8f622010-12-07 08:25:19 +00002034 KnownZero = KnownZero.zext(BitWidth);
2035 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002036
Dan Gohman977a76f2008-02-13 22:28:48 +00002037 // If the sign bit is known zero or one, the top bits match.
2038 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00002039 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00002040 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00002041 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002042 return;
2043 }
2044 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00002045 EVT InVT = Op.getOperand(0).getValueType();
2046 unsigned InBits = InVT.getScalarType().getSizeInBits();
2047 KnownZero = KnownZero.trunc(InBits);
2048 KnownOne = KnownOne.trunc(InBits);
2049 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2050 KnownZero = KnownZero.zext(BitWidth);
2051 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002052 return;
2053 }
2054 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00002055 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002056 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00002057 KnownZero = KnownZero.zext(InBits);
2058 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002059 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002060 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00002061 KnownZero = KnownZero.trunc(BitWidth);
2062 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002063 break;
2064 }
2065 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002066 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002067 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002068 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2069 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002070 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002071 return;
2072 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002073 case ISD::FGETSIGN:
2074 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002075 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00002076 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00002077
Dan Gohman23e8b712008-04-28 17:02:21 +00002078 case ISD::SUB: {
2079 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2080 // We know that the top bits of C-X are clear if X contains less bits
2081 // than C (i.e. no wrap-around can happen). For example, 20-X is
2082 // positive if we can prove that X is >= 0 and < 16.
2083 if (CLHS->getAPIntValue().isNonNegative()) {
2084 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2085 // NLZ can't be BitWidth with no sign bit
2086 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002087 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002088
2089 // If all of the MaskV bits are known to be zero, then we know the
2090 // output top bits are zero, because we now know that the output is
2091 // from [0-C].
2092 if ((KnownZero2 & MaskV) == MaskV) {
2093 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2094 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002095 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002096 }
2097 }
2098 }
2099 }
2100 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002101 case ISD::ADD:
2102 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002103 // Output known-0 bits are known if clear or set in both the low clear bits
2104 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2105 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002106 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002107 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002108 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2109
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002110 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002111 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002112 KnownZeroOut = std::min(KnownZeroOut,
2113 KnownZero2.countTrailingOnes());
2114
Chris Lattnerda605882010-12-19 20:38:28 +00002115 if (Op.getOpcode() == ISD::ADD) {
2116 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2117 return;
2118 }
2119
2120 // With ADDE, a carry bit may be added in, so we can only use this
2121 // information if we know (at least) that the low two bits are clear. We
2122 // then return to the caller that the low bit is unknown but that other bits
2123 // are known zero.
2124 if (KnownZeroOut >= 2) // ADDE
2125 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002126 return;
2127 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002128 case ISD::SREM:
2129 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002130 const APInt &RA = Rem->getAPIntValue().abs();
2131 if (RA.isPowerOf2()) {
2132 APInt LowBits = RA - 1;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002133 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002134
Duncan Sands5c2873a2010-01-29 09:45:26 +00002135 // The low bits of the first operand are unchanged by the srem.
2136 KnownZero = KnownZero2 & LowBits;
2137 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002138
Duncan Sands5c2873a2010-01-29 09:45:26 +00002139 // If the first operand is non-negative or has all low bits zero, then
2140 // the upper bits are all zero.
2141 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2142 KnownZero |= ~LowBits;
2143
2144 // If the first operand is negative and not all low bits are zero, then
2145 // the upper bits are all one.
2146 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2147 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002148 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002149 }
2150 }
2151 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002152 case ISD::UREM: {
2153 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002154 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002155 if (RA.isPowerOf2()) {
2156 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002157 KnownZero |= ~LowBits;
2158 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002159 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2160 break;
2161 }
2162 }
2163
2164 // Since the result is less than or equal to either operand, any leading
2165 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002166 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2167 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002168
2169 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2170 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002171 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002172 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002173 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002174 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002175 case ISD::FrameIndex:
2176 case ISD::TargetFrameIndex:
2177 if (unsigned Align = InferPtrAlignment(Op)) {
2178 // The low bits are known zero if the pointer is aligned.
2179 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2180 return;
2181 }
2182 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002183
Dan Gohmanea859be2007-06-22 14:59:07 +00002184 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002185 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2186 break;
2187 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002188 case ISD::INTRINSIC_WO_CHAIN:
2189 case ISD::INTRINSIC_W_CHAIN:
2190 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002191 // Allow the target to implement this method for its nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002192 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002193 return;
2194 }
2195}
2196
2197/// ComputeNumSignBits - Return the number of times the sign bit of the
2198/// register is replicated into the other bits. We know that at least 1 bit
2199/// is always equal to the sign bit (itself), but other cases can give us
2200/// information. For example, immediately after an "SRA X, 2", we know that
2201/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002202unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlingba54bca2013-06-19 21:36:55 +00002203 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00002204 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002205 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002206 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002207 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002208 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002209
Dan Gohmanea859be2007-06-22 14:59:07 +00002210 if (Depth == 6)
2211 return 1; // Limit search depth.
2212
2213 switch (Op.getOpcode()) {
2214 default: break;
2215 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002216 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002217 return VTBits-Tmp+1;
2218 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002219 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002220 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002221
Dan Gohmanea859be2007-06-22 14:59:07 +00002222 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002223 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002224 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002225 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002226
Dan Gohmanea859be2007-06-22 14:59:07 +00002227 case ISD::SIGN_EXTEND:
Jack Carter3af4d252013-09-09 22:02:08 +00002228 Tmp =
2229 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002230 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Dan Gohmanea859be2007-06-22 14:59:07 +00002232 case ISD::SIGN_EXTEND_INREG:
2233 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002234 Tmp =
2235 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002236 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002237
Dan Gohmanea859be2007-06-22 14:59:07 +00002238 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2239 return std::max(Tmp, Tmp2);
2240
2241 case ISD::SRA:
2242 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2243 // SRA X, C -> adds C sign bits.
2244 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002245 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002246 if (Tmp > VTBits) Tmp = VTBits;
2247 }
2248 return Tmp;
2249 case ISD::SHL:
2250 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2251 // shl destroys sign bits.
2252 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002253 if (C->getZExtValue() >= VTBits || // Bad shift.
2254 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2255 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002256 }
2257 break;
2258 case ISD::AND:
2259 case ISD::OR:
2260 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002261 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002262 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002263 if (Tmp != 1) {
2264 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2265 FirstAnswer = std::min(Tmp, Tmp2);
2266 // We computed what we know about the sign bits as our first
2267 // answer. Now proceed to the generic code that uses
2268 // ComputeMaskedBits, and pick whichever answer is better.
2269 }
2270 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002271
2272 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002273 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002274 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002275 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002276 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002277
2278 case ISD::SADDO:
2279 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002280 case ISD::SSUBO:
2281 case ISD::USUBO:
2282 case ISD::SMULO:
2283 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002284 if (Op.getResNo() != 1)
2285 break;
Duncan Sands03228082008-11-23 15:47:28 +00002286 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002287 case ISD::SETCC:
2288 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002289 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002290 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002291 return VTBits;
2292 break;
2293 case ISD::ROTL:
2294 case ISD::ROTR:
2295 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002296 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002297
Dan Gohmanea859be2007-06-22 14:59:07 +00002298 // Handle rotate right by N like a rotate left by 32-N.
2299 if (Op.getOpcode() == ISD::ROTR)
2300 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2301
2302 // If we aren't rotating out all of the known-in sign bits, return the
2303 // number that are left. This handles rotl(sext(x), 1) for example.
2304 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2305 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2306 }
2307 break;
2308 case ISD::ADD:
2309 // Add can have at most one carry bit. Thus we know that the output
2310 // is, at worst, one more bit than the inputs.
2311 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2312 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002313
Dan Gohmanea859be2007-06-22 14:59:07 +00002314 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002315 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002316 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002317 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002318 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002319
Dan Gohmanea859be2007-06-22 14:59:07 +00002320 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2321 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002322 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002323 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002324
Dan Gohmanea859be2007-06-22 14:59:07 +00002325 // If we are subtracting one from a positive number, there is no carry
2326 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002327 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002328 return Tmp;
2329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002330
Dan Gohmanea859be2007-06-22 14:59:07 +00002331 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2332 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002333 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002334
Dan Gohmanea859be2007-06-22 14:59:07 +00002335 case ISD::SUB:
2336 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2337 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002338
Dan Gohmanea859be2007-06-22 14:59:07 +00002339 // Handle NEG.
2340 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002341 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002342 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002343 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002344 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2345 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002346 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002347 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002348
Dan Gohmanea859be2007-06-22 14:59:07 +00002349 // If the input is known to be positive (the sign bit is known clear),
2350 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002351 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002352 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002353
Dan Gohmanea859be2007-06-22 14:59:07 +00002354 // Otherwise, we treat this like a SUB.
2355 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002356
Dan Gohmanea859be2007-06-22 14:59:07 +00002357 // Sub can have at most one carry bit. Thus we know that the output
2358 // is, at worst, one more bit than the inputs.
2359 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2360 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002361 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002362 case ISD::TRUNCATE:
2363 // FIXME: it's tricky to do anything useful for this, but it is an important
2364 // case for targets like X86.
2365 break;
2366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002367
Nadav Rotem77451752013-03-20 22:53:44 +00002368 // If we are looking at the loaded value of the SDNode.
2369 if (Op.getResNo() == 0) {
2370 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2371 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2372 unsigned ExtType = LD->getExtensionType();
2373 switch (ExtType) {
2374 default: break;
2375 case ISD::SEXTLOAD: // '17' bits known
2376 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2377 return VTBits-Tmp+1;
2378 case ISD::ZEXTLOAD: // '16' bits known
2379 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2380 return VTBits-Tmp;
2381 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002382 }
2383 }
2384
2385 // Allow the target to implement this method for its nodes.
2386 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002387 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002388 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2389 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002390 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002391 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002393
Dan Gohmanea859be2007-06-22 14:59:07 +00002394 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2395 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002396 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002397 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002398
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002399 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002400 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002401 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002402 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002403 Mask = KnownOne;
2404 } else {
2405 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002406 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002407 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002408
Dan Gohmanea859be2007-06-22 14:59:07 +00002409 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2410 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002411 Mask = ~Mask;
2412 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002413 // Return # leading zeros. We use 'min' here in case Val was zero before
2414 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002415 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002416}
2417
Chris Lattner0a9481f2011-02-13 22:25:43 +00002418/// isBaseWithConstantOffset - Return true if the specified operand is an
2419/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2420/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2421/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002422/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002423bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2424 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2425 !isa<ConstantSDNode>(Op.getOperand(1)))
2426 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002427
2428 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002429 !MaskedValueIsZero(Op.getOperand(0),
2430 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2431 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002432
Chris Lattner0a9481f2011-02-13 22:25:43 +00002433 return true;
2434}
2435
2436
Dan Gohman8d44b282009-09-03 20:34:31 +00002437bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2438 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002439 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002440 return true;
2441
2442 // If the value is a constant, we can obviously see if it is a NaN or not.
2443 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2444 return !C->getValueAPF().isNaN();
2445
2446 // TODO: Recognize more cases here.
2447
2448 return false;
2449}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002450
Dan Gohmane8326932010-02-24 06:52:40 +00002451bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2452 // If the value is a constant, we can obviously see if it is a zero or not.
2453 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2454 return !C->isZero();
2455
2456 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002457 switch (Op.getOpcode()) {
2458 default: break;
2459 case ISD::OR:
2460 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2461 return !C->isNullValue();
2462 break;
2463 }
Dan Gohmane8326932010-02-24 06:52:40 +00002464
2465 return false;
2466}
2467
2468bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2469 // Check the obvious case.
2470 if (A == B) return true;
2471
2472 // For for negative and positive zero.
2473 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2474 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2475 if (CA->isZero() && CB->isZero()) return true;
2476
2477 // Otherwise they may not be equal.
2478 return false;
2479}
2480
Chris Lattnerc3aae252005-01-07 07:46:32 +00002481/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002482///
Andrew Trickac6d9be2013-05-25 02:42:55 +00002483SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002484 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002485 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002486 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002487 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002488 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002489
Jack Carter3af4d252013-09-09 22:02:08 +00002490 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2491 DL.getDebugLoc(), getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002492 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002493
Chris Lattner4a283e92006-08-11 18:38:11 +00002494 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002495#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002496 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002497#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002498 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002499}
2500
Andrew Trickac6d9be2013-05-25 02:42:55 +00002501SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002502 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002503 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002504 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002505 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002506 switch (Opcode) {
2507 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002508 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002509 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002510 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002511 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002512 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002513 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002514 case ISD::UINT_TO_FP:
2515 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002516 APFloat apf(EVTToAPFloatSemantics(VT),
2517 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002518 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002519 Opcode==ISD::SINT_TO_FP,
2520 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002521 return getConstantFP(apf, VT);
2522 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002523 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002524 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002525 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002527 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002528 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002529 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002530 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002531 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002532 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002533 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002534 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002535 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002536 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002537 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002538 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002539 }
2540 }
2541
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002542 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002543 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002544 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002545 switch (Opcode) {
2546 case ISD::FNEG:
2547 V.changeSign();
2548 return getConstantFP(V, VT);
2549 case ISD::FABS:
2550 V.clearSign();
2551 return getConstantFP(V, VT);
2552 case ISD::FCEIL: {
2553 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2554 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002555 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002556 break;
2557 }
2558 case ISD::FTRUNC: {
2559 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2560 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002561 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002562 break;
2563 }
2564 case ISD::FFLOOR: {
2565 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2566 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002567 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002568 break;
2569 }
2570 case ISD::FP_EXTEND: {
2571 bool ignored;
2572 // This can return overflow, underflow, or inexact; we don't care.
2573 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002574 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002575 APFloat::rmNearestTiesToEven, &ignored);
2576 return getConstantFP(V, VT);
2577 }
2578 case ISD::FP_TO_SINT:
2579 case ISD::FP_TO_UINT: {
2580 integerPart x[2];
2581 bool ignored;
2582 assert(integerPartWidth >= 64);
2583 // FIXME need to be more flexible about rounding mode.
2584 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2585 Opcode==ISD::FP_TO_SINT,
2586 APFloat::rmTowardZero, &ignored);
2587 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002588 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002589 APInt api(VT.getSizeInBits(), x);
2590 return getConstant(api, VT);
2591 }
2592 case ISD::BITCAST:
2593 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2594 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2595 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2596 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2597 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002598 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002599 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002600
Gabor Greifba36cb52008-08-28 21:40:38 +00002601 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002602 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002603 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002604 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002605 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002606 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002607 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002608 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002609 assert(VT.isFloatingPoint() &&
2610 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002611 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002612 assert((!VT.isVector() ||
2613 VT.getVectorNumElements() ==
2614 Operand.getValueType().getVectorNumElements()) &&
2615 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002616 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002617 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002618 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002619 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002620 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002621 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002622 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002623 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2624 "Invalid sext node, dst < src!");
2625 assert((!VT.isVector() ||
2626 VT.getVectorNumElements() ==
2627 Operand.getValueType().getVectorNumElements()) &&
2628 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002629 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2630 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2631 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002632 // sext(undef) = 0, because the top bits will all be the same.
2633 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002634 break;
2635 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002636 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002637 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002638 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002639 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2640 "Invalid zext node, dst < src!");
2641 assert((!VT.isVector() ||
2642 VT.getVectorNumElements() ==
2643 Operand.getValueType().getVectorNumElements()) &&
2644 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002645 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002646 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002647 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002648 else if (OpOpcode == ISD::UNDEF)
2649 // zext(undef) = 0, because the top bits will be zero.
2650 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002651 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002652 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002653 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002654 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002655 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002656 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2657 "Invalid anyext node, dst < src!");
2658 assert((!VT.isVector() ||
2659 VT.getVectorNumElements() ==
2660 Operand.getValueType().getVectorNumElements()) &&
2661 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002662
Dan Gohman9e86a732010-06-18 00:08:30 +00002663 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2664 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002665 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002666 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002667 else if (OpOpcode == ISD::UNDEF)
2668 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002669
2670 // (ext (trunx x)) -> x
2671 if (OpOpcode == ISD::TRUNCATE) {
2672 SDValue OpOp = Operand.getNode()->getOperand(0);
2673 if (OpOp.getValueType() == VT)
2674 return OpOp;
2675 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002676 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002677 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002678 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002679 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002680 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002681 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2682 "Invalid truncate node, src < dst!");
2683 assert((!VT.isVector() ||
2684 VT.getVectorNumElements() ==
2685 Operand.getValueType().getVectorNumElements()) &&
2686 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002687 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002688 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002689 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2690 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002691 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002692 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2693 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002694 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002695 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002696 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002697 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002698 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002699 if (OpOpcode == ISD::UNDEF)
2700 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002701 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002702 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002703 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002704 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002705 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002706 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002707 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2708 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002709 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002710 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002711 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002712 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002713 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002714 (VT.getVectorElementType() == Operand.getValueType() ||
2715 (VT.getVectorElementType().isInteger() &&
2716 Operand.getValueType().isInteger() &&
2717 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002718 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002719 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002720 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002721 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2722 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2723 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2724 Operand.getConstantOperandVal(1) == 0 &&
2725 Operand.getOperand(0).getValueType() == VT)
2726 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002727 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002728 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002729 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002730 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002731 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002732 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002733 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002734 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002735 break;
2736 case ISD::FABS:
2737 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002738 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002739 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002740 }
2741
Chris Lattner43247a12005-08-25 19:12:10 +00002742 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002743 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002744 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002745 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002747 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002748 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002749 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002750 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002751
Jack Carter3af4d252013-09-09 22:02:08 +00002752 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2753 DL.getDebugLoc(), VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002754 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002755 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00002756 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2757 DL.getDebugLoc(), VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002758 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002759
Chris Lattnerc3aae252005-01-07 07:46:32 +00002760 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002761#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002762 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002763#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002764 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002765}
2766
Benjamin Kramer49693102013-02-04 15:19:18 +00002767SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2768 SDNode *Cst1, SDNode *Cst2) {
2769 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2770 SmallVector<SDValue, 4> Outputs;
2771 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00002772
Benjamin Kramer49693102013-02-04 15:19:18 +00002773 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2774 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
2775 if (Scalar1 && Scalar2) {
2776 // Scalar instruction.
2777 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
2778 } else {
2779 // For vectors extract each constant element into Inputs so we can constant
2780 // fold them individually.
2781 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2782 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2783 if (!BV1 || !BV2)
2784 return SDValue();
2785
2786 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2787
2788 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2789 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2790 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2791 if (!V1 || !V2) // Not a constant, bail.
2792 return SDValue();
2793
2794 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2795 // FIXME: This is valid and could be handled by truncating the APInts.
2796 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2797 return SDValue();
2798
2799 Inputs.push_back(std::make_pair(V1, V2));
2800 }
Bill Wendling688d1c42008-09-24 10:16:24 +00002801 }
2802
Benjamin Kramer49693102013-02-04 15:19:18 +00002803 // We have a number of constant values, constant fold them element by element.
2804 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2805 const APInt &C1 = Inputs[I].first->getAPIntValue();
2806 const APInt &C2 = Inputs[I].second->getAPIntValue();
2807
2808 switch (Opcode) {
2809 case ISD::ADD:
2810 Outputs.push_back(getConstant(C1 + C2, SVT));
2811 break;
2812 case ISD::SUB:
2813 Outputs.push_back(getConstant(C1 - C2, SVT));
2814 break;
2815 case ISD::MUL:
2816 Outputs.push_back(getConstant(C1 * C2, SVT));
2817 break;
2818 case ISD::UDIV:
2819 if (!C2.getBoolValue())
2820 return SDValue();
2821 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2822 break;
2823 case ISD::UREM:
2824 if (!C2.getBoolValue())
2825 return SDValue();
2826 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2827 break;
2828 case ISD::SDIV:
2829 if (!C2.getBoolValue())
2830 return SDValue();
2831 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2832 break;
2833 case ISD::SREM:
2834 if (!C2.getBoolValue())
2835 return SDValue();
2836 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2837 break;
2838 case ISD::AND:
2839 Outputs.push_back(getConstant(C1 & C2, SVT));
2840 break;
2841 case ISD::OR:
2842 Outputs.push_back(getConstant(C1 | C2, SVT));
2843 break;
2844 case ISD::XOR:
2845 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2846 break;
2847 case ISD::SHL:
2848 Outputs.push_back(getConstant(C1 << C2, SVT));
2849 break;
2850 case ISD::SRL:
2851 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2852 break;
2853 case ISD::SRA:
2854 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2855 break;
2856 case ISD::ROTL:
2857 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2858 break;
2859 case ISD::ROTR:
2860 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2861 break;
2862 default:
2863 return SDValue();
2864 }
2865 }
2866
2867 // Handle the scalar case first.
Silviu Baranga02066832013-04-25 09:32:33 +00002868 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00002869 return Outputs.back();
2870
2871 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002872 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer49693102013-02-04 15:19:18 +00002873 Outputs.size());
Bill Wendling688d1c42008-09-24 10:16:24 +00002874}
2875
Andrew Trickac6d9be2013-05-25 02:42:55 +00002876SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer49693102013-02-04 15:19:18 +00002877 SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002878 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2879 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002880 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002881 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002882 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002883 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2884 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002885 // Fold trivial token factors.
2886 if (N1.getOpcode() == ISD::EntryToken) return N2;
2887 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002888 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002889 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002890 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002891 // Concat of UNDEFs is UNDEF.
2892 if (N1.getOpcode() == ISD::UNDEF &&
2893 N2.getOpcode() == ISD::UNDEF)
2894 return getUNDEF(VT);
2895
Dan Gohman7f8613e2008-08-14 20:04:46 +00002896 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2897 // one big BUILD_VECTOR.
2898 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2899 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002900 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2901 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002902 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002903 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002904 }
2905 break;
Chris Lattner76365122005-01-16 02:23:22 +00002906 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002907 assert(VT.isInteger() && "This operator does not apply to FP types!");
2908 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002909 N1.getValueType() == VT && "Binary operator types must match!");
2910 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2911 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002912 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002913 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002914 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2915 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002916 break;
Chris Lattner76365122005-01-16 02:23:22 +00002917 case ISD::OR:
2918 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002919 case ISD::ADD:
2920 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002921 assert(VT.isInteger() && "This operator does not apply to FP types!");
2922 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002923 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002924 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2925 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002926 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002927 return N1;
2928 break;
Chris Lattner76365122005-01-16 02:23:22 +00002929 case ISD::UDIV:
2930 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002931 case ISD::MULHU:
2932 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002933 case ISD::MUL:
2934 case ISD::SDIV:
2935 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002936 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002937 assert(N1.getValueType() == N2.getValueType() &&
2938 N1.getValueType() == VT && "Binary operator types must match!");
2939 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002940 case ISD::FADD:
2941 case ISD::FSUB:
2942 case ISD::FMUL:
2943 case ISD::FDIV:
2944 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002945 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002946 if (Opcode == ISD::FADD) {
2947 // 0+x --> x
2948 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2949 if (CFP->getValueAPF().isZero())
2950 return N2;
2951 // x+0 --> x
2952 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2953 if (CFP->getValueAPF().isZero())
2954 return N1;
2955 } else if (Opcode == ISD::FSUB) {
2956 // x-0 --> x
2957 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2958 if (CFP->getValueAPF().isZero())
2959 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00002960 } else if (Opcode == ISD::FMUL) {
2961 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2962 SDValue V = N2;
2963
2964 // If the first operand isn't the constant, try the second
2965 if (!CFP) {
2966 CFP = dyn_cast<ConstantFPSDNode>(N2);
2967 V = N1;
2968 }
2969
2970 if (CFP) {
2971 // 0*x --> 0
2972 if (CFP->isZero())
2973 return SDValue(CFP,0);
2974 // 1*x --> x
2975 if (CFP->isExactlyValue(1.0))
2976 return V;
2977 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00002978 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002979 }
Dale Johannesen78995512010-05-15 18:38:02 +00002980 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002981 assert(N1.getValueType() == N2.getValueType() &&
2982 N1.getValueType() == VT && "Binary operator types must match!");
2983 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002984 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2985 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002986 N1.getValueType().isFloatingPoint() &&
2987 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002988 "Invalid FCOPYSIGN!");
2989 break;
Chris Lattner76365122005-01-16 02:23:22 +00002990 case ISD::SHL:
2991 case ISD::SRA:
2992 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002993 case ISD::ROTL:
2994 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002995 assert(VT == N1.getValueType() &&
2996 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002997 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002998 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00002999 assert((!VT.isVector() || VT == N2.getValueType()) &&
3000 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00003001 // Verify that the shift amount VT is bit enough to hold valid shift
3002 // amounts. This catches things like trying to shift an i1024 value by an
3003 // i8, which is easy to fall into in generic code that uses
3004 // TLI.getShiftAmount().
3005 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00003006 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00003007 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00003008
3009 // Always fold shifts of i1 values so the code generator doesn't need to
3010 // handle them. Since we know the size of the shift has to be less than the
3011 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00003012 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00003013 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00003014 if (N2C && N2C->isNullValue())
3015 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00003016 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00003017 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003018 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003019 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003020 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003021 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00003022 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003023 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003024 "type is vector!");
3025 assert((!EVT.isVector() ||
3026 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3027 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003028 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00003029 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003030 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00003031 break;
3032 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003033 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003034 assert(VT.isFloatingPoint() &&
3035 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003036 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00003037 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003038 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00003039 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00003040 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003041 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00003042 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003043 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003044 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003045 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00003046 assert(!EVT.isVector() &&
3047 "AssertSExt/AssertZExt type should be the vector element type "
3048 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003049 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00003050 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003051 break;
3052 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003053 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003054 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003055 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003056 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003057 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00003058 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003059 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003060 "type is vector!");
3061 assert((!EVT.isVector() ||
3062 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3063 "Vector element counts must match in SIGN_EXTEND_INREG");
3064 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003065 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00003066
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003067 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00003068 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003069 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00003070 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00003071 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00003072 return getConstant(Val, VT);
3073 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003074 break;
3075 }
3076 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00003077 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3078 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00003079 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003080
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003081 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3082 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003083 if (N2C &&
3084 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003085 N1.getNumOperands() > 0) {
3086 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003087 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003088 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003089 N1.getOperand(N2C->getZExtValue() / Factor),
3090 getConstant(N2C->getZExtValue() % Factor,
3091 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003092 }
3093
3094 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3095 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003096 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3097 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003098
3099 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003100 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003101 // are promoted and implicitly truncated, and the result implicitly
3102 // extended. Make that explicit here.
3103 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003104
Bob Wilsonb1303d02009-04-13 22:05:19 +00003105 return Elt;
3106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003107
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003108 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3109 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003110 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003111 // If the indices are the same, return the inserted element else
3112 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003113 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003114 SDValue N1Op2 = N1.getOperand(2);
3115 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3116
3117 if (N1Op2C && N2C) {
3118 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3119 if (VT == N1.getOperand(1).getValueType())
3120 return N1.getOperand(1);
3121 else
3122 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3123 }
3124
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003125 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003126 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003127 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003128 break;
3129 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003130 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003131 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3132 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003133 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003134 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003135
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003136 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3137 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003138 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003139 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003140 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003141
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003142 // EXTRACT_ELEMENT of a constant int is also very common.
3143 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003144 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003145 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003146 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3147 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003148 }
3149 break;
David Greene91585092011-01-26 15:38:49 +00003150 case ISD::EXTRACT_SUBVECTOR: {
3151 SDValue Index = N2;
3152 if (VT.isSimple() && N1.getValueType().isSimple()) {
3153 assert(VT.isVector() && N1.getValueType().isVector() &&
3154 "Extract subvector VTs must be a vectors!");
Jack Carter3af4d252013-09-09 22:02:08 +00003155 assert(VT.getVectorElementType() ==
3156 N1.getValueType().getVectorElementType() &&
David Greene91585092011-01-26 15:38:49 +00003157 "Extract subvector VTs must have the same element type!");
Craig Topper0ff11902013-08-15 02:44:19 +00003158 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greene91585092011-01-26 15:38:49 +00003159 "Extract subvector must be from larger vector to smaller vector!");
3160
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003161 if (isa<ConstantSDNode>(Index.getNode())) {
3162 assert((VT.getVectorNumElements() +
3163 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003164 <= N1.getValueType().getVectorNumElements())
3165 && "Extract subvector overflow!");
3166 }
3167
3168 // Trivial extraction.
Craig Topper0ff11902013-08-15 02:44:19 +00003169 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greene91585092011-01-26 15:38:49 +00003170 return N1;
3171 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003172 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003173 }
David Greene91585092011-01-26 15:38:49 +00003174 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003175
Benjamin Kramer49693102013-02-04 15:19:18 +00003176 // Perform trivial constant folding.
3177 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3178 if (SV.getNode()) return SV;
3179
3180 // Canonicalize constant to RHS if commutative.
3181 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3182 std::swap(N1C, N2C);
3183 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003184 }
3185
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003186 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003187 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3188 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003189 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003190 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003191 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003192 std::swap(N1CFP, N2CFP);
3193 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003194 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003195 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3196 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003197 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003198 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003199 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003200 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003201 return getConstantFP(V1, VT);
3202 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003203 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003204 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3205 if (s!=APFloat::opInvalidOp)
3206 return getConstantFP(V1, VT);
3207 break;
3208 case ISD::FMUL:
3209 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3210 if (s!=APFloat::opInvalidOp)
3211 return getConstantFP(V1, VT);
3212 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003213 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003214 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3215 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3216 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003217 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003218 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003219 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3220 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3221 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003222 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003223 case ISD::FCOPYSIGN:
3224 V1.copySign(V2);
3225 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003226 default: break;
3227 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003228 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003229
3230 if (Opcode == ISD::FP_ROUND) {
3231 APFloat V = N1CFP->getValueAPF(); // make copy
3232 bool ignored;
3233 // This can return overflow, underflow, or inexact; we don't care.
3234 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003235 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003236 APFloat::rmNearestTiesToEven, &ignored);
3237 return getConstantFP(V, VT);
3238 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003239 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003240
Chris Lattner62b57722006-04-20 05:39:12 +00003241 // Canonicalize an UNDEF to the RHS, even over a constant.
3242 if (N1.getOpcode() == ISD::UNDEF) {
3243 if (isCommutativeBinOp(Opcode)) {
3244 std::swap(N1, N2);
3245 } else {
3246 switch (Opcode) {
3247 case ISD::FP_ROUND_INREG:
3248 case ISD::SIGN_EXTEND_INREG:
3249 case ISD::SUB:
3250 case ISD::FSUB:
3251 case ISD::FDIV:
3252 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003253 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003254 return N1; // fold op(undef, arg2) -> undef
3255 case ISD::UDIV:
3256 case ISD::SDIV:
3257 case ISD::UREM:
3258 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003259 case ISD::SRL:
3260 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003261 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003262 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3263 // For vectors, we can't easily build an all zero vector, just return
3264 // the LHS.
3265 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003266 }
3267 }
3268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003269
3270 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003271 if (N2.getOpcode() == ISD::UNDEF) {
3272 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003273 case ISD::XOR:
3274 if (N1.getOpcode() == ISD::UNDEF)
3275 // Handle undef ^ undef -> 0 special case. This is a common
3276 // idiom (misuse).
3277 return getConstant(0, VT);
3278 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003279 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003280 case ISD::ADDC:
3281 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003282 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003283 case ISD::UDIV:
3284 case ISD::SDIV:
3285 case ISD::UREM:
3286 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003287 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003288 case ISD::FADD:
3289 case ISD::FSUB:
3290 case ISD::FMUL:
3291 case ISD::FDIV:
3292 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003293 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003294 return N2;
3295 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003296 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003297 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003298 case ISD::SRL:
3299 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003300 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003301 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3302 // For vectors, we can't easily build an all zero vector, just return
3303 // the LHS.
3304 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003305 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003306 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003307 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003308 // For vectors, we can't easily build an all one vector, just return
3309 // the LHS.
3310 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003311 case ISD::SRA:
3312 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003313 }
3314 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003315
Chris Lattner27e9b412005-05-11 18:57:39 +00003316 // Memoize this node if possible.
3317 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003318 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003319 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003320 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003321 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003322 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003323 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003324 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003325 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003326
Jack Carter3af4d252013-09-09 22:02:08 +00003327 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3328 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003329 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003330 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003331 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3332 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003333 }
3334
Chris Lattnerc3aae252005-01-07 07:46:32 +00003335 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003336#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003337 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003338#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003339 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003340}
3341
Andrew Trickac6d9be2013-05-25 02:42:55 +00003342SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003343 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003344 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003345 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003346 switch (Opcode) {
Owen Anderson58dcd202013-05-09 22:27:13 +00003347 case ISD::FMA: {
3348 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3349 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3350 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3351 if (N1CFP && N2CFP && N3CFP) {
3352 APFloat V1 = N1CFP->getValueAPF();
3353 const APFloat &V2 = N2CFP->getValueAPF();
3354 const APFloat &V3 = N3CFP->getValueAPF();
3355 APFloat::opStatus s =
3356 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3357 if (s != APFloat::opInvalidOp)
3358 return getConstantFP(V1, VT);
3359 }
3360 break;
3361 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00003362 case ISD::CONCAT_VECTORS:
3363 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3364 // one big BUILD_VECTOR.
3365 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3366 N2.getOpcode() == ISD::BUILD_VECTOR &&
3367 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003368 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3369 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003370 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3371 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003372 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003373 }
3374 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003375 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003376 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003377 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003378 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003379 break;
3380 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003381 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003382 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003383 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003384 return N2; // select true, X, Y -> X
3385 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003386 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003387
3388 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003389 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003390 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003391 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003392 case ISD::INSERT_SUBVECTOR: {
3393 SDValue Index = N3;
3394 if (VT.isSimple() && N1.getValueType().isSimple()
3395 && N2.getValueType().isSimple()) {
3396 assert(VT.isVector() && N1.getValueType().isVector() &&
3397 N2.getValueType().isVector() &&
3398 "Insert subvector VTs must be a vectors");
3399 assert(VT == N1.getValueType() &&
3400 "Dest and insert subvector source types must match!");
Craig Topper0ff11902013-08-15 02:44:19 +00003401 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenecfe33c42011-01-26 19:13:22 +00003402 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003403 if (isa<ConstantSDNode>(Index.getNode())) {
3404 assert((N2.getValueType().getVectorNumElements() +
3405 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003406 <= VT.getVectorNumElements())
3407 && "Insert subvector overflow!");
3408 }
3409
3410 // Trivial insertion.
Craig Topper0ff11902013-08-15 02:44:19 +00003411 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenecfe33c42011-01-26 19:13:22 +00003412 return N2;
3413 }
3414 break;
3415 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003416 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003417 // Fold bit_convert nodes from a type to themselves.
3418 if (N1.getValueType() == VT)
3419 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003420 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003421 }
3422
Chris Lattner43247a12005-08-25 19:12:10 +00003423 // Memoize node if it doesn't produce a flag.
3424 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003425 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003426 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003427 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003428 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003429 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003430 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003431 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003432 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003433
Jack Carter3af4d252013-09-09 22:02:08 +00003434 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3435 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003436 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003437 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003438 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3439 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003440 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003441
Chris Lattnerc3aae252005-01-07 07:46:32 +00003442 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003443#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003444 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003445#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003446 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003447}
3448
Andrew Trickac6d9be2013-05-25 02:42:55 +00003449SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003450 SDValue N1, SDValue N2, SDValue N3,
3451 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003452 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003453 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003454}
3455
Andrew Trickac6d9be2013-05-25 02:42:55 +00003456SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003457 SDValue N1, SDValue N2, SDValue N3,
3458 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003460 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003461}
3462
Dan Gohman98ca4f22009-08-05 01:29:28 +00003463/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3464/// the incoming stack arguments to be loaded from the stack.
3465SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3466 SmallVector<SDValue, 8> ArgChains;
3467
3468 // Include the original chain at the beginning of the list. When this is
3469 // used by target LowerCall hooks, this helps legalize find the
3470 // CALLSEQ_BEGIN node.
3471 ArgChains.push_back(Chain);
3472
3473 // Add a chain value for each stack argument.
3474 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3475 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3476 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3477 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3478 if (FI->getIndex() < 0)
3479 ArgChains.push_back(SDValue(L, 1));
3480
3481 // Build a tokenfactor for all the chains.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003482 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003483 &ArgChains[0], ArgChains.size());
3484}
3485
Dan Gohman707e0182008-04-12 04:36:06 +00003486/// getMemsetValue - Vectorized representation of the memset value
3487/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003488static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003489 SDLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003490 assert(Value.getOpcode() != ISD::UNDEF);
3491
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003492 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003493 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003494 assert(C->getAPIntValue().getBitWidth() == 8);
3495 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003496 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003497 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003498 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003499 }
Evan Chengf0df0312008-05-15 08:39:06 +00003500
Dale Johannesen0f502f62009-02-03 22:26:09 +00003501 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003502 if (NumBits > 8) {
3503 // Use a multiplication with 0x010101... to extend the input to the
3504 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003505 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003506 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003507 }
3508
3509 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003510}
3511
Dan Gohman707e0182008-04-12 04:36:06 +00003512/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3513/// used when a memcpy is turned into a memset when the source is a constant
3514/// string ptr.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003515static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003516 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003517 // Handle vector with all elements zero.
3518 if (Str.empty()) {
3519 if (VT.isInteger())
3520 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003521 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003522 return DAG.getConstantFP(0.0, VT);
3523 else if (VT.isVector()) {
3524 unsigned NumElts = VT.getVectorNumElements();
3525 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003526 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003527 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3528 EltVT, NumElts)));
3529 } else
3530 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003531 }
3532
Duncan Sands83ec4b62008-06-06 12:08:01 +00003533 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003534 unsigned NumVTBits = VT.getSizeInBits();
3535 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003536 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3537
Evan Cheng4ff23d02013-01-10 22:13:27 +00003538 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003539 if (TLI.isLittleEndian()) {
3540 for (unsigned i = 0; i != NumBytes; ++i)
3541 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3542 } else {
3543 for (unsigned i = 0; i != NumBytes; ++i)
3544 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003545 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003546
Evan Chenge07f85e2012-12-11 23:26:14 +00003547 // If the "cost" of materializing the integer immediate is 1 or free, then
3548 // it is cost effective to turn the load into the immediate.
Chandler Carruthe4b4edd2013-01-05 12:32:17 +00003549 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
3550 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng376642e2012-12-10 23:21:26 +00003551 return DAG.getConstant(Val, VT);
3552 return SDValue(0, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003553}
3554
Scott Michelfdc40a02009-02-17 22:15:04 +00003555/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003556///
Andrew Trickdd0fb012013-05-25 03:08:10 +00003557static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman707e0182008-04-12 04:36:06 +00003558 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003559 EVT VT = Base.getValueType();
Andrew Trickdd0fb012013-05-25 03:08:10 +00003560 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003561 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003562}
3563
Evan Chengf0df0312008-05-15 08:39:06 +00003564/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3565///
Chris Lattner18c7f802012-02-05 02:29:43 +00003566static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003567 unsigned SrcDelta = 0;
3568 GlobalAddressSDNode *G = NULL;
3569 if (Src.getOpcode() == ISD::GlobalAddress)
3570 G = cast<GlobalAddressSDNode>(Src);
3571 else if (Src.getOpcode() == ISD::ADD &&
3572 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3573 Src.getOperand(1).getOpcode() == ISD::Constant) {
3574 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003575 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003576 }
3577 if (!G)
3578 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003579
Chris Lattner18c7f802012-02-05 02:29:43 +00003580 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003581}
Dan Gohman707e0182008-04-12 04:36:06 +00003582
Evan Cheng255f20f2010-04-01 06:04:33 +00003583/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3584/// to replace the memset / memcpy. Return true if the number of memory ops
3585/// is below the threshold. It returns the types of the sequence of
3586/// memory ops to perform memset / memcpy by reference.
3587static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003588 unsigned Limit, uint64_t Size,
3589 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003590 bool IsMemset,
3591 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003592 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003593 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003594 SelectionDAG &DAG,
3595 const TargetLowering &TLI) {
3596 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3597 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003598 // If 'SrcAlign' is zero, that means the memory operation does not need to
3599 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3600 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3601 // is the specified alignment of the memory operation. If it is zero, that
3602 // means it's possible to change the alignment of the destination.
3603 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3604 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003605 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003606 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003607 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003608
Chris Lattneracee6472010-03-07 07:45:08 +00003609 if (VT == MVT::Other) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +00003610 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003611 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003612 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003613 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003614 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003615 case 0: VT = MVT::i64; break;
3616 case 4: VT = MVT::i32; break;
3617 case 2: VT = MVT::i16; break;
3618 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003619 }
3620 }
3621
Owen Anderson766b5ef2009-08-11 21:59:30 +00003622 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003623 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003624 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003625 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003626
Duncan Sands8e4eb092008-06-08 20:54:56 +00003627 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003628 VT = LVT;
3629 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003630
Dan Gohman707e0182008-04-12 04:36:06 +00003631 unsigned NumMemOps = 0;
3632 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003633 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003634 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003635 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003636 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003637 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003638
3639 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003640 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003641 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003642 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003643 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003644 Found = true;
3645 else if (NewVT == MVT::i64 &&
3646 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003647 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003648 // i64 is usually not legal on 32-bit targets, but f64 may be.
3649 NewVT = MVT::f64;
3650 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003651 }
Evan Cheng376642e2012-12-10 23:21:26 +00003652 }
3653
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003654 if (!Found) {
3655 do {
3656 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3657 if (NewVT == MVT::i8)
3658 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003659 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003660 }
3661 NewVTSize = NewVT.getSizeInBits() / 8;
3662
Evan Cheng376642e2012-12-10 23:21:26 +00003663 // If the new VT cannot cover all of the remaining bits, then consider
3664 // issuing a (or a pair of) unaligned and overlapping load / store.
3665 // FIXME: Only does this for 64-bit or more since we don't have proper
3666 // cost model for unaligned load / store.
3667 bool Fast;
Evan Chenga16e49d2012-12-12 20:43:23 +00003668 if (NumMemOps && AllowOverlap &&
3669 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng376642e2012-12-10 23:21:26 +00003670 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3671 VTSize = Size;
3672 else {
3673 VT = NewVT;
3674 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003675 }
Dan Gohman707e0182008-04-12 04:36:06 +00003676 }
Dan Gohman707e0182008-04-12 04:36:06 +00003677
Evan Chenga16e49d2012-12-12 20:43:23 +00003678 if (++NumMemOps > Limit)
3679 return false;
3680
Dan Gohman707e0182008-04-12 04:36:06 +00003681 MemOps.push_back(VT);
3682 Size -= VTSize;
3683 }
3684
3685 return true;
3686}
3687
Andrew Trickac6d9be2013-05-25 02:42:55 +00003688static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003689 SDValue Chain, SDValue Dst,
3690 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003691 unsigned Align, bool isVol,
3692 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003693 MachinePointerInfo DstPtrInfo,
3694 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003695 // Turn a memcpy of undef to nop.
3696 if (Src.getOpcode() == ISD::UNDEF)
3697 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003698
Dan Gohman21323f32008-05-29 19:42:22 +00003699 // Expand memcpy to a series of load and store ops if the size operand falls
3700 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003701 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3702 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003703 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003704 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003705 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003706 MachineFunction &MF = DAG.getMachineFunction();
3707 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003708 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003709 MF.getFunction()->getAttributes().
3710 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003711 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3712 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3713 DstAlignCanChange = true;
3714 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3715 if (Align > SrcAlign)
3716 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003717 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003718 bool CopyFromStr = isMemSrcFromString(Src, Str);
3719 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003720 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003721
Evan Cheng94107ba2010-04-01 18:19:11 +00003722 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003723 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003724 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003725 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003726 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003727
Evan Cheng255f20f2010-04-01 06:04:33 +00003728 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003729 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003730 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00003731
3732 // Don't promote to an alignment that would require dynamic stack
Stephen Lin155615d2013-07-08 00:37:03 +00003733 // realignment.
Lang Hames2d95e432013-01-31 20:23:43 +00003734 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3735 if (!TRI->needsStackRealignment(MF))
3736 while (NewAlign > Align &&
3737 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3738 NewAlign /= 2;
3739
Evan Cheng255f20f2010-04-01 06:04:33 +00003740 if (NewAlign > Align) {
3741 // Give the stack frame object a larger alignment if needed.
3742 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3743 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3744 Align = NewAlign;
3745 }
3746 }
Dan Gohman707e0182008-04-12 04:36:06 +00003747
Dan Gohman475871a2008-07-27 21:46:04 +00003748 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003749 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003750 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003751 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003752 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003753 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003754 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003755
Evan Cheng376642e2012-12-10 23:21:26 +00003756 if (VTSize > Size) {
3757 // Issuing an unaligned load / store pair that overlaps with the previous
3758 // pair. Adjust the offset accordingly.
3759 assert(i == NumMemOps-1 && i != 0);
3760 SrcOff -= VTSize - Size;
3761 DstOff -= VTSize - Size;
3762 }
3763
Evan Cheng255f20f2010-04-01 06:04:33 +00003764 if (CopyFromStr &&
3765 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003766 // It's unlikely a store of a vector immediate can be done in a single
3767 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003768 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003769 // FIXME: Handle other cases where store of vector immediate is done in
3770 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003771 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003772 if (Value.getNode())
3773 Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003774 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng376642e2012-12-10 23:21:26 +00003775 DstPtrInfo.getWithOffset(DstOff), isVol,
3776 false, Align);
3777 }
3778
3779 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003780 // The type might not be legal for the target. This should only happen
3781 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003782 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3783 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003784 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003785 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003786 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003787 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003788 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003789 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003790 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003791 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003792 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003793 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3794 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003795 }
3796 OutChains.push_back(Store);
3797 SrcOff += VTSize;
3798 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003799 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003800 }
3801
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003803 &OutChains[0], OutChains.size());
3804}
3805
Andrew Trickac6d9be2013-05-25 02:42:55 +00003806static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003807 SDValue Chain, SDValue Dst,
3808 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003809 unsigned Align, bool isVol,
3810 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003811 MachinePointerInfo DstPtrInfo,
3812 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003813 // Turn a memmove of undef to nop.
3814 if (Src.getOpcode() == ISD::UNDEF)
3815 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003816
3817 // Expand memmove to a series of load and store ops if the size operand falls
3818 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003819 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003820 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003821 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003822 MachineFunction &MF = DAG.getMachineFunction();
3823 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003824 bool OptSize = MF.getFunction()->getAttributes().
3825 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003826 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3827 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3828 DstAlignCanChange = true;
3829 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3830 if (Align > SrcAlign)
3831 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003832 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003833
Evan Cheng94107ba2010-04-01 18:19:11 +00003834 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00003835 (DstAlignCanChange ? 0 : Align), SrcAlign,
3836 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003837 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003838
Evan Cheng255f20f2010-04-01 06:04:33 +00003839 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003840 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003841 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003842 if (NewAlign > Align) {
3843 // Give the stack frame object a larger alignment if needed.
3844 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3845 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3846 Align = NewAlign;
3847 }
3848 }
Dan Gohman21323f32008-05-29 19:42:22 +00003849
Evan Cheng255f20f2010-04-01 06:04:33 +00003850 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003851 SmallVector<SDValue, 8> LoadValues;
3852 SmallVector<SDValue, 8> LoadChains;
3853 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003854 unsigned NumMemOps = MemOps.size();
3855 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003856 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003857 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003858 SDValue Value;
Dan Gohman21323f32008-05-29 19:42:22 +00003859
Dale Johannesen0f502f62009-02-03 22:26:09 +00003860 Value = DAG.getLoad(VT, dl, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003861 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003862 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003863 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003864 LoadValues.push_back(Value);
3865 LoadChains.push_back(Value.getValue(1));
3866 SrcOff += VTSize;
3867 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003869 &LoadChains[0], LoadChains.size());
3870 OutChains.clear();
3871 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003872 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003873 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003874 SDValue Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003875
Dale Johannesen0f502f62009-02-03 22:26:09 +00003876 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Trickdd0fb012013-05-25 03:08:10 +00003877 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003878 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003879 OutChains.push_back(Store);
3880 DstOff += VTSize;
3881 }
3882
Owen Anderson825b72b2009-08-11 20:47:22 +00003883 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003884 &OutChains[0], OutChains.size());
3885}
3886
Serge Pavlov496f0242013-09-17 16:24:42 +00003887/// \brief Lower the call to 'memset' intrinsic function into a series of store
3888/// operations.
3889///
3890/// \param DAG Selection DAG where lowered code is placed.
3891/// \param dl Link to corresponding IR location.
3892/// \param Chain Control flow dependency.
3893/// \param Dst Pointer to destination memory location.
3894/// \param Src Value of byte to write into the memory.
3895/// \param Size Number of bytes to write.
3896/// \param Align Alignment of the destination in bytes.
3897/// \param isVol True if destination is volatile.
3898/// \param DstPtrInfo IR information on the memory pointer.
3899/// \returns New head in the control flow, if lowering was successful, empty
3900/// SDValue otherwise.
3901///
3902/// The function tries to replace 'llvm.memset' intrinsic with several store
3903/// operations and value calculation code. This is usually profitable for small
3904/// memory size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003905static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003906 SDValue Chain, SDValue Dst,
3907 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003908 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003909 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003910 // Turn a memset of undef to nop.
3911 if (Src.getOpcode() == ISD::UNDEF)
3912 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003913
3914 // Expand memset to a series of load/store ops if the size operand
3915 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003916 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003917 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003918 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003919 MachineFunction &MF = DAG.getMachineFunction();
3920 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003921 bool OptSize = MF.getFunction()->getAttributes().
3922 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003923 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3924 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3925 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003926 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003927 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003928 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003929 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00003930 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003931 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003932
Evan Cheng255f20f2010-04-01 06:04:33 +00003933 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003934 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003935 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003936 if (NewAlign > Align) {
3937 // Give the stack frame object a larger alignment if needed.
3938 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3939 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3940 Align = NewAlign;
3941 }
3942 }
3943
Dan Gohman475871a2008-07-27 21:46:04 +00003944 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003945 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003946 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003947
3948 // Find the largest store and generate the bit pattern for it.
3949 EVT LargestVT = MemOps[0];
3950 for (unsigned i = 1; i < NumMemOps; i++)
3951 if (MemOps[i].bitsGT(LargestVT))
3952 LargestVT = MemOps[i];
3953 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3954
Dan Gohman707e0182008-04-12 04:36:06 +00003955 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003956 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00003957 unsigned VTSize = VT.getSizeInBits() / 8;
3958 if (VTSize > Size) {
3959 // Issuing an unaligned load / store pair that overlaps with the previous
3960 // pair. Adjust the offset accordingly.
3961 assert(i == NumMemOps-1 && i != 0);
3962 DstOff -= VTSize - Size;
3963 }
Benjamin Kramer80220362011-01-02 19:57:05 +00003964
3965 // If this store is smaller than the largest store see whether we can get
3966 // the smaller value for free with a truncate.
3967 SDValue Value = MemSetValue;
3968 if (VT.bitsLT(LargestVT)) {
3969 if (!LargestVT.isVector() && !VT.isVector() &&
3970 TLI.isTruncateFree(LargestVT, VT))
3971 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3972 else
3973 Value = getMemsetValue(Src, VT, DAG, dl);
3974 }
3975 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003976 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003977 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003978 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003979 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003980 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003981 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00003982 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003983 }
3984
Owen Anderson825b72b2009-08-11 20:47:22 +00003985 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003986 &OutChains[0], OutChains.size());
3987}
3988
Andrew Trickac6d9be2013-05-25 02:42:55 +00003989SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003990 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003991 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003992 MachinePointerInfo DstPtrInfo,
3993 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00003994 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00003995
3996 // Check to see if we should lower the memcpy to loads and stores first.
3997 // For cases within the target-specified limits, this is the best choice.
3998 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3999 if (ConstantSize) {
4000 // Memcpy with size zero? Just return the original chain.
4001 if (ConstantSize->isNullValue())
4002 return Chain;
4003
Evan Cheng255f20f2010-04-01 06:04:33 +00004004 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4005 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00004006 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004007 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004008 return Result;
4009 }
4010
4011 // Then check to see if we should lower the memcpy with target-specific
4012 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004013 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004014 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004015 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004016 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004017 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004018 return Result;
4019
4020 // If we really need inline code and the target declined to provide it,
4021 // use a (potentially long) sequence of loads and stores.
4022 if (AlwaysInline) {
4023 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004024 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004025 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004026 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00004027 }
4028
Dan Gohman7b55d362010-04-05 20:24:08 +00004029 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4030 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4031 // respect volatile, so they may do things like read or write memory
4032 // beyond the given memory regions. But fixing this isn't easy, and most
4033 // people don't care.
4034
Bill Wendlingba54bca2013-06-19 21:36:55 +00004035 const TargetLowering *TLI = TM.getTargetLowering();
4036
Dan Gohman707e0182008-04-12 04:36:06 +00004037 // Emit a library call.
4038 TargetLowering::ArgListTy Args;
4039 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004040 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004041 Entry.Node = Dst; Args.push_back(Entry);
4042 Entry.Node = Src; Args.push_back(Entry);
4043 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004044 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004045 TargetLowering::
4046 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004047 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004048 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004049 /*isTailCall=*/false,
4050 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004051 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4052 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004053 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004054 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004055
Dan Gohman707e0182008-04-12 04:36:06 +00004056 return CallResult.second;
4057}
4058
Andrew Trickac6d9be2013-05-25 02:42:55 +00004059SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004060 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004061 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004062 MachinePointerInfo DstPtrInfo,
4063 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004064 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004065
Dan Gohman21323f32008-05-29 19:42:22 +00004066 // Check to see if we should lower the memmove to loads and stores first.
4067 // For cases within the target-specified limits, this is the best choice.
4068 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4069 if (ConstantSize) {
4070 // Memmove with size zero? Just return the original chain.
4071 if (ConstantSize->isNullValue())
4072 return Chain;
4073
Dan Gohman475871a2008-07-27 21:46:04 +00004074 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00004075 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004076 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004077 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004078 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00004079 return Result;
4080 }
Dan Gohman707e0182008-04-12 04:36:06 +00004081
4082 // Then check to see if we should lower the memmove with target-specific
4083 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004084 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004085 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004086 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004087 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004088 return Result;
4089
Mon P Wang01f0e852010-04-06 08:27:51 +00004090 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4091 // not be safe. See memcpy above for more details.
4092
Bill Wendlingba54bca2013-06-19 21:36:55 +00004093 const TargetLowering *TLI = TM.getTargetLowering();
4094
Dan Gohman707e0182008-04-12 04:36:06 +00004095 // Emit a library call.
4096 TargetLowering::ArgListTy Args;
4097 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004098 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004099 Entry.Node = Dst; Args.push_back(Entry);
4100 Entry.Node = Src; Args.push_back(Entry);
4101 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004102 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004103 TargetLowering::
4104 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004105 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004106 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004107 /*isTailCall=*/false,
4108 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004109 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4110 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004111 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004112 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004113
Dan Gohman707e0182008-04-12 04:36:06 +00004114 return CallResult.second;
4115}
4116
Andrew Trickac6d9be2013-05-25 02:42:55 +00004117SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004118 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004119 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004120 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004121 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004122
4123 // Check to see if we should lower the memset to stores first.
4124 // For cases within the target-specified limits, this is the best choice.
4125 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4126 if (ConstantSize) {
4127 // Memset with size zero? Just return the original chain.
4128 if (ConstantSize->isNullValue())
4129 return Chain;
4130
Mon P Wang20adc9d2010-04-04 03:10:48 +00004131 SDValue Result =
4132 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004133 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004134
Gabor Greifba36cb52008-08-28 21:40:38 +00004135 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004136 return Result;
4137 }
4138
4139 // Then check to see if we should lower the memset with target-specific
4140 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004141 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004142 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004143 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004144 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004145 return Result;
4146
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004147 // Emit a library call.
Bill Wendlingba54bca2013-06-19 21:36:55 +00004148 const TargetLowering *TLI = TM.getTargetLowering();
4149 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004150 TargetLowering::ArgListTy Args;
4151 TargetLowering::ArgListEntry Entry;
4152 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4153 Args.push_back(Entry);
4154 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00004155 if (Src.getValueType().bitsGT(MVT::i32))
4156 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00004157 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004158 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00004159 Entry.Node = Src;
4160 Entry.Ty = Type::getInt32Ty(*getContext());
4161 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00004162 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004163 Entry.Node = Size;
4164 Entry.Ty = IntPtrTy;
4165 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00004166 Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004167 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004168 TargetLowering::
4169 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004170 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004171 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004172 /*isTailCall=*/false,
4173 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004174 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4175 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004176 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004177 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004178
Dan Gohman707e0182008-04-12 04:36:06 +00004179 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004180}
4181
Andrew Trickac6d9be2013-05-25 02:42:55 +00004182SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Amara Emerson268c7432013-09-26 12:22:36 +00004183 SDVTList VTList, SDValue* Ops, unsigned NumOps,
4184 MachineMemOperand *MMO,
4185 AtomicOrdering Ordering,
4186 SynchronizationScope SynchScope) {
4187 FoldingSetNodeID ID;
4188 ID.AddInteger(MemVT.getRawBits());
4189 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4190 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4191 void* IP = 0;
4192 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4193 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4194 return SDValue(E, 0);
4195 }
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004196
4197 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4198 // SDNode doesn't have access to it. This memory will be "leaked" when
4199 // the node is deallocated, but recovered when the allocator is released.
4200 // If the number of operands is less than 5 we use AtomicSDNode's internal
4201 // storage.
4202 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : 0;
4203
Amara Emerson268c7432013-09-26 12:22:36 +00004204 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4205 dl.getDebugLoc(), VTList, MemVT,
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004206 Ops, DynOps, NumOps, MMO,
4207 Ordering, SynchScope);
Amara Emerson268c7432013-09-26 12:22:36 +00004208 CSEMap.InsertNode(N, IP);
4209 AllNodes.push_back(N);
4210 return SDValue(N, 0);
4211}
4212
4213SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00004214 SDValue Chain, SDValue Ptr, SDValue Cmp,
4215 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00004216 unsigned Alignment,
4217 AtomicOrdering Ordering,
Michael Ilseman06b96902012-09-10 16:56:31 +00004218 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004219 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4220 Alignment = getEVTAlignment(MemVT);
4221
Evan Chengff89dcb2009-10-18 18:16:27 +00004222 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004223
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004224 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004225 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004226 // FIXME: Volatile isn't really correct; we should keep track of atomic
4227 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004228 unsigned Flags = MachineMemOperand::MOVolatile;
4229 if (Opcode != ISD::ATOMIC_STORE)
4230 Flags |= MachineMemOperand::MOLoad;
4231 if (Opcode != ISD::ATOMIC_LOAD)
4232 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004233
4234 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004235 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004236
Eli Friedman55ba8162011-07-29 03:05:32 +00004237 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4238 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004239}
4240
Andrew Trickac6d9be2013-05-25 02:42:55 +00004241SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004242 SDValue Chain,
4243 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00004244 SDValue Swp, MachineMemOperand *MMO,
4245 AtomicOrdering Ordering,
4246 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004247 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4248 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4249
Owen Andersone50ed302009-08-10 22:56:29 +00004250 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004251
Owen Anderson825b72b2009-08-11 20:47:22 +00004252 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004253 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Amara Emerson268c7432013-09-26 12:22:36 +00004254 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004255}
4256
Andrew Trickac6d9be2013-05-25 02:42:55 +00004257SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004258 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004259 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004260 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004261 unsigned Alignment,
4262 AtomicOrdering Ordering,
4263 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004264 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4265 Alignment = getEVTAlignment(MemVT);
4266
Evan Chengff89dcb2009-10-18 18:16:27 +00004267 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004268 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004269 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004270 // For now, atomics are considered to be volatile always, and they are
4271 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004272 // FIXME: Volatile isn't really correct; we should keep track of atomic
4273 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004274 unsigned Flags = MachineMemOperand::MOVolatile;
4275 if (Opcode != ISD::ATOMIC_STORE)
4276 Flags |= MachineMemOperand::MOLoad;
4277 if (Opcode != ISD::ATOMIC_LOAD)
4278 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004279
4280 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004281 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004282 MemVT.getStoreSize(), Alignment);
4283
Eli Friedman55ba8162011-07-29 03:05:32 +00004284 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4285 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004286}
4287
Andrew Trickac6d9be2013-05-25 02:42:55 +00004288SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004289 SDValue Chain,
4290 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004291 MachineMemOperand *MMO,
4292 AtomicOrdering Ordering,
4293 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004294 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4295 Opcode == ISD::ATOMIC_LOAD_SUB ||
4296 Opcode == ISD::ATOMIC_LOAD_AND ||
4297 Opcode == ISD::ATOMIC_LOAD_OR ||
4298 Opcode == ISD::ATOMIC_LOAD_XOR ||
4299 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004300 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004301 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004302 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004303 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004304 Opcode == ISD::ATOMIC_SWAP ||
4305 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004306 "Invalid Atomic Op");
4307
Owen Andersone50ed302009-08-10 22:56:29 +00004308 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004309
Eli Friedman327236c2011-08-24 20:50:09 +00004310 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4311 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004312 SDValue Ops[] = {Chain, Ptr, Val};
Amara Emerson268c7432013-09-26 12:22:36 +00004313 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004314}
4315
Andrew Trickac6d9be2013-05-25 02:42:55 +00004316SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004317 EVT VT, SDValue Chain,
4318 SDValue Ptr,
4319 const Value* PtrVal,
4320 unsigned Alignment,
4321 AtomicOrdering Ordering,
4322 SynchronizationScope SynchScope) {
4323 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4324 Alignment = getEVTAlignment(MemVT);
4325
4326 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004327 // An atomic store does not load. An atomic load does not store.
4328 // (An atomicrmw obviously both loads and stores.)
4329 // For now, atomics are considered to be volatile always, and they are
4330 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004331 // FIXME: Volatile isn't really correct; we should keep track of atomic
4332 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004333 unsigned Flags = MachineMemOperand::MOVolatile;
4334 if (Opcode != ISD::ATOMIC_STORE)
4335 Flags |= MachineMemOperand::MOLoad;
4336 if (Opcode != ISD::ATOMIC_LOAD)
4337 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004338
4339 MachineMemOperand *MMO =
4340 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4341 MemVT.getStoreSize(), Alignment);
4342
4343 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4344 Ordering, SynchScope);
4345}
4346
Andrew Trickac6d9be2013-05-25 02:42:55 +00004347SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004348 EVT VT, SDValue Chain,
4349 SDValue Ptr,
4350 MachineMemOperand *MMO,
4351 AtomicOrdering Ordering,
4352 SynchronizationScope SynchScope) {
4353 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4354
4355 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman327236c2011-08-24 20:50:09 +00004356 SDValue Ops[] = {Chain, Ptr};
Amara Emerson268c7432013-09-26 12:22:36 +00004357 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004358}
4359
Duncan Sands4bdcb612008-07-02 17:40:58 +00004360/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004361SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004362 SDLoc dl) {
Dale Johannesen54c94522009-02-02 20:47:48 +00004363 if (NumOps == 1)
4364 return Ops[0];
4365
Owen Andersone50ed302009-08-10 22:56:29 +00004366 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004367 VTs.reserve(NumOps);
4368 for (unsigned i = 0; i < NumOps; ++i)
4369 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004370 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004371 Ops, NumOps);
4372}
4373
Dan Gohman475871a2008-07-27 21:46:04 +00004374SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004375SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004376 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004377 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004378 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004379 unsigned Align, bool Vol,
4380 bool ReadMem, bool WriteMem) {
4381 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004382 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004383 ReadMem, WriteMem);
4384}
4385
4386SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004387SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesene8c17332009-01-29 00:47:48 +00004388 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004389 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004390 unsigned Align, bool Vol,
4391 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004392 if (Align == 0) // Ensure that codegen never sees alignment 0
4393 Align = getEVTAlignment(MemVT);
4394
4395 MachineFunction &MF = getMachineFunction();
4396 unsigned Flags = 0;
4397 if (WriteMem)
4398 Flags |= MachineMemOperand::MOStore;
4399 if (ReadMem)
4400 Flags |= MachineMemOperand::MOLoad;
4401 if (Vol)
4402 Flags |= MachineMemOperand::MOVolatile;
4403 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004404 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004405
4406 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4407}
4408
4409SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004410SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004411 const SDValue *Ops, unsigned NumOps,
4412 EVT MemVT, MachineMemOperand *MMO) {
4413 assert((Opcode == ISD::INTRINSIC_VOID ||
4414 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004415 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004416 Opcode == ISD::LIFETIME_START ||
4417 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004418 (Opcode <= INT_MAX &&
4419 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4420 "Opcode is not a memory-accessing opcode!");
4421
Dale Johannesene8c17332009-01-29 00:47:48 +00004422 // Memoize the node unless it returns a flag.
4423 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004424 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004425 FoldingSetNodeID ID;
4426 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004427 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004428 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004429 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4430 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004431 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004433
Jack Carter3af4d252013-09-09 22:02:08 +00004434 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4435 dl.getDebugLoc(), VTList, Ops,
4436 NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004437 CSEMap.InsertNode(N, IP);
4438 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004439 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4440 dl.getDebugLoc(), VTList, Ops,
4441 NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004442 }
4443 AllNodes.push_back(N);
4444 return SDValue(N, 0);
4445}
4446
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004447/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4448/// MachinePointerInfo record from it. This is particularly useful because the
4449/// code generator has many cases where it doesn't bother passing in a
4450/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4451static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4452 // If this is FI+Offset, we can model it.
4453 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4454 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4455
4456 // If this is (FI+Offset1)+Offset2, we can model it.
4457 if (Ptr.getOpcode() != ISD::ADD ||
4458 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4459 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4460 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004461
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004462 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4463 return MachinePointerInfo::getFixedStack(FI, Offset+
4464 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4465}
4466
4467/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4468/// MachinePointerInfo record from it. This is particularly useful because the
4469/// code generator has many cases where it doesn't bother passing in a
4470/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4471static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4472 // If the 'Offset' value isn't a constant, we can't handle this.
4473 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4474 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4475 if (OffsetOp.getOpcode() == ISD::UNDEF)
4476 return InferPointerInfo(Ptr);
4477 return MachinePointerInfo();
4478}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004479
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004480
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004481SDValue
4482SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004483 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004484 SDValue Ptr, SDValue Offset,
4485 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004486 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004487 unsigned Alignment, const MDNode *TBAAInfo,
4488 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004489 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004490 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004491 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4492 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004493
Dan Gohmanc76909a2009-09-25 20:36:54 +00004494 unsigned Flags = MachineMemOperand::MOLoad;
4495 if (isVolatile)
4496 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004497 if (isNonTemporal)
4498 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004499 if (isInvariant)
4500 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004501
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004502 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4503 // clients.
4504 if (PtrInfo.V == 0)
4505 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004506
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004507 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004508 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004509 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004510 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004511 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004512}
4513
4514SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004515SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004516 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004517 SDValue Ptr, SDValue Offset, EVT MemVT,
4518 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004519 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004520 ExtType = ISD::NON_EXTLOAD;
4521 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004522 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004523 } else {
4524 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004525 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4526 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004527 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004528 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004529 assert(VT.isVector() == MemVT.isVector() &&
4530 "Cannot use trunc store to convert to or from a vector!");
4531 assert((!VT.isVector() ||
4532 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4533 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004534 }
4535
4536 bool Indexed = AM != ISD::UNINDEXED;
4537 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4538 "Unindexed load with an offset!");
4539
4540 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004541 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004542 SDValue Ops[] = { Chain, Ptr, Offset };
4543 FoldingSetNodeID ID;
4544 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004545 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004546 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004547 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004548 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004549 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004550 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004551 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4552 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004553 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004554 }
Jack Carter3af4d252013-09-09 22:02:08 +00004555 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4556 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman95531882010-03-18 18:49:47 +00004557 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004558 CSEMap.InsertNode(N, IP);
4559 AllNodes.push_back(N);
4560 return SDValue(N, 0);
4561}
4562
Andrew Trickac6d9be2013-05-25 02:42:55 +00004563SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004564 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004565 MachinePointerInfo PtrInfo,
4566 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004567 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004568 const MDNode *TBAAInfo,
4569 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004570 SDValue Undef = getUNDEF(Ptr.getValueType());
4571 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004572 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4573 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004574}
Dale Johannesene8c17332009-01-29 00:47:48 +00004575
Richard Sandiford66589dc2013-10-28 11:17:59 +00004576SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4577 SDValue Chain, SDValue Ptr,
4578 MachineMemOperand *MMO) {
4579 SDValue Undef = getUNDEF(Ptr.getValueType());
4580 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4581 VT, MMO);
4582}
4583
Andrew Trickac6d9be2013-05-25 02:42:55 +00004584SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004585 SDValue Chain, SDValue Ptr,
4586 MachinePointerInfo PtrInfo, EVT MemVT,
4587 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004588 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004589 SDValue Undef = getUNDEF(Ptr.getValueType());
4590 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004591 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004592 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004593}
4594
4595
Richard Sandiford66589dc2013-10-28 11:17:59 +00004596SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4597 SDValue Chain, SDValue Ptr, EVT MemVT,
4598 MachineMemOperand *MMO) {
4599 SDValue Undef = getUNDEF(Ptr.getValueType());
4600 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4601 MemVT, MMO);
4602}
4603
Dan Gohman475871a2008-07-27 21:46:04 +00004604SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004605SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004606 SDValue Offset, ISD::MemIndexedMode AM) {
4607 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4608 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4609 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004610 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004611 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004612 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004613 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004614}
4615
Andrew Trickac6d9be2013-05-25 02:42:55 +00004616SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004617 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004618 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004619 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004620 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004621 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004622 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004623 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004624
Dan Gohmanc76909a2009-09-25 20:36:54 +00004625 unsigned Flags = MachineMemOperand::MOStore;
4626 if (isVolatile)
4627 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004628 if (isNonTemporal)
4629 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004630
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004631 if (PtrInfo.V == 0)
4632 PtrInfo = InferPointerInfo(Ptr);
4633
4634 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004635 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004636 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004637 Val.getValueType().getStoreSize(), Alignment,
4638 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004639
4640 return getStore(Chain, dl, Val, Ptr, MMO);
4641}
4642
Andrew Trickac6d9be2013-05-25 02:42:55 +00004643SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004644 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004645 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004646 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004647 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004648 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004649 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004650 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4651 FoldingSetNodeID ID;
4652 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004653 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004654 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004655 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004656 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004657 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004658 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4659 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004660 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004661 }
Jack Carter3af4d252013-09-09 22:02:08 +00004662 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4663 dl.getDebugLoc(), VTs,
4664 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004665 CSEMap.InsertNode(N, IP);
4666 AllNodes.push_back(N);
4667 return SDValue(N, 0);
4668}
4669
Andrew Trickac6d9be2013-05-25 02:42:55 +00004670SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004671 SDValue Ptr, MachinePointerInfo PtrInfo,
4672 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004673 unsigned Alignment,
4674 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004675 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004676 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004677 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4678 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004679
Dan Gohmanc76909a2009-09-25 20:36:54 +00004680 unsigned Flags = MachineMemOperand::MOStore;
4681 if (isVolatile)
4682 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004683 if (isNonTemporal)
4684 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004685
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004686 if (PtrInfo.V == 0)
4687 PtrInfo = InferPointerInfo(Ptr);
4688
4689 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004690 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004691 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4692 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004693
4694 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4695}
4696
Andrew Trickac6d9be2013-05-25 02:42:55 +00004697SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004698 SDValue Ptr, EVT SVT,
4699 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004700 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004701
Michael Ilseman06b96902012-09-10 16:56:31 +00004702 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004703 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004704 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004705 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004706
Dan Gohman2e141d72009-12-14 23:40:38 +00004707 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4708 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004709 assert(VT.isInteger() == SVT.isInteger() &&
4710 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004711 assert(VT.isVector() == SVT.isVector() &&
4712 "Cannot use trunc store to convert to or from a vector!");
4713 assert((!VT.isVector() ||
4714 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4715 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004716
Owen Anderson825b72b2009-08-11 20:47:22 +00004717 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004718 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004719 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4720 FoldingSetNodeID ID;
4721 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004722 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004723 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004724 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004725 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004726 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004727 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4728 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004729 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004730 }
Jack Carter3af4d252013-09-09 22:02:08 +00004731 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4732 dl.getDebugLoc(), VTs,
4733 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004734 CSEMap.InsertNode(N, IP);
4735 AllNodes.push_back(N);
4736 return SDValue(N, 0);
4737}
4738
Dan Gohman475871a2008-07-27 21:46:04 +00004739SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004740SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004741 SDValue Offset, ISD::MemIndexedMode AM) {
4742 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4743 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4744 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004746 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4747 FoldingSetNodeID ID;
4748 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004749 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004750 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004751 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004752 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004753 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004754 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004755
Jack Carter3af4d252013-09-09 22:02:08 +00004756 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4757 dl.getDebugLoc(), VTs, AM,
Dan Gohman95531882010-03-18 18:49:47 +00004758 ST->isTruncatingStore(),
4759 ST->getMemoryVT(),
4760 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004761 CSEMap.InsertNode(N, IP);
4762 AllNodes.push_back(N);
4763 return SDValue(N, 0);
4764}
4765
Andrew Trickac6d9be2013-05-25 02:42:55 +00004766SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004767 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004768 SDValue SV,
4769 unsigned Align) {
4770 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4771 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004772}
4773
Andrew Trickac6d9be2013-05-25 02:42:55 +00004774SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004775 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004776 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004777 case 0: return getNode(Opcode, DL, VT);
4778 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4779 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4780 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004781 default: break;
4782 }
4783
Dan Gohman475871a2008-07-27 21:46:04 +00004784 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004785 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004786 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004787 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004788}
4789
Andrew Trickac6d9be2013-05-25 02:42:55 +00004790SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004791 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004792 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004793 case 0: return getNode(Opcode, DL, VT);
4794 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4795 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4796 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004797 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004798 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004799
Chris Lattneref847df2005-04-09 03:27:28 +00004800 switch (Opcode) {
4801 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004802 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004803 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004804 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4805 "LHS and RHS of condition must have same type!");
4806 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4807 "True and False arms of SelectCC must have same type!");
4808 assert(Ops[2].getValueType() == VT &&
4809 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004810 break;
4811 }
4812 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004813 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004814 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4815 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004816 break;
4817 }
Chris Lattneref847df2005-04-09 03:27:28 +00004818 }
4819
Chris Lattner385328c2005-05-14 07:42:29 +00004820 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004821 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004822 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004823
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004824 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004825 FoldingSetNodeID ID;
4826 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004827 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004828
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004829 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004830 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004831
Jack Carter3af4d252013-09-09 22:02:08 +00004832 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4833 VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004834 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004835 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004836 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4837 VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004838 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004839
Chris Lattneref847df2005-04-09 03:27:28 +00004840 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004841#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004842 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004843#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004844 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004845}
4846
Andrew Trickac6d9be2013-05-25 02:42:55 +00004847SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Benjamin Kramer3853f742013-03-07 20:33:29 +00004848 ArrayRef<EVT> ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004849 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004850 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004851 Ops, NumOps);
4852}
4853
Andrew Trickac6d9be2013-05-25 02:42:55 +00004854SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004855 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004856 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004857 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004858 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4859 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004860}
4861
Andrew Trickac6d9be2013-05-25 02:42:55 +00004862SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004863 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004864 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004865 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004866
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004867#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004868 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004869 // FIXME: figure out how to safely handle things like
4870 // int foo(int x) { return 1 << (x & 255); }
4871 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004872 case ISD::SRA_PARTS:
4873 case ISD::SRL_PARTS:
4874 case ISD::SHL_PARTS:
4875 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004877 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004878 else if (N3.getOpcode() == ISD::AND)
4879 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4880 // If the and is only masking out bits that cannot effect the shift,
4881 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004882 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004883 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004884 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004885 }
4886 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004887 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004888#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004889
Chris Lattner43247a12005-08-25 19:12:10 +00004890 // Memoize the node unless it returns a flag.
4891 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004892 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004893 FoldingSetNodeID ID;
4894 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004895 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004896 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004897 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004898
Dan Gohman0e5f1302008-07-07 23:02:41 +00004899 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004900 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4901 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004902 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004903 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4904 DL.getDebugLoc(), VTList, Ops[0],
4905 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004906 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004907 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4908 DL.getDebugLoc(), VTList, Ops[0],
4909 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004910 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004911 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4912 VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004913 }
Chris Lattnera5682852006-08-07 23:03:03 +00004914 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004915 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004916 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004917 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4918 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004919 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004920 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4921 DL.getDebugLoc(), VTList, Ops[0],
4922 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004923 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004924 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4925 DL.getDebugLoc(), VTList, Ops[0],
4926 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004927 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004928 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4929 VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004930 }
Chris Lattner43247a12005-08-25 19:12:10 +00004931 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004932 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004933#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004934 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004935#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004936 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004937}
4938
Andrew Trickac6d9be2013-05-25 02:42:55 +00004939SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004940 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004941}
4942
Andrew Trickac6d9be2013-05-25 02:42:55 +00004943SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004944 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004945 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004946 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004947}
4948
Andrew Trickac6d9be2013-05-25 02:42:55 +00004949SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004950 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004951 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004952 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004953}
4954
Andrew Trickac6d9be2013-05-25 02:42:55 +00004955SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004956 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004957 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004958 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004959}
4960
Andrew Trickac6d9be2013-05-25 02:42:55 +00004961SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004962 SDValue N1, SDValue N2, SDValue N3,
4963 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004964 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004965 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004966}
4967
Andrew Trickac6d9be2013-05-25 02:42:55 +00004968SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004969 SDValue N1, SDValue N2, SDValue N3,
4970 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004971 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004972 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004973}
4974
Owen Andersone50ed302009-08-10 22:56:29 +00004975SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004976 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004977}
4978
Owen Andersone50ed302009-08-10 22:56:29 +00004979SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004980 FoldingSetNodeID ID;
4981 ID.AddInteger(2U);
4982 ID.AddInteger(VT1.getRawBits());
4983 ID.AddInteger(VT2.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004984
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004985 void *IP = 0;
4986 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
4987 if (Result == NULL) {
4988 EVT *Array = Allocator.Allocate<EVT>(2);
4989 Array[0] = VT1;
4990 Array[1] = VT2;
4991 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
4992 VTListMap.InsertNode(Result, IP);
4993 }
4994 return Result->getSDVTList();
Chris Lattnera3255112005-11-08 23:30:28 +00004995}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004996
Owen Andersone50ed302009-08-10 22:56:29 +00004997SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004998 FoldingSetNodeID ID;
4999 ID.AddInteger(3U);
5000 ID.AddInteger(VT1.getRawBits());
5001 ID.AddInteger(VT2.getRawBits());
5002 ID.AddInteger(VT3.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005003
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005004 void *IP = 0;
5005 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5006 if (Result == NULL) {
5007 EVT *Array = Allocator.Allocate<EVT>(3);
5008 Array[0] = VT1;
5009 Array[1] = VT2;
5010 Array[2] = VT3;
5011 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5012 VTListMap.InsertNode(Result, IP);
5013 }
5014 return Result->getSDVTList();
Chris Lattner70046e92006-08-15 17:46:01 +00005015}
5016
Owen Andersone50ed302009-08-10 22:56:29 +00005017SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005018 FoldingSetNodeID ID;
5019 ID.AddInteger(4U);
5020 ID.AddInteger(VT1.getRawBits());
5021 ID.AddInteger(VT2.getRawBits());
5022 ID.AddInteger(VT3.getRawBits());
5023 ID.AddInteger(VT4.getRawBits());
Bill Wendling13d6d442008-12-01 23:28:22 +00005024
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005025 void *IP = 0;
5026 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5027 if (Result == NULL) {
5028 EVT *Array = Allocator.Allocate<EVT>(4);
5029 Array[0] = VT1;
5030 Array[1] = VT2;
5031 Array[2] = VT3;
5032 Array[3] = VT4;
5033 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5034 VTListMap.InsertNode(Result, IP);
5035 }
5036 return Result->getSDVTList();
Bill Wendling13d6d442008-12-01 23:28:22 +00005037}
5038
Owen Andersone50ed302009-08-10 22:56:29 +00005039SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005040 FoldingSetNodeID ID;
5041 ID.AddInteger(NumVTs);
5042 for (unsigned index = 0; index < NumVTs; index++) {
5043 ID.AddInteger(VTs[index].getRawBits());
Chris Lattner70046e92006-08-15 17:46:01 +00005044 }
5045
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005046 void *IP = 0;
5047 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5048 if (Result == NULL) {
5049 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5050 std::copy(VTs, VTs + NumVTs, Array);
5051 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5052 VTListMap.InsertNode(Result, IP);
Chris Lattner70046e92006-08-15 17:46:01 +00005053 }
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005054 return Result->getSDVTList();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00005055}
5056
5057
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005058/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5059/// specified operands. If the resultant node already exists in the DAG,
5060/// this does not modify the specified node, instead it returns the node that
5061/// already exists. If the resultant node does not exist in the DAG, the
5062/// input node is returned. As a degenerate case, if you specify the same
5063/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00005064SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005065 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005066
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005067 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00005068 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005069
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005070 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005071 void *InsertPos = 0;
5072 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005073 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005074
Dan Gohman79acd2b2008-07-21 22:38:59 +00005075 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005076 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005077 if (!RemoveNodeFromCSEMaps(N))
5078 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005079
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005080 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005081 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00005082
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005083 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005084 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005085 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005086}
5087
Dan Gohman027657d2010-06-18 15:30:29 +00005088SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005089 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005090
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005091 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005092 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00005093 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00005094
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005095 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005096 void *InsertPos = 0;
5097 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005098 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005099
Dan Gohman79acd2b2008-07-21 22:38:59 +00005100 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005101 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005102 if (!RemoveNodeFromCSEMaps(N))
5103 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005104
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005105 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005106 if (N->OperandList[0] != Op1)
5107 N->OperandList[0].set(Op1);
5108 if (N->OperandList[1] != Op2)
5109 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005110
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005111 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005112 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005113 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005114}
5115
Dan Gohman027657d2010-06-18 15:30:29 +00005116SDNode *SelectionDAG::
5117UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005118 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005119 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005120}
5121
Dan Gohman027657d2010-06-18 15:30:29 +00005122SDNode *SelectionDAG::
5123UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005124 SDValue Op3, SDValue Op4) {
5125 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005126 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005127}
5128
Dan Gohman027657d2010-06-18 15:30:29 +00005129SDNode *SelectionDAG::
5130UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005131 SDValue Op3, SDValue Op4, SDValue Op5) {
5132 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005133 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00005134}
5135
Dan Gohman027657d2010-06-18 15:30:29 +00005136SDNode *SelectionDAG::
5137UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005138 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005139 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005140
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005141 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005142 bool AnyChange = false;
5143 for (unsigned i = 0; i != NumOps; ++i) {
5144 if (Ops[i] != N->getOperand(i)) {
5145 AnyChange = true;
5146 break;
5147 }
5148 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005149
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005150 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00005151 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005152
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005153 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005154 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005155 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005156 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005157
Dan Gohman7ceda162008-05-02 00:05:03 +00005158 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005159 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005160 if (!RemoveNodeFromCSEMaps(N))
5161 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005162
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005163 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005164 for (unsigned i = 0; i != NumOps; ++i)
5165 if (N->OperandList[i] != Ops[i])
5166 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005167
5168 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005169 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005170 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005171}
5172
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005173/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005174/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005175void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005176 // Unlike the code in MorphNodeTo that does this, we don't need to
5177 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005178 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5179 SDUse &Use = *I++;
5180 Use.set(SDValue());
5181 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005182}
Chris Lattner149c58c2005-08-16 18:17:10 +00005183
Dan Gohmane8be6c62008-07-17 19:10:17 +00005184/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5185/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005186///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005187SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005188 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005189 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005190 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00005191}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005192
Dan Gohmane8be6c62008-07-17 19:10:17 +00005193SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005194 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005195 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005196 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005197 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00005198}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005199
Dan Gohmane8be6c62008-07-17 19:10:17 +00005200SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005201 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005202 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005203 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005204 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005205 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00005206}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005207
Dan Gohmane8be6c62008-07-17 19:10:17 +00005208SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005209 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005210 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005211 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005212 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005213 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00005214}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005215
Dan Gohmane8be6c62008-07-17 19:10:17 +00005216SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005217 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00005218 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005219 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005220 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005221}
5222
Dan Gohmane8be6c62008-07-17 19:10:17 +00005223SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005224 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005225 unsigned NumOps) {
5226 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005227 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005228}
5229
Dan Gohmane8be6c62008-07-17 19:10:17 +00005230SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005231 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005232 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005233 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00005234}
5235
Dan Gohmane8be6c62008-07-17 19:10:17 +00005236SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005237 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00005238 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005239 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005240 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005241}
5242
Bill Wendling13d6d442008-12-01 23:28:22 +00005243SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005244 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00005245 const SDValue *Ops, unsigned NumOps) {
5246 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5247 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5248}
5249
Scott Michelfdc40a02009-02-17 22:15:04 +00005250SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005251 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005252 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005253 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005254 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005255 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005256}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005257
Scott Michelfdc40a02009-02-17 22:15:04 +00005258SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005259 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005260 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005261 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005262 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005263 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005264}
5265
Dan Gohmane8be6c62008-07-17 19:10:17 +00005266SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005267 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005268 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005269 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005270 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005271 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005272 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00005273}
5274
Dan Gohmane8be6c62008-07-17 19:10:17 +00005275SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005276 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005277 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005278 SDValue Op3) {
5279 SDVTList VTs = getVTList(VT1, VT2, VT3);
5280 SDValue Ops[] = { Op1, Op2, Op3 };
5281 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5282}
5283
5284SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00005285 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005286 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00005287 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5288 // Reset the NodeID to -1.
5289 N->setNodeId(-1);
5290 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005291}
5292
Andrew Trickac6d9be2013-05-25 02:42:55 +00005293/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel0508d042011-12-15 18:21:18 +00005294/// the line number information on the merged node since it is not possible to
5295/// preserve the information that operation is associated with multiple lines.
5296/// This will make the debugger working better at -O0, were there is a higher
5297/// probability having other instructions associated with that line.
5298///
Andrew Trickac6d9be2013-05-25 02:42:55 +00005299/// For IROrder, we keep the smaller of the two
5300SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel0508d042011-12-15 18:21:18 +00005301 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005302 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5303 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel0508d042011-12-15 18:21:18 +00005304 N->setDebugLoc(DebugLoc());
5305 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005306 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5307 N->setIROrder(Order);
Devang Patel0508d042011-12-15 18:21:18 +00005308 return N;
5309}
5310
Chris Lattner21221e32010-02-28 21:36:14 +00005311/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005312/// return type, opcode, and operands.
5313///
5314/// Note that MorphNodeTo returns the resultant node. If there is already a
5315/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickac6d9be2013-05-25 02:42:55 +00005316/// the current one. Note that the SDLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005317///
5318/// Using MorphNodeTo is faster than creating a new node and swapping it in
5319/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005320/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005321/// the node's users.
5322///
5323SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005325 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005326 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00005327 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005328 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005329 FoldingSetNodeID ID;
5330 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005331 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005332 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohmane8be6c62008-07-17 19:10:17 +00005333 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005334
Dan Gohman095cc292008-09-13 01:54:27 +00005335 if (!RemoveNodeFromCSEMaps(N))
5336 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00005337
Dan Gohmane8be6c62008-07-17 19:10:17 +00005338 // Start the morphing.
5339 N->NodeType = Opc;
5340 N->ValueList = VTs.VTs;
5341 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005342
Dan Gohmane8be6c62008-07-17 19:10:17 +00005343 // Clear the operands list, updating used nodes to remove this from their
5344 // use list. Keep track of any operands that become dead as a result.
5345 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005346 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5347 SDUse &Use = *I++;
5348 SDNode *Used = Use.getNode();
5349 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005350 if (Used->use_empty())
5351 DeadNodeSet.insert(Used);
5352 }
5353
Dan Gohmanc76909a2009-09-25 20:36:54 +00005354 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5355 // Initialize the memory references information.
5356 MN->setMemRefs(0, 0);
5357 // If NumOps is larger than the # of operands we can have in a
5358 // MachineSDNode, reallocate the operand list.
5359 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5360 if (MN->OperandsNeedDelete)
5361 delete[] MN->OperandList;
5362 if (NumOps > array_lengthof(MN->LocalOperands))
5363 // We're creating a final node that will live unmorphed for the
5364 // remainder of the current SelectionDAG iteration, so we can allocate
5365 // the operands directly out of a pool with no recycling metadata.
5366 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005367 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005368 else
5369 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5370 MN->OperandsNeedDelete = false;
5371 } else
5372 MN->InitOperands(MN->OperandList, Ops, NumOps);
5373 } else {
5374 // If NumOps is larger than the # of operands we currently have, reallocate
5375 // the operand list.
5376 if (NumOps > N->NumOperands) {
5377 if (N->OperandsNeedDelete)
5378 delete[] N->OperandList;
5379 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005380 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005381 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005382 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005383 }
5384
5385 // Delete any nodes that are still dead after adding the uses for the
5386 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005387 if (!DeadNodeSet.empty()) {
5388 SmallVector<SDNode *, 16> DeadNodes;
5389 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5390 E = DeadNodeSet.end(); I != E; ++I)
5391 if ((*I)->use_empty())
5392 DeadNodes.push_back(*I);
5393 RemoveDeadNodes(DeadNodes);
5394 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005395
Dan Gohmane8be6c62008-07-17 19:10:17 +00005396 if (IP)
5397 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005398 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005399}
5400
Chris Lattner0fb094f2005-11-19 01:44:53 +00005401
Dan Gohman602b0c82009-09-25 18:54:59 +00005402/// getMachineNode - These are used for target selectors to create a new node
5403/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005404///
Dan Gohman602b0c82009-09-25 18:54:59 +00005405/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005406/// node of the specified opcode and operands, it returns that node instead of
5407/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005408MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005409SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005410 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005411 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005412}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005413
Dan Gohmanc81b7832009-10-10 01:29:16 +00005414MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005415SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005416 SDVTList VTs = getVTList(VT);
5417 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005418 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005419}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005420
Dan Gohmanc81b7832009-10-10 01:29:16 +00005421MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005422SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005423 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005424 SDVTList VTs = getVTList(VT);
5425 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005426 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005427}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005428
Dan Gohmanc81b7832009-10-10 01:29:16 +00005429MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005430SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005431 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005432 SDVTList VTs = getVTList(VT);
5433 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005434 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005435}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005436
Dan Gohmanc81b7832009-10-10 01:29:16 +00005437MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005438SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005439 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005440 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005441 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005442}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005443
Dan Gohmanc81b7832009-10-10 01:29:16 +00005444MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005445SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005446 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005447 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005448}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005449
Dan Gohmanc81b7832009-10-10 01:29:16 +00005450MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005451SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005452 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005453 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005454 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005455 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005456}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005457
Dan Gohmanc81b7832009-10-10 01:29:16 +00005458MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005459SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005460 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005461 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005462 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005463 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005464}
5465
Dan Gohmanc81b7832009-10-10 01:29:16 +00005466MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005467SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005468 EVT VT1, EVT VT2, SDValue Op1,
5469 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005470 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005471 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005472 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005473}
5474
Dan Gohmanc81b7832009-10-10 01:29:16 +00005475MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005476SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005477 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005478 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005479 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005480 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005481}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005482
Dan Gohmanc81b7832009-10-10 01:29:16 +00005483MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005484SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005485 EVT VT1, EVT VT2, EVT VT3,
5486 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005487 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005488 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005489 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005490}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005491
Dan Gohmanc81b7832009-10-10 01:29:16 +00005492MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005493SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005494 EVT VT1, EVT VT2, EVT VT3,
5495 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005496 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005497 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005498 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005499}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005500
Dan Gohmanc81b7832009-10-10 01:29:16 +00005501MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005502SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005503 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005504 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005505 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005506 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005507}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005508
Dan Gohmanc81b7832009-10-10 01:29:16 +00005509MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005510SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005511 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005512 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005513 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005514 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005515}
5516
Dan Gohmanc81b7832009-10-10 01:29:16 +00005517MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005518SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005519 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005520 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005521 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liao2a8bea72013-04-19 22:22:57 +00005522 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005523}
5524
Dan Gohmanc81b7832009-10-10 01:29:16 +00005525MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005526SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005527 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005528 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005529 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005530 void *IP = 0;
Michael Liao2a8bea72013-04-19 22:22:57 +00005531 const SDValue *Ops = OpsArray.data();
5532 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005533
5534 if (DoCSE) {
5535 FoldingSetNodeID ID;
5536 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5537 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005538 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005539 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel0508d042011-12-15 18:21:18 +00005540 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005541 }
5542
5543 // Allocate a new MachineSDNode.
Jack Carter3af4d252013-09-09 22:02:08 +00005544 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5545 DL.getDebugLoc(), VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005546
5547 // Initialize the operands list.
5548 if (NumOps > array_lengthof(N->LocalOperands))
5549 // We're creating a final node that will live unmorphed for the
5550 // remainder of the current SelectionDAG iteration, so we can allocate
5551 // the operands directly out of a pool with no recycling metadata.
5552 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5553 Ops, NumOps);
5554 else
5555 N->InitOperands(N->LocalOperands, Ops, NumOps);
5556 N->OperandsNeedDelete = false;
5557
5558 if (DoCSE)
5559 CSEMap.InsertNode(N, IP);
5560
5561 AllNodes.push_back(N);
5562#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005563 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005564#endif
5565 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005566}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005567
Dan Gohman6a402dc2009-08-19 18:16:17 +00005568/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005569/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005570SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005571SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohman6a402dc2009-08-19 18:16:17 +00005572 SDValue Operand) {
5573 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005574 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005575 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005576 return SDValue(Subreg, 0);
5577}
5578
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005579/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005580/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005581SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005582SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005583 SDValue Operand, SDValue Subreg) {
5584 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005585 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005586 VT, Operand, Subreg, SRIdxVal);
5587 return SDValue(Result, 0);
5588}
5589
Evan Cheng08b11732008-03-22 01:55:50 +00005590/// getNodeIfExists - Get the specified node if it's already available, or
5591/// else return NULL.
5592SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005593 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005594 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005595 FoldingSetNodeID ID;
5596 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5597 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005598 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005599 return E;
5600 }
5601 return NULL;
5602}
5603
Evan Cheng31441b72010-03-29 20:48:30 +00005604/// getDbgValue - Creates a SDDbgValue node.
5605///
5606SDDbgValue *
5607SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5608 DebugLoc DL, unsigned O) {
5609 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5610}
5611
5612SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005613SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005614 DebugLoc DL, unsigned O) {
5615 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5616}
5617
5618SDDbgValue *
5619SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5620 DebugLoc DL, unsigned O) {
5621 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5622}
5623
Dan Gohmana72d2a22010-03-03 21:33:37 +00005624namespace {
5625
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005626/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005627/// pointed to by a use iterator is deleted, increment the use iterator
5628/// so that it doesn't dangle.
5629///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005630class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005631 SDNode::use_iterator &UI;
5632 SDNode::use_iterator &UE;
5633
5634 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5635 // Increment the iterator as needed.
5636 while (UI != UE && N == *UI)
5637 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005638 }
5639
5640public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005641 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005642 SDNode::use_iterator &ui,
5643 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005644 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005645};
5646
5647}
5648
Evan Cheng99157a02006-08-07 22:13:29 +00005649/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005650/// This can cause recursive merging of nodes in the DAG.
5651///
Chris Lattner11d049c2008-02-03 03:35:22 +00005652/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005653///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005654void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005655 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005656 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005657 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005658 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005659
Dan Gohman39946102009-01-25 16:29:12 +00005660 // Iterate over all the existing uses of From. New uses will be added
5661 // to the beginning of the use list, which we avoid visiting.
5662 // This specifically avoids visiting uses of From that arise while the
5663 // replacement is happening, because any such uses would be the result
5664 // of CSE: If an existing node looks like From after one of its operands
5665 // is replaced by To, we don't want to replace of all its users with To
5666 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005667 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005668 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005669 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005670 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005671
Chris Lattner8b8749f2005-08-17 19:00:20 +00005672 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005673 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005674
Dan Gohman39946102009-01-25 16:29:12 +00005675 // A user can appear in a use list multiple times, and when this
5676 // happens the uses are usually next to each other in the list.
5677 // To help reduce the number of CSE recomputations, process all
5678 // the uses of this user that we can find this way.
5679 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005680 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005681 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005682 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005683 } while (UI != UE && *UI == User);
5684
5685 // Now that we have modified User, add it back to the CSE maps. If it
5686 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005687 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005688 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005689
5690 // If we just RAUW'd the root, take note.
5691 if (FromN == getRoot())
5692 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005693}
5694
5695/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5696/// This can cause recursive merging of nodes in the DAG.
5697///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005698/// This version assumes that for each value of From, there is a
5699/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005700///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005701void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005702#ifndef NDEBUG
5703 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5704 assert((!From->hasAnyUseOfValue(i) ||
5705 From->getValueType(i) == To->getValueType(i)) &&
5706 "Cannot use this version of ReplaceAllUsesWith!");
5707#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005708
5709 // Handle the trivial case.
5710 if (From == To)
5711 return;
5712
Dan Gohmandbe664a2009-01-19 21:44:21 +00005713 // Iterate over just the existing users of From. See the comments in
5714 // the ReplaceAllUsesWith above.
5715 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005716 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005717 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005718 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005719
Chris Lattner8b52f212005-08-26 18:36:28 +00005720 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005721 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005722
Dan Gohman39946102009-01-25 16:29:12 +00005723 // A user can appear in a use list multiple times, and when this
5724 // happens the uses are usually next to each other in the list.
5725 // To help reduce the number of CSE recomputations, process all
5726 // the uses of this user that we can find this way.
5727 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005728 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005729 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005730 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005731 } while (UI != UE && *UI == User);
5732
5733 // Now that we have modified User, add it back to the CSE maps. If it
5734 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005735 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005736 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005737
5738 // If we just RAUW'd the root, take note.
5739 if (From == getRoot().getNode())
5740 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005741}
5742
Chris Lattner8b52f212005-08-26 18:36:28 +00005743/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5744/// This can cause recursive merging of nodes in the DAG.
5745///
5746/// This version can replace From with any result values. To must match the
5747/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005748void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005749 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005750 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005751
Dan Gohmandbe664a2009-01-19 21:44:21 +00005752 // Iterate over just the existing users of From. See the comments in
5753 // the ReplaceAllUsesWith above.
5754 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005755 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005756 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005757 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005758
Chris Lattner7b2880c2005-08-24 22:44:39 +00005759 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005760 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005761
Dan Gohman39946102009-01-25 16:29:12 +00005762 // A user can appear in a use list multiple times, and when this
5763 // happens the uses are usually next to each other in the list.
5764 // To help reduce the number of CSE recomputations, process all
5765 // the uses of this user that we can find this way.
5766 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005767 SDUse &Use = UI.getUse();
5768 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005769 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005770 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005771 } while (UI != UE && *UI == User);
5772
5773 // Now that we have modified User, add it back to the CSE maps. If it
5774 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005775 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005776 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005777
5778 // If we just RAUW'd the root, take note.
5779 if (From == getRoot().getNode())
5780 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005781}
5782
Chris Lattner012f2412006-02-17 21:58:01 +00005783/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005784/// uses of other values produced by From.getNode() alone. The Deleted
5785/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005786void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005787 // Handle the really simple, really trivial case efficiently.
5788 if (From == To) return;
5789
Chris Lattner012f2412006-02-17 21:58:01 +00005790 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005791 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005792 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005793 return;
5794 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005795
Dan Gohman39946102009-01-25 16:29:12 +00005796 // Iterate over just the existing users of From. See the comments in
5797 // the ReplaceAllUsesWith above.
5798 SDNode::use_iterator UI = From.getNode()->use_begin(),
5799 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005800 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005801 while (UI != UE) {
5802 SDNode *User = *UI;
5803 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005804
Dan Gohman39946102009-01-25 16:29:12 +00005805 // A user can appear in a use list multiple times, and when this
5806 // happens the uses are usually next to each other in the list.
5807 // To help reduce the number of CSE recomputations, process all
5808 // the uses of this user that we can find this way.
5809 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005810 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005811
5812 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005813 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005814 ++UI;
5815 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005816 }
Dan Gohman39946102009-01-25 16:29:12 +00005817
5818 // If this node hasn't been modified yet, it's still in the CSE maps,
5819 // so remove its old self from the CSE maps.
5820 if (!UserRemovedFromCSEMaps) {
5821 RemoveNodeFromCSEMaps(User);
5822 UserRemovedFromCSEMaps = true;
5823 }
5824
5825 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005826 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005827 } while (UI != UE && *UI == User);
5828
5829 // We are iterating over all uses of the From node, so if a use
5830 // doesn't use the specific value, no changes are made.
5831 if (!UserRemovedFromCSEMaps)
5832 continue;
5833
Chris Lattner01d029b2007-10-15 06:10:22 +00005834 // Now that we have modified User, add it back to the CSE maps. If it
5835 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005836 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005837 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005838
5839 // If we just RAUW'd the root, take note.
5840 if (From == getRoot())
5841 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005842}
5843
Dan Gohman39946102009-01-25 16:29:12 +00005844namespace {
5845 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5846 /// to record information about a use.
5847 struct UseMemo {
5848 SDNode *User;
5849 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005850 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005851 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005852
5853 /// operator< - Sort Memos by User.
5854 bool operator<(const UseMemo &L, const UseMemo &R) {
5855 return (intptr_t)L.User < (intptr_t)R.User;
5856 }
Dan Gohman39946102009-01-25 16:29:12 +00005857}
5858
Dan Gohmane8be6c62008-07-17 19:10:17 +00005859/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005860/// uses of other values produced by From.getNode() alone. The same value
5861/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005862/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005863void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5864 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005865 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005866 // Handle the simple, trivial case efficiently.
5867 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005868 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005869
Dan Gohman39946102009-01-25 16:29:12 +00005870 // Read up all the uses and make records of them. This helps
5871 // processing new uses that are introduced during the
5872 // replacement process.
5873 SmallVector<UseMemo, 4> Uses;
5874 for (unsigned i = 0; i != Num; ++i) {
5875 unsigned FromResNo = From[i].getResNo();
5876 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005877 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005878 E = FromNode->use_end(); UI != E; ++UI) {
5879 SDUse &Use = UI.getUse();
5880 if (Use.getResNo() == FromResNo) {
5881 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005882 Uses.push_back(Memo);
5883 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005884 }
Dan Gohman39946102009-01-25 16:29:12 +00005885 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005886
Dan Gohmane7852d02009-01-26 04:35:06 +00005887 // Sort the uses, so that all the uses from a given User are together.
5888 std::sort(Uses.begin(), Uses.end());
5889
Dan Gohman39946102009-01-25 16:29:12 +00005890 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5891 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005892 // We know that this user uses some value of From. If it is the right
5893 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005894 SDNode *User = Uses[UseIndex].User;
5895
5896 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005897 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005898
Dan Gohmane7852d02009-01-26 04:35:06 +00005899 // The Uses array is sorted, so all the uses for a given User
5900 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005901 // To help reduce the number of CSE recomputations, process all
5902 // the uses of this user that we can find this way.
5903 do {
5904 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005905 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005906 ++UseIndex;
5907
Dan Gohmane7852d02009-01-26 04:35:06 +00005908 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005909 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5910
Dan Gohmane8be6c62008-07-17 19:10:17 +00005911 // Now that we have modified User, add it back to the CSE maps. If it
5912 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005913 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005914 }
5915}
5916
Evan Chenge6f35d82006-08-01 08:20:41 +00005917/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005918/// based on their topological order. It returns the maximum id and a vector
5919/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005920unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005921
Dan Gohmanf06c8352008-09-30 18:30:35 +00005922 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005923
Dan Gohmanf06c8352008-09-30 18:30:35 +00005924 // SortedPos tracks the progress of the algorithm. Nodes before it are
5925 // sorted, nodes after it are unsorted. When the algorithm completes
5926 // it is at the end of the list.
5927 allnodes_iterator SortedPos = allnodes_begin();
5928
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005929 // Visit all the nodes. Move nodes with no operands to the front of
5930 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005931 // operand count. Before we do this, the Node Id fields of the nodes
5932 // may contain arbitrary values. After, the Node Id fields for nodes
5933 // before SortedPos will contain the topological sort index, and the
5934 // Node Id fields for nodes At SortedPos and after will contain the
5935 // count of outstanding operands.
5936 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5937 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005938 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005939 unsigned Degree = N->getNumOperands();
5940 if (Degree == 0) {
5941 // A node with no uses, add it to the result array immediately.
5942 N->setNodeId(DAGSize++);
5943 allnodes_iterator Q = N;
5944 if (Q != SortedPos)
5945 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005946 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005947 ++SortedPos;
5948 } else {
5949 // Temporarily use the Node Id as scratch space for the degree count.
5950 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005951 }
5952 }
5953
Chad Rosierf496dea2012-05-21 17:13:41 +00005954 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005955 // such that by the time the end is reached all nodes will be sorted.
5956 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5957 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005958 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005959 // N is in sorted position, so all its uses have one less operand
5960 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005961 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5962 UI != UE; ++UI) {
5963 SDNode *P = *UI;
5964 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005965 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005966 --Degree;
5967 if (Degree == 0) {
5968 // All of P's operands are sorted, so P may sorted now.
5969 P->setNodeId(DAGSize++);
5970 if (P != SortedPos)
5971 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005972 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005973 ++SortedPos;
5974 } else {
5975 // Update P's outstanding operand count.
5976 P->setNodeId(Degree);
5977 }
5978 }
David Greene221925e2010-01-20 00:59:23 +00005979 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005980#ifndef NDEBUG
5981 SDNode *S = ++I;
5982 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005983 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005984#endif
5985 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005986 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005987 }
5988
5989 assert(SortedPos == AllNodes.end() &&
5990 "Topological sort incomplete!");
5991 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5992 "First node in topological sort is not the entry token!");
5993 assert(AllNodes.front().getNodeId() == 0 &&
5994 "First node in topological sort has non-zero id!");
5995 assert(AllNodes.front().getNumOperands() == 0 &&
5996 "First node in topological sort has operands!");
5997 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5998 "Last node in topologic sort has unexpected id!");
5999 assert(AllNodes.back().use_empty() &&
6000 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006001 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006002 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00006003}
6004
Evan Chengbfcb3052010-03-25 01:38:16 +00006005/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6006/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00006007void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6008 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00006009 if (SD)
6010 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00006011}
Evan Cheng091cba12006-07-27 06:39:06 +00006012
Devang Patela2e868d2011-01-25 23:27:42 +00006013/// TransferDbgValues - Transfer SDDbgValues.
6014void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6015 if (From == To || !From.getNode()->getHasDebugValue())
6016 return;
6017 SDNode *FromNode = From.getNode();
6018 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006019 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00006020 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006021 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00006022 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00006023 SDDbgValue *Dbg = *I;
6024 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6025 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
6026 Dbg->getOffset(), Dbg->getDebugLoc(),
6027 Dbg->getOrder());
6028 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00006029 }
6030 }
Craig Topperf22fd3f2013-07-03 05:11:49 +00006031 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patela778f5c2011-02-18 22:43:42 +00006032 E = ClonedDVs.end(); I != E; ++I)
6033 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00006034}
6035
Jim Laskey58b968b2005-08-17 20:08:02 +00006036//===----------------------------------------------------------------------===//
6037// SDNode Class
6038//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00006039
Chris Lattner48b85922007-02-04 02:41:42 +00006040HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00006041 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00006042}
6043
Andrew Trickac6d9be2013-05-25 02:42:55 +00006044GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6045 DebugLoc DL, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006046 EVT VT, int64_t o, unsigned char TF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006047 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00006048 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00006049}
Chris Lattner48b85922007-02-04 02:41:42 +00006050
Matt Arsenault59d3ae62013-11-15 01:34:59 +00006051AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6052 SDValue X, unsigned SrcAS,
6053 unsigned DestAS)
6054 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6055 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6056
Andrew Trickac6d9be2013-05-25 02:42:55 +00006057MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6058 EVT memvt, MachineMemOperand *mmo)
6059 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006060 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006061 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006062 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00006063 assert(isNonTemporal() == MMO->isNonTemporal() &&
6064 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00006065 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00006066}
6067
Andrew Trickac6d9be2013-05-25 02:42:55 +00006068MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006069 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00006070 MachineMemOperand *mmo)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006071 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00006072 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006073 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006074 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006075 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6076 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00006077}
6078
Jim Laskey583bd472006-10-27 23:46:08 +00006079/// Profile - Gather unique data for the node.
6080///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00006081void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00006082 AddNodeIDNode(ID, this);
6083}
6084
Owen Andersond8110fb2009-08-25 22:27:22 +00006085namespace {
6086 struct EVTArray {
6087 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006088
Owen Andersond8110fb2009-08-25 22:27:22 +00006089 EVTArray() {
6090 VTs.reserve(MVT::LAST_VALUETYPE);
6091 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6092 VTs.push_back(MVT((MVT::SimpleValueType)i));
6093 }
6094 };
6095}
6096
Owen Andersone50ed302009-08-10 22:56:29 +00006097static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00006098static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00006099static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00006100
Chris Lattnera3255112005-11-08 23:30:28 +00006101/// getValueTypeList - Return a pointer to the specified value type.
6102///
Owen Andersone50ed302009-08-10 22:56:29 +00006103const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006104 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00006105 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00006106 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00006107 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00006108 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00006109 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00006110 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00006111 }
Chris Lattnera3255112005-11-08 23:30:28 +00006112}
Duncan Sandsaf47b112007-10-16 09:56:48 +00006113
Chris Lattner5c884562005-01-12 18:37:47 +00006114/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6115/// indicated value. This method ignores uses of other values defined by this
6116/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00006117bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00006118 assert(Value < getNumValues() && "Bad value!");
6119
Roman Levensteindc1adac2008-04-07 10:06:32 +00006120 // TODO: Only iterate over uses of a given value of the node
6121 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00006122 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00006123 if (NUses == 0)
6124 return false;
6125 --NUses;
6126 }
Chris Lattner5c884562005-01-12 18:37:47 +00006127 }
6128
6129 // Found exactly the right number of uses?
6130 return NUses == 0;
6131}
6132
6133
Evan Cheng33d55952007-08-02 05:29:38 +00006134/// hasAnyUseOfValue - Return true if there are any use of the indicated
6135/// value. This method ignores uses of other values defined by this operation.
6136bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6137 assert(Value < getNumValues() && "Bad value!");
6138
Dan Gohman1373c1c2008-07-09 22:39:01 +00006139 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00006140 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00006141 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00006142
6143 return false;
6144}
6145
6146
Dan Gohman2a629952008-07-27 18:06:42 +00006147/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00006148///
Dan Gohman2a629952008-07-27 18:06:42 +00006149bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00006150 bool Seen = false;
6151 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006152 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006153 if (User == this)
6154 Seen = true;
6155 else
6156 return false;
6157 }
6158
6159 return Seen;
6160}
6161
Evan Chenge6e97e62006-11-03 07:31:32 +00006162/// isOperand - Return true if this node is an operand of N.
6163///
Dan Gohman475871a2008-07-27 21:46:04 +00006164bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006165 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6166 if (*this == N->getOperand(i))
6167 return true;
6168 return false;
6169}
6170
Evan Cheng917be682008-03-04 00:41:45 +00006171bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006172 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006173 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006174 return true;
6175 return false;
6176}
Evan Cheng4ee62112006-02-05 06:29:23 +00006177
Chris Lattner572dee72008-01-16 05:49:24 +00006178/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006179/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006180/// side-effecting instructions on any chain path. In practice, this looks
6181/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006182/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006183bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006184 unsigned Depth) const {
6185 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006186
Chris Lattner572dee72008-01-16 05:49:24 +00006187 // Don't search too deeply, we just want to be able to see through
6188 // TokenFactor's etc.
6189 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006190
Chris Lattner572dee72008-01-16 05:49:24 +00006191 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006192 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006193 if (getOpcode() == ISD::TokenFactor) {
6194 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006195 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6196 return false;
6197 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006199
Chris Lattner572dee72008-01-16 05:49:24 +00006200 // Loads don't have side effects, look through them.
6201 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6202 if (!Ld->isVolatile())
6203 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6204 }
6205 return false;
6206}
6207
Lang Hames944520f2011-07-07 04:31:51 +00006208/// hasPredecessor - Return true if N is a predecessor of this node.
6209/// N is either an operand of this node, or can be reached by recursively
6210/// traversing up the operands.
6211/// NOTE: This is an expensive method. Use it carefully.
6212bool SDNode::hasPredecessor(const SDNode *N) const {
6213 SmallPtrSet<const SDNode *, 32> Visited;
6214 SmallVector<const SDNode *, 16> Worklist;
6215 return hasPredecessorHelper(N, Visited, Worklist);
6216}
Dan Gohman6688d612009-10-28 03:44:30 +00006217
Craig Toppera0ec3f92013-07-14 04:42:23 +00006218bool
6219SDNode::hasPredecessorHelper(const SDNode *N,
6220 SmallPtrSet<const SDNode *, 32> &Visited,
6221 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames944520f2011-07-07 04:31:51 +00006222 if (Visited.empty()) {
6223 Worklist.push_back(this);
6224 } else {
6225 // Take a look in the visited set. If we've already encountered this node
6226 // we needn't search further.
6227 if (Visited.count(N))
6228 return true;
6229 }
6230
6231 // Haven't visited N yet. Continue the search.
6232 while (!Worklist.empty()) {
6233 const SDNode *M = Worklist.pop_back_val();
6234 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6235 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006236 if (Visited.insert(Op))
6237 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006238 if (Op == N)
6239 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006240 }
Lang Hames944520f2011-07-07 04:31:51 +00006241 }
Dan Gohman6688d612009-10-28 03:44:30 +00006242
6243 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006244}
6245
Evan Chengc5484282006-10-04 00:56:09 +00006246uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6247 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006248 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006249}
6250
Mon P Wangcd6e7252009-11-30 02:42:02 +00006251SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6252 assert(N->getNumValues() == 1 &&
6253 "Can't unroll a vector with multiple results!");
6254
6255 EVT VT = N->getValueType(0);
6256 unsigned NE = VT.getVectorNumElements();
6257 EVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006258 SDLoc dl(N);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006259
6260 SmallVector<SDValue, 8> Scalars;
6261 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6262
6263 // If ResNE is 0, fully unroll the vector op.
6264 if (ResNE == 0)
6265 ResNE = NE;
6266 else if (NE > ResNE)
6267 NE = ResNE;
6268
6269 unsigned i;
6270 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006271 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006272 SDValue Operand = N->getOperand(j);
6273 EVT OperandVT = Operand.getValueType();
6274 if (OperandVT.isVector()) {
6275 // A vector operand; extract a single element.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006276 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wangcd6e7252009-11-30 02:42:02 +00006277 EVT OperandEltVT = OperandVT.getVectorElementType();
6278 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6279 OperandEltVT,
6280 Operand,
Tom Stellard425b76c2013-08-05 22:22:01 +00006281 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006282 } else {
6283 // A scalar operand; just use it as is.
6284 Operands[j] = Operand;
6285 }
6286 }
6287
6288 switch (N->getOpcode()) {
6289 default:
6290 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6291 &Operands[0], Operands.size()));
6292 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006293 case ISD::VSELECT:
6294 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6295 &Operands[0], Operands.size()));
6296 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006297 case ISD::SHL:
6298 case ISD::SRA:
6299 case ISD::SRL:
6300 case ISD::ROTL:
6301 case ISD::ROTR:
6302 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter3af4d252013-09-09 22:02:08 +00006303 getShiftAmountOperand(Operands[0].getValueType(),
6304 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006305 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006306 case ISD::SIGN_EXTEND_INREG:
6307 case ISD::FP_ROUND_INREG: {
6308 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6309 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6310 Operands[0],
6311 getValueType(ExtVT)));
6312 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006313 }
6314 }
6315
6316 for (; i < ResNE; ++i)
6317 Scalars.push_back(getUNDEF(EltVT));
6318
6319 return getNode(ISD::BUILD_VECTOR, dl,
6320 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6321 &Scalars[0], Scalars.size());
6322}
6323
Evan Cheng64fa4a92009-12-09 01:36:00 +00006324
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006325/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6326/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006327/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006328bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006329 unsigned Bytes, int Dist) const {
6330 if (LD->getChain() != Base->getChain())
6331 return false;
6332 EVT VT = LD->getValueType(0);
6333 if (VT.getSizeInBits() / 8 != Bytes)
6334 return false;
6335
6336 SDValue Loc = LD->getOperand(1);
6337 SDValue BaseLoc = Base->getOperand(1);
6338 if (Loc.getOpcode() == ISD::FrameIndex) {
6339 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6340 return false;
6341 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6342 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6343 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6344 int FS = MFI->getObjectSize(FI);
6345 int BFS = MFI->getObjectSize(BFI);
6346 if (FS != BFS || FS != (int)Bytes) return false;
6347 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6348 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006349
6350 // Handle X+C
6351 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6352 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6353 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006354
Dan Gohman46510a72010-04-15 01:51:59 +00006355 const GlobalValue *GV1 = NULL;
6356 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006357 int64_t Offset1 = 0;
6358 int64_t Offset2 = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006359 const TargetLowering *TLI = TM.getTargetLowering();
6360 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6361 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Cheng64fa4a92009-12-09 01:36:00 +00006362 if (isGA1 && isGA2 && GV1 == GV2)
6363 return Offset1 == (Offset2 + Dist*Bytes);
6364 return false;
6365}
6366
6367
Evan Chengf2dc5c72009-12-09 01:04:59 +00006368/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6369/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006370unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006371 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006372 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006373 int64_t GVOffset = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006374 const TargetLowering *TLI = TM.getTargetLowering();
6375 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaulte6e81122013-11-16 20:50:54 +00006376 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006377 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006378 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006379 TLI->getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006380 unsigned AlignBits = KnownZero.countTrailingOnes();
6381 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6382 if (Align)
6383 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006384 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006385
Evan Chengf2dc5c72009-12-09 01:04:59 +00006386 // If this is a direct reference to a stack slot, use information about the
6387 // stack slot's alignment.
6388 int FrameIdx = 1 << 31;
6389 int64_t FrameOffset = 0;
6390 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6391 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006392 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006393 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006394 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006395 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6396 FrameOffset = Ptr.getConstantOperandVal(1);
6397 }
6398
6399 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006400 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006401 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6402 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006403 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006404 }
6405
6406 return 0;
6407}
6408
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006409// getAddressSpace - Return the address space this GlobalAddress belongs to.
6410unsigned GlobalAddressSDNode::getAddressSpace() const {
6411 return getGlobal()->getType()->getAddressSpace();
6412}
6413
6414
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006415Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006416 if (isMachineConstantPoolEntry())
6417 return Val.MachineCPVal->getType();
6418 return Val.ConstVal->getType();
6419}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006420
Bob Wilson24e338e2009-03-02 23:24:16 +00006421bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6422 APInt &SplatUndef,
6423 unsigned &SplatBitSize,
6424 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006425 unsigned MinSplatBits,
6426 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006427 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006428 assert(VT.isVector() && "Expected a vector type");
6429 unsigned sz = VT.getSizeInBits();
6430 if (MinSplatBits > sz)
6431 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006432
Bob Wilson24e338e2009-03-02 23:24:16 +00006433 SplatValue = APInt(sz, 0);
6434 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006435
Bob Wilson24e338e2009-03-02 23:24:16 +00006436 // Get the bits. Bits with undefined values (when the corresponding element
6437 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6438 // in SplatValue. If any of the values are not constant, give up and return
6439 // false.
6440 unsigned int nOps = getNumOperands();
6441 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6442 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006443
6444 for (unsigned j = 0; j < nOps; ++j) {
6445 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006446 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006447 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006448
Bob Wilson24e338e2009-03-02 23:24:16 +00006449 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006450 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006451 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006452 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006453 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006454 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006455 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006456 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006457 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006458 }
6459
Bob Wilson24e338e2009-03-02 23:24:16 +00006460 // The build_vector is all constants or undefs. Find the smallest element
6461 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006462
Bob Wilson24e338e2009-03-02 23:24:16 +00006463 HasAnyUndefs = (SplatUndef != 0);
6464 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006465
Bob Wilson24e338e2009-03-02 23:24:16 +00006466 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006467 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6468 APInt LowValue = SplatValue.trunc(HalfSize);
6469 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6470 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006471
Bob Wilson24e338e2009-03-02 23:24:16 +00006472 // If the two halves do not match (ignoring undef bits), stop here.
6473 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6474 MinSplatBits > HalfSize)
6475 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006476
Bob Wilson24e338e2009-03-02 23:24:16 +00006477 SplatValue = HighValue | LowValue;
6478 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006479
Bob Wilson24e338e2009-03-02 23:24:16 +00006480 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006481 }
6482
Bob Wilson24e338e2009-03-02 23:24:16 +00006483 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006484 return true;
6485}
Nate Begeman9008ca62009-04-27 18:41:29 +00006486
Owen Andersone50ed302009-08-10 22:56:29 +00006487bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006488 // Find the first non-undef value in the shuffle mask.
6489 unsigned i, e;
6490 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6491 /* search */;
6492
Nate Begemana6415752009-04-29 18:13:31 +00006493 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006494
Nate Begeman5a5ca152009-04-29 05:20:52 +00006495 // Make sure all remaining elements are either undef or the same as the first
6496 // non-undef value.
6497 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006498 if (Mask[i] >= 0 && Mask[i] != Idx)
6499 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006500 return true;
6501}
David Greenecf495bc2010-01-20 20:13:31 +00006502
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006503#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006504static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006505 SmallPtrSet<const SDNode*, 32> &Visited,
6506 SmallPtrSet<const SDNode*, 32> &Checked) {
6507 // If this node has already been checked, don't check it again.
6508 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006509 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006510
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006511 // If a node has already been visited on this depth-first walk, reject it as
6512 // a cycle.
6513 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006514 dbgs() << "Offending node:\n";
6515 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006516 errs() << "Detected cycle in SelectionDAG\n";
6517 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006518 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006519
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006520 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6521 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006522
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006523 Checked.insert(N);
6524 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006525}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006526#endif
David Greenecf495bc2010-01-20 20:13:31 +00006527
6528void llvm::checkForCycles(const llvm::SDNode *N) {
6529#ifdef XDEBUG
Alp Toker18a988e2013-10-29 02:35:28 +00006530 assert(N && "Checking nonexistent SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006531 SmallPtrSet<const SDNode*, 32> visited;
6532 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006533 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006534#endif
6535}
6536
6537void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6538 checkForCycles(DAG->getRoot().getNode());
6539}