blob: 94ce2ebf8c1d3cd7a7ae814c76b952cc54da1bfc [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)),
Andrew Trickdd0fb012013-05-25 03:08:10 +0000874 Root(getEntryNode()), UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000875 AllNodes.push_back(&EntryNode);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000876 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000877}
878
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000879void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000880 MF = &mf;
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000881 TTI = tti;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000882 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000883}
884
Chris Lattner78ec3112003-08-11 14:57:33 +0000885SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000886 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000887 allnodes_clear();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000888 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000889}
890
891void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000892 assert(&*AllNodes.begin() == &EntryNode);
893 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000894 while (!AllNodes.empty())
895 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000896}
897
Dan Gohman7c3234c2008-08-27 23:52:12 +0000898void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000899 allnodes_clear();
900 OperandAllocator.Reset();
901 CSEMap.clear();
902
903 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000904 ExternalSymbols.clear();
905 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000906 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
907 static_cast<CondCodeSDNode*>(0));
908 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
909 static_cast<SDNode*>(0));
910
Dan Gohmane7852d02009-01-26 04:35:06 +0000911 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000912 AllNodes.push_back(&EntryNode);
913 Root = getEntryNode();
Evan Cheng31441b72010-03-29 20:48:30 +0000914 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000915}
916
Andrew Trickac6d9be2013-05-25 02:42:55 +0000917SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotema3c42f32011-09-27 11:16:47 +0000918 return VT.bitsGT(Op.getValueType()) ?
919 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
920 getNode(ISD::TRUNCATE, DL, VT, Op);
921}
922
Andrew Trickac6d9be2013-05-25 02:42:55 +0000923SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000924 return VT.bitsGT(Op.getValueType()) ?
925 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
926 getNode(ISD::TRUNCATE, DL, VT, Op);
927}
928
Andrew Trickac6d9be2013-05-25 02:42:55 +0000929SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000930 return VT.bitsGT(Op.getValueType()) ?
931 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
932 getNode(ISD::TRUNCATE, DL, VT, Op);
933}
934
Andrew Trickac6d9be2013-05-25 02:42:55 +0000935SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000936 assert(!VT.isVector() &&
937 "getZeroExtendInReg should use the vector element type instead of "
938 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000939 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000940 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
941 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000942 VT.getSizeInBits());
943 return getNode(ISD::AND, DL, Op.getValueType(), Op,
944 getConstant(Imm, Op.getValueType()));
945}
946
Bob Wilson4c245462009-01-22 17:39:32 +0000947/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
948///
Andrew Trickac6d9be2013-05-25 02:42:55 +0000949SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000950 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000951 SDValue NegOne =
952 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000953 return getNode(ISD::XOR, DL, VT, Val, NegOne);
954}
955
Owen Andersone50ed302009-08-10 22:56:29 +0000956SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000957 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000958 assert((EltVT.getSizeInBits() >= 64 ||
959 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
960 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000961 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000962}
963
Owen Andersone50ed302009-08-10 22:56:29 +0000964SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000965 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000966}
967
Owen Andersone50ed302009-08-10 22:56:29 +0000968SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000969 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000970
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000971 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000972 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000973
Bill Wendlingba54bca2013-06-19 21:36:55 +0000974 const TargetLowering *TLI = TM.getTargetLowering();
975
Duncan Sands28b77e92011-09-06 19:07:46 +0000976 // In some cases the vector type is legal but the element type is illegal and
977 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
978 // inserted value (the type does not need to match the vector element type).
979 // Any extra bits introduced will be truncated away.
Bill Wendlingba54bca2013-06-19 21:36:55 +0000980 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sands28b77e92011-09-06 19:07:46 +0000981 TargetLowering::TypePromoteInteger) {
Bill Wendlingba54bca2013-06-19 21:36:55 +0000982 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sands28b77e92011-09-06 19:07:46 +0000983 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
984 Elt = ConstantInt::get(*getContext(), NewVal);
985 }
986
987 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
988 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000989 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000990 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000991 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +0000992 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +0000993 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000994 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000995 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000996 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000997 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000998
Dan Gohman89081322007-12-12 22:21:26 +0000999 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001000 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001001 CSEMap.InsertNode(N, IP);
1002 AllNodes.push_back(N);
1003 }
1004
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001006 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001007 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001008 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001009 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001010 }
1011 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001012}
1013
Dan Gohman475871a2008-07-27 21:46:04 +00001014SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001015 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner0bd48932008-01-17 07:00:52 +00001016}
1017
1018
Owen Andersone50ed302009-08-10 22:56:29 +00001019SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001020 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001021}
1022
Owen Andersone50ed302009-08-10 22:56:29 +00001023SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001024 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001025
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001026 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001027
Chris Lattnerd8658612005-02-17 20:17:32 +00001028 // Do the map lookup using the actual bit pattern for the floating point
1029 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1030 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001031 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001032 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001033 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001034 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001035 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001036 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001037 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001038 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001039 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001040
Evan Chengc908dcd2007-06-29 21:36:04 +00001041 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001042 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001043 CSEMap.InsertNode(N, IP);
1044 AllNodes.push_back(N);
1045 }
1046
Dan Gohman475871a2008-07-27 21:46:04 +00001047 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001048 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001049 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001050 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001051 // FIXME SDLoc info might be appropriate here
1052 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001053 }
1054 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055}
1056
Owen Andersone50ed302009-08-10 22:56:29 +00001057SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001058 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001059 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001060 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001061 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001062 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001063 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1064 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001065 bool ignored;
1066 APFloat apf = APFloat(Val);
Tim Northover0a29cb02013-01-22 09:46:31 +00001067 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001068 &ignored);
1069 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001070 } else
1071 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001072}
1073
Andrew Trickac6d9be2013-05-25 02:42:55 +00001074SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001075 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001076 bool isTargetGA,
1077 unsigned char TargetFlags) {
1078 assert((TargetFlags == 0 || isTargetGA) &&
1079 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001080
Dan Gohman6520e202008-10-18 02:06:02 +00001081 // Truncate (with sign-extension) the offset value to the pointer size.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001082 unsigned BitWidth = TM.getTargetLowering()->getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +00001083 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001084 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001085
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001086 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1087 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001088 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001089 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001090 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001091 }
1092
Chris Lattner2a4ed822009-06-25 21:21:14 +00001093 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001094 if (GVar && GVar->isThreadLocal())
1095 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1096 else
1097 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001098
Jim Laskey583bd472006-10-27 23:46:08 +00001099 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001100 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001101 ID.AddPointer(GV);
1102 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001103 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001104 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001105 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001106 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001107 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001108
Andrew Trickac6d9be2013-05-25 02:42:55 +00001109 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1110 DL.getDebugLoc(), GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001111 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001112 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001113 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001114 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001115}
1116
Owen Andersone50ed302009-08-10 22:56:29 +00001117SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001118 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001119 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001120 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001121 ID.AddInteger(FI);
1122 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001123 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001124 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001125
Dan Gohman95531882010-03-18 18:49:47 +00001126 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001127 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001128 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001130}
1131
Owen Andersone50ed302009-08-10 22:56:29 +00001132SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001133 unsigned char TargetFlags) {
1134 assert((TargetFlags == 0 || isTarget) &&
1135 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001136 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001137 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001138 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001139 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001140 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001141 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001142 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001143 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001144
Dan Gohman95531882010-03-18 18:49:47 +00001145 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1146 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001147 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001148 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001149 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001150}
1151
Dan Gohman46510a72010-04-15 01:51:59 +00001152SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001153 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001154 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001155 unsigned char TargetFlags) {
1156 assert((TargetFlags == 0 || isTarget) &&
1157 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001158 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001159 Alignment =
1160 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001161 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001162 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001163 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001164 ID.AddInteger(Alignment);
1165 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001166 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001167 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001168 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001169 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001170 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001171
Dan Gohman95531882010-03-18 18:49:47 +00001172 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1173 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001174 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001175 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001176 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001177}
1178
Chris Lattnerc3aae252005-01-07 07:46:32 +00001179
Owen Andersone50ed302009-08-10 22:56:29 +00001180SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001181 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001182 bool isTarget,
1183 unsigned char TargetFlags) {
1184 assert((TargetFlags == 0 || isTarget) &&
1185 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001186 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001187 Alignment =
1188 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001189 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001190 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001191 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001192 ID.AddInteger(Alignment);
1193 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001194 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001195 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001196 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001197 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001198 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001199
Dan Gohman95531882010-03-18 18:49:47 +00001200 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1201 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001202 CSEMap.InsertNode(N, IP);
1203 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001204 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001205}
1206
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001207SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1208 unsigned char TargetFlags) {
1209 FoldingSetNodeID ID;
1210 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1211 ID.AddInteger(Index);
1212 ID.AddInteger(Offset);
1213 ID.AddInteger(TargetFlags);
1214 void *IP = 0;
1215 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1216 return SDValue(E, 0);
1217
1218 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1219 TargetFlags);
1220 CSEMap.InsertNode(N, IP);
1221 AllNodes.push_back(N);
1222 return SDValue(N, 0);
1223}
1224
Dan Gohman475871a2008-07-27 21:46:04 +00001225SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001226 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001228 ID.AddPointer(MBB);
1229 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001230 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001231 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001232
Dan Gohman95531882010-03-18 18:49:47 +00001233 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001234 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001235 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001236 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001237}
1238
Owen Andersone50ed302009-08-10 22:56:29 +00001239SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1241 ValueTypeNodes.size())
1242 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001243
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001245 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001246
Dan Gohman475871a2008-07-27 21:46:04 +00001247 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001248 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001249 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001250 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001251}
1252
Owen Andersone50ed302009-08-10 22:56:29 +00001253SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001254 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001255 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001256 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001257 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001258 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001259}
1260
Owen Andersone50ed302009-08-10 22:56:29 +00001261SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001262 unsigned char TargetFlags) {
1263 SDNode *&N =
1264 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1265 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001266 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001267 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001268 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001269 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001270}
1271
Dan Gohman475871a2008-07-27 21:46:04 +00001272SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001273 if ((unsigned)Cond >= CondCodeNodes.size())
1274 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001275
Chris Lattner079a27a2005-08-09 20:40:02 +00001276 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001277 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001278 CondCodeNodes[Cond] = N;
1279 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001280 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001281
Dan Gohman475871a2008-07-27 21:46:04 +00001282 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001283}
1284
Nate Begeman5a5ca152009-04-29 05:20:52 +00001285// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1286// the shuffle mask M that point at N1 to point at N2, and indices that point
1287// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001288static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1289 std::swap(N1, N2);
1290 int NElts = M.size();
1291 for (int i = 0; i != NElts; ++i) {
1292 if (M[i] >= NElts)
1293 M[i] -= NElts;
1294 else if (M[i] >= 0)
1295 M[i] += NElts;
1296 }
1297}
1298
Andrew Trickac6d9be2013-05-25 02:42:55 +00001299SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001300 SDValue N2, const int *Mask) {
Craig Topperad445a62013-08-09 04:37:24 +00001301 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1302 "Invalid VECTOR_SHUFFLE");
Nate Begeman9008ca62009-04-27 18:41:29 +00001303
1304 // Canonicalize shuffle undef, undef -> undef
1305 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001306 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001307
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001308 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001309 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001310 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001311 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001312 for (unsigned i = 0; i != NElts; ++i) {
1313 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001314 MaskVec.push_back(Mask[i]);
1315 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001316
Nate Begeman9008ca62009-04-27 18:41:29 +00001317 // Canonicalize shuffle v, v -> v, undef
1318 if (N1 == N2) {
1319 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001320 for (unsigned i = 0; i != NElts; ++i)
1321 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001322 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001323
Nate Begeman9008ca62009-04-27 18:41:29 +00001324 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1325 if (N1.getOpcode() == ISD::UNDEF)
1326 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001327
Nate Begeman9008ca62009-04-27 18:41:29 +00001328 // Canonicalize all index into lhs, -> shuffle lhs, undef
1329 // Canonicalize all index into rhs, -> shuffle rhs, undef
1330 bool AllLHS = true, AllRHS = true;
1331 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001332 for (unsigned i = 0; i != NElts; ++i) {
1333 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001334 if (N2Undef)
1335 MaskVec[i] = -1;
1336 else
1337 AllLHS = false;
1338 } else if (MaskVec[i] >= 0) {
1339 AllRHS = false;
1340 }
1341 }
1342 if (AllLHS && AllRHS)
1343 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001344 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001345 N2 = getUNDEF(VT);
1346 if (AllRHS) {
1347 N1 = getUNDEF(VT);
1348 commuteShuffle(N1, N2, MaskVec);
1349 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001350
Craig Toppereee2a112013-08-08 08:03:12 +00001351 // If Identity shuffle return that node.
Nate Begeman9008ca62009-04-27 18:41:29 +00001352 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001353 for (unsigned i = 0; i != NElts; ++i) {
1354 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001355 }
Craig Topperad445a62013-08-09 04:37:24 +00001356 if (Identity && NElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00001357 return N1;
Nate Begeman9008ca62009-04-27 18:41:29 +00001358
1359 FoldingSetNodeID ID;
1360 SDValue Ops[2] = { N1, N2 };
1361 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001362 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001363 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001364
Nate Begeman9008ca62009-04-27 18:41:29 +00001365 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001366 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001367 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001368
Nate Begeman9008ca62009-04-27 18:41:29 +00001369 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1370 // SDNode doesn't have access to it. This memory will be "leaked" when
1371 // the node is deallocated, but recovered when the NodeAllocator is released.
1372 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1373 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001374
Dan Gohman95531882010-03-18 18:49:47 +00001375 ShuffleVectorSDNode *N =
Jack Carter3af4d252013-09-09 22:02:08 +00001376 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1377 dl.getDebugLoc(), N1, N2,
1378 MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001379 CSEMap.InsertNode(N, IP);
1380 AllNodes.push_back(N);
1381 return SDValue(N, 0);
1382}
1383
Andrew Trickac6d9be2013-05-25 02:42:55 +00001384SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001385 SDValue Val, SDValue DTy,
1386 SDValue STy, SDValue Rnd, SDValue Sat,
1387 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001388 // If the src and dest types are the same and the conversion is between
1389 // integer types of the same sign or two floats, no conversion is necessary.
1390 if (DTy == STy &&
1391 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001392 return Val;
1393
1394 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001395 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1396 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001397 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001398 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001399 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001400
Jack Carter3af4d252013-09-09 22:02:08 +00001401 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1402 dl.getDebugLoc(),
1403 Ops, 5, Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001404 CSEMap.InsertNode(N, IP);
1405 AllNodes.push_back(N);
1406 return SDValue(N, 0);
1407}
1408
Owen Andersone50ed302009-08-10 22:56:29 +00001409SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001410 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001411 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001412 ID.AddInteger(RegNo);
1413 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001414 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001415 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001416
Dan Gohman95531882010-03-18 18:49:47 +00001417 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001418 CSEMap.InsertNode(N, IP);
1419 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001420 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001421}
1422
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001423SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1424 FoldingSetNodeID ID;
1425 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1426 ID.AddPointer(RegMask);
1427 void *IP = 0;
1428 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1429 return SDValue(E, 0);
1430
1431 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1432 CSEMap.InsertNode(N, IP);
1433 AllNodes.push_back(N);
1434 return SDValue(N, 0);
1435}
1436
Andrew Trickac6d9be2013-05-25 02:42:55 +00001437SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001438 FoldingSetNodeID ID;
1439 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001440 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1441 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001442 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001443 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001444 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001445
Jack Carter3af4d252013-09-09 22:02:08 +00001446 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1447 dl.getDebugLoc(), Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001448 CSEMap.InsertNode(N, IP);
1449 AllNodes.push_back(N);
1450 return SDValue(N, 0);
1451}
1452
Chris Lattner7561d482010-03-14 02:33:54 +00001453
Dan Gohman46510a72010-04-15 01:51:59 +00001454SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001455 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001456 bool isTarget,
1457 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001458 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1459
1460 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001461 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001462 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001463 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001464 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001465 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001466 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001467 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001468
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001469 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1470 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001471 CSEMap.InsertNode(N, IP);
1472 AllNodes.push_back(N);
1473 return SDValue(N, 0);
1474}
1475
Dan Gohman475871a2008-07-27 21:46:04 +00001476SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001477 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001478 "SrcValue is not a pointer?");
1479
Jim Laskey583bd472006-10-27 23:46:08 +00001480 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001481 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001482 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001483
Chris Lattner61b09412006-08-11 21:01:22 +00001484 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001485 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001486 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001487
Dan Gohman95531882010-03-18 18:49:47 +00001488 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001489 CSEMap.InsertNode(N, IP);
1490 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001491 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001492}
1493
Chris Lattnerdecc2672010-04-07 05:20:54 +00001494/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1495SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1496 FoldingSetNodeID ID;
1497 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1498 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001499
Chris Lattnerdecc2672010-04-07 05:20:54 +00001500 void *IP = 0;
1501 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1502 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001503
Chris Lattnerdecc2672010-04-07 05:20:54 +00001504 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1505 CSEMap.InsertNode(N, IP);
1506 AllNodes.push_back(N);
1507 return SDValue(N, 0);
1508}
1509
1510
Duncan Sands92abc622009-01-31 15:50:11 +00001511/// getShiftAmountOperand - Return the specified value casted to
1512/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001513SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001514 EVT OpTy = Op.getValueType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001515 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001516 if (OpTy == ShTy || OpTy.isVector()) return Op;
1517
1518 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001519 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001520}
1521
Chris Lattner37ce9df2007-10-15 17:47:20 +00001522/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1523/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001524SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001525 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001526 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001527 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001528 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang364d73d2008-07-05 20:40:31 +00001529 unsigned StackAlign =
Bill Wendlingba54bca2013-06-19 21:36:55 +00001530 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001531
David Greene3f2bf852009-11-12 20:49:22 +00001532 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001533 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner37ce9df2007-10-15 17:47:20 +00001534}
1535
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001536/// CreateStackTemporary - Create a stack temporary suitable for holding
1537/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001538SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001539 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1540 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001541 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1542 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001543 const TargetLowering *TLI = TM.getTargetLowering();
1544 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001545 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1546 TD->getPrefTypeAlignment(Ty2));
1547
1548 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001549 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001550 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001551}
1552
Owen Andersone50ed302009-08-10 22:56:29 +00001553SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001554 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001555 // These setcc operations always fold.
1556 switch (Cond) {
1557 default: break;
1558 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001559 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001560 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001561 case ISD::SETTRUE2: {
1562 const TargetLowering *TLI = TM.getTargetLowering();
1563 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1564 return getConstant(
1565 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001567
Chris Lattnera83385f2006-04-27 05:01:07 +00001568 case ISD::SETOEQ:
1569 case ISD::SETOGT:
1570 case ISD::SETOGE:
1571 case ISD::SETOLT:
1572 case ISD::SETOLE:
1573 case ISD::SETONE:
1574 case ISD::SETO:
1575 case ISD::SETUO:
1576 case ISD::SETUEQ:
1577 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001578 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001579 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Gabor Greifba36cb52008-08-28 21:40:38 +00001582 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001583 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001584 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001585 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001586
Chris Lattnerc3aae252005-01-07 07:46:32 +00001587 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001588 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001589 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1590 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001591 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1592 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1593 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1594 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1595 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1596 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1597 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1598 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001599 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001600 }
Chris Lattner67255a12005-04-07 18:14:58 +00001601 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001602 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1603 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001604 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001605 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001606 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001607 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001608 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001609 // fall through
1610 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001611 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001612 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001613 // fall through
1614 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001615 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001616 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001617 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001618 // fall through
1619 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001620 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001621 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001622 // fall through
1623 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001624 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001625 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001626 // fall through
1627 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001628 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001629 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001630 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001631 // fall through
1632 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001633 R==APFloat::cmpEqual, VT);
1634 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1635 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1636 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1637 R==APFloat::cmpEqual, VT);
1638 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1639 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1640 R==APFloat::cmpLessThan, VT);
1641 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1642 R==APFloat::cmpUnordered, VT);
1643 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1644 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001645 }
1646 } else {
1647 // Ensure that the constant occurs on the RHS.
Tom Stellard12d43f92013-09-28 02:50:38 +00001648 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1649 MVT CompVT = N1.getValueType().getSimpleVT();
1650 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1651 return SDValue();
1652
1653 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001654 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001655 }
1656
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001657 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001658 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001659}
1660
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001661/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1662/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001663bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001664 // This predicate is not safe for vector operations.
1665 if (Op.getValueType().isVector())
1666 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001667
Dan Gohman87862e72009-12-11 21:31:27 +00001668 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001669 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1670}
1671
Dan Gohmanea859be2007-06-22 14:59:07 +00001672/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1673/// this predicate to simplify operations downstream. Mask is known to be zero
1674/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001675bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001676 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001677 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001678 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001679 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001680 return (KnownZero & Mask) == Mask;
1681}
1682
1683/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1684/// known to be either zero or one and return them in the KnownZero/KnownOne
1685/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1686/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001687void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1688 APInt &KnownOne, unsigned Depth) const {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001689 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001690 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001691
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001692 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001693 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001694 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001695
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001696 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001697
1698 switch (Op.getOpcode()) {
1699 case ISD::Constant:
1700 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001701 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1702 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001703 return;
1704 case ISD::AND:
1705 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001706 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1707 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001708 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1709 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001710
1711 // Output known-1 bits are only known if set in both the LHS & RHS.
1712 KnownOne &= KnownOne2;
1713 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1714 KnownZero |= KnownZero2;
1715 return;
1716 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001717 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1718 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001719 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1720 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1721
Dan Gohmanea859be2007-06-22 14:59:07 +00001722 // Output known-0 bits are only known if clear in both the LHS & RHS.
1723 KnownZero &= KnownZero2;
1724 // Output known-1 are known to be set if set in either the LHS | RHS.
1725 KnownOne |= KnownOne2;
1726 return;
1727 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001728 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1729 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001730 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1731 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1732
Dan Gohmanea859be2007-06-22 14:59:07 +00001733 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001734 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001735 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1736 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1737 KnownZero = KnownZeroOut;
1738 return;
1739 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001740 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001741 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1742 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001743 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1744 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1745
1746 // If low bits are zero in either operand, output low known-0 bits.
1747 // Also compute a conserative estimate for high known-0 bits.
1748 // More trickiness is possible, but this is sufficient for the
1749 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001750 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001751 unsigned TrailZ = KnownZero.countTrailingOnes() +
1752 KnownZero2.countTrailingOnes();
1753 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001754 KnownZero2.countLeadingOnes(),
1755 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001756
1757 TrailZ = std::min(TrailZ, BitWidth);
1758 LeadZ = std::min(LeadZ, BitWidth);
1759 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1760 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001761 return;
1762 }
1763 case ISD::UDIV: {
1764 // For the purposes of computing leading zeros we can conservatively
1765 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001766 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001767 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001768 unsigned LeadZ = KnownZero2.countLeadingOnes();
1769
Jay Foad7a874dd2010-12-01 08:53:58 +00001770 KnownOne2.clearAllBits();
1771 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001772 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001773 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1774 if (RHSUnknownLeadingOnes != BitWidth)
1775 LeadZ = std::min(BitWidth,
1776 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001777
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001778 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001779 return;
1780 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001781 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001782 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1783 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001784 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1785 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1786
Dan Gohmanea859be2007-06-22 14:59:07 +00001787 // Only known if known in both the LHS and RHS.
1788 KnownOne &= KnownOne2;
1789 KnownZero &= KnownZero2;
1790 return;
1791 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001792 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1793 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001794 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1795 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1796
Dan Gohmanea859be2007-06-22 14:59:07 +00001797 // Only known if known in both the LHS and RHS.
1798 KnownOne &= KnownOne2;
1799 KnownZero &= KnownZero2;
1800 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001801 case ISD::SADDO:
1802 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001803 case ISD::SSUBO:
1804 case ISD::USUBO:
1805 case ISD::SMULO:
1806 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001807 if (Op.getResNo() != 1)
1808 return;
Duncan Sands03228082008-11-23 15:47:28 +00001809 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001810 case ISD::SETCC:
1811 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001812 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands28b77e92011-09-06 19:07:46 +00001813 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001814 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001815 return;
1816 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001817 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001818 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001819 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001820
1821 // If the shift count is an invalid immediate, don't do anything.
1822 if (ShAmt >= BitWidth)
1823 return;
1824
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001825 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001826 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001827 KnownZero <<= ShAmt;
1828 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001829 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001830 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001831 }
1832 return;
1833 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001834 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001836 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001837
Dan Gohmand4cf9922008-02-26 18:50:50 +00001838 // If the shift count is an invalid immediate, don't do anything.
1839 if (ShAmt >= BitWidth)
1840 return;
1841
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001842 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001843 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001844 KnownZero = KnownZero.lshr(ShAmt);
1845 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001846
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001847 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001848 KnownZero |= HighBits; // High bits known zero.
1849 }
1850 return;
1851 case ISD::SRA:
1852 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001853 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001854
Dan Gohmand4cf9922008-02-26 18:50:50 +00001855 // If the shift count is an invalid immediate, don't do anything.
1856 if (ShAmt >= BitWidth)
1857 return;
1858
Dan Gohmanea859be2007-06-22 14:59:07 +00001859 // If any of the demanded bits are produced by the sign extension, we also
1860 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001861 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001863 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001864 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001865 KnownZero = KnownZero.lshr(ShAmt);
1866 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001867
Dan Gohmanea859be2007-06-22 14:59:07 +00001868 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001869 APInt SignBit = APInt::getSignBit(BitWidth);
1870 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001871
Dan Gohmanca93a432008-02-20 16:30:17 +00001872 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001873 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001874 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001875 KnownOne |= HighBits; // New bits are known one.
1876 }
1877 }
1878 return;
1879 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001880 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001881 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001882
1883 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001884 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001885 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001886
Dan Gohman977a76f2008-02-13 22:28:48 +00001887 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001888 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001889
Dan Gohmanea859be2007-06-22 14:59:07 +00001890 // If the sign extended bits are demanded, we know that the sign
1891 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001892 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001893 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001894 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001895
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001896 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1897 KnownOne &= InputDemandedBits;
1898 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001899 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1900
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 // If the sign bit of the input is known set or clear, then we know the
1902 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001903 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001904 KnownZero |= NewBits;
1905 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001906 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001907 KnownOne |= NewBits;
1908 KnownZero &= ~NewBits;
1909 } else { // Input sign bit unknown
1910 KnownZero &= ~NewBits;
1911 KnownOne &= ~NewBits;
1912 }
1913 return;
1914 }
1915 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001916 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001917 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001918 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001919 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001920 unsigned LowBits = Log2_32(BitWidth)+1;
1921 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001922 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001923 return;
1924 }
1925 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001926 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00001927 // If this is a ZEXTLoad and we are looking at the loaded value.
1928 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00001929 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001930 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001931 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00001932 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001933 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001934 }
1935 return;
1936 }
1937 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001938 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001939 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001940 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00001941 KnownZero = KnownZero.trunc(InBits);
1942 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001943 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001944 KnownZero = KnownZero.zext(BitWidth);
1945 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001946 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001947 return;
1948 }
1949 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001950 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001951 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001952 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001953
Jay Foad40f8f622010-12-07 08:25:19 +00001954 KnownZero = KnownZero.trunc(InBits);
1955 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001956 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001957
1958 // Note if the sign bit is known to be zero or one.
1959 bool SignBitKnownZero = KnownZero.isNegative();
1960 bool SignBitKnownOne = KnownOne.isNegative();
1961 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1962 "Sign bit can't be known to be both zero and one!");
1963
Jay Foad40f8f622010-12-07 08:25:19 +00001964 KnownZero = KnownZero.zext(BitWidth);
1965 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001966
Dan Gohman977a76f2008-02-13 22:28:48 +00001967 // If the sign bit is known zero or one, the top bits match.
1968 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001969 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001970 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001971 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001972 return;
1973 }
1974 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00001975 EVT InVT = Op.getOperand(0).getValueType();
1976 unsigned InBits = InVT.getScalarType().getSizeInBits();
1977 KnownZero = KnownZero.trunc(InBits);
1978 KnownOne = KnownOne.trunc(InBits);
1979 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1980 KnownZero = KnownZero.zext(BitWidth);
1981 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001982 return;
1983 }
1984 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001985 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001986 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001987 KnownZero = KnownZero.zext(InBits);
1988 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001989 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001990 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001991 KnownZero = KnownZero.trunc(BitWidth);
1992 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001993 break;
1994 }
1995 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001996 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001997 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001998 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1999 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002000 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002001 return;
2002 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002003 case ISD::FGETSIGN:
2004 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002005 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00002006 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00002007
Dan Gohman23e8b712008-04-28 17:02:21 +00002008 case ISD::SUB: {
2009 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2010 // We know that the top bits of C-X are clear if X contains less bits
2011 // than C (i.e. no wrap-around can happen). For example, 20-X is
2012 // positive if we can prove that X is >= 0 and < 16.
2013 if (CLHS->getAPIntValue().isNonNegative()) {
2014 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2015 // NLZ can't be BitWidth with no sign bit
2016 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002017 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002018
2019 // If all of the MaskV bits are known to be zero, then we know the
2020 // output top bits are zero, because we now know that the output is
2021 // from [0-C].
2022 if ((KnownZero2 & MaskV) == MaskV) {
2023 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2024 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002025 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002026 }
2027 }
2028 }
2029 }
2030 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002031 case ISD::ADD:
2032 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002033 // Output known-0 bits are known if clear or set in both the low clear bits
2034 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2035 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002036 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002037 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002038 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2039
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002040 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002041 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002042 KnownZeroOut = std::min(KnownZeroOut,
2043 KnownZero2.countTrailingOnes());
2044
Chris Lattnerda605882010-12-19 20:38:28 +00002045 if (Op.getOpcode() == ISD::ADD) {
2046 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2047 return;
2048 }
2049
2050 // With ADDE, a carry bit may be added in, so we can only use this
2051 // information if we know (at least) that the low two bits are clear. We
2052 // then return to the caller that the low bit is unknown but that other bits
2053 // are known zero.
2054 if (KnownZeroOut >= 2) // ADDE
2055 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002056 return;
2057 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002058 case ISD::SREM:
2059 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002060 const APInt &RA = Rem->getAPIntValue().abs();
2061 if (RA.isPowerOf2()) {
2062 APInt LowBits = RA - 1;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002063 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002064
Duncan Sands5c2873a2010-01-29 09:45:26 +00002065 // The low bits of the first operand are unchanged by the srem.
2066 KnownZero = KnownZero2 & LowBits;
2067 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002068
Duncan Sands5c2873a2010-01-29 09:45:26 +00002069 // If the first operand is non-negative or has all low bits zero, then
2070 // the upper bits are all zero.
2071 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2072 KnownZero |= ~LowBits;
2073
2074 // If the first operand is negative and not all low bits are zero, then
2075 // the upper bits are all one.
2076 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2077 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002078 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002079 }
2080 }
2081 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002082 case ISD::UREM: {
2083 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002084 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002085 if (RA.isPowerOf2()) {
2086 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002087 KnownZero |= ~LowBits;
2088 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002089 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2090 break;
2091 }
2092 }
2093
2094 // Since the result is less than or equal to either operand, any leading
2095 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002096 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2097 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002098
2099 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2100 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002101 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002102 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002103 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002104 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002105 case ISD::FrameIndex:
2106 case ISD::TargetFrameIndex:
2107 if (unsigned Align = InferPtrAlignment(Op)) {
2108 // The low bits are known zero if the pointer is aligned.
2109 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2110 return;
2111 }
2112 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002113
Dan Gohmanea859be2007-06-22 14:59:07 +00002114 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002115 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2116 break;
2117 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002118 case ISD::INTRINSIC_WO_CHAIN:
2119 case ISD::INTRINSIC_W_CHAIN:
2120 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002121 // Allow the target to implement this method for its nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002122 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002123 return;
2124 }
2125}
2126
2127/// ComputeNumSignBits - Return the number of times the sign bit of the
2128/// register is replicated into the other bits. We know that at least 1 bit
2129/// is always equal to the sign bit (itself), but other cases can give us
2130/// information. For example, immediately after an "SRA X, 2", we know that
2131/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002132unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlingba54bca2013-06-19 21:36:55 +00002133 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00002134 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002135 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002136 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002137 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002138 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002139
Dan Gohmanea859be2007-06-22 14:59:07 +00002140 if (Depth == 6)
2141 return 1; // Limit search depth.
2142
2143 switch (Op.getOpcode()) {
2144 default: break;
2145 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002146 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002147 return VTBits-Tmp+1;
2148 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002149 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002150 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002151
Dan Gohmanea859be2007-06-22 14:59:07 +00002152 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002153 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002154 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002156
Dan Gohmanea859be2007-06-22 14:59:07 +00002157 case ISD::SIGN_EXTEND:
Jack Carter3af4d252013-09-09 22:02:08 +00002158 Tmp =
2159 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002160 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002161
Dan Gohmanea859be2007-06-22 14:59:07 +00002162 case ISD::SIGN_EXTEND_INREG:
2163 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002164 Tmp =
2165 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002166 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002167
Dan Gohmanea859be2007-06-22 14:59:07 +00002168 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2169 return std::max(Tmp, Tmp2);
2170
2171 case ISD::SRA:
2172 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2173 // SRA X, C -> adds C sign bits.
2174 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002175 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002176 if (Tmp > VTBits) Tmp = VTBits;
2177 }
2178 return Tmp;
2179 case ISD::SHL:
2180 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2181 // shl destroys sign bits.
2182 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002183 if (C->getZExtValue() >= VTBits || // Bad shift.
2184 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2185 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002186 }
2187 break;
2188 case ISD::AND:
2189 case ISD::OR:
2190 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002191 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002192 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002193 if (Tmp != 1) {
2194 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2195 FirstAnswer = std::min(Tmp, Tmp2);
2196 // We computed what we know about the sign bits as our first
2197 // answer. Now proceed to the generic code that uses
2198 // ComputeMaskedBits, and pick whichever answer is better.
2199 }
2200 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002201
2202 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002203 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002204 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002205 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002206 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002207
2208 case ISD::SADDO:
2209 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002210 case ISD::SSUBO:
2211 case ISD::USUBO:
2212 case ISD::SMULO:
2213 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002214 if (Op.getResNo() != 1)
2215 break;
Duncan Sands03228082008-11-23 15:47:28 +00002216 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002217 case ISD::SETCC:
2218 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002219 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002220 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002221 return VTBits;
2222 break;
2223 case ISD::ROTL:
2224 case ISD::ROTR:
2225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002226 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002227
Dan Gohmanea859be2007-06-22 14:59:07 +00002228 // Handle rotate right by N like a rotate left by 32-N.
2229 if (Op.getOpcode() == ISD::ROTR)
2230 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2231
2232 // If we aren't rotating out all of the known-in sign bits, return the
2233 // number that are left. This handles rotl(sext(x), 1) for example.
2234 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2235 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2236 }
2237 break;
2238 case ISD::ADD:
2239 // Add can have at most one carry bit. Thus we know that the output
2240 // is, at worst, one more bit than the inputs.
2241 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2242 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002243
Dan Gohmanea859be2007-06-22 14:59:07 +00002244 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002245 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002246 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002247 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002248 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002249
Dan Gohmanea859be2007-06-22 14:59:07 +00002250 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2251 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002252 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002253 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002254
Dan Gohmanea859be2007-06-22 14:59:07 +00002255 // If we are subtracting one from a positive number, there is no carry
2256 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002257 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 return Tmp;
2259 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002260
Dan Gohmanea859be2007-06-22 14:59:07 +00002261 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2262 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002263 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002264
Dan Gohmanea859be2007-06-22 14:59:07 +00002265 case ISD::SUB:
2266 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2267 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002268
Dan Gohmanea859be2007-06-22 14:59:07 +00002269 // Handle NEG.
2270 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002271 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002272 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002273 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002274 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2275 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002276 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002277 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002278
Dan Gohmanea859be2007-06-22 14:59:07 +00002279 // If the input is known to be positive (the sign bit is known clear),
2280 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002281 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002282 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002283
Dan Gohmanea859be2007-06-22 14:59:07 +00002284 // Otherwise, we treat this like a SUB.
2285 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Dan Gohmanea859be2007-06-22 14:59:07 +00002287 // Sub can have at most one carry bit. Thus we know that the output
2288 // is, at worst, one more bit than the inputs.
2289 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2290 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002291 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002292 case ISD::TRUNCATE:
2293 // FIXME: it's tricky to do anything useful for this, but it is an important
2294 // case for targets like X86.
2295 break;
2296 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002297
Nadav Rotem77451752013-03-20 22:53:44 +00002298 // If we are looking at the loaded value of the SDNode.
2299 if (Op.getResNo() == 0) {
2300 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2301 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2302 unsigned ExtType = LD->getExtensionType();
2303 switch (ExtType) {
2304 default: break;
2305 case ISD::SEXTLOAD: // '17' bits known
2306 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2307 return VTBits-Tmp+1;
2308 case ISD::ZEXTLOAD: // '16' bits known
2309 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2310 return VTBits-Tmp;
2311 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002312 }
2313 }
2314
2315 // Allow the target to implement this method for its nodes.
2316 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002317 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002318 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2319 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002320 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002321 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002322 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002323
Dan Gohmanea859be2007-06-22 14:59:07 +00002324 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2325 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002326 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002327 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002328
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002329 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002330 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002331 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002332 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002333 Mask = KnownOne;
2334 } else {
2335 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002336 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002338
Dan Gohmanea859be2007-06-22 14:59:07 +00002339 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2340 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002341 Mask = ~Mask;
2342 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002343 // Return # leading zeros. We use 'min' here in case Val was zero before
2344 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002345 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002346}
2347
Chris Lattner0a9481f2011-02-13 22:25:43 +00002348/// isBaseWithConstantOffset - Return true if the specified operand is an
2349/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2350/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2351/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002352/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002353bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2354 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2355 !isa<ConstantSDNode>(Op.getOperand(1)))
2356 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002357
2358 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002359 !MaskedValueIsZero(Op.getOperand(0),
2360 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2361 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002362
Chris Lattner0a9481f2011-02-13 22:25:43 +00002363 return true;
2364}
2365
2366
Dan Gohman8d44b282009-09-03 20:34:31 +00002367bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2368 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002369 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002370 return true;
2371
2372 // If the value is a constant, we can obviously see if it is a NaN or not.
2373 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2374 return !C->getValueAPF().isNaN();
2375
2376 // TODO: Recognize more cases here.
2377
2378 return false;
2379}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002380
Dan Gohmane8326932010-02-24 06:52:40 +00002381bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2382 // If the value is a constant, we can obviously see if it is a zero or not.
2383 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2384 return !C->isZero();
2385
2386 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002387 switch (Op.getOpcode()) {
2388 default: break;
2389 case ISD::OR:
2390 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2391 return !C->isNullValue();
2392 break;
2393 }
Dan Gohmane8326932010-02-24 06:52:40 +00002394
2395 return false;
2396}
2397
2398bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2399 // Check the obvious case.
2400 if (A == B) return true;
2401
2402 // For for negative and positive zero.
2403 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2404 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2405 if (CA->isZero() && CB->isZero()) return true;
2406
2407 // Otherwise they may not be equal.
2408 return false;
2409}
2410
Chris Lattnerc3aae252005-01-07 07:46:32 +00002411/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002412///
Andrew Trickac6d9be2013-05-25 02:42:55 +00002413SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002414 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002415 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002416 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002417 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002418 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002419
Jack Carter3af4d252013-09-09 22:02:08 +00002420 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2421 DL.getDebugLoc(), getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002422 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002423
Chris Lattner4a283e92006-08-11 18:38:11 +00002424 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002425#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002426 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002427#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002428 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002429}
2430
Andrew Trickac6d9be2013-05-25 02:42:55 +00002431SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002432 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002433 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002435 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002436 switch (Opcode) {
2437 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002438 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002439 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002440 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002441 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002442 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002443 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002444 case ISD::UINT_TO_FP:
2445 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002446 APFloat apf(EVTToAPFloatSemantics(VT),
2447 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002448 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002449 Opcode==ISD::SINT_TO_FP,
2450 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002451 return getConstantFP(apf, VT);
2452 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002453 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002455 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002456 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002457 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002458 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002459 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002460 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002461 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002462 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002463 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002464 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002465 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002466 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002467 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002468 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002469 }
2470 }
2471
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002472 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002473 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002474 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002475 switch (Opcode) {
2476 case ISD::FNEG:
2477 V.changeSign();
2478 return getConstantFP(V, VT);
2479 case ISD::FABS:
2480 V.clearSign();
2481 return getConstantFP(V, VT);
2482 case ISD::FCEIL: {
2483 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2484 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002485 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002486 break;
2487 }
2488 case ISD::FTRUNC: {
2489 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2490 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002491 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002492 break;
2493 }
2494 case ISD::FFLOOR: {
2495 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2496 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002497 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002498 break;
2499 }
2500 case ISD::FP_EXTEND: {
2501 bool ignored;
2502 // This can return overflow, underflow, or inexact; we don't care.
2503 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002504 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002505 APFloat::rmNearestTiesToEven, &ignored);
2506 return getConstantFP(V, VT);
2507 }
2508 case ISD::FP_TO_SINT:
2509 case ISD::FP_TO_UINT: {
2510 integerPart x[2];
2511 bool ignored;
2512 assert(integerPartWidth >= 64);
2513 // FIXME need to be more flexible about rounding mode.
2514 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2515 Opcode==ISD::FP_TO_SINT,
2516 APFloat::rmTowardZero, &ignored);
2517 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002518 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002519 APInt api(VT.getSizeInBits(), x);
2520 return getConstant(api, VT);
2521 }
2522 case ISD::BITCAST:
2523 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2524 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2525 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2526 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2527 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002528 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002529 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002530
Gabor Greifba36cb52008-08-28 21:40:38 +00002531 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002532 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002533 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002534 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002535 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002536 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002537 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002538 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002539 assert(VT.isFloatingPoint() &&
2540 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002541 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002542 assert((!VT.isVector() ||
2543 VT.getVectorNumElements() ==
2544 Operand.getValueType().getVectorNumElements()) &&
2545 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002546 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002547 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002548 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002549 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002550 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002551 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002552 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002553 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2554 "Invalid sext node, dst < src!");
2555 assert((!VT.isVector() ||
2556 VT.getVectorNumElements() ==
2557 Operand.getValueType().getVectorNumElements()) &&
2558 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002559 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2560 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2561 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002562 // sext(undef) = 0, because the top bits will all be the same.
2563 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002564 break;
2565 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002566 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002567 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002568 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002569 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2570 "Invalid zext node, dst < src!");
2571 assert((!VT.isVector() ||
2572 VT.getVectorNumElements() ==
2573 Operand.getValueType().getVectorNumElements()) &&
2574 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002575 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002576 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002577 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002578 else if (OpOpcode == ISD::UNDEF)
2579 // zext(undef) = 0, because the top bits will be zero.
2580 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002581 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002582 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002583 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002584 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002585 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002586 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2587 "Invalid anyext node, dst < src!");
2588 assert((!VT.isVector() ||
2589 VT.getVectorNumElements() ==
2590 Operand.getValueType().getVectorNumElements()) &&
2591 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002592
Dan Gohman9e86a732010-06-18 00:08:30 +00002593 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2594 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002595 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002596 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002597 else if (OpOpcode == ISD::UNDEF)
2598 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002599
2600 // (ext (trunx x)) -> x
2601 if (OpOpcode == ISD::TRUNCATE) {
2602 SDValue OpOp = Operand.getNode()->getOperand(0);
2603 if (OpOp.getValueType() == VT)
2604 return OpOp;
2605 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002606 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002607 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002608 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002609 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002610 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002611 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2612 "Invalid truncate node, src < dst!");
2613 assert((!VT.isVector() ||
2614 VT.getVectorNumElements() ==
2615 Operand.getValueType().getVectorNumElements()) &&
2616 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002617 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002618 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002619 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2620 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002621 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002622 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2623 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002624 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002625 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002626 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002627 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002628 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002629 if (OpOpcode == ISD::UNDEF)
2630 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002631 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002632 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002633 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002634 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002635 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002636 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002637 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2638 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002639 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002640 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002641 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002642 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002643 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002644 (VT.getVectorElementType() == Operand.getValueType() ||
2645 (VT.getVectorElementType().isInteger() &&
2646 Operand.getValueType().isInteger() &&
2647 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002648 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002649 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002650 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002651 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2652 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2653 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2654 Operand.getConstantOperandVal(1) == 0 &&
2655 Operand.getOperand(0).getValueType() == VT)
2656 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002657 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002658 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002659 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002660 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002661 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002662 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002663 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002664 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002665 break;
2666 case ISD::FABS:
2667 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002668 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002669 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002670 }
2671
Chris Lattner43247a12005-08-25 19:12:10 +00002672 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002673 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002674 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002675 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002676 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002677 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002678 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002679 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002680 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002681
Jack Carter3af4d252013-09-09 22:02:08 +00002682 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2683 DL.getDebugLoc(), VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002684 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002685 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00002686 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2687 DL.getDebugLoc(), VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002688 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002689
Chris Lattnerc3aae252005-01-07 07:46:32 +00002690 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002691#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002692 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002693#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002694 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002695}
2696
Benjamin Kramer49693102013-02-04 15:19:18 +00002697SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2698 SDNode *Cst1, SDNode *Cst2) {
2699 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2700 SmallVector<SDValue, 4> Outputs;
2701 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00002702
Benjamin Kramer49693102013-02-04 15:19:18 +00002703 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2704 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
2705 if (Scalar1 && Scalar2) {
2706 // Scalar instruction.
2707 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
2708 } else {
2709 // For vectors extract each constant element into Inputs so we can constant
2710 // fold them individually.
2711 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2712 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2713 if (!BV1 || !BV2)
2714 return SDValue();
2715
2716 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2717
2718 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2719 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2720 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2721 if (!V1 || !V2) // Not a constant, bail.
2722 return SDValue();
2723
2724 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2725 // FIXME: This is valid and could be handled by truncating the APInts.
2726 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2727 return SDValue();
2728
2729 Inputs.push_back(std::make_pair(V1, V2));
2730 }
Bill Wendling688d1c42008-09-24 10:16:24 +00002731 }
2732
Benjamin Kramer49693102013-02-04 15:19:18 +00002733 // We have a number of constant values, constant fold them element by element.
2734 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2735 const APInt &C1 = Inputs[I].first->getAPIntValue();
2736 const APInt &C2 = Inputs[I].second->getAPIntValue();
2737
2738 switch (Opcode) {
2739 case ISD::ADD:
2740 Outputs.push_back(getConstant(C1 + C2, SVT));
2741 break;
2742 case ISD::SUB:
2743 Outputs.push_back(getConstant(C1 - C2, SVT));
2744 break;
2745 case ISD::MUL:
2746 Outputs.push_back(getConstant(C1 * C2, SVT));
2747 break;
2748 case ISD::UDIV:
2749 if (!C2.getBoolValue())
2750 return SDValue();
2751 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2752 break;
2753 case ISD::UREM:
2754 if (!C2.getBoolValue())
2755 return SDValue();
2756 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2757 break;
2758 case ISD::SDIV:
2759 if (!C2.getBoolValue())
2760 return SDValue();
2761 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2762 break;
2763 case ISD::SREM:
2764 if (!C2.getBoolValue())
2765 return SDValue();
2766 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2767 break;
2768 case ISD::AND:
2769 Outputs.push_back(getConstant(C1 & C2, SVT));
2770 break;
2771 case ISD::OR:
2772 Outputs.push_back(getConstant(C1 | C2, SVT));
2773 break;
2774 case ISD::XOR:
2775 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2776 break;
2777 case ISD::SHL:
2778 Outputs.push_back(getConstant(C1 << C2, SVT));
2779 break;
2780 case ISD::SRL:
2781 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2782 break;
2783 case ISD::SRA:
2784 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2785 break;
2786 case ISD::ROTL:
2787 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2788 break;
2789 case ISD::ROTR:
2790 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2791 break;
2792 default:
2793 return SDValue();
2794 }
2795 }
2796
2797 // Handle the scalar case first.
Silviu Baranga02066832013-04-25 09:32:33 +00002798 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00002799 return Outputs.back();
2800
2801 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002802 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer49693102013-02-04 15:19:18 +00002803 Outputs.size());
Bill Wendling688d1c42008-09-24 10:16:24 +00002804}
2805
Andrew Trickac6d9be2013-05-25 02:42:55 +00002806SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer49693102013-02-04 15:19:18 +00002807 SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002808 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2809 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002810 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002811 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002812 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002813 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2814 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002815 // Fold trivial token factors.
2816 if (N1.getOpcode() == ISD::EntryToken) return N2;
2817 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002818 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002819 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002820 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002821 // Concat of UNDEFs is UNDEF.
2822 if (N1.getOpcode() == ISD::UNDEF &&
2823 N2.getOpcode() == ISD::UNDEF)
2824 return getUNDEF(VT);
2825
Dan Gohman7f8613e2008-08-14 20:04:46 +00002826 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2827 // one big BUILD_VECTOR.
2828 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2829 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002830 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2831 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002832 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002833 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002834 }
2835 break;
Chris Lattner76365122005-01-16 02:23:22 +00002836 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002837 assert(VT.isInteger() && "This operator does not apply to FP types!");
2838 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002839 N1.getValueType() == VT && "Binary operator types must match!");
2840 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2841 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002842 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002843 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002844 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2845 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002846 break;
Chris Lattner76365122005-01-16 02:23:22 +00002847 case ISD::OR:
2848 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002849 case ISD::ADD:
2850 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002851 assert(VT.isInteger() && "This operator does not apply to FP types!");
2852 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002853 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002854 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2855 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002856 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002857 return N1;
2858 break;
Chris Lattner76365122005-01-16 02:23:22 +00002859 case ISD::UDIV:
2860 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002861 case ISD::MULHU:
2862 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002863 case ISD::MUL:
2864 case ISD::SDIV:
2865 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002866 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002867 assert(N1.getValueType() == N2.getValueType() &&
2868 N1.getValueType() == VT && "Binary operator types must match!");
2869 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002870 case ISD::FADD:
2871 case ISD::FSUB:
2872 case ISD::FMUL:
2873 case ISD::FDIV:
2874 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002875 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002876 if (Opcode == ISD::FADD) {
2877 // 0+x --> x
2878 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2879 if (CFP->getValueAPF().isZero())
2880 return N2;
2881 // x+0 --> x
2882 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2883 if (CFP->getValueAPF().isZero())
2884 return N1;
2885 } else if (Opcode == ISD::FSUB) {
2886 // x-0 --> x
2887 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2888 if (CFP->getValueAPF().isZero())
2889 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00002890 } else if (Opcode == ISD::FMUL) {
2891 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2892 SDValue V = N2;
2893
2894 // If the first operand isn't the constant, try the second
2895 if (!CFP) {
2896 CFP = dyn_cast<ConstantFPSDNode>(N2);
2897 V = N1;
2898 }
2899
2900 if (CFP) {
2901 // 0*x --> 0
2902 if (CFP->isZero())
2903 return SDValue(CFP,0);
2904 // 1*x --> x
2905 if (CFP->isExactlyValue(1.0))
2906 return V;
2907 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00002908 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002909 }
Dale Johannesen78995512010-05-15 18:38:02 +00002910 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002911 assert(N1.getValueType() == N2.getValueType() &&
2912 N1.getValueType() == VT && "Binary operator types must match!");
2913 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002914 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2915 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002916 N1.getValueType().isFloatingPoint() &&
2917 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002918 "Invalid FCOPYSIGN!");
2919 break;
Chris Lattner76365122005-01-16 02:23:22 +00002920 case ISD::SHL:
2921 case ISD::SRA:
2922 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002923 case ISD::ROTL:
2924 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002925 assert(VT == N1.getValueType() &&
2926 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002927 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002928 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00002929 assert((!VT.isVector() || VT == N2.getValueType()) &&
2930 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00002931 // Verify that the shift amount VT is bit enough to hold valid shift
2932 // amounts. This catches things like trying to shift an i1024 value by an
2933 // i8, which is easy to fall into in generic code that uses
2934 // TLI.getShiftAmount().
2935 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002936 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002937 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002938
2939 // Always fold shifts of i1 values so the code generator doesn't need to
2940 // handle them. Since we know the size of the shift has to be less than the
2941 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002942 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002943 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002944 if (N2C && N2C->isNullValue())
2945 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002946 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002947 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002948 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002949 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002950 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002951 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002952 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002953 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002954 "type is vector!");
2955 assert((!EVT.isVector() ||
2956 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2957 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002958 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002959 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002960 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002961 break;
2962 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002963 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002964 assert(VT.isFloatingPoint() &&
2965 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002966 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002967 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002968 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002969 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002970 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002971 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002972 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002973 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002974 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002975 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002976 assert(!EVT.isVector() &&
2977 "AssertSExt/AssertZExt type should be the vector element type "
2978 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002979 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002980 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002981 break;
2982 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002983 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002984 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002985 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002986 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002987 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002988 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002989 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002990 "type is vector!");
2991 assert((!EVT.isVector() ||
2992 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2993 "Vector element counts must match in SIGN_EXTEND_INREG");
2994 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002995 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002996
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002997 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002998 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002999 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00003000 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00003001 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00003002 return getConstant(Val, VT);
3003 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003004 break;
3005 }
3006 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00003007 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3008 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00003009 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003010
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003011 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3012 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003013 if (N2C &&
3014 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003015 N1.getNumOperands() > 0) {
3016 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003017 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003018 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003019 N1.getOperand(N2C->getZExtValue() / Factor),
3020 getConstant(N2C->getZExtValue() % Factor,
3021 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003022 }
3023
3024 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3025 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003026 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3027 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003028
3029 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003030 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003031 // are promoted and implicitly truncated, and the result implicitly
3032 // extended. Make that explicit here.
3033 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003034
Bob Wilsonb1303d02009-04-13 22:05:19 +00003035 return Elt;
3036 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003037
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003038 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3039 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003040 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003041 // If the indices are the same, return the inserted element else
3042 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003043 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003044 SDValue N1Op2 = N1.getOperand(2);
3045 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3046
3047 if (N1Op2C && N2C) {
3048 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3049 if (VT == N1.getOperand(1).getValueType())
3050 return N1.getOperand(1);
3051 else
3052 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3053 }
3054
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003055 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003056 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003057 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003058 break;
3059 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003060 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003061 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3062 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003063 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003064 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003065
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003066 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3067 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003068 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003069 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003070 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003071
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003072 // EXTRACT_ELEMENT of a constant int is also very common.
3073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003074 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003075 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003076 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3077 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003078 }
3079 break;
David Greene91585092011-01-26 15:38:49 +00003080 case ISD::EXTRACT_SUBVECTOR: {
3081 SDValue Index = N2;
3082 if (VT.isSimple() && N1.getValueType().isSimple()) {
3083 assert(VT.isVector() && N1.getValueType().isVector() &&
3084 "Extract subvector VTs must be a vectors!");
Jack Carter3af4d252013-09-09 22:02:08 +00003085 assert(VT.getVectorElementType() ==
3086 N1.getValueType().getVectorElementType() &&
David Greene91585092011-01-26 15:38:49 +00003087 "Extract subvector VTs must have the same element type!");
Craig Topper0ff11902013-08-15 02:44:19 +00003088 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greene91585092011-01-26 15:38:49 +00003089 "Extract subvector must be from larger vector to smaller vector!");
3090
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003091 if (isa<ConstantSDNode>(Index.getNode())) {
3092 assert((VT.getVectorNumElements() +
3093 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003094 <= N1.getValueType().getVectorNumElements())
3095 && "Extract subvector overflow!");
3096 }
3097
3098 // Trivial extraction.
Craig Topper0ff11902013-08-15 02:44:19 +00003099 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greene91585092011-01-26 15:38:49 +00003100 return N1;
3101 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003102 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003103 }
David Greene91585092011-01-26 15:38:49 +00003104 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003105
Benjamin Kramer49693102013-02-04 15:19:18 +00003106 // Perform trivial constant folding.
3107 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3108 if (SV.getNode()) return SV;
3109
3110 // Canonicalize constant to RHS if commutative.
3111 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3112 std::swap(N1C, N2C);
3113 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003114 }
3115
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003116 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003117 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3118 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003119 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003120 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003121 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003122 std::swap(N1CFP, N2CFP);
3123 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003124 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003125 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3126 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003127 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003128 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003129 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003130 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003131 return getConstantFP(V1, VT);
3132 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003133 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003134 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3135 if (s!=APFloat::opInvalidOp)
3136 return getConstantFP(V1, VT);
3137 break;
3138 case ISD::FMUL:
3139 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3140 if (s!=APFloat::opInvalidOp)
3141 return getConstantFP(V1, VT);
3142 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003143 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003144 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3145 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3146 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003147 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003148 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003149 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3150 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3151 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003152 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003153 case ISD::FCOPYSIGN:
3154 V1.copySign(V2);
3155 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003156 default: break;
3157 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003158 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003159
3160 if (Opcode == ISD::FP_ROUND) {
3161 APFloat V = N1CFP->getValueAPF(); // make copy
3162 bool ignored;
3163 // This can return overflow, underflow, or inexact; we don't care.
3164 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003165 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003166 APFloat::rmNearestTiesToEven, &ignored);
3167 return getConstantFP(V, VT);
3168 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003169 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003170
Chris Lattner62b57722006-04-20 05:39:12 +00003171 // Canonicalize an UNDEF to the RHS, even over a constant.
3172 if (N1.getOpcode() == ISD::UNDEF) {
3173 if (isCommutativeBinOp(Opcode)) {
3174 std::swap(N1, N2);
3175 } else {
3176 switch (Opcode) {
3177 case ISD::FP_ROUND_INREG:
3178 case ISD::SIGN_EXTEND_INREG:
3179 case ISD::SUB:
3180 case ISD::FSUB:
3181 case ISD::FDIV:
3182 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003183 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003184 return N1; // fold op(undef, arg2) -> undef
3185 case ISD::UDIV:
3186 case ISD::SDIV:
3187 case ISD::UREM:
3188 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003189 case ISD::SRL:
3190 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003191 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003192 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3193 // For vectors, we can't easily build an all zero vector, just return
3194 // the LHS.
3195 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003196 }
3197 }
3198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003199
3200 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003201 if (N2.getOpcode() == ISD::UNDEF) {
3202 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003203 case ISD::XOR:
3204 if (N1.getOpcode() == ISD::UNDEF)
3205 // Handle undef ^ undef -> 0 special case. This is a common
3206 // idiom (misuse).
3207 return getConstant(0, VT);
3208 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003209 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003210 case ISD::ADDC:
3211 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003212 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003213 case ISD::UDIV:
3214 case ISD::SDIV:
3215 case ISD::UREM:
3216 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003217 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003218 case ISD::FADD:
3219 case ISD::FSUB:
3220 case ISD::FMUL:
3221 case ISD::FDIV:
3222 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003223 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003224 return N2;
3225 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003226 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003227 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003228 case ISD::SRL:
3229 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003230 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003231 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3232 // For vectors, we can't easily build an all zero vector, just return
3233 // the LHS.
3234 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003235 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003236 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003237 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003238 // For vectors, we can't easily build an all one vector, just return
3239 // the LHS.
3240 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003241 case ISD::SRA:
3242 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003243 }
3244 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003245
Chris Lattner27e9b412005-05-11 18:57:39 +00003246 // Memoize this node if possible.
3247 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003248 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003249 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003250 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003251 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003252 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003253 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003254 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003255 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003256
Jack Carter3af4d252013-09-09 22:02:08 +00003257 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3258 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003259 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003260 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003261 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3262 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003263 }
3264
Chris Lattnerc3aae252005-01-07 07:46:32 +00003265 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003266#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003267 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003268#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003269 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003270}
3271
Andrew Trickac6d9be2013-05-25 02:42:55 +00003272SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003273 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003274 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003276 switch (Opcode) {
Owen Anderson58dcd202013-05-09 22:27:13 +00003277 case ISD::FMA: {
3278 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3279 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3280 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3281 if (N1CFP && N2CFP && N3CFP) {
3282 APFloat V1 = N1CFP->getValueAPF();
3283 const APFloat &V2 = N2CFP->getValueAPF();
3284 const APFloat &V3 = N3CFP->getValueAPF();
3285 APFloat::opStatus s =
3286 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3287 if (s != APFloat::opInvalidOp)
3288 return getConstantFP(V1, VT);
3289 }
3290 break;
3291 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00003292 case ISD::CONCAT_VECTORS:
3293 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3294 // one big BUILD_VECTOR.
3295 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3296 N2.getOpcode() == ISD::BUILD_VECTOR &&
3297 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003298 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3299 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003300 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3301 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003302 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003303 }
3304 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003305 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003306 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003307 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003308 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003309 break;
3310 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003311 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003312 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003313 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003314 return N2; // select true, X, Y -> X
3315 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003316 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003317
3318 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003319 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003320 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003321 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003322 case ISD::INSERT_SUBVECTOR: {
3323 SDValue Index = N3;
3324 if (VT.isSimple() && N1.getValueType().isSimple()
3325 && N2.getValueType().isSimple()) {
3326 assert(VT.isVector() && N1.getValueType().isVector() &&
3327 N2.getValueType().isVector() &&
3328 "Insert subvector VTs must be a vectors");
3329 assert(VT == N1.getValueType() &&
3330 "Dest and insert subvector source types must match!");
Craig Topper0ff11902013-08-15 02:44:19 +00003331 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenecfe33c42011-01-26 19:13:22 +00003332 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003333 if (isa<ConstantSDNode>(Index.getNode())) {
3334 assert((N2.getValueType().getVectorNumElements() +
3335 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003336 <= VT.getVectorNumElements())
3337 && "Insert subvector overflow!");
3338 }
3339
3340 // Trivial insertion.
Craig Topper0ff11902013-08-15 02:44:19 +00003341 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenecfe33c42011-01-26 19:13:22 +00003342 return N2;
3343 }
3344 break;
3345 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003346 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003347 // Fold bit_convert nodes from a type to themselves.
3348 if (N1.getValueType() == VT)
3349 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003350 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003351 }
3352
Chris Lattner43247a12005-08-25 19:12:10 +00003353 // Memoize node if it doesn't produce a flag.
3354 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003355 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003356 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003357 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003358 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003359 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003360 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003361 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003362 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003363
Jack Carter3af4d252013-09-09 22:02:08 +00003364 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3365 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003366 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003367 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003368 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3369 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003370 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003371
Chris Lattnerc3aae252005-01-07 07:46:32 +00003372 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003373#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003374 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003375#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003376 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003377}
3378
Andrew Trickac6d9be2013-05-25 02:42:55 +00003379SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003380 SDValue N1, SDValue N2, SDValue N3,
3381 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003382 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003383 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003384}
3385
Andrew Trickac6d9be2013-05-25 02:42:55 +00003386SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003387 SDValue N1, SDValue N2, SDValue N3,
3388 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003389 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003390 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003391}
3392
Dan Gohman98ca4f22009-08-05 01:29:28 +00003393/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3394/// the incoming stack arguments to be loaded from the stack.
3395SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3396 SmallVector<SDValue, 8> ArgChains;
3397
3398 // Include the original chain at the beginning of the list. When this is
3399 // used by target LowerCall hooks, this helps legalize find the
3400 // CALLSEQ_BEGIN node.
3401 ArgChains.push_back(Chain);
3402
3403 // Add a chain value for each stack argument.
3404 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3405 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3406 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3407 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3408 if (FI->getIndex() < 0)
3409 ArgChains.push_back(SDValue(L, 1));
3410
3411 // Build a tokenfactor for all the chains.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003412 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003413 &ArgChains[0], ArgChains.size());
3414}
3415
Dan Gohman707e0182008-04-12 04:36:06 +00003416/// getMemsetValue - Vectorized representation of the memset value
3417/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003418static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003419 SDLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003420 assert(Value.getOpcode() != ISD::UNDEF);
3421
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003422 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003423 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003424 assert(C->getAPIntValue().getBitWidth() == 8);
3425 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003426 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003427 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003428 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003429 }
Evan Chengf0df0312008-05-15 08:39:06 +00003430
Dale Johannesen0f502f62009-02-03 22:26:09 +00003431 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003432 if (NumBits > 8) {
3433 // Use a multiplication with 0x010101... to extend the input to the
3434 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003435 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003436 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003437 }
3438
3439 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003440}
3441
Dan Gohman707e0182008-04-12 04:36:06 +00003442/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3443/// used when a memcpy is turned into a memset when the source is a constant
3444/// string ptr.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003445static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003446 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003447 // Handle vector with all elements zero.
3448 if (Str.empty()) {
3449 if (VT.isInteger())
3450 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003451 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003452 return DAG.getConstantFP(0.0, VT);
3453 else if (VT.isVector()) {
3454 unsigned NumElts = VT.getVectorNumElements();
3455 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003456 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003457 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3458 EltVT, NumElts)));
3459 } else
3460 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003461 }
3462
Duncan Sands83ec4b62008-06-06 12:08:01 +00003463 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003464 unsigned NumVTBits = VT.getSizeInBits();
3465 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003466 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3467
Evan Cheng4ff23d02013-01-10 22:13:27 +00003468 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003469 if (TLI.isLittleEndian()) {
3470 for (unsigned i = 0; i != NumBytes; ++i)
3471 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3472 } else {
3473 for (unsigned i = 0; i != NumBytes; ++i)
3474 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003475 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003476
Evan Chenge07f85e2012-12-11 23:26:14 +00003477 // If the "cost" of materializing the integer immediate is 1 or free, then
3478 // it is cost effective to turn the load into the immediate.
Chandler Carruthe4b4edd2013-01-05 12:32:17 +00003479 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
3480 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng376642e2012-12-10 23:21:26 +00003481 return DAG.getConstant(Val, VT);
3482 return SDValue(0, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003483}
3484
Scott Michelfdc40a02009-02-17 22:15:04 +00003485/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003486///
Andrew Trickdd0fb012013-05-25 03:08:10 +00003487static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman707e0182008-04-12 04:36:06 +00003488 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003489 EVT VT = Base.getValueType();
Andrew Trickdd0fb012013-05-25 03:08:10 +00003490 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003491 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003492}
3493
Evan Chengf0df0312008-05-15 08:39:06 +00003494/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3495///
Chris Lattner18c7f802012-02-05 02:29:43 +00003496static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003497 unsigned SrcDelta = 0;
3498 GlobalAddressSDNode *G = NULL;
3499 if (Src.getOpcode() == ISD::GlobalAddress)
3500 G = cast<GlobalAddressSDNode>(Src);
3501 else if (Src.getOpcode() == ISD::ADD &&
3502 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3503 Src.getOperand(1).getOpcode() == ISD::Constant) {
3504 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003505 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003506 }
3507 if (!G)
3508 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003509
Chris Lattner18c7f802012-02-05 02:29:43 +00003510 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003511}
Dan Gohman707e0182008-04-12 04:36:06 +00003512
Evan Cheng255f20f2010-04-01 06:04:33 +00003513/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3514/// to replace the memset / memcpy. Return true if the number of memory ops
3515/// is below the threshold. It returns the types of the sequence of
3516/// memory ops to perform memset / memcpy by reference.
3517static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003518 unsigned Limit, uint64_t Size,
3519 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003520 bool IsMemset,
3521 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003522 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003523 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003524 SelectionDAG &DAG,
3525 const TargetLowering &TLI) {
3526 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3527 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003528 // If 'SrcAlign' is zero, that means the memory operation does not need to
3529 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3530 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3531 // is the specified alignment of the memory operation. If it is zero, that
3532 // means it's possible to change the alignment of the destination.
3533 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3534 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003535 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003536 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003537 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003538
Chris Lattneracee6472010-03-07 07:45:08 +00003539 if (VT == MVT::Other) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +00003540 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003541 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003542 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003543 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003544 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003545 case 0: VT = MVT::i64; break;
3546 case 4: VT = MVT::i32; break;
3547 case 2: VT = MVT::i16; break;
3548 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003549 }
3550 }
3551
Owen Anderson766b5ef2009-08-11 21:59:30 +00003552 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003553 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003554 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003555 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003556
Duncan Sands8e4eb092008-06-08 20:54:56 +00003557 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003558 VT = LVT;
3559 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003560
Dan Gohman707e0182008-04-12 04:36:06 +00003561 unsigned NumMemOps = 0;
3562 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003563 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003564 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003565 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003566 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003567 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003568
3569 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003570 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003571 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003572 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003573 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003574 Found = true;
3575 else if (NewVT == MVT::i64 &&
3576 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003577 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003578 // i64 is usually not legal on 32-bit targets, but f64 may be.
3579 NewVT = MVT::f64;
3580 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003581 }
Evan Cheng376642e2012-12-10 23:21:26 +00003582 }
3583
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003584 if (!Found) {
3585 do {
3586 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3587 if (NewVT == MVT::i8)
3588 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003589 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003590 }
3591 NewVTSize = NewVT.getSizeInBits() / 8;
3592
Evan Cheng376642e2012-12-10 23:21:26 +00003593 // If the new VT cannot cover all of the remaining bits, then consider
3594 // issuing a (or a pair of) unaligned and overlapping load / store.
3595 // FIXME: Only does this for 64-bit or more since we don't have proper
3596 // cost model for unaligned load / store.
3597 bool Fast;
Evan Chenga16e49d2012-12-12 20:43:23 +00003598 if (NumMemOps && AllowOverlap &&
3599 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng376642e2012-12-10 23:21:26 +00003600 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3601 VTSize = Size;
3602 else {
3603 VT = NewVT;
3604 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003605 }
Dan Gohman707e0182008-04-12 04:36:06 +00003606 }
Dan Gohman707e0182008-04-12 04:36:06 +00003607
Evan Chenga16e49d2012-12-12 20:43:23 +00003608 if (++NumMemOps > Limit)
3609 return false;
3610
Dan Gohman707e0182008-04-12 04:36:06 +00003611 MemOps.push_back(VT);
3612 Size -= VTSize;
3613 }
3614
3615 return true;
3616}
3617
Andrew Trickac6d9be2013-05-25 02:42:55 +00003618static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003619 SDValue Chain, SDValue Dst,
3620 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003621 unsigned Align, bool isVol,
3622 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003623 MachinePointerInfo DstPtrInfo,
3624 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003625 // Turn a memcpy of undef to nop.
3626 if (Src.getOpcode() == ISD::UNDEF)
3627 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003628
Dan Gohman21323f32008-05-29 19:42:22 +00003629 // Expand memcpy to a series of load and store ops if the size operand falls
3630 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003631 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3632 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003633 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003634 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003635 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003636 MachineFunction &MF = DAG.getMachineFunction();
3637 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003638 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003639 MF.getFunction()->getAttributes().
3640 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003641 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3642 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3643 DstAlignCanChange = true;
3644 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3645 if (Align > SrcAlign)
3646 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003647 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003648 bool CopyFromStr = isMemSrcFromString(Src, Str);
3649 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003650 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003651
Evan Cheng94107ba2010-04-01 18:19:11 +00003652 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003653 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003654 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003655 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003656 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003657
Evan Cheng255f20f2010-04-01 06:04:33 +00003658 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003659 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003660 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00003661
3662 // Don't promote to an alignment that would require dynamic stack
Stephen Lin155615d2013-07-08 00:37:03 +00003663 // realignment.
Lang Hames2d95e432013-01-31 20:23:43 +00003664 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3665 if (!TRI->needsStackRealignment(MF))
3666 while (NewAlign > Align &&
3667 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3668 NewAlign /= 2;
3669
Evan Cheng255f20f2010-04-01 06:04:33 +00003670 if (NewAlign > Align) {
3671 // Give the stack frame object a larger alignment if needed.
3672 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3673 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3674 Align = NewAlign;
3675 }
3676 }
Dan Gohman707e0182008-04-12 04:36:06 +00003677
Dan Gohman475871a2008-07-27 21:46:04 +00003678 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003679 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003680 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003681 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003682 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003683 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003684 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003685
Evan Cheng376642e2012-12-10 23:21:26 +00003686 if (VTSize > Size) {
3687 // Issuing an unaligned load / store pair that overlaps with the previous
3688 // pair. Adjust the offset accordingly.
3689 assert(i == NumMemOps-1 && i != 0);
3690 SrcOff -= VTSize - Size;
3691 DstOff -= VTSize - Size;
3692 }
3693
Evan Cheng255f20f2010-04-01 06:04:33 +00003694 if (CopyFromStr &&
3695 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003696 // It's unlikely a store of a vector immediate can be done in a single
3697 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003698 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003699 // FIXME: Handle other cases where store of vector immediate is done in
3700 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003701 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003702 if (Value.getNode())
3703 Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003704 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng376642e2012-12-10 23:21:26 +00003705 DstPtrInfo.getWithOffset(DstOff), isVol,
3706 false, Align);
3707 }
3708
3709 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003710 // The type might not be legal for the target. This should only happen
3711 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003712 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3713 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003714 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003715 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003716 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003717 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003718 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003719 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003720 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003721 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003722 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003723 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3724 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003725 }
3726 OutChains.push_back(Store);
3727 SrcOff += VTSize;
3728 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003729 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003730 }
3731
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003733 &OutChains[0], OutChains.size());
3734}
3735
Andrew Trickac6d9be2013-05-25 02:42:55 +00003736static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003737 SDValue Chain, SDValue Dst,
3738 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003739 unsigned Align, bool isVol,
3740 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003741 MachinePointerInfo DstPtrInfo,
3742 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003743 // Turn a memmove of undef to nop.
3744 if (Src.getOpcode() == ISD::UNDEF)
3745 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003746
3747 // Expand memmove to a series of load and store ops if the size operand falls
3748 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003749 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003750 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003751 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003752 MachineFunction &MF = DAG.getMachineFunction();
3753 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003754 bool OptSize = MF.getFunction()->getAttributes().
3755 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003756 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3757 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3758 DstAlignCanChange = true;
3759 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3760 if (Align > SrcAlign)
3761 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003762 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003763
Evan Cheng94107ba2010-04-01 18:19:11 +00003764 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00003765 (DstAlignCanChange ? 0 : Align), SrcAlign,
3766 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003767 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003768
Evan Cheng255f20f2010-04-01 06:04:33 +00003769 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003770 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003771 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003772 if (NewAlign > Align) {
3773 // Give the stack frame object a larger alignment if needed.
3774 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3775 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3776 Align = NewAlign;
3777 }
3778 }
Dan Gohman21323f32008-05-29 19:42:22 +00003779
Evan Cheng255f20f2010-04-01 06:04:33 +00003780 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003781 SmallVector<SDValue, 8> LoadValues;
3782 SmallVector<SDValue, 8> LoadChains;
3783 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003784 unsigned NumMemOps = MemOps.size();
3785 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003786 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003787 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003788 SDValue Value;
Dan Gohman21323f32008-05-29 19:42:22 +00003789
Dale Johannesen0f502f62009-02-03 22:26:09 +00003790 Value = DAG.getLoad(VT, dl, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003791 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003792 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003793 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003794 LoadValues.push_back(Value);
3795 LoadChains.push_back(Value.getValue(1));
3796 SrcOff += VTSize;
3797 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003799 &LoadChains[0], LoadChains.size());
3800 OutChains.clear();
3801 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003802 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003803 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003804 SDValue Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003805
Dale Johannesen0f502f62009-02-03 22:26:09 +00003806 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Trickdd0fb012013-05-25 03:08:10 +00003807 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003808 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003809 OutChains.push_back(Store);
3810 DstOff += VTSize;
3811 }
3812
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003814 &OutChains[0], OutChains.size());
3815}
3816
Serge Pavlov496f0242013-09-17 16:24:42 +00003817/// \brief Lower the call to 'memset' intrinsic function into a series of store
3818/// operations.
3819///
3820/// \param DAG Selection DAG where lowered code is placed.
3821/// \param dl Link to corresponding IR location.
3822/// \param Chain Control flow dependency.
3823/// \param Dst Pointer to destination memory location.
3824/// \param Src Value of byte to write into the memory.
3825/// \param Size Number of bytes to write.
3826/// \param Align Alignment of the destination in bytes.
3827/// \param isVol True if destination is volatile.
3828/// \param DstPtrInfo IR information on the memory pointer.
3829/// \returns New head in the control flow, if lowering was successful, empty
3830/// SDValue otherwise.
3831///
3832/// The function tries to replace 'llvm.memset' intrinsic with several store
3833/// operations and value calculation code. This is usually profitable for small
3834/// memory size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003835static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003836 SDValue Chain, SDValue Dst,
3837 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003838 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003839 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003840 // Turn a memset of undef to nop.
3841 if (Src.getOpcode() == ISD::UNDEF)
3842 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003843
3844 // Expand memset to a series of load/store ops if the size operand
3845 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003846 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003847 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003848 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003849 MachineFunction &MF = DAG.getMachineFunction();
3850 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003851 bool OptSize = MF.getFunction()->getAttributes().
3852 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003853 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3854 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3855 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003856 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003857 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003858 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003859 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00003860 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003861 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003862
Evan Cheng255f20f2010-04-01 06:04:33 +00003863 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003864 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003865 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003866 if (NewAlign > Align) {
3867 // Give the stack frame object a larger alignment if needed.
3868 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3869 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3870 Align = NewAlign;
3871 }
3872 }
3873
Dan Gohman475871a2008-07-27 21:46:04 +00003874 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003875 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003876 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003877
3878 // Find the largest store and generate the bit pattern for it.
3879 EVT LargestVT = MemOps[0];
3880 for (unsigned i = 1; i < NumMemOps; i++)
3881 if (MemOps[i].bitsGT(LargestVT))
3882 LargestVT = MemOps[i];
3883 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3884
Dan Gohman707e0182008-04-12 04:36:06 +00003885 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003886 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00003887 unsigned VTSize = VT.getSizeInBits() / 8;
3888 if (VTSize > Size) {
3889 // Issuing an unaligned load / store pair that overlaps with the previous
3890 // pair. Adjust the offset accordingly.
3891 assert(i == NumMemOps-1 && i != 0);
3892 DstOff -= VTSize - Size;
3893 }
Benjamin Kramer80220362011-01-02 19:57:05 +00003894
3895 // If this store is smaller than the largest store see whether we can get
3896 // the smaller value for free with a truncate.
3897 SDValue Value = MemSetValue;
3898 if (VT.bitsLT(LargestVT)) {
3899 if (!LargestVT.isVector() && !VT.isVector() &&
3900 TLI.isTruncateFree(LargestVT, VT))
3901 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3902 else
3903 Value = getMemsetValue(Src, VT, DAG, dl);
3904 }
3905 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003906 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003907 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003908 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003909 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003910 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003911 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00003912 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003913 }
3914
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003916 &OutChains[0], OutChains.size());
3917}
3918
Andrew Trickac6d9be2013-05-25 02:42:55 +00003919SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003920 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003921 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003922 MachinePointerInfo DstPtrInfo,
3923 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00003924 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00003925
3926 // Check to see if we should lower the memcpy to loads and stores first.
3927 // For cases within the target-specified limits, this is the best choice.
3928 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3929 if (ConstantSize) {
3930 // Memcpy with size zero? Just return the original chain.
3931 if (ConstantSize->isNullValue())
3932 return Chain;
3933
Evan Cheng255f20f2010-04-01 06:04:33 +00003934 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3935 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003936 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003937 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003938 return Result;
3939 }
3940
3941 // Then check to see if we should lower the memcpy with target-specific
3942 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003943 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003944 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003945 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003946 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003947 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003948 return Result;
3949
3950 // If we really need inline code and the target declined to provide it,
3951 // use a (potentially long) sequence of loads and stores.
3952 if (AlwaysInline) {
3953 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003954 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003955 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003956 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003957 }
3958
Dan Gohman7b55d362010-04-05 20:24:08 +00003959 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3960 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3961 // respect volatile, so they may do things like read or write memory
3962 // beyond the given memory regions. But fixing this isn't easy, and most
3963 // people don't care.
3964
Bill Wendlingba54bca2013-06-19 21:36:55 +00003965 const TargetLowering *TLI = TM.getTargetLowering();
3966
Dan Gohman707e0182008-04-12 04:36:06 +00003967 // Emit a library call.
3968 TargetLowering::ArgListTy Args;
3969 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003970 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003971 Entry.Node = Dst; Args.push_back(Entry);
3972 Entry.Node = Src; Args.push_back(Entry);
3973 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003974 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003975 TargetLowering::
3976 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003977 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003978 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003979 /*isTailCall=*/false,
3980 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003981 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
3982 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003983 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00003984 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003985
Dan Gohman707e0182008-04-12 04:36:06 +00003986 return CallResult.second;
3987}
3988
Andrew Trickac6d9be2013-05-25 02:42:55 +00003989SDValue SelectionDAG::getMemmove(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,
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
Dan Gohman21323f32008-05-29 19:42:22 +00003996 // Check to see if we should lower the memmove 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 // Memmove with size zero? Just return the original chain.
4001 if (ConstantSize->isNullValue())
4002 return Chain;
4003
Dan Gohman475871a2008-07-27 21:46:04 +00004004 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00004005 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004006 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004007 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004008 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00004009 return Result;
4010 }
Dan Gohman707e0182008-04-12 04:36:06 +00004011
4012 // Then check to see if we should lower the memmove with target-specific
4013 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004014 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004015 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
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
Mon P Wang01f0e852010-04-06 08:27:51 +00004020 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4021 // not be safe. See memcpy above for more details.
4022
Bill Wendlingba54bca2013-06-19 21:36:55 +00004023 const TargetLowering *TLI = TM.getTargetLowering();
4024
Dan Gohman707e0182008-04-12 04:36:06 +00004025 // Emit a library call.
4026 TargetLowering::ArgListTy Args;
4027 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004028 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004029 Entry.Node = Dst; Args.push_back(Entry);
4030 Entry.Node = Src; Args.push_back(Entry);
4031 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004032 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004033 TargetLowering::
4034 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004035 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004036 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004037 /*isTailCall=*/false,
4038 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004039 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4040 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004041 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004042 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004043
Dan Gohman707e0182008-04-12 04:36:06 +00004044 return CallResult.second;
4045}
4046
Andrew Trickac6d9be2013-05-25 02:42:55 +00004047SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004048 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004049 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004050 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004051 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004052
4053 // Check to see if we should lower the memset to stores first.
4054 // For cases within the target-specified limits, this is the best choice.
4055 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4056 if (ConstantSize) {
4057 // Memset with size zero? Just return the original chain.
4058 if (ConstantSize->isNullValue())
4059 return Chain;
4060
Mon P Wang20adc9d2010-04-04 03:10:48 +00004061 SDValue Result =
4062 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004063 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004064
Gabor Greifba36cb52008-08-28 21:40:38 +00004065 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004066 return Result;
4067 }
4068
4069 // Then check to see if we should lower the memset with target-specific
4070 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004071 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004072 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004073 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004074 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004075 return Result;
4076
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004077 // Emit a library call.
Bill Wendlingba54bca2013-06-19 21:36:55 +00004078 const TargetLowering *TLI = TM.getTargetLowering();
4079 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004080 TargetLowering::ArgListTy Args;
4081 TargetLowering::ArgListEntry Entry;
4082 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4083 Args.push_back(Entry);
4084 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 if (Src.getValueType().bitsGT(MVT::i32))
4086 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00004087 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004088 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00004089 Entry.Node = Src;
4090 Entry.Ty = Type::getInt32Ty(*getContext());
4091 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00004092 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004093 Entry.Node = Size;
4094 Entry.Ty = IntPtrTy;
4095 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00004096 Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004097 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004098 TargetLowering::
4099 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004100 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004101 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004102 /*isTailCall=*/false,
4103 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004104 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4105 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004106 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004107 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004108
Dan Gohman707e0182008-04-12 04:36:06 +00004109 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004110}
4111
Andrew Trickac6d9be2013-05-25 02:42:55 +00004112SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Amara Emerson268c7432013-09-26 12:22:36 +00004113 SDVTList VTList, SDValue* Ops, unsigned NumOps,
4114 MachineMemOperand *MMO,
4115 AtomicOrdering Ordering,
4116 SynchronizationScope SynchScope) {
4117 FoldingSetNodeID ID;
4118 ID.AddInteger(MemVT.getRawBits());
4119 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4120 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4121 void* IP = 0;
4122 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4123 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4124 return SDValue(E, 0);
4125 }
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004126
4127 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4128 // SDNode doesn't have access to it. This memory will be "leaked" when
4129 // the node is deallocated, but recovered when the allocator is released.
4130 // If the number of operands is less than 5 we use AtomicSDNode's internal
4131 // storage.
4132 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : 0;
4133
Amara Emerson268c7432013-09-26 12:22:36 +00004134 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4135 dl.getDebugLoc(), VTList, MemVT,
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004136 Ops, DynOps, NumOps, MMO,
4137 Ordering, SynchScope);
Amara Emerson268c7432013-09-26 12:22:36 +00004138 CSEMap.InsertNode(N, IP);
4139 AllNodes.push_back(N);
4140 return SDValue(N, 0);
4141}
4142
4143SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00004144 SDValue Chain, SDValue Ptr, SDValue Cmp,
4145 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00004146 unsigned Alignment,
4147 AtomicOrdering Ordering,
Michael Ilseman06b96902012-09-10 16:56:31 +00004148 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004149 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4150 Alignment = getEVTAlignment(MemVT);
4151
Evan Chengff89dcb2009-10-18 18:16:27 +00004152 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004153
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004154 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004155 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004156 // FIXME: Volatile isn't really correct; we should keep track of atomic
4157 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004158 unsigned Flags = MachineMemOperand::MOVolatile;
4159 if (Opcode != ISD::ATOMIC_STORE)
4160 Flags |= MachineMemOperand::MOLoad;
4161 if (Opcode != ISD::ATOMIC_LOAD)
4162 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004163
4164 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004165 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004166
Eli Friedman55ba8162011-07-29 03:05:32 +00004167 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4168 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004169}
4170
Andrew Trickac6d9be2013-05-25 02:42:55 +00004171SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004172 SDValue Chain,
4173 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00004174 SDValue Swp, MachineMemOperand *MMO,
4175 AtomicOrdering Ordering,
4176 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004177 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4178 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4179
Owen Andersone50ed302009-08-10 22:56:29 +00004180 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004181
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004183 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Amara Emerson268c7432013-09-26 12:22:36 +00004184 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004185}
4186
Andrew Trickac6d9be2013-05-25 02:42:55 +00004187SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004188 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004189 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004190 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004191 unsigned Alignment,
4192 AtomicOrdering Ordering,
4193 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004194 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4195 Alignment = getEVTAlignment(MemVT);
4196
Evan Chengff89dcb2009-10-18 18:16:27 +00004197 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004198 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004199 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004200 // For now, atomics are considered to be volatile always, and they are
4201 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004202 // FIXME: Volatile isn't really correct; we should keep track of atomic
4203 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004204 unsigned Flags = MachineMemOperand::MOVolatile;
4205 if (Opcode != ISD::ATOMIC_STORE)
4206 Flags |= MachineMemOperand::MOLoad;
4207 if (Opcode != ISD::ATOMIC_LOAD)
4208 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004209
4210 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004211 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004212 MemVT.getStoreSize(), Alignment);
4213
Eli Friedman55ba8162011-07-29 03:05:32 +00004214 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4215 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004216}
4217
Andrew Trickac6d9be2013-05-25 02:42:55 +00004218SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004219 SDValue Chain,
4220 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004221 MachineMemOperand *MMO,
4222 AtomicOrdering Ordering,
4223 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004224 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4225 Opcode == ISD::ATOMIC_LOAD_SUB ||
4226 Opcode == ISD::ATOMIC_LOAD_AND ||
4227 Opcode == ISD::ATOMIC_LOAD_OR ||
4228 Opcode == ISD::ATOMIC_LOAD_XOR ||
4229 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004230 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004231 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004232 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004233 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004234 Opcode == ISD::ATOMIC_SWAP ||
4235 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004236 "Invalid Atomic Op");
4237
Owen Andersone50ed302009-08-10 22:56:29 +00004238 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004239
Eli Friedman327236c2011-08-24 20:50:09 +00004240 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4241 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004242 SDValue Ops[] = {Chain, Ptr, Val};
Amara Emerson268c7432013-09-26 12:22:36 +00004243 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004244}
4245
Andrew Trickac6d9be2013-05-25 02:42:55 +00004246SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004247 EVT VT, SDValue Chain,
4248 SDValue Ptr,
4249 const Value* PtrVal,
4250 unsigned Alignment,
4251 AtomicOrdering Ordering,
4252 SynchronizationScope SynchScope) {
4253 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4254 Alignment = getEVTAlignment(MemVT);
4255
4256 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004257 // An atomic store does not load. An atomic load does not store.
4258 // (An atomicrmw obviously both loads and stores.)
4259 // For now, atomics are considered to be volatile always, and they are
4260 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004261 // FIXME: Volatile isn't really correct; we should keep track of atomic
4262 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004263 unsigned Flags = MachineMemOperand::MOVolatile;
4264 if (Opcode != ISD::ATOMIC_STORE)
4265 Flags |= MachineMemOperand::MOLoad;
4266 if (Opcode != ISD::ATOMIC_LOAD)
4267 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004268
4269 MachineMemOperand *MMO =
4270 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4271 MemVT.getStoreSize(), Alignment);
4272
4273 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4274 Ordering, SynchScope);
4275}
4276
Andrew Trickac6d9be2013-05-25 02:42:55 +00004277SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004278 EVT VT, SDValue Chain,
4279 SDValue Ptr,
4280 MachineMemOperand *MMO,
4281 AtomicOrdering Ordering,
4282 SynchronizationScope SynchScope) {
4283 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4284
4285 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman327236c2011-08-24 20:50:09 +00004286 SDValue Ops[] = {Chain, Ptr};
Amara Emerson268c7432013-09-26 12:22:36 +00004287 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004288}
4289
Duncan Sands4bdcb612008-07-02 17:40:58 +00004290/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004291SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004292 SDLoc dl) {
Dale Johannesen54c94522009-02-02 20:47:48 +00004293 if (NumOps == 1)
4294 return Ops[0];
4295
Owen Andersone50ed302009-08-10 22:56:29 +00004296 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004297 VTs.reserve(NumOps);
4298 for (unsigned i = 0; i < NumOps; ++i)
4299 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004300 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004301 Ops, NumOps);
4302}
4303
Dan Gohman475871a2008-07-27 21:46:04 +00004304SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004305SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004306 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004307 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004308 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004309 unsigned Align, bool Vol,
4310 bool ReadMem, bool WriteMem) {
4311 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004312 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004313 ReadMem, WriteMem);
4314}
4315
4316SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004317SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesene8c17332009-01-29 00:47:48 +00004318 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004319 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004320 unsigned Align, bool Vol,
4321 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004322 if (Align == 0) // Ensure that codegen never sees alignment 0
4323 Align = getEVTAlignment(MemVT);
4324
4325 MachineFunction &MF = getMachineFunction();
4326 unsigned Flags = 0;
4327 if (WriteMem)
4328 Flags |= MachineMemOperand::MOStore;
4329 if (ReadMem)
4330 Flags |= MachineMemOperand::MOLoad;
4331 if (Vol)
4332 Flags |= MachineMemOperand::MOVolatile;
4333 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004334 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004335
4336 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4337}
4338
4339SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004340SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004341 const SDValue *Ops, unsigned NumOps,
4342 EVT MemVT, MachineMemOperand *MMO) {
4343 assert((Opcode == ISD::INTRINSIC_VOID ||
4344 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004345 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004346 Opcode == ISD::LIFETIME_START ||
4347 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004348 (Opcode <= INT_MAX &&
4349 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4350 "Opcode is not a memory-accessing opcode!");
4351
Dale Johannesene8c17332009-01-29 00:47:48 +00004352 // Memoize the node unless it returns a flag.
4353 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004354 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004355 FoldingSetNodeID ID;
4356 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004357 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004358 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004359 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4360 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004361 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004362 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004363
Jack Carter3af4d252013-09-09 22:02:08 +00004364 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4365 dl.getDebugLoc(), VTList, Ops,
4366 NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004367 CSEMap.InsertNode(N, IP);
4368 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004369 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4370 dl.getDebugLoc(), VTList, Ops,
4371 NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004372 }
4373 AllNodes.push_back(N);
4374 return SDValue(N, 0);
4375}
4376
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004377/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4378/// MachinePointerInfo record from it. This is particularly useful because the
4379/// code generator has many cases where it doesn't bother passing in a
4380/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4381static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4382 // If this is FI+Offset, we can model it.
4383 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4384 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4385
4386 // If this is (FI+Offset1)+Offset2, we can model it.
4387 if (Ptr.getOpcode() != ISD::ADD ||
4388 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4389 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4390 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004391
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004392 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4393 return MachinePointerInfo::getFixedStack(FI, Offset+
4394 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4395}
4396
4397/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4398/// MachinePointerInfo record from it. This is particularly useful because the
4399/// code generator has many cases where it doesn't bother passing in a
4400/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4401static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4402 // If the 'Offset' value isn't a constant, we can't handle this.
4403 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4404 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4405 if (OffsetOp.getOpcode() == ISD::UNDEF)
4406 return InferPointerInfo(Ptr);
4407 return MachinePointerInfo();
4408}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004409
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004410
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004411SDValue
4412SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004413 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004414 SDValue Ptr, SDValue Offset,
4415 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004416 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004417 unsigned Alignment, const MDNode *TBAAInfo,
4418 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004419 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004420 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004421 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4422 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004423
Dan Gohmanc76909a2009-09-25 20:36:54 +00004424 unsigned Flags = MachineMemOperand::MOLoad;
4425 if (isVolatile)
4426 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004427 if (isNonTemporal)
4428 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004429 if (isInvariant)
4430 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004431
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004432 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4433 // clients.
4434 if (PtrInfo.V == 0)
4435 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004436
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004437 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004438 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004439 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004440 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004441 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004442}
4443
4444SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004445SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004446 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004447 SDValue Ptr, SDValue Offset, EVT MemVT,
4448 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004449 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004450 ExtType = ISD::NON_EXTLOAD;
4451 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004452 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004453 } else {
4454 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004455 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4456 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004457 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004458 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004459 assert(VT.isVector() == MemVT.isVector() &&
4460 "Cannot use trunc store to convert to or from a vector!");
4461 assert((!VT.isVector() ||
4462 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4463 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004464 }
4465
4466 bool Indexed = AM != ISD::UNINDEXED;
4467 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4468 "Unindexed load with an offset!");
4469
4470 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004471 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004472 SDValue Ops[] = { Chain, Ptr, Offset };
4473 FoldingSetNodeID ID;
4474 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004475 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004476 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004477 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004478 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004479 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004480 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004481 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4482 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004483 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004484 }
Jack Carter3af4d252013-09-09 22:02:08 +00004485 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4486 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman95531882010-03-18 18:49:47 +00004487 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004488 CSEMap.InsertNode(N, IP);
4489 AllNodes.push_back(N);
4490 return SDValue(N, 0);
4491}
4492
Andrew Trickac6d9be2013-05-25 02:42:55 +00004493SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004494 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004495 MachinePointerInfo PtrInfo,
4496 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004497 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004498 const MDNode *TBAAInfo,
4499 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004500 SDValue Undef = getUNDEF(Ptr.getValueType());
4501 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004502 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4503 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004504}
Dale Johannesene8c17332009-01-29 00:47:48 +00004505
Andrew Trickac6d9be2013-05-25 02:42:55 +00004506SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004507 SDValue Chain, SDValue Ptr,
4508 MachinePointerInfo PtrInfo, EVT MemVT,
4509 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004510 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004511 SDValue Undef = getUNDEF(Ptr.getValueType());
4512 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004513 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004514 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004515}
4516
4517
Dan Gohman475871a2008-07-27 21:46:04 +00004518SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004519SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004520 SDValue Offset, ISD::MemIndexedMode AM) {
4521 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4522 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4523 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004524 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004525 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004526 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004527 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004528}
4529
Andrew Trickac6d9be2013-05-25 02:42:55 +00004530SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004531 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004532 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004533 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004534 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004535 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004536 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004537 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004538
Dan Gohmanc76909a2009-09-25 20:36:54 +00004539 unsigned Flags = MachineMemOperand::MOStore;
4540 if (isVolatile)
4541 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004542 if (isNonTemporal)
4543 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004544
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004545 if (PtrInfo.V == 0)
4546 PtrInfo = InferPointerInfo(Ptr);
4547
4548 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004549 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004550 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004551 Val.getValueType().getStoreSize(), Alignment,
4552 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004553
4554 return getStore(Chain, dl, Val, Ptr, MMO);
4555}
4556
Andrew Trickac6d9be2013-05-25 02:42:55 +00004557SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004558 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004559 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004560 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004561 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004562 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004563 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004564 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4565 FoldingSetNodeID ID;
4566 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004567 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004568 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004569 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004570 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004571 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4573 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004574 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004575 }
Jack Carter3af4d252013-09-09 22:02:08 +00004576 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4577 dl.getDebugLoc(), VTs,
4578 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004579 CSEMap.InsertNode(N, IP);
4580 AllNodes.push_back(N);
4581 return SDValue(N, 0);
4582}
4583
Andrew Trickac6d9be2013-05-25 02:42:55 +00004584SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004585 SDValue Ptr, MachinePointerInfo PtrInfo,
4586 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004587 unsigned Alignment,
4588 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004589 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004590 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004591 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4592 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004593
Dan Gohmanc76909a2009-09-25 20:36:54 +00004594 unsigned Flags = MachineMemOperand::MOStore;
4595 if (isVolatile)
4596 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004597 if (isNonTemporal)
4598 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004599
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004600 if (PtrInfo.V == 0)
4601 PtrInfo = InferPointerInfo(Ptr);
4602
4603 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004604 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004605 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4606 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004607
4608 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4609}
4610
Andrew Trickac6d9be2013-05-25 02:42:55 +00004611SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004612 SDValue Ptr, EVT SVT,
4613 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004614 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004615
Michael Ilseman06b96902012-09-10 16:56:31 +00004616 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004617 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004618 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004619 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004620
Dan Gohman2e141d72009-12-14 23:40:38 +00004621 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4622 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004623 assert(VT.isInteger() == SVT.isInteger() &&
4624 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004625 assert(VT.isVector() == SVT.isVector() &&
4626 "Cannot use trunc store to convert to or from a vector!");
4627 assert((!VT.isVector() ||
4628 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4629 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004630
Owen Anderson825b72b2009-08-11 20:47:22 +00004631 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004632 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004633 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4634 FoldingSetNodeID ID;
4635 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004636 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004637 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004638 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004639 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004640 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004641 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4642 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004643 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004644 }
Jack Carter3af4d252013-09-09 22:02:08 +00004645 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4646 dl.getDebugLoc(), VTs,
4647 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004648 CSEMap.InsertNode(N, IP);
4649 AllNodes.push_back(N);
4650 return SDValue(N, 0);
4651}
4652
Dan Gohman475871a2008-07-27 21:46:04 +00004653SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004654SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004655 SDValue Offset, ISD::MemIndexedMode AM) {
4656 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4657 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4658 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004660 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4661 FoldingSetNodeID ID;
4662 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004663 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004664 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004665 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004666 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004667 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004668 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004669
Jack Carter3af4d252013-09-09 22:02:08 +00004670 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4671 dl.getDebugLoc(), VTs, AM,
Dan Gohman95531882010-03-18 18:49:47 +00004672 ST->isTruncatingStore(),
4673 ST->getMemoryVT(),
4674 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004675 CSEMap.InsertNode(N, IP);
4676 AllNodes.push_back(N);
4677 return SDValue(N, 0);
4678}
4679
Andrew Trickac6d9be2013-05-25 02:42:55 +00004680SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004681 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004682 SDValue SV,
4683 unsigned Align) {
4684 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4685 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004686}
4687
Andrew Trickac6d9be2013-05-25 02:42:55 +00004688SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004689 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004690 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004691 case 0: return getNode(Opcode, DL, VT);
4692 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4693 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4694 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004695 default: break;
4696 }
4697
Dan Gohman475871a2008-07-27 21:46:04 +00004698 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004699 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004700 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004701 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004702}
4703
Andrew Trickac6d9be2013-05-25 02:42:55 +00004704SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004705 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004706 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004707 case 0: return getNode(Opcode, DL, VT);
4708 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4709 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4710 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004711 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004712 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004713
Chris Lattneref847df2005-04-09 03:27:28 +00004714 switch (Opcode) {
4715 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004716 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004717 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004718 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4719 "LHS and RHS of condition must have same type!");
4720 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4721 "True and False arms of SelectCC must have same type!");
4722 assert(Ops[2].getValueType() == VT &&
4723 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004724 break;
4725 }
4726 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004727 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004728 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4729 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004730 break;
4731 }
Chris Lattneref847df2005-04-09 03:27:28 +00004732 }
4733
Chris Lattner385328c2005-05-14 07:42:29 +00004734 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004735 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004736 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004737
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004738 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004739 FoldingSetNodeID ID;
4740 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004741 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004742
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004743 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004744 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004745
Jack Carter3af4d252013-09-09 22:02:08 +00004746 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4747 VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004748 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004749 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004750 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4751 VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004752 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004753
Chris Lattneref847df2005-04-09 03:27:28 +00004754 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004755#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004756 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004757#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004758 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004759}
4760
Andrew Trickac6d9be2013-05-25 02:42:55 +00004761SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Benjamin Kramer3853f742013-03-07 20:33:29 +00004762 ArrayRef<EVT> ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004763 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004764 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004765 Ops, NumOps);
4766}
4767
Andrew Trickac6d9be2013-05-25 02:42:55 +00004768SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004769 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004770 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004771 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004772 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4773 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004774}
4775
Andrew Trickac6d9be2013-05-25 02:42:55 +00004776SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004777 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004778 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004779 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004780
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004781#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004782 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004783 // FIXME: figure out how to safely handle things like
4784 // int foo(int x) { return 1 << (x & 255); }
4785 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004786 case ISD::SRA_PARTS:
4787 case ISD::SRL_PARTS:
4788 case ISD::SHL_PARTS:
4789 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004791 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004792 else if (N3.getOpcode() == ISD::AND)
4793 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4794 // If the and is only masking out bits that cannot effect the shift,
4795 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004796 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004797 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004798 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004799 }
4800 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004801 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004802#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004803
Chris Lattner43247a12005-08-25 19:12:10 +00004804 // Memoize the node unless it returns a flag.
4805 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004806 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004807 FoldingSetNodeID ID;
4808 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004809 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004810 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004811 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004812
Dan Gohman0e5f1302008-07-07 23:02:41 +00004813 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004814 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4815 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004816 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004817 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4818 DL.getDebugLoc(), VTList, Ops[0],
4819 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004820 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004821 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4822 DL.getDebugLoc(), VTList, Ops[0],
4823 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004824 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004825 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4826 VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004827 }
Chris Lattnera5682852006-08-07 23:03:03 +00004828 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004829 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004830 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004831 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4832 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004833 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004834 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4835 DL.getDebugLoc(), VTList, Ops[0],
4836 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004837 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004838 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4839 DL.getDebugLoc(), VTList, Ops[0],
4840 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004841 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004842 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4843 VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004844 }
Chris Lattner43247a12005-08-25 19:12:10 +00004845 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004846 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004847#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004848 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004849#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004850 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004851}
4852
Andrew Trickac6d9be2013-05-25 02:42:55 +00004853SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004854 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004855}
4856
Andrew Trickac6d9be2013-05-25 02:42:55 +00004857SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004858 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004859 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004860 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004861}
4862
Andrew Trickac6d9be2013-05-25 02:42:55 +00004863SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004864 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004865 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004866 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004867}
4868
Andrew Trickac6d9be2013-05-25 02:42:55 +00004869SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004870 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004871 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004872 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004873}
4874
Andrew Trickac6d9be2013-05-25 02:42:55 +00004875SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004876 SDValue N1, SDValue N2, SDValue N3,
4877 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004878 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004879 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004880}
4881
Andrew Trickac6d9be2013-05-25 02:42:55 +00004882SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004883 SDValue N1, SDValue N2, SDValue N3,
4884 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004885 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004886 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004887}
4888
Owen Andersone50ed302009-08-10 22:56:29 +00004889SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004890 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004891}
4892
Owen Andersone50ed302009-08-10 22:56:29 +00004893SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004894 FoldingSetNodeID ID;
4895 ID.AddInteger(2U);
4896 ID.AddInteger(VT1.getRawBits());
4897 ID.AddInteger(VT2.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004898
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004899 void *IP = 0;
4900 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
4901 if (Result == NULL) {
4902 EVT *Array = Allocator.Allocate<EVT>(2);
4903 Array[0] = VT1;
4904 Array[1] = VT2;
4905 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
4906 VTListMap.InsertNode(Result, IP);
4907 }
4908 return Result->getSDVTList();
Chris Lattnera3255112005-11-08 23:30:28 +00004909}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004910
Owen Andersone50ed302009-08-10 22:56:29 +00004911SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004912 FoldingSetNodeID ID;
4913 ID.AddInteger(3U);
4914 ID.AddInteger(VT1.getRawBits());
4915 ID.AddInteger(VT2.getRawBits());
4916 ID.AddInteger(VT3.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004917
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004918 void *IP = 0;
4919 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
4920 if (Result == NULL) {
4921 EVT *Array = Allocator.Allocate<EVT>(3);
4922 Array[0] = VT1;
4923 Array[1] = VT2;
4924 Array[2] = VT3;
4925 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
4926 VTListMap.InsertNode(Result, IP);
4927 }
4928 return Result->getSDVTList();
Chris Lattner70046e92006-08-15 17:46:01 +00004929}
4930
Owen Andersone50ed302009-08-10 22:56:29 +00004931SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004932 FoldingSetNodeID ID;
4933 ID.AddInteger(4U);
4934 ID.AddInteger(VT1.getRawBits());
4935 ID.AddInteger(VT2.getRawBits());
4936 ID.AddInteger(VT3.getRawBits());
4937 ID.AddInteger(VT4.getRawBits());
Bill Wendling13d6d442008-12-01 23:28:22 +00004938
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004939 void *IP = 0;
4940 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
4941 if (Result == NULL) {
4942 EVT *Array = Allocator.Allocate<EVT>(4);
4943 Array[0] = VT1;
4944 Array[1] = VT2;
4945 Array[2] = VT3;
4946 Array[3] = VT4;
4947 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
4948 VTListMap.InsertNode(Result, IP);
4949 }
4950 return Result->getSDVTList();
Bill Wendling13d6d442008-12-01 23:28:22 +00004951}
4952
Owen Andersone50ed302009-08-10 22:56:29 +00004953SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004954 FoldingSetNodeID ID;
4955 ID.AddInteger(NumVTs);
4956 for (unsigned index = 0; index < NumVTs; index++) {
4957 ID.AddInteger(VTs[index].getRawBits());
Chris Lattner70046e92006-08-15 17:46:01 +00004958 }
4959
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004960 void *IP = 0;
4961 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
4962 if (Result == NULL) {
4963 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
4964 std::copy(VTs, VTs + NumVTs, Array);
4965 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
4966 VTListMap.InsertNode(Result, IP);
Chris Lattner70046e92006-08-15 17:46:01 +00004967 }
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004968 return Result->getSDVTList();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004969}
4970
4971
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004972/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4973/// specified operands. If the resultant node already exists in the DAG,
4974/// this does not modify the specified node, instead it returns the node that
4975/// already exists. If the resultant node does not exist in the DAG, the
4976/// input node is returned. As a degenerate case, if you specify the same
4977/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004978SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004979 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004980
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004981 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004982 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004983
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004984 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004985 void *InsertPos = 0;
4986 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004987 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004988
Dan Gohman79acd2b2008-07-21 22:38:59 +00004989 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004990 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004991 if (!RemoveNodeFromCSEMaps(N))
4992 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004993
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004994 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004995 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004996
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004997 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004998 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004999 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005000}
5001
Dan Gohman027657d2010-06-18 15:30:29 +00005002SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005003 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005004
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005005 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005006 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00005007 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00005008
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005009 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005010 void *InsertPos = 0;
5011 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005012 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005013
Dan Gohman79acd2b2008-07-21 22:38:59 +00005014 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005015 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005016 if (!RemoveNodeFromCSEMaps(N))
5017 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005018
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005019 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005020 if (N->OperandList[0] != Op1)
5021 N->OperandList[0].set(Op1);
5022 if (N->OperandList[1] != Op2)
5023 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005024
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005025 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005026 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005027 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005028}
5029
Dan Gohman027657d2010-06-18 15:30:29 +00005030SDNode *SelectionDAG::
5031UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005032 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005033 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005034}
5035
Dan Gohman027657d2010-06-18 15:30:29 +00005036SDNode *SelectionDAG::
5037UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005038 SDValue Op3, SDValue Op4) {
5039 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005040 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005041}
5042
Dan Gohman027657d2010-06-18 15:30:29 +00005043SDNode *SelectionDAG::
5044UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SDValue Op3, SDValue Op4, SDValue Op5) {
5046 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005047 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00005048}
5049
Dan Gohman027657d2010-06-18 15:30:29 +00005050SDNode *SelectionDAG::
5051UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005052 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005053 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005054
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005055 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005056 bool AnyChange = false;
5057 for (unsigned i = 0; i != NumOps; ++i) {
5058 if (Ops[i] != N->getOperand(i)) {
5059 AnyChange = true;
5060 break;
5061 }
5062 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005063
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005064 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00005065 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005066
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005067 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005068 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005069 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005070 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005071
Dan Gohman7ceda162008-05-02 00:05:03 +00005072 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005073 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005074 if (!RemoveNodeFromCSEMaps(N))
5075 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005076
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005077 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005078 for (unsigned i = 0; i != NumOps; ++i)
5079 if (N->OperandList[i] != Ops[i])
5080 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005081
5082 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005083 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005084 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005085}
5086
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005087/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005088/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005089void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005090 // Unlike the code in MorphNodeTo that does this, we don't need to
5091 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005092 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5093 SDUse &Use = *I++;
5094 Use.set(SDValue());
5095 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005096}
Chris Lattner149c58c2005-08-16 18:17:10 +00005097
Dan Gohmane8be6c62008-07-17 19:10:17 +00005098/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5099/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005100///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005101SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005102 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005103 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005104 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00005105}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005106
Dan Gohmane8be6c62008-07-17 19:10:17 +00005107SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005108 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005109 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005110 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005111 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00005112}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005113
Dan Gohmane8be6c62008-07-17 19:10:17 +00005114SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005115 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005116 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005117 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005118 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005119 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00005120}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005121
Dan Gohmane8be6c62008-07-17 19:10:17 +00005122SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005123 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005124 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005125 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005126 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005127 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00005128}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005129
Dan Gohmane8be6c62008-07-17 19:10:17 +00005130SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005131 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00005132 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005133 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005134 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005135}
5136
Dan Gohmane8be6c62008-07-17 19:10:17 +00005137SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005138 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005139 unsigned NumOps) {
5140 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005141 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005142}
5143
Dan Gohmane8be6c62008-07-17 19:10:17 +00005144SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005145 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005146 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005147 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00005148}
5149
Dan Gohmane8be6c62008-07-17 19:10:17 +00005150SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005151 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00005152 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005153 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005154 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005155}
5156
Bill Wendling13d6d442008-12-01 23:28:22 +00005157SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005158 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00005159 const SDValue *Ops, unsigned NumOps) {
5160 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5161 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5162}
5163
Scott Michelfdc40a02009-02-17 22:15:04 +00005164SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005165 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005166 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005167 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005168 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005169 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005170}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005171
Scott Michelfdc40a02009-02-17 22:15:04 +00005172SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005173 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005174 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005175 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005176 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005177 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005178}
5179
Dan Gohmane8be6c62008-07-17 19:10:17 +00005180SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005181 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005182 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005183 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005184 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005185 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005186 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00005187}
5188
Dan Gohmane8be6c62008-07-17 19:10:17 +00005189SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005190 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005191 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005192 SDValue Op3) {
5193 SDVTList VTs = getVTList(VT1, VT2, VT3);
5194 SDValue Ops[] = { Op1, Op2, Op3 };
5195 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5196}
5197
5198SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00005199 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005200 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00005201 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5202 // Reset the NodeID to -1.
5203 N->setNodeId(-1);
5204 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005205}
5206
Andrew Trickac6d9be2013-05-25 02:42:55 +00005207/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel0508d042011-12-15 18:21:18 +00005208/// the line number information on the merged node since it is not possible to
5209/// preserve the information that operation is associated with multiple lines.
5210/// This will make the debugger working better at -O0, were there is a higher
5211/// probability having other instructions associated with that line.
5212///
Andrew Trickac6d9be2013-05-25 02:42:55 +00005213/// For IROrder, we keep the smaller of the two
5214SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel0508d042011-12-15 18:21:18 +00005215 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005216 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5217 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel0508d042011-12-15 18:21:18 +00005218 N->setDebugLoc(DebugLoc());
5219 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005220 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5221 N->setIROrder(Order);
Devang Patel0508d042011-12-15 18:21:18 +00005222 return N;
5223}
5224
Chris Lattner21221e32010-02-28 21:36:14 +00005225/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005226/// return type, opcode, and operands.
5227///
5228/// Note that MorphNodeTo returns the resultant node. If there is already a
5229/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickac6d9be2013-05-25 02:42:55 +00005230/// the current one. Note that the SDLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005231///
5232/// Using MorphNodeTo is faster than creating a new node and swapping it in
5233/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005234/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005235/// the node's users.
5236///
5237SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00005238 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005239 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005240 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00005241 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005242 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005243 FoldingSetNodeID ID;
5244 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005245 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005246 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohmane8be6c62008-07-17 19:10:17 +00005247 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005248
Dan Gohman095cc292008-09-13 01:54:27 +00005249 if (!RemoveNodeFromCSEMaps(N))
5250 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00005251
Dan Gohmane8be6c62008-07-17 19:10:17 +00005252 // Start the morphing.
5253 N->NodeType = Opc;
5254 N->ValueList = VTs.VTs;
5255 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005256
Dan Gohmane8be6c62008-07-17 19:10:17 +00005257 // Clear the operands list, updating used nodes to remove this from their
5258 // use list. Keep track of any operands that become dead as a result.
5259 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005260 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5261 SDUse &Use = *I++;
5262 SDNode *Used = Use.getNode();
5263 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005264 if (Used->use_empty())
5265 DeadNodeSet.insert(Used);
5266 }
5267
Dan Gohmanc76909a2009-09-25 20:36:54 +00005268 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5269 // Initialize the memory references information.
5270 MN->setMemRefs(0, 0);
5271 // If NumOps is larger than the # of operands we can have in a
5272 // MachineSDNode, reallocate the operand list.
5273 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5274 if (MN->OperandsNeedDelete)
5275 delete[] MN->OperandList;
5276 if (NumOps > array_lengthof(MN->LocalOperands))
5277 // We're creating a final node that will live unmorphed for the
5278 // remainder of the current SelectionDAG iteration, so we can allocate
5279 // the operands directly out of a pool with no recycling metadata.
5280 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005281 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005282 else
5283 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5284 MN->OperandsNeedDelete = false;
5285 } else
5286 MN->InitOperands(MN->OperandList, Ops, NumOps);
5287 } else {
5288 // If NumOps is larger than the # of operands we currently have, reallocate
5289 // the operand list.
5290 if (NumOps > N->NumOperands) {
5291 if (N->OperandsNeedDelete)
5292 delete[] N->OperandList;
5293 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005294 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005295 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005296 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005297 }
5298
5299 // Delete any nodes that are still dead after adding the uses for the
5300 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005301 if (!DeadNodeSet.empty()) {
5302 SmallVector<SDNode *, 16> DeadNodes;
5303 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5304 E = DeadNodeSet.end(); I != E; ++I)
5305 if ((*I)->use_empty())
5306 DeadNodes.push_back(*I);
5307 RemoveDeadNodes(DeadNodes);
5308 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005309
Dan Gohmane8be6c62008-07-17 19:10:17 +00005310 if (IP)
5311 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005312 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005313}
5314
Chris Lattner0fb094f2005-11-19 01:44:53 +00005315
Dan Gohman602b0c82009-09-25 18:54:59 +00005316/// getMachineNode - These are used for target selectors to create a new node
5317/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005318///
Dan Gohman602b0c82009-09-25 18:54:59 +00005319/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005320/// node of the specified opcode and operands, it returns that node instead of
5321/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005322MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005323SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005324 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005325 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005326}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005327
Dan Gohmanc81b7832009-10-10 01:29:16 +00005328MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005329SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005330 SDVTList VTs = getVTList(VT);
5331 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005332 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005333}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005334
Dan Gohmanc81b7832009-10-10 01:29:16 +00005335MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005336SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005337 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005338 SDVTList VTs = getVTList(VT);
5339 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005340 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005341}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005342
Dan Gohmanc81b7832009-10-10 01:29:16 +00005343MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005344SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005345 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005346 SDVTList VTs = getVTList(VT);
5347 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005348 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005349}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005350
Dan Gohmanc81b7832009-10-10 01:29:16 +00005351MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005352SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005353 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005354 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005355 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005356}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005357
Dan Gohmanc81b7832009-10-10 01:29:16 +00005358MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005359SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005360 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005361 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005362}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005363
Dan Gohmanc81b7832009-10-10 01:29:16 +00005364MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005365SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005366 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005367 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005368 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005369 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005370}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005371
Dan Gohmanc81b7832009-10-10 01:29:16 +00005372MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005373SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005374 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005375 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005376 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005377 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005378}
5379
Dan Gohmanc81b7832009-10-10 01:29:16 +00005380MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005381SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005382 EVT VT1, EVT VT2, SDValue Op1,
5383 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005384 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005385 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005386 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005387}
5388
Dan Gohmanc81b7832009-10-10 01:29:16 +00005389MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005390SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005391 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005392 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005393 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005394 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005395}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005396
Dan Gohmanc81b7832009-10-10 01:29:16 +00005397MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005398SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005399 EVT VT1, EVT VT2, EVT VT3,
5400 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005401 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005402 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005403 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005404}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005405
Dan Gohmanc81b7832009-10-10 01:29:16 +00005406MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005407SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005408 EVT VT1, EVT VT2, EVT VT3,
5409 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005410 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005411 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005412 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005413}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005414
Dan Gohmanc81b7832009-10-10 01:29:16 +00005415MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005416SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005417 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005418 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005419 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005420 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005421}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005422
Dan Gohmanc81b7832009-10-10 01:29:16 +00005423MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005424SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005425 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005426 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005427 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005428 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005429}
5430
Dan Gohmanc81b7832009-10-10 01:29:16 +00005431MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005432SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005433 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005434 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005435 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liao2a8bea72013-04-19 22:22:57 +00005436 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005437}
5438
Dan Gohmanc81b7832009-10-10 01:29:16 +00005439MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005440SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005441 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005442 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005443 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005444 void *IP = 0;
Michael Liao2a8bea72013-04-19 22:22:57 +00005445 const SDValue *Ops = OpsArray.data();
5446 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005447
5448 if (DoCSE) {
5449 FoldingSetNodeID ID;
5450 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5451 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005452 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005453 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel0508d042011-12-15 18:21:18 +00005454 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005455 }
5456
5457 // Allocate a new MachineSDNode.
Jack Carter3af4d252013-09-09 22:02:08 +00005458 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5459 DL.getDebugLoc(), VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005460
5461 // Initialize the operands list.
5462 if (NumOps > array_lengthof(N->LocalOperands))
5463 // We're creating a final node that will live unmorphed for the
5464 // remainder of the current SelectionDAG iteration, so we can allocate
5465 // the operands directly out of a pool with no recycling metadata.
5466 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5467 Ops, NumOps);
5468 else
5469 N->InitOperands(N->LocalOperands, Ops, NumOps);
5470 N->OperandsNeedDelete = false;
5471
5472 if (DoCSE)
5473 CSEMap.InsertNode(N, IP);
5474
5475 AllNodes.push_back(N);
5476#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005477 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005478#endif
5479 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005480}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005481
Dan Gohman6a402dc2009-08-19 18:16:17 +00005482/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005483/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005484SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005485SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohman6a402dc2009-08-19 18:16:17 +00005486 SDValue Operand) {
5487 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005488 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005489 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005490 return SDValue(Subreg, 0);
5491}
5492
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005493/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005494/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005495SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005496SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005497 SDValue Operand, SDValue Subreg) {
5498 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005499 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005500 VT, Operand, Subreg, SRIdxVal);
5501 return SDValue(Result, 0);
5502}
5503
Evan Cheng08b11732008-03-22 01:55:50 +00005504/// getNodeIfExists - Get the specified node if it's already available, or
5505/// else return NULL.
5506SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005507 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005508 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005509 FoldingSetNodeID ID;
5510 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5511 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005512 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005513 return E;
5514 }
5515 return NULL;
5516}
5517
Evan Cheng31441b72010-03-29 20:48:30 +00005518/// getDbgValue - Creates a SDDbgValue node.
5519///
5520SDDbgValue *
5521SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5522 DebugLoc DL, unsigned O) {
5523 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5524}
5525
5526SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005527SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005528 DebugLoc DL, unsigned O) {
5529 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5530}
5531
5532SDDbgValue *
5533SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5534 DebugLoc DL, unsigned O) {
5535 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5536}
5537
Dan Gohmana72d2a22010-03-03 21:33:37 +00005538namespace {
5539
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005540/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005541/// pointed to by a use iterator is deleted, increment the use iterator
5542/// so that it doesn't dangle.
5543///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005544class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005545 SDNode::use_iterator &UI;
5546 SDNode::use_iterator &UE;
5547
5548 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5549 // Increment the iterator as needed.
5550 while (UI != UE && N == *UI)
5551 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005552 }
5553
5554public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005555 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005556 SDNode::use_iterator &ui,
5557 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005558 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005559};
5560
5561}
5562
Evan Cheng99157a02006-08-07 22:13:29 +00005563/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005564/// This can cause recursive merging of nodes in the DAG.
5565///
Chris Lattner11d049c2008-02-03 03:35:22 +00005566/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005567///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005568void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005569 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005570 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005571 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005572 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005573
Dan Gohman39946102009-01-25 16:29:12 +00005574 // Iterate over all the existing uses of From. New uses will be added
5575 // to the beginning of the use list, which we avoid visiting.
5576 // This specifically avoids visiting uses of From that arise while the
5577 // replacement is happening, because any such uses would be the result
5578 // of CSE: If an existing node looks like From after one of its operands
5579 // is replaced by To, we don't want to replace of all its users with To
5580 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005581 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005582 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005583 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005584 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005585
Chris Lattner8b8749f2005-08-17 19:00:20 +00005586 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005587 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005588
Dan Gohman39946102009-01-25 16:29:12 +00005589 // A user can appear in a use list multiple times, and when this
5590 // happens the uses are usually next to each other in the list.
5591 // To help reduce the number of CSE recomputations, process all
5592 // the uses of this user that we can find this way.
5593 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005594 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005595 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005596 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005597 } while (UI != UE && *UI == User);
5598
5599 // Now that we have modified User, add it back to the CSE maps. If it
5600 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005601 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005602 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005603
5604 // If we just RAUW'd the root, take note.
5605 if (FromN == getRoot())
5606 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005607}
5608
5609/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5610/// This can cause recursive merging of nodes in the DAG.
5611///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005612/// This version assumes that for each value of From, there is a
5613/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005614///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005615void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005616#ifndef NDEBUG
5617 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5618 assert((!From->hasAnyUseOfValue(i) ||
5619 From->getValueType(i) == To->getValueType(i)) &&
5620 "Cannot use this version of ReplaceAllUsesWith!");
5621#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005622
5623 // Handle the trivial case.
5624 if (From == To)
5625 return;
5626
Dan Gohmandbe664a2009-01-19 21:44:21 +00005627 // Iterate over just the existing users of From. See the comments in
5628 // the ReplaceAllUsesWith above.
5629 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005630 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005631 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005632 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005633
Chris Lattner8b52f212005-08-26 18:36:28 +00005634 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005635 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005636
Dan Gohman39946102009-01-25 16:29:12 +00005637 // A user can appear in a use list multiple times, and when this
5638 // happens the uses are usually next to each other in the list.
5639 // To help reduce the number of CSE recomputations, process all
5640 // the uses of this user that we can find this way.
5641 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005642 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005643 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005644 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005645 } while (UI != UE && *UI == User);
5646
5647 // Now that we have modified User, add it back to the CSE maps. If it
5648 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005649 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005650 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005651
5652 // If we just RAUW'd the root, take note.
5653 if (From == getRoot().getNode())
5654 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005655}
5656
Chris Lattner8b52f212005-08-26 18:36:28 +00005657/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5658/// This can cause recursive merging of nodes in the DAG.
5659///
5660/// This version can replace From with any result values. To must match the
5661/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005662void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005663 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005664 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005665
Dan Gohmandbe664a2009-01-19 21:44:21 +00005666 // Iterate over just the existing users of From. See the comments in
5667 // the ReplaceAllUsesWith above.
5668 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005669 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005670 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005671 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005672
Chris Lattner7b2880c2005-08-24 22:44:39 +00005673 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005674 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005675
Dan Gohman39946102009-01-25 16:29:12 +00005676 // A user can appear in a use list multiple times, and when this
5677 // happens the uses are usually next to each other in the list.
5678 // To help reduce the number of CSE recomputations, process all
5679 // the uses of this user that we can find this way.
5680 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005681 SDUse &Use = UI.getUse();
5682 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005683 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005684 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005685 } while (UI != UE && *UI == User);
5686
5687 // Now that we have modified User, add it back to the CSE maps. If it
5688 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005689 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005690 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005691
5692 // If we just RAUW'd the root, take note.
5693 if (From == getRoot().getNode())
5694 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005695}
5696
Chris Lattner012f2412006-02-17 21:58:01 +00005697/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005698/// uses of other values produced by From.getNode() alone. The Deleted
5699/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005700void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005701 // Handle the really simple, really trivial case efficiently.
5702 if (From == To) return;
5703
Chris Lattner012f2412006-02-17 21:58:01 +00005704 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005705 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005706 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005707 return;
5708 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005709
Dan Gohman39946102009-01-25 16:29:12 +00005710 // Iterate over just the existing users of From. See the comments in
5711 // the ReplaceAllUsesWith above.
5712 SDNode::use_iterator UI = From.getNode()->use_begin(),
5713 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005714 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005715 while (UI != UE) {
5716 SDNode *User = *UI;
5717 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005718
Dan Gohman39946102009-01-25 16:29:12 +00005719 // A user can appear in a use list multiple times, and when this
5720 // happens the uses are usually next to each other in the list.
5721 // To help reduce the number of CSE recomputations, process all
5722 // the uses of this user that we can find this way.
5723 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005724 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005725
5726 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005727 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005728 ++UI;
5729 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005730 }
Dan Gohman39946102009-01-25 16:29:12 +00005731
5732 // If this node hasn't been modified yet, it's still in the CSE maps,
5733 // so remove its old self from the CSE maps.
5734 if (!UserRemovedFromCSEMaps) {
5735 RemoveNodeFromCSEMaps(User);
5736 UserRemovedFromCSEMaps = true;
5737 }
5738
5739 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005740 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005741 } while (UI != UE && *UI == User);
5742
5743 // We are iterating over all uses of the From node, so if a use
5744 // doesn't use the specific value, no changes are made.
5745 if (!UserRemovedFromCSEMaps)
5746 continue;
5747
Chris Lattner01d029b2007-10-15 06:10:22 +00005748 // Now that we have modified User, add it back to the CSE maps. If it
5749 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005750 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005751 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005752
5753 // If we just RAUW'd the root, take note.
5754 if (From == getRoot())
5755 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005756}
5757
Dan Gohman39946102009-01-25 16:29:12 +00005758namespace {
5759 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5760 /// to record information about a use.
5761 struct UseMemo {
5762 SDNode *User;
5763 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005764 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005765 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005766
5767 /// operator< - Sort Memos by User.
5768 bool operator<(const UseMemo &L, const UseMemo &R) {
5769 return (intptr_t)L.User < (intptr_t)R.User;
5770 }
Dan Gohman39946102009-01-25 16:29:12 +00005771}
5772
Dan Gohmane8be6c62008-07-17 19:10:17 +00005773/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005774/// uses of other values produced by From.getNode() alone. The same value
5775/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005776/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005777void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5778 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005779 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005780 // Handle the simple, trivial case efficiently.
5781 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005782 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005783
Dan Gohman39946102009-01-25 16:29:12 +00005784 // Read up all the uses and make records of them. This helps
5785 // processing new uses that are introduced during the
5786 // replacement process.
5787 SmallVector<UseMemo, 4> Uses;
5788 for (unsigned i = 0; i != Num; ++i) {
5789 unsigned FromResNo = From[i].getResNo();
5790 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005791 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005792 E = FromNode->use_end(); UI != E; ++UI) {
5793 SDUse &Use = UI.getUse();
5794 if (Use.getResNo() == FromResNo) {
5795 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005796 Uses.push_back(Memo);
5797 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005798 }
Dan Gohman39946102009-01-25 16:29:12 +00005799 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005800
Dan Gohmane7852d02009-01-26 04:35:06 +00005801 // Sort the uses, so that all the uses from a given User are together.
5802 std::sort(Uses.begin(), Uses.end());
5803
Dan Gohman39946102009-01-25 16:29:12 +00005804 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5805 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005806 // We know that this user uses some value of From. If it is the right
5807 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005808 SDNode *User = Uses[UseIndex].User;
5809
5810 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005811 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005812
Dan Gohmane7852d02009-01-26 04:35:06 +00005813 // The Uses array is sorted, so all the uses for a given User
5814 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005815 // To help reduce the number of CSE recomputations, process all
5816 // the uses of this user that we can find this way.
5817 do {
5818 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005819 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005820 ++UseIndex;
5821
Dan Gohmane7852d02009-01-26 04:35:06 +00005822 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005823 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5824
Dan Gohmane8be6c62008-07-17 19:10:17 +00005825 // Now that we have modified User, add it back to the CSE maps. If it
5826 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005827 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005828 }
5829}
5830
Evan Chenge6f35d82006-08-01 08:20:41 +00005831/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005832/// based on their topological order. It returns the maximum id and a vector
5833/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005834unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005835
Dan Gohmanf06c8352008-09-30 18:30:35 +00005836 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005837
Dan Gohmanf06c8352008-09-30 18:30:35 +00005838 // SortedPos tracks the progress of the algorithm. Nodes before it are
5839 // sorted, nodes after it are unsorted. When the algorithm completes
5840 // it is at the end of the list.
5841 allnodes_iterator SortedPos = allnodes_begin();
5842
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005843 // Visit all the nodes. Move nodes with no operands to the front of
5844 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005845 // operand count. Before we do this, the Node Id fields of the nodes
5846 // may contain arbitrary values. After, the Node Id fields for nodes
5847 // before SortedPos will contain the topological sort index, and the
5848 // Node Id fields for nodes At SortedPos and after will contain the
5849 // count of outstanding operands.
5850 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5851 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005852 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005853 unsigned Degree = N->getNumOperands();
5854 if (Degree == 0) {
5855 // A node with no uses, add it to the result array immediately.
5856 N->setNodeId(DAGSize++);
5857 allnodes_iterator Q = N;
5858 if (Q != SortedPos)
5859 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005860 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005861 ++SortedPos;
5862 } else {
5863 // Temporarily use the Node Id as scratch space for the degree count.
5864 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005865 }
5866 }
5867
Chad Rosierf496dea2012-05-21 17:13:41 +00005868 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005869 // such that by the time the end is reached all nodes will be sorted.
5870 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5871 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005872 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005873 // N is in sorted position, so all its uses have one less operand
5874 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005875 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5876 UI != UE; ++UI) {
5877 SDNode *P = *UI;
5878 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005879 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005880 --Degree;
5881 if (Degree == 0) {
5882 // All of P's operands are sorted, so P may sorted now.
5883 P->setNodeId(DAGSize++);
5884 if (P != SortedPos)
5885 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005886 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005887 ++SortedPos;
5888 } else {
5889 // Update P's outstanding operand count.
5890 P->setNodeId(Degree);
5891 }
5892 }
David Greene221925e2010-01-20 00:59:23 +00005893 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005894#ifndef NDEBUG
5895 SDNode *S = ++I;
5896 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005897 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005898#endif
5899 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005900 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005901 }
5902
5903 assert(SortedPos == AllNodes.end() &&
5904 "Topological sort incomplete!");
5905 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5906 "First node in topological sort is not the entry token!");
5907 assert(AllNodes.front().getNodeId() == 0 &&
5908 "First node in topological sort has non-zero id!");
5909 assert(AllNodes.front().getNumOperands() == 0 &&
5910 "First node in topological sort has operands!");
5911 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5912 "Last node in topologic sort has unexpected id!");
5913 assert(AllNodes.back().use_empty() &&
5914 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005915 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005916 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005917}
5918
Evan Chengbfcb3052010-03-25 01:38:16 +00005919/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5920/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005921void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5922 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005923 if (SD)
5924 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005925}
Evan Cheng091cba12006-07-27 06:39:06 +00005926
Devang Patela2e868d2011-01-25 23:27:42 +00005927/// TransferDbgValues - Transfer SDDbgValues.
5928void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5929 if (From == To || !From.getNode()->getHasDebugValue())
5930 return;
5931 SDNode *FromNode = From.getNode();
5932 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005933 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005934 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005935 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005936 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005937 SDDbgValue *Dbg = *I;
5938 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5939 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5940 Dbg->getOffset(), Dbg->getDebugLoc(),
5941 Dbg->getOrder());
5942 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005943 }
5944 }
Craig Topperf22fd3f2013-07-03 05:11:49 +00005945 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patela778f5c2011-02-18 22:43:42 +00005946 E = ClonedDVs.end(); I != E; ++I)
5947 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005948}
5949
Jim Laskey58b968b2005-08-17 20:08:02 +00005950//===----------------------------------------------------------------------===//
5951// SDNode Class
5952//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005953
Chris Lattner48b85922007-02-04 02:41:42 +00005954HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005955 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005956}
5957
Andrew Trickac6d9be2013-05-25 02:42:55 +00005958GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
5959 DebugLoc DL, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005960 EVT VT, int64_t o, unsigned char TF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005961 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005962 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005963}
Chris Lattner48b85922007-02-04 02:41:42 +00005964
Andrew Trickac6d9be2013-05-25 02:42:55 +00005965MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
5966 EVT memvt, MachineMemOperand *mmo)
5967 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005968 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005969 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005970 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005971 assert(isNonTemporal() == MMO->isNonTemporal() &&
5972 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005973 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005974}
5975
Andrew Trickac6d9be2013-05-25 02:42:55 +00005976MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005977 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005978 MachineMemOperand *mmo)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005979 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005980 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005981 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005982 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005983 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5984 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005985}
5986
Jim Laskey583bd472006-10-27 23:46:08 +00005987/// Profile - Gather unique data for the node.
5988///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005989void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005990 AddNodeIDNode(ID, this);
5991}
5992
Owen Andersond8110fb2009-08-25 22:27:22 +00005993namespace {
5994 struct EVTArray {
5995 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005996
Owen Andersond8110fb2009-08-25 22:27:22 +00005997 EVTArray() {
5998 VTs.reserve(MVT::LAST_VALUETYPE);
5999 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6000 VTs.push_back(MVT((MVT::SimpleValueType)i));
6001 }
6002 };
6003}
6004
Owen Andersone50ed302009-08-10 22:56:29 +00006005static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00006006static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00006007static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00006008
Chris Lattnera3255112005-11-08 23:30:28 +00006009/// getValueTypeList - Return a pointer to the specified value type.
6010///
Owen Andersone50ed302009-08-10 22:56:29 +00006011const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006012 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00006013 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00006014 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00006015 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00006016 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00006017 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00006018 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00006019 }
Chris Lattnera3255112005-11-08 23:30:28 +00006020}
Duncan Sandsaf47b112007-10-16 09:56:48 +00006021
Chris Lattner5c884562005-01-12 18:37:47 +00006022/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6023/// indicated value. This method ignores uses of other values defined by this
6024/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00006025bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00006026 assert(Value < getNumValues() && "Bad value!");
6027
Roman Levensteindc1adac2008-04-07 10:06:32 +00006028 // TODO: Only iterate over uses of a given value of the node
6029 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00006030 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00006031 if (NUses == 0)
6032 return false;
6033 --NUses;
6034 }
Chris Lattner5c884562005-01-12 18:37:47 +00006035 }
6036
6037 // Found exactly the right number of uses?
6038 return NUses == 0;
6039}
6040
6041
Evan Cheng33d55952007-08-02 05:29:38 +00006042/// hasAnyUseOfValue - Return true if there are any use of the indicated
6043/// value. This method ignores uses of other values defined by this operation.
6044bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6045 assert(Value < getNumValues() && "Bad value!");
6046
Dan Gohman1373c1c2008-07-09 22:39:01 +00006047 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00006048 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00006049 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00006050
6051 return false;
6052}
6053
6054
Dan Gohman2a629952008-07-27 18:06:42 +00006055/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00006056///
Dan Gohman2a629952008-07-27 18:06:42 +00006057bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00006058 bool Seen = false;
6059 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006060 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006061 if (User == this)
6062 Seen = true;
6063 else
6064 return false;
6065 }
6066
6067 return Seen;
6068}
6069
Evan Chenge6e97e62006-11-03 07:31:32 +00006070/// isOperand - Return true if this node is an operand of N.
6071///
Dan Gohman475871a2008-07-27 21:46:04 +00006072bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006073 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6074 if (*this == N->getOperand(i))
6075 return true;
6076 return false;
6077}
6078
Evan Cheng917be682008-03-04 00:41:45 +00006079bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006080 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006081 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006082 return true;
6083 return false;
6084}
Evan Cheng4ee62112006-02-05 06:29:23 +00006085
Chris Lattner572dee72008-01-16 05:49:24 +00006086/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006087/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006088/// side-effecting instructions on any chain path. In practice, this looks
6089/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006090/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006091bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006092 unsigned Depth) const {
6093 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006094
Chris Lattner572dee72008-01-16 05:49:24 +00006095 // Don't search too deeply, we just want to be able to see through
6096 // TokenFactor's etc.
6097 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006098
Chris Lattner572dee72008-01-16 05:49:24 +00006099 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006100 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006101 if (getOpcode() == ISD::TokenFactor) {
6102 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006103 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6104 return false;
6105 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006107
Chris Lattner572dee72008-01-16 05:49:24 +00006108 // Loads don't have side effects, look through them.
6109 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6110 if (!Ld->isVolatile())
6111 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6112 }
6113 return false;
6114}
6115
Lang Hames944520f2011-07-07 04:31:51 +00006116/// hasPredecessor - Return true if N is a predecessor of this node.
6117/// N is either an operand of this node, or can be reached by recursively
6118/// traversing up the operands.
6119/// NOTE: This is an expensive method. Use it carefully.
6120bool SDNode::hasPredecessor(const SDNode *N) const {
6121 SmallPtrSet<const SDNode *, 32> Visited;
6122 SmallVector<const SDNode *, 16> Worklist;
6123 return hasPredecessorHelper(N, Visited, Worklist);
6124}
Dan Gohman6688d612009-10-28 03:44:30 +00006125
Craig Toppera0ec3f92013-07-14 04:42:23 +00006126bool
6127SDNode::hasPredecessorHelper(const SDNode *N,
6128 SmallPtrSet<const SDNode *, 32> &Visited,
6129 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames944520f2011-07-07 04:31:51 +00006130 if (Visited.empty()) {
6131 Worklist.push_back(this);
6132 } else {
6133 // Take a look in the visited set. If we've already encountered this node
6134 // we needn't search further.
6135 if (Visited.count(N))
6136 return true;
6137 }
6138
6139 // Haven't visited N yet. Continue the search.
6140 while (!Worklist.empty()) {
6141 const SDNode *M = Worklist.pop_back_val();
6142 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6143 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006144 if (Visited.insert(Op))
6145 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006146 if (Op == N)
6147 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006148 }
Lang Hames944520f2011-07-07 04:31:51 +00006149 }
Dan Gohman6688d612009-10-28 03:44:30 +00006150
6151 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006152}
6153
Evan Chengc5484282006-10-04 00:56:09 +00006154uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6155 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006156 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006157}
6158
Mon P Wangcd6e7252009-11-30 02:42:02 +00006159SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6160 assert(N->getNumValues() == 1 &&
6161 "Can't unroll a vector with multiple results!");
6162
6163 EVT VT = N->getValueType(0);
6164 unsigned NE = VT.getVectorNumElements();
6165 EVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006166 SDLoc dl(N);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006167
6168 SmallVector<SDValue, 8> Scalars;
6169 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6170
6171 // If ResNE is 0, fully unroll the vector op.
6172 if (ResNE == 0)
6173 ResNE = NE;
6174 else if (NE > ResNE)
6175 NE = ResNE;
6176
6177 unsigned i;
6178 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006179 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006180 SDValue Operand = N->getOperand(j);
6181 EVT OperandVT = Operand.getValueType();
6182 if (OperandVT.isVector()) {
6183 // A vector operand; extract a single element.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006184 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wangcd6e7252009-11-30 02:42:02 +00006185 EVT OperandEltVT = OperandVT.getVectorElementType();
6186 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6187 OperandEltVT,
6188 Operand,
Tom Stellard425b76c2013-08-05 22:22:01 +00006189 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006190 } else {
6191 // A scalar operand; just use it as is.
6192 Operands[j] = Operand;
6193 }
6194 }
6195
6196 switch (N->getOpcode()) {
6197 default:
6198 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6199 &Operands[0], Operands.size()));
6200 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006201 case ISD::VSELECT:
6202 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6203 &Operands[0], Operands.size()));
6204 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006205 case ISD::SHL:
6206 case ISD::SRA:
6207 case ISD::SRL:
6208 case ISD::ROTL:
6209 case ISD::ROTR:
6210 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter3af4d252013-09-09 22:02:08 +00006211 getShiftAmountOperand(Operands[0].getValueType(),
6212 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006213 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006214 case ISD::SIGN_EXTEND_INREG:
6215 case ISD::FP_ROUND_INREG: {
6216 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6217 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6218 Operands[0],
6219 getValueType(ExtVT)));
6220 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006221 }
6222 }
6223
6224 for (; i < ResNE; ++i)
6225 Scalars.push_back(getUNDEF(EltVT));
6226
6227 return getNode(ISD::BUILD_VECTOR, dl,
6228 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6229 &Scalars[0], Scalars.size());
6230}
6231
Evan Cheng64fa4a92009-12-09 01:36:00 +00006232
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006233/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6234/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006235/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006236bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006237 unsigned Bytes, int Dist) const {
6238 if (LD->getChain() != Base->getChain())
6239 return false;
6240 EVT VT = LD->getValueType(0);
6241 if (VT.getSizeInBits() / 8 != Bytes)
6242 return false;
6243
6244 SDValue Loc = LD->getOperand(1);
6245 SDValue BaseLoc = Base->getOperand(1);
6246 if (Loc.getOpcode() == ISD::FrameIndex) {
6247 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6248 return false;
6249 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6250 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6251 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6252 int FS = MFI->getObjectSize(FI);
6253 int BFS = MFI->getObjectSize(BFI);
6254 if (FS != BFS || FS != (int)Bytes) return false;
6255 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6256 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006257
6258 // Handle X+C
6259 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6260 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6261 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006262
Dan Gohman46510a72010-04-15 01:51:59 +00006263 const GlobalValue *GV1 = NULL;
6264 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006265 int64_t Offset1 = 0;
6266 int64_t Offset2 = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006267 const TargetLowering *TLI = TM.getTargetLowering();
6268 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6269 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Cheng64fa4a92009-12-09 01:36:00 +00006270 if (isGA1 && isGA2 && GV1 == GV2)
6271 return Offset1 == (Offset2 + Dist*Bytes);
6272 return false;
6273}
6274
6275
Evan Chengf2dc5c72009-12-09 01:04:59 +00006276/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6277/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006278unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006279 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006280 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006281 int64_t GVOffset = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006282 const TargetLowering *TLI = TM.getTargetLowering();
6283 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
6284 unsigned PtrWidth = TLI->getPointerTy().getSizeInBits();
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006285 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006286 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006287 TLI->getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006288 unsigned AlignBits = KnownZero.countTrailingOnes();
6289 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6290 if (Align)
6291 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006292 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006293
Evan Chengf2dc5c72009-12-09 01:04:59 +00006294 // If this is a direct reference to a stack slot, use information about the
6295 // stack slot's alignment.
6296 int FrameIdx = 1 << 31;
6297 int64_t FrameOffset = 0;
6298 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6299 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006300 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006301 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006302 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006303 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6304 FrameOffset = Ptr.getConstantOperandVal(1);
6305 }
6306
6307 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006308 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006309 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6310 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006311 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006312 }
6313
6314 return 0;
6315}
6316
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006317// getAddressSpace - Return the address space this GlobalAddress belongs to.
6318unsigned GlobalAddressSDNode::getAddressSpace() const {
6319 return getGlobal()->getType()->getAddressSpace();
6320}
6321
6322
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006323Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006324 if (isMachineConstantPoolEntry())
6325 return Val.MachineCPVal->getType();
6326 return Val.ConstVal->getType();
6327}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006328
Bob Wilson24e338e2009-03-02 23:24:16 +00006329bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6330 APInt &SplatUndef,
6331 unsigned &SplatBitSize,
6332 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006333 unsigned MinSplatBits,
6334 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006335 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006336 assert(VT.isVector() && "Expected a vector type");
6337 unsigned sz = VT.getSizeInBits();
6338 if (MinSplatBits > sz)
6339 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006340
Bob Wilson24e338e2009-03-02 23:24:16 +00006341 SplatValue = APInt(sz, 0);
6342 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006343
Bob Wilson24e338e2009-03-02 23:24:16 +00006344 // Get the bits. Bits with undefined values (when the corresponding element
6345 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6346 // in SplatValue. If any of the values are not constant, give up and return
6347 // false.
6348 unsigned int nOps = getNumOperands();
6349 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6350 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006351
6352 for (unsigned j = 0; j < nOps; ++j) {
6353 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006354 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006355 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006356
Bob Wilson24e338e2009-03-02 23:24:16 +00006357 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006358 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006359 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006360 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006361 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006362 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006363 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006364 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006365 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006366 }
6367
Bob Wilson24e338e2009-03-02 23:24:16 +00006368 // The build_vector is all constants or undefs. Find the smallest element
6369 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006370
Bob Wilson24e338e2009-03-02 23:24:16 +00006371 HasAnyUndefs = (SplatUndef != 0);
6372 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006373
Bob Wilson24e338e2009-03-02 23:24:16 +00006374 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006375 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6376 APInt LowValue = SplatValue.trunc(HalfSize);
6377 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6378 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006379
Bob Wilson24e338e2009-03-02 23:24:16 +00006380 // If the two halves do not match (ignoring undef bits), stop here.
6381 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6382 MinSplatBits > HalfSize)
6383 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006384
Bob Wilson24e338e2009-03-02 23:24:16 +00006385 SplatValue = HighValue | LowValue;
6386 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006387
Bob Wilson24e338e2009-03-02 23:24:16 +00006388 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006389 }
6390
Bob Wilson24e338e2009-03-02 23:24:16 +00006391 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006392 return true;
6393}
Nate Begeman9008ca62009-04-27 18:41:29 +00006394
Owen Andersone50ed302009-08-10 22:56:29 +00006395bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006396 // Find the first non-undef value in the shuffle mask.
6397 unsigned i, e;
6398 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6399 /* search */;
6400
Nate Begemana6415752009-04-29 18:13:31 +00006401 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006402
Nate Begeman5a5ca152009-04-29 05:20:52 +00006403 // Make sure all remaining elements are either undef or the same as the first
6404 // non-undef value.
6405 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006406 if (Mask[i] >= 0 && Mask[i] != Idx)
6407 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006408 return true;
6409}
David Greenecf495bc2010-01-20 20:13:31 +00006410
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006411#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006412static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006413 SmallPtrSet<const SDNode*, 32> &Visited,
6414 SmallPtrSet<const SDNode*, 32> &Checked) {
6415 // If this node has already been checked, don't check it again.
6416 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006417 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006418
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006419 // If a node has already been visited on this depth-first walk, reject it as
6420 // a cycle.
6421 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006422 dbgs() << "Offending node:\n";
6423 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006424 errs() << "Detected cycle in SelectionDAG\n";
6425 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006426 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006427
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006428 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6429 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006430
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006431 Checked.insert(N);
6432 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006433}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006434#endif
David Greenecf495bc2010-01-20 20:13:31 +00006435
6436void llvm::checkForCycles(const llvm::SDNode *N) {
6437#ifdef XDEBUG
6438 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006439 SmallPtrSet<const SDNode*, 32> visited;
6440 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006441 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006442#endif
6443}
6444
6445void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6446 checkForCycles(DAG->getRoot().getNode());
6447}