blob: 1cee3262e8db91fcb1148fff9c94fa863dc3e11e [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"
Bill Wendlinge36025e2009-12-21 19:34:59 +000015#include "SDNodeOrdering.h"
Evan Chenga8efe282010-03-14 19:56:39 +000016#include "SDNodeDbgValue.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000017#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000018#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000019#include "llvm/DebugInfo.h"
20#include "llvm/DerivedTypes.h"
Dan Gohmanffef8ac2009-08-11 16:02:12 +000021#include "llvm/Function.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000022#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000024#include "llvm/Intrinsics.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000025#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include "llvm/Assembly/Writer.h"
27#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000032#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000033#include "llvm/Target/TargetLowering.h"
Dan Gohmanff7a5622010-05-11 17:31:57 +000034#include "llvm/Target/TargetSelectionDAGInfo.h"
Dan Gohman760f86f2009-01-22 21:58:43 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000036#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +000037#include "llvm/Target/TargetIntrinsicInfo.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000038#include "llvm/Target/TargetMachine.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000039#include "llvm/Support/CommandLine.h"
David Greene55d146e2010-01-05 01:24:36 +000040#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000041#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000042#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000043#include "llvm/Support/MathExtras.h"
44#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000045#include "llvm/Support/Mutex.h"
Chris Lattner012f2412006-02-17 21:58:01 +000046#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000047#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000048#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000049#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000050#include "llvm/ADT/StringExtras.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
Owen Andersone50ed302009-08-10 22:56:29 +000062static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +000063 switch (VT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +000064 default: llvm_unreachable("Unknown FP format");
Owen Andersond7484e52012-04-05 18:50:32 +000065 case MVT::f16: return &APFloat::IEEEhalf;
Owen Anderson825b72b2009-08-11 20:47:22 +000066 case MVT::f32: return &APFloat::IEEEsingle;
67 case MVT::f64: return &APFloat::IEEEdouble;
68 case MVT::f80: return &APFloat::x87DoubleExtended;
69 case MVT::f128: return &APFloat::IEEEquad;
70 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
Chris Lattnerec4a5672008-03-05 06:48:13 +000071 }
72}
73
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000074// Default null implementations of the callbacks.
75void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
76void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000077
Jim Laskey58b968b2005-08-17 20:08:02 +000078//===----------------------------------------------------------------------===//
79// ConstantFPSDNode Class
80//===----------------------------------------------------------------------===//
81
82/// isExactlyValue - We don't rely on operator== working on double values, as
83/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
84/// As such, this method can be used to do an exact bit-for-bit comparison of
85/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000086bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000087 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000088}
89
Owen Andersone50ed302009-08-10 22:56:29 +000090bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000091 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000092 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000093
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000094 // PPC long double cannot be converted to any other type.
Owen Anderson825b72b2009-08-11 20:47:22 +000095 if (VT == MVT::ppcf128 ||
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000096 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000097 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000098
Dale Johannesenf04afdb2007-08-30 00:23:21 +000099 // convert modifies in place, so make a copy.
100 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +0000101 bool losesInfo;
Owen Andersone50ed302009-08-10 22:56:29 +0000102 (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +0000103 &losesInfo);
104 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000105}
106
Jim Laskey58b968b2005-08-17 20:08:02 +0000107//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +0000108// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +0000109//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +0000110
Evan Chenga8df1662006-03-27 06:58:47 +0000111/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +0000112/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000113bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000114 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000115 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000116 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Evan Chenga8df1662006-03-27 06:58:47 +0000118 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000119
Chris Lattner61d43992006-03-25 22:57:01 +0000120 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000121
Chris Lattner61d43992006-03-25 22:57:01 +0000122 // Skip over all of the undef values.
123 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
124 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000125
Chris Lattner61d43992006-03-25 22:57:01 +0000126 // Do not accept an all-undef vector.
127 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000128
Chris Lattner61d43992006-03-25 22:57:01 +0000129 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000130 // elements. We have to be a bit careful here, as the type of the constant
131 // may not be the same as the type of the vector elements due to type
132 // legalization (the elements are promoted to a legal type for the target and
133 // a vector of a type may be legal when the base element type is not).
134 // We only want to check enough bits to cover the vector elements, because
135 // we care if the resultant vector is all ones, not whether the individual
136 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000137 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000138 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Evan Chenga8df1662006-03-27 06:58:47 +0000139 if (isa<ConstantSDNode>(NotZero)) {
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000140 if (cast<ConstantSDNode>(NotZero)->getAPIntValue().countTrailingOnes() <
141 EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000142 return false;
143 } else if (isa<ConstantFPSDNode>(NotZero)) {
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000144 if (cast<ConstantFPSDNode>(NotZero)->getValueAPF()
145 .bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000146 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000147 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000148 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000149
Chris Lattner61d43992006-03-25 22:57:01 +0000150 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000151 // undefs. Even with the above element type twiddling, this should be OK, as
152 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000153 for (++i; i != e; ++i)
154 if (N->getOperand(i) != NotZero &&
155 N->getOperand(i).getOpcode() != ISD::UNDEF)
156 return false;
157 return true;
158}
159
160
Evan Cheng4a147842006-03-26 09:50:58 +0000161/// isBuildVectorAllZeros - Return true if the specified node is a
162/// BUILD_VECTOR where all of the elements are 0 or undef.
163bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000164 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000165 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000166 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000167
Evan Cheng4a147842006-03-26 09:50:58 +0000168 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000169
Evan Chenga8df1662006-03-27 06:58:47 +0000170 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000171
Evan Chenga8df1662006-03-27 06:58:47 +0000172 // Skip over all of the undef values.
173 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
174 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000175
Evan Chenga8df1662006-03-27 06:58:47 +0000176 // Do not accept an all-undef vector.
177 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000178
Dan Gohman68f32cb2009-06-04 16:49:15 +0000179 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000180 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000181 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000182 if (isa<ConstantSDNode>(Zero)) {
183 if (!cast<ConstantSDNode>(Zero)->isNullValue())
184 return false;
185 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000186 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000187 return false;
188 } else
189 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000190
Dan Gohman68f32cb2009-06-04 16:49:15 +0000191 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000192 // undefs.
193 for (++i; i != e; ++i)
194 if (N->getOperand(i) != Zero &&
195 N->getOperand(i).getOpcode() != ISD::UNDEF)
196 return false;
197 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000198}
199
Evan Chengefec7512008-02-18 23:04:32 +0000200/// isScalarToVector - Return true if the specified node is a
201/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
202/// element is not an undef.
203bool ISD::isScalarToVector(const SDNode *N) {
204 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
205 return true;
206
207 if (N->getOpcode() != ISD::BUILD_VECTOR)
208 return false;
209 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
210 return false;
211 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000212 if (NumElems == 1)
213 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000214 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000216 if (V.getOpcode() != ISD::UNDEF)
217 return false;
218 }
219 return true;
220}
221
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000222/// allOperandsUndef - Return true if the node has at least one operand
223/// and all operands of the specified node are ISD::UNDEF.
224bool ISD::allOperandsUndef(const SDNode *N) {
225 // Return false if the node has no operands.
226 // This is "logically inconsistent" with the definition of "all" but
227 // is probably the desired behavior.
228 if (N->getNumOperands() == 0)
229 return false;
230
231 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
232 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
233 return false;
234
235 return true;
236}
237
Chris Lattnerc3aae252005-01-07 07:46:32 +0000238/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
239/// when given the operation for (X op Y).
240ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
241 // To perform this operation, we just need to swap the L and G bits of the
242 // operation.
243 unsigned OldL = (Operation >> 2) & 1;
244 unsigned OldG = (Operation >> 1) & 1;
245 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
246 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000247 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000248}
249
250/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
251/// 'op' is a valid SetCC operation.
252ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
253 unsigned Operation = Op;
254 if (isInteger)
255 Operation ^= 7; // Flip L, G, E bits, but not U.
256 else
257 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000258
Chris Lattnerc3aae252005-01-07 07:46:32 +0000259 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000260 Operation &= ~8; // Don't let N and U bits get set.
261
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262 return ISD::CondCode(Operation);
263}
264
265
266/// isSignedOp - For an integer comparison, return 1 if the comparison is a
267/// signed operation and 2 if the result is an unsigned comparison. Return zero
268/// if the operation does not depend on the sign of the input (setne and seteq).
269static int isSignedOp(ISD::CondCode Opcode) {
270 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000271 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 case ISD::SETEQ:
273 case ISD::SETNE: return 0;
274 case ISD::SETLT:
275 case ISD::SETLE:
276 case ISD::SETGT:
277 case ISD::SETGE: return 1;
278 case ISD::SETULT:
279 case ISD::SETULE:
280 case ISD::SETUGT:
281 case ISD::SETUGE: return 2;
282 }
283}
284
285/// getSetCCOrOperation - Return the result of a logical OR between different
286/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
287/// returns SETCC_INVALID if it is not possible to represent the resultant
288/// comparison.
289ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
290 bool isInteger) {
291 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
292 // Cannot fold a signed integer setcc with an unsigned integer setcc.
293 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000294
Chris Lattnerc3aae252005-01-07 07:46:32 +0000295 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000296
Chris Lattnerc3aae252005-01-07 07:46:32 +0000297 // If the N and U bits get set then the resultant comparison DOES suddenly
298 // care about orderedness, and is true when ordered.
299 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000300 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000301
Chris Lattnere41102b2006-05-12 17:03:46 +0000302 // Canonicalize illegal integer setcc's.
303 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
304 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000305
Chris Lattnerc3aae252005-01-07 07:46:32 +0000306 return ISD::CondCode(Op);
307}
308
309/// getSetCCAndOperation - Return the result of a logical AND between different
310/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
311/// function returns zero if it is not possible to represent the resultant
312/// comparison.
313ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
314 bool isInteger) {
315 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
316 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000317 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000318
319 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000320 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000321
Chris Lattnera83385f2006-04-27 05:01:07 +0000322 // Canonicalize illegal integer setcc's.
323 if (isInteger) {
324 switch (Result) {
325 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000326 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000327 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000328 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
329 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
330 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000331 }
332 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000333
Chris Lattnera83385f2006-04-27 05:01:07 +0000334 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000335}
336
Jim Laskey58b968b2005-08-17 20:08:02 +0000337//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000338// SDNode Profile Support
339//===----------------------------------------------------------------------===//
340
Jim Laskeydef69b92006-10-27 23:52:51 +0000341/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
342///
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
344 ID.AddInteger(OpC);
345}
346
347/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
348/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000349static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000350 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000351}
352
Jim Laskeydef69b92006-10-27 23:52:51 +0000353/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
354///
Jim Laskey583bd472006-10-27 23:46:08 +0000355static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000356 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000357 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000358 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000359 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000360 }
Jim Laskey583bd472006-10-27 23:46:08 +0000361}
362
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000363/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
364///
365static void AddNodeIDOperands(FoldingSetNodeID &ID,
366 const SDUse *Ops, unsigned NumOps) {
367 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000368 ID.AddPointer(Ops->getNode());
369 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000370 }
371}
372
Jim Laskey583bd472006-10-27 23:46:08 +0000373static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000374 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000375 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000376 AddNodeIDOpcode(ID, OpC);
377 AddNodeIDValueTypes(ID, VTList);
378 AddNodeIDOperands(ID, OpList, N);
379}
380
Duncan Sands0dc40452008-10-27 15:30:53 +0000381/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
382/// the NodeID data.
383static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000384 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000385 case ISD::TargetExternalSymbol:
386 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000387 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000388 default: break; // Normal nodes don't need extra info.
389 case ISD::TargetConstant:
390 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000391 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000392 break;
393 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000394 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000395 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000396 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000397 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000398 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000399 case ISD::GlobalAddress:
400 case ISD::TargetGlobalTLSAddress:
401 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000402 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000403 ID.AddPointer(GA->getGlobal());
404 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000405 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000406 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000407 break;
408 }
409 case ISD::BasicBlock:
410 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
411 break;
412 case ISD::Register:
413 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
414 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000415 case ISD::RegisterMask:
416 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
417 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000418 case ISD::SRCVALUE:
419 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
420 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000421 case ISD::FrameIndex:
422 case ISD::TargetFrameIndex:
423 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
424 break;
425 case ISD::JumpTable:
426 case ISD::TargetJumpTable:
427 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000428 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000429 break;
430 case ISD::ConstantPool:
431 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000432 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000433 ID.AddInteger(CP->getAlignment());
434 ID.AddInteger(CP->getOffset());
435 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000436 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000437 else
438 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000439 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
442 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000443 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000444 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000445 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000446 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000447 break;
448 }
449 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000450 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000451 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000452 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000453 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000454 break;
455 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000456 case ISD::ATOMIC_CMP_SWAP:
457 case ISD::ATOMIC_SWAP:
458 case ISD::ATOMIC_LOAD_ADD:
459 case ISD::ATOMIC_LOAD_SUB:
460 case ISD::ATOMIC_LOAD_AND:
461 case ISD::ATOMIC_LOAD_OR:
462 case ISD::ATOMIC_LOAD_XOR:
463 case ISD::ATOMIC_LOAD_NAND:
464 case ISD::ATOMIC_LOAD_MIN:
465 case ISD::ATOMIC_LOAD_MAX:
466 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000467 case ISD::ATOMIC_LOAD_UMAX:
468 case ISD::ATOMIC_LOAD:
469 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000470 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000471 ID.AddInteger(AT->getMemoryVT().getRawBits());
472 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000473 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
474 break;
475 }
476 case ISD::PREFETCH: {
477 const MemSDNode *PF = cast<MemSDNode>(N);
478 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000479 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000480 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000481 case ISD::VECTOR_SHUFFLE: {
482 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000483 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000484 i != e; ++i)
485 ID.AddInteger(SVN->getMaskElt(i));
486 break;
487 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000488 case ISD::TargetBlockAddress:
489 case ISD::BlockAddress: {
Dan Gohman29cbade2009-11-20 23:18:13 +0000490 ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
491 ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000492 break;
493 }
Mon P Wang28873102008-06-25 08:15:39 +0000494 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000495
496 // Target specific memory nodes could also have address spaces to check.
497 if (N->isTargetMemoryOpcode())
498 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000499}
500
Duncan Sands0dc40452008-10-27 15:30:53 +0000501/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
502/// data.
503static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
504 AddNodeIDOpcode(ID, N->getOpcode());
505 // Add the return value info.
506 AddNodeIDValueTypes(ID, N->getVTList());
507 // Add the operand info.
508 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
509
510 // Handle SDNode leafs with special info.
511 AddNodeIDCustom(ID, N);
512}
513
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000514/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000515/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000516/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000517///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000518static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000519encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000520 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000521 assert((ConvType & 3) == ConvType &&
522 "ConvType may not require more than 2 bits!");
523 assert((AM & 7) == AM &&
524 "AM may not require more than 3 bits!");
525 return ConvType |
526 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000527 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000528 (isNonTemporal << 6) |
529 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000530}
531
Jim Laskey583bd472006-10-27 23:46:08 +0000532//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000533// SelectionDAG Class
534//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000535
Duncan Sands0dc40452008-10-27 15:30:53 +0000536/// doNotCSE - Return true if CSE should not be performed for this node.
537static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000538 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000539 return true; // Never CSE anything that produces a flag.
540
541 switch (N->getOpcode()) {
542 default: break;
543 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000544 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000545 return true; // Never CSE these nodes.
546 }
547
548 // Check that remaining values produced are not flags.
549 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000550 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000551 return true; // Never CSE anything that produces a flag.
552
553 return false;
554}
555
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000557/// SelectionDAG.
558void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000559 // Create a dummy node (which is not added to allnodes), that adds a reference
560 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000561 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000562
Chris Lattner190a4182006-08-04 17:45:20 +0000563 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000564
Chris Lattner190a4182006-08-04 17:45:20 +0000565 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000566 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000567 if (I->use_empty())
568 DeadNodes.push_back(I);
569
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000570 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000571
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000572 // If the root changed (e.g. it was a dead load, update the root).
573 setRoot(Dummy.getValue());
574}
575
576/// RemoveDeadNodes - This method deletes the unreachable nodes in the
577/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000578void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000579
Chris Lattner190a4182006-08-04 17:45:20 +0000580 // Process the worklist, deleting the nodes and adding their uses to the
581 // worklist.
582 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000583 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000584
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000585 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
586 DUL->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000587
Chris Lattner190a4182006-08-04 17:45:20 +0000588 // Take the node out of the appropriate CSE map.
589 RemoveNodeFromCSEMaps(N);
590
591 // Next, brutally remove the operand list. This is safe to do, as there are
592 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000593 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
594 SDUse &Use = *I++;
595 SDNode *Operand = Use.getNode();
596 Use.set(SDValue());
597
Chris Lattner190a4182006-08-04 17:45:20 +0000598 // Now that we removed this operand, see if there are no uses of it left.
599 if (Operand->use_empty())
600 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000601 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000602
Dan Gohmanc5336122009-01-19 22:39:36 +0000603 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000604 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000605}
606
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000607void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000608 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000609
610 // Create a dummy node that adds a reference to the root node, preventing
611 // it from being deleted. (This matters if the root is an operand of the
612 // dead node.)
613 HandleSDNode Dummy(getRoot());
614
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000615 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000616}
617
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000618void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000619 // First take this out of the appropriate CSE map.
620 RemoveNodeFromCSEMaps(N);
621
Scott Michelfdc40a02009-02-17 22:15:04 +0000622 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000623 // AllNodes list, and delete the node.
624 DeleteNodeNotInCSEMaps(N);
625}
626
627void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000628 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
629 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000630
Dan Gohmanf06c8352008-09-30 18:30:35 +0000631 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000632 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000633
Dan Gohmanc5336122009-01-19 22:39:36 +0000634 DeallocateNode(N);
635}
636
637void SelectionDAG::DeallocateNode(SDNode *N) {
638 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000639 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000640
Dan Gohmanc5336122009-01-19 22:39:36 +0000641 // Set the opcode to DELETED_NODE to help catch bugs when node
642 // memory is reallocated.
643 N->NodeType = ISD::DELETED_NODE;
644
Dan Gohman11467282008-08-26 01:44:34 +0000645 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000646
647 // Remove the ordering of this node.
Bill Wendling187361b2010-01-23 10:26:57 +0000648 Ordering->remove(N);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000649
Evan Chengbfcb3052010-03-25 01:38:16 +0000650 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000651 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000652 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
653 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000654}
655
Chris Lattner149c58c2005-08-16 18:17:10 +0000656/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
657/// correspond to it. This is useful when we're about to delete or repurpose
658/// the node. We don't want future request for structurally identical nodes
659/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000660bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000661 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000662 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000663 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000664 case ISD::CONDCODE:
665 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
666 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000667 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000668 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
669 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000670 case ISD::ExternalSymbol:
671 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000672 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000673 case ISD::TargetExternalSymbol: {
674 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
675 Erased = TargetExternalSymbols.erase(
676 std::pair<std::string,unsigned char>(ESN->getSymbol(),
677 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000678 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000679 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000680 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000681 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000682 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000683 Erased = ExtendedValueTypeNodes.erase(VT);
684 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
686 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000687 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000688 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000689 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000690 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000691 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000692 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
693 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000694 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000695 break;
696 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000697#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000698 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000699 // flag result (which cannot be CSE'd) or is one of the special cases that are
700 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000701 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000702 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000703 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000704 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000705 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000706 }
707#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000708 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000709}
710
Dan Gohman39946102009-01-25 16:29:12 +0000711/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
712/// maps and modified in place. Add it back to the CSE maps, unless an identical
713/// node already exists, in which case transfer all its users to the existing
714/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000715///
Dan Gohman39946102009-01-25 16:29:12 +0000716void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000717SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000718 // For node types that aren't CSE'd, just act as if no identical node
719 // already exists.
720 if (!doNotCSE(N)) {
721 SDNode *Existing = CSEMap.GetOrInsertNode(N);
722 if (Existing != N) {
723 // If there was already an existing matching node, use ReplaceAllUsesWith
724 // to replace the dead one with the existing one. This can cause
725 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000726 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000727
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000728 // N is now dead. Inform the listeners and delete it.
729 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
730 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000731 DeleteNodeNotInCSEMaps(N);
732 return;
733 }
734 }
Evan Cheng71e86852008-07-08 20:06:39 +0000735
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000736 // If the node doesn't already exist, we updated it. Inform listeners.
737 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
738 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000739}
740
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000741/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000742/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000743/// return null, otherwise return a pointer to the slot it would take. If a
744/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000745SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000746 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000747 if (doNotCSE(N))
748 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000749
Dan Gohman475871a2008-07-27 21:46:04 +0000750 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000751 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000752 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000753 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000754 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000755 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000756}
757
758/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000759/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000760/// return null, otherwise return a pointer to the slot it would take. If a
761/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000762SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000763 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000764 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000765 if (doNotCSE(N))
766 return 0;
767
Dan Gohman475871a2008-07-27 21:46:04 +0000768 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000769 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000770 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000771 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000772 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000773 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000774}
775
776
777/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000778/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000779/// return null, otherwise return a pointer to the slot it would take. If a
780/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000781SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000782 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000783 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000784 if (doNotCSE(N))
785 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000786
Jim Laskey583bd472006-10-27 23:46:08 +0000787 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000788 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000789 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000790 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000791 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000792}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000793
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000794#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000795/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
796static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000797 switch (N->getOpcode()) {
798 default:
799 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000800 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000801 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000802 assert(N->getNumValues() == 1 && "Too many results!");
803 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
804 "Wrong return type!");
805 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
806 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
807 "Mismatched operand types!");
808 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
809 "Wrong operand type!");
810 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
811 "Wrong return type size");
812 break;
813 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000814 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000815 assert(N->getNumValues() == 1 && "Too many results!");
816 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000817 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000818 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000819 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000820 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000821 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000822 (EltVT.isInteger() && I->getValueType().isInteger() &&
823 EltVT.bitsLE(I->getValueType()))) &&
824 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000825 assert(I->getValueType() == N->getOperand(0).getValueType() &&
826 "Operands must all have the same type");
827 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000828 break;
829 }
830 }
831}
832
Duncan Sands59d2dad2010-11-20 11:25:00 +0000833/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
834static void VerifySDNode(SDNode *N) {
835 // The SDNode allocators cannot be used to allocate nodes with fields that are
836 // not present in an SDNode!
837 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
838 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
839 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
840 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
841 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
842 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
843 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
844 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
845 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
846 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
847 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
848 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
849 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
850 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
851 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
852 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
853 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
854 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
855 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
856
857 VerifyNodeCommon(N);
858}
859
860/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
861/// invalid.
862static void VerifyMachineNode(SDNode *N) {
863 // The MachineNode allocators cannot be used to allocate nodes with fields
864 // that are not present in a MachineNode!
865 // Currently there are no such nodes.
866
867 VerifyNodeCommon(N);
868}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000869#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000870
Owen Andersone50ed302009-08-10 22:56:29 +0000871/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000872/// given type.
873///
Owen Andersone50ed302009-08-10 22:56:29 +0000874unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000875 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000876 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000877 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000878
879 return TLI.getTargetData()->getABITypeAlignment(Ty);
880}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000881
Dale Johannesen92570c42009-02-07 02:15:05 +0000882// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000883SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Dan Gohmanff7a5622010-05-11 17:31:57 +0000884 : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()),
Devang Patel0508d042011-12-15 18:21:18 +0000885 OptLevel(OL), EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000886 Root(getEntryNode()), Ordering(0), UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000887 AllNodes.push_back(&EntryNode);
Bill Wendling187361b2010-01-23 10:26:57 +0000888 Ordering = new SDNodeOrdering();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000889 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000890}
891
Chris Lattnerde6e7832010-04-05 06:10:13 +0000892void SelectionDAG::init(MachineFunction &mf) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000893 MF = &mf;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000894 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000895}
896
Chris Lattner78ec3112003-08-11 14:57:33 +0000897SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000898 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000899 allnodes_clear();
Daniel Dunbar819309e2009-12-16 20:10:05 +0000900 delete Ordering;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000901 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000902}
903
904void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000905 assert(&*AllNodes.begin() == &EntryNode);
906 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000907 while (!AllNodes.empty())
908 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000909}
910
Dan Gohman7c3234c2008-08-27 23:52:12 +0000911void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000912 allnodes_clear();
913 OperandAllocator.Reset();
914 CSEMap.clear();
915
916 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000917 ExternalSymbols.clear();
918 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000919 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
920 static_cast<CondCodeSDNode*>(0));
921 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
922 static_cast<SDNode*>(0));
923
Dan Gohmane7852d02009-01-26 04:35:06 +0000924 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000925 AllNodes.push_back(&EntryNode);
926 Root = getEntryNode();
Dan Gohman27445f02010-06-18 15:40:58 +0000927 Ordering->clear();
Evan Cheng31441b72010-03-29 20:48:30 +0000928 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000929}
930
Nadav Rotema3c42f32011-09-27 11:16:47 +0000931SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
932 return VT.bitsGT(Op.getValueType()) ?
933 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
934 getNode(ISD::TRUNCATE, DL, VT, Op);
935}
936
Duncan Sands3a66a682009-10-13 21:04:12 +0000937SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
938 return VT.bitsGT(Op.getValueType()) ?
939 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
940 getNode(ISD::TRUNCATE, DL, VT, Op);
941}
942
943SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
944 return VT.bitsGT(Op.getValueType()) ?
945 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
946 getNode(ISD::TRUNCATE, DL, VT, Op);
947}
948
Owen Andersone50ed302009-08-10 22:56:29 +0000949SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000950 assert(!VT.isVector() &&
951 "getZeroExtendInReg should use the vector element type instead of "
952 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000953 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000954 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
955 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000956 VT.getSizeInBits());
957 return getNode(ISD::AND, DL, Op.getValueType(), Op,
958 getConstant(Imm, Op.getValueType()));
959}
960
Bob Wilson4c245462009-01-22 17:39:32 +0000961/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
962///
Owen Andersone50ed302009-08-10 22:56:29 +0000963SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000964 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000965 SDValue NegOne =
966 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000967 return getNode(ISD::XOR, DL, VT, Val, NegOne);
968}
969
Owen Andersone50ed302009-08-10 22:56:29 +0000970SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000971 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000972 assert((EltVT.getSizeInBits() >= 64 ||
973 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
974 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000975 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000976}
977
Owen Andersone50ed302009-08-10 22:56:29 +0000978SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000979 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000980}
981
Owen Andersone50ed302009-08-10 22:56:29 +0000982SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000983 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000984
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000985 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000986 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000987
Duncan Sands28b77e92011-09-06 19:07:46 +0000988 // In some cases the vector type is legal but the element type is illegal and
989 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
990 // inserted value (the type does not need to match the vector element type).
991 // Any extra bits introduced will be truncated away.
992 if (VT.isVector() && TLI.getTypeAction(*getContext(), EltVT) ==
993 TargetLowering::TypePromoteInteger) {
994 EltVT = TLI.getTypeToTransformTo(*getContext(), EltVT);
995 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
996 Elt = ConstantInt::get(*getContext(), NewVal);
997 }
998
999 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1000 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001001 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001002 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001003 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +00001004 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +00001005 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +00001006 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001007 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001008 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001009 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001010
Dan Gohman89081322007-12-12 22:21:26 +00001011 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001012 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001013 CSEMap.InsertNode(N, IP);
1014 AllNodes.push_back(N);
1015 }
1016
Dan Gohman475871a2008-07-27 21:46:04 +00001017 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001018 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001019 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001020 Ops.assign(VT.getVectorNumElements(), Result);
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001021 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001022 }
1023 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001024}
1025
Dan Gohman475871a2008-07-27 21:46:04 +00001026SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +00001027 return getConstant(Val, TLI.getPointerTy(), isTarget);
1028}
1029
1030
Owen Andersone50ed302009-08-10 22:56:29 +00001031SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001032 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001033}
1034
Owen Andersone50ed302009-08-10 22:56:29 +00001035SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001036 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001037
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001038 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001039
Chris Lattnerd8658612005-02-17 20:17:32 +00001040 // Do the map lookup using the actual bit pattern for the floating point
1041 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1042 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001043 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001044 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001045 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001046 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001047 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001048 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001049 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001050 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001051 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001052
Evan Chengc908dcd2007-06-29 21:36:04 +00001053 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001054 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001055 CSEMap.InsertNode(N, IP);
1056 AllNodes.push_back(N);
1057 }
1058
Dan Gohman475871a2008-07-27 21:46:04 +00001059 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001060 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001061 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001062 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +00001063 // FIXME DebugLoc info might be appropriate here
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001064 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001065 }
1066 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067}
1068
Owen Andersone50ed302009-08-10 22:56:29 +00001069SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001070 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001072 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001073 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001074 return getConstantFP(APFloat(Val), VT, isTarget);
Owen Andersond7484e52012-04-05 18:50:32 +00001075 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001076 bool ignored;
1077 APFloat apf = APFloat(Val);
1078 apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1079 &ignored);
1080 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001081 } else
1082 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001083}
1084
Devang Patel0d881da2010-07-06 22:08:15 +00001085SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001086 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001087 bool isTargetGA,
1088 unsigned char TargetFlags) {
1089 assert((TargetFlags == 0 || isTargetGA) &&
1090 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001091
Dan Gohman6520e202008-10-18 02:06:02 +00001092 // Truncate (with sign-extension) the offset value to the pointer size.
Owen Andersone50ed302009-08-10 22:56:29 +00001093 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001094 unsigned BitWidth = PTy.getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +00001095 if (BitWidth < 64)
1096 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
1097
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001098 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1099 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001100 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001101 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001102 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001103 }
1104
Chris Lattner2a4ed822009-06-25 21:21:14 +00001105 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001106 if (GVar && GVar->isThreadLocal())
1107 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1108 else
1109 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001110
Jim Laskey583bd472006-10-27 23:46:08 +00001111 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001112 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001113 ID.AddPointer(GV);
1114 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001115 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001116 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001117 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001118 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001119 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001120
Devang Patel0d881da2010-07-06 22:08:15 +00001121 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001122 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001123 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001125 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001126}
1127
Owen Andersone50ed302009-08-10 22:56:29 +00001128SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001129 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001130 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001131 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001132 ID.AddInteger(FI);
1133 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001134 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001135 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001136
Dan Gohman95531882010-03-18 18:49:47 +00001137 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001138 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001139 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001140 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001141}
1142
Owen Andersone50ed302009-08-10 22:56:29 +00001143SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001144 unsigned char TargetFlags) {
1145 assert((TargetFlags == 0 || isTarget) &&
1146 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001147 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001148 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001149 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001150 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001151 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001152 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001153 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001154 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001155
Dan Gohman95531882010-03-18 18:49:47 +00001156 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1157 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001158 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001159 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001160 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001161}
1162
Dan Gohman46510a72010-04-15 01:51:59 +00001163SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001164 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001165 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001166 unsigned char TargetFlags) {
1167 assert((TargetFlags == 0 || isTarget) &&
1168 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001169 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001170 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001171 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001172 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001173 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001174 ID.AddInteger(Alignment);
1175 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001176 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001177 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001178 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001179 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001180 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001181
Dan Gohman95531882010-03-18 18:49:47 +00001182 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1183 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001184 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001185 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001186 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001187}
1188
Chris Lattnerc3aae252005-01-07 07:46:32 +00001189
Owen Andersone50ed302009-08-10 22:56:29 +00001190SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001191 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001192 bool isTarget,
1193 unsigned char TargetFlags) {
1194 assert((TargetFlags == 0 || isTarget) &&
1195 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001196 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001197 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001198 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001199 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001200 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001201 ID.AddInteger(Alignment);
1202 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001203 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001204 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001205 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001206 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001207 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001208
Dan Gohman95531882010-03-18 18:49:47 +00001209 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1210 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001211 CSEMap.InsertNode(N, IP);
1212 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001213 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001214}
1215
Dan Gohman475871a2008-07-27 21:46:04 +00001216SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001217 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001219 ID.AddPointer(MBB);
1220 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001221 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001222 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001223
Dan Gohman95531882010-03-18 18:49:47 +00001224 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001225 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001226 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001227 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001228}
1229
Owen Andersone50ed302009-08-10 22:56:29 +00001230SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001231 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1232 ValueTypeNodes.size())
1233 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001234
Duncan Sands83ec4b62008-06-06 12:08:01 +00001235 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001237
Dan Gohman475871a2008-07-27 21:46:04 +00001238 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001239 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001240 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001241 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001242}
1243
Owen Andersone50ed302009-08-10 22:56:29 +00001244SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001245 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001246 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001247 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001248 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001249 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001250}
1251
Owen Andersone50ed302009-08-10 22:56:29 +00001252SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001253 unsigned char TargetFlags) {
1254 SDNode *&N =
1255 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1256 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001257 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001258 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001259 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001260 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001261}
1262
Dan Gohman475871a2008-07-27 21:46:04 +00001263SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001264 if ((unsigned)Cond >= CondCodeNodes.size())
1265 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001266
Chris Lattner079a27a2005-08-09 20:40:02 +00001267 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001268 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001269 CondCodeNodes[Cond] = N;
1270 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001271 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001272
Dan Gohman475871a2008-07-27 21:46:04 +00001273 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001274}
1275
Nate Begeman5a5ca152009-04-29 05:20:52 +00001276// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1277// the shuffle mask M that point at N1 to point at N2, and indices that point
1278// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001279static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1280 std::swap(N1, N2);
1281 int NElts = M.size();
1282 for (int i = 0; i != NElts; ++i) {
1283 if (M[i] >= NElts)
1284 M[i] -= NElts;
1285 else if (M[i] >= 0)
1286 M[i] += NElts;
1287 }
1288}
1289
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001290SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001291 SDValue N2, const int *Mask) {
1292 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001293 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001294 "Vector Shuffle VTs must be a vectors");
1295 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1296 && "Vector Shuffle VTs must have same element type");
1297
1298 // Canonicalize shuffle undef, undef -> undef
1299 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001300 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001301
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001302 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001303 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001304 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001305 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001306 for (unsigned i = 0; i != NElts; ++i) {
1307 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001308 MaskVec.push_back(Mask[i]);
1309 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001310
Nate Begeman9008ca62009-04-27 18:41:29 +00001311 // Canonicalize shuffle v, v -> v, undef
1312 if (N1 == N2) {
1313 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001314 for (unsigned i = 0; i != NElts; ++i)
1315 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001316 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001317
Nate Begeman9008ca62009-04-27 18:41:29 +00001318 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1319 if (N1.getOpcode() == ISD::UNDEF)
1320 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001321
Nate Begeman9008ca62009-04-27 18:41:29 +00001322 // Canonicalize all index into lhs, -> shuffle lhs, undef
1323 // Canonicalize all index into rhs, -> shuffle rhs, undef
1324 bool AllLHS = true, AllRHS = true;
1325 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001326 for (unsigned i = 0; i != NElts; ++i) {
1327 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001328 if (N2Undef)
1329 MaskVec[i] = -1;
1330 else
1331 AllLHS = false;
1332 } else if (MaskVec[i] >= 0) {
1333 AllRHS = false;
1334 }
1335 }
1336 if (AllLHS && AllRHS)
1337 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001338 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001339 N2 = getUNDEF(VT);
1340 if (AllRHS) {
1341 N1 = getUNDEF(VT);
1342 commuteShuffle(N1, N2, MaskVec);
1343 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001344
Nate Begeman9008ca62009-04-27 18:41:29 +00001345 // If Identity shuffle, or all shuffle in to undef, return that node.
1346 bool AllUndef = true;
1347 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001348 for (unsigned i = 0; i != NElts; ++i) {
1349 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001350 if (MaskVec[i] >= 0) AllUndef = false;
1351 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001352 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001353 return N1;
1354 if (AllUndef)
1355 return getUNDEF(VT);
1356
1357 FoldingSetNodeID ID;
1358 SDValue Ops[2] = { N1, N2 };
1359 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001360 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001361 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001362
Nate Begeman9008ca62009-04-27 18:41:29 +00001363 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001364 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001365 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001366
Nate Begeman9008ca62009-04-27 18:41:29 +00001367 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1368 // SDNode doesn't have access to it. This memory will be "leaked" when
1369 // the node is deallocated, but recovered when the NodeAllocator is released.
1370 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1371 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001372
Dan Gohman95531882010-03-18 18:49:47 +00001373 ShuffleVectorSDNode *N =
1374 new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001375 CSEMap.InsertNode(N, IP);
1376 AllNodes.push_back(N);
1377 return SDValue(N, 0);
1378}
1379
Owen Andersone50ed302009-08-10 22:56:29 +00001380SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001381 SDValue Val, SDValue DTy,
1382 SDValue STy, SDValue Rnd, SDValue Sat,
1383 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001384 // If the src and dest types are the same and the conversion is between
1385 // integer types of the same sign or two floats, no conversion is necessary.
1386 if (DTy == STy &&
1387 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001388 return Val;
1389
1390 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001391 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1392 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001393 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001394 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001395 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001396
Dan Gohman95531882010-03-18 18:49:47 +00001397 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
1398 Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001399 CSEMap.InsertNode(N, IP);
1400 AllNodes.push_back(N);
1401 return SDValue(N, 0);
1402}
1403
Owen Andersone50ed302009-08-10 22:56:29 +00001404SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001405 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001406 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001407 ID.AddInteger(RegNo);
1408 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001409 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001410 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001411
Dan Gohman95531882010-03-18 18:49:47 +00001412 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001413 CSEMap.InsertNode(N, IP);
1414 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001415 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001416}
1417
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001418SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1419 FoldingSetNodeID ID;
1420 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1421 ID.AddPointer(RegMask);
1422 void *IP = 0;
1423 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1424 return SDValue(E, 0);
1425
1426 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1427 CSEMap.InsertNode(N, IP);
1428 AllNodes.push_back(N);
1429 return SDValue(N, 0);
1430}
1431
Chris Lattner7561d482010-03-14 02:33:54 +00001432SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001433 FoldingSetNodeID ID;
1434 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001435 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1436 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001437 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001438 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001439 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001440
Dan Gohman95531882010-03-18 18:49:47 +00001441 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001442 CSEMap.InsertNode(N, IP);
1443 AllNodes.push_back(N);
1444 return SDValue(N, 0);
1445}
1446
Chris Lattner7561d482010-03-14 02:33:54 +00001447
Dan Gohman46510a72010-04-15 01:51:59 +00001448SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Dan Gohman29cbade2009-11-20 23:18:13 +00001449 bool isTarget,
1450 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001451 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1452
1453 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001454 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001455 ID.AddPointer(BA);
Dan Gohman29cbade2009-11-20 23:18:13 +00001456 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001457 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001458 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001459 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001460
Dan Gohman95531882010-03-18 18:49:47 +00001461 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001462 CSEMap.InsertNode(N, IP);
1463 AllNodes.push_back(N);
1464 return SDValue(N, 0);
1465}
1466
Dan Gohman475871a2008-07-27 21:46:04 +00001467SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001468 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001469 "SrcValue is not a pointer?");
1470
Jim Laskey583bd472006-10-27 23:46:08 +00001471 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001472 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001473 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001474
Chris Lattner61b09412006-08-11 21:01:22 +00001475 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001476 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001477 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001478
Dan Gohman95531882010-03-18 18:49:47 +00001479 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001480 CSEMap.InsertNode(N, IP);
1481 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001482 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001483}
1484
Chris Lattnerdecc2672010-04-07 05:20:54 +00001485/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1486SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1487 FoldingSetNodeID ID;
1488 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1489 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001490
Chris Lattnerdecc2672010-04-07 05:20:54 +00001491 void *IP = 0;
1492 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1493 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001494
Chris Lattnerdecc2672010-04-07 05:20:54 +00001495 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1496 CSEMap.InsertNode(N, IP);
1497 AllNodes.push_back(N);
1498 return SDValue(N, 0);
1499}
1500
1501
Duncan Sands92abc622009-01-31 15:50:11 +00001502/// getShiftAmountOperand - Return the specified value casted to
1503/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001504SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001505 EVT OpTy = Op.getValueType();
Owen Anderson6154f6c2011-03-07 18:29:47 +00001506 MVT ShTy = TLI.getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001507 if (OpTy == ShTy || OpTy.isVector()) return Op;
1508
1509 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001510 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001511}
1512
Chris Lattner37ce9df2007-10-15 17:47:20 +00001513/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1514/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001515SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001516 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001517 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001518 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001519 unsigned StackAlign =
1520 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001521
David Greene3f2bf852009-11-12 20:49:22 +00001522 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner37ce9df2007-10-15 17:47:20 +00001523 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1524}
1525
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001526/// CreateStackTemporary - Create a stack temporary suitable for holding
1527/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001528SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001529 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1530 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001531 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1532 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001533 const TargetData *TD = TLI.getTargetData();
1534 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1535 TD->getPrefTypeAlignment(Ty2));
1536
1537 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001538 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001539 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1540}
1541
Owen Andersone50ed302009-08-10 22:56:29 +00001542SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001543 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001544 // These setcc operations always fold.
1545 switch (Cond) {
1546 default: break;
1547 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001548 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001549 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001550 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001551
Chris Lattnera83385f2006-04-27 05:01:07 +00001552 case ISD::SETOEQ:
1553 case ISD::SETOGT:
1554 case ISD::SETOGE:
1555 case ISD::SETOLT:
1556 case ISD::SETOLE:
1557 case ISD::SETONE:
1558 case ISD::SETO:
1559 case ISD::SETUO:
1560 case ISD::SETUEQ:
1561 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001562 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001563 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001564 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Gabor Greifba36cb52008-08-28 21:40:38 +00001566 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001567 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001568 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001569 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Chris Lattnerc3aae252005-01-07 07:46:32 +00001571 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001572 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001573 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1574 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001575 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1576 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1577 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1578 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1579 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1580 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1581 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1582 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001583 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001584 }
Chris Lattner67255a12005-04-07 18:14:58 +00001585 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001586 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1587 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001588 // No compile time operations on this type yet.
Owen Anderson825b72b2009-08-11 20:47:22 +00001589 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001590 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001591
1592 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001593 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001594 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001595 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001596 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001597 // fall through
1598 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001599 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001600 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001601 // fall through
1602 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001603 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001604 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001605 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001606 // fall through
1607 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001608 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001609 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001610 // fall through
1611 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001612 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001613 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001614 // fall through
1615 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001616 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001617 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001618 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001619 // fall through
1620 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001621 R==APFloat::cmpEqual, VT);
1622 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1623 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1624 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1625 R==APFloat::cmpEqual, VT);
1626 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1627 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1628 R==APFloat::cmpLessThan, VT);
1629 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1630 R==APFloat::cmpUnordered, VT);
1631 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1632 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001633 }
1634 } else {
1635 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001636 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001637 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001638 }
1639
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001640 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001641 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001642}
1643
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001644/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1645/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001646bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001647 // This predicate is not safe for vector operations.
1648 if (Op.getValueType().isVector())
1649 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001650
Dan Gohman87862e72009-12-11 21:31:27 +00001651 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001652 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1653}
1654
Dan Gohmanea859be2007-06-22 14:59:07 +00001655/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1656/// this predicate to simplify operations downstream. Mask is known to be zero
1657/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001658bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001659 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001660 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001661 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001662 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001663 return (KnownZero & Mask) == Mask;
1664}
1665
1666/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1667/// known to be either zero or one and return them in the KnownZero/KnownOne
1668/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1669/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001670void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1671 APInt &KnownOne, unsigned Depth) const {
1672 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001673
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001674 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001675 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001676 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001677
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001678 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001679
1680 switch (Op.getOpcode()) {
1681 case ISD::Constant:
1682 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001683 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1684 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001685 return;
1686 case ISD::AND:
1687 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001688 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1689 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001690 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1691 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001692
1693 // Output known-1 bits are only known if set in both the LHS & RHS.
1694 KnownOne &= KnownOne2;
1695 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1696 KnownZero |= KnownZero2;
1697 return;
1698 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001699 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1700 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001701 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1702 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1703
Dan Gohmanea859be2007-06-22 14:59:07 +00001704 // Output known-0 bits are only known if clear in both the LHS & RHS.
1705 KnownZero &= KnownZero2;
1706 // Output known-1 are known to be set if set in either the LHS | RHS.
1707 KnownOne |= KnownOne2;
1708 return;
1709 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001710 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1711 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001712 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1713 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1714
Dan Gohmanea859be2007-06-22 14:59:07 +00001715 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001716 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001717 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1718 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1719 KnownZero = KnownZeroOut;
1720 return;
1721 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001722 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001723 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1724 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001725 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1726 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1727
1728 // If low bits are zero in either operand, output low known-0 bits.
1729 // Also compute a conserative estimate for high known-0 bits.
1730 // More trickiness is possible, but this is sufficient for the
1731 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001732 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001733 unsigned TrailZ = KnownZero.countTrailingOnes() +
1734 KnownZero2.countTrailingOnes();
1735 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001736 KnownZero2.countLeadingOnes(),
1737 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001738
1739 TrailZ = std::min(TrailZ, BitWidth);
1740 LeadZ = std::min(LeadZ, BitWidth);
1741 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1742 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001743 return;
1744 }
1745 case ISD::UDIV: {
1746 // For the purposes of computing leading zeros we can conservatively
1747 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001748 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001749 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001750 unsigned LeadZ = KnownZero2.countLeadingOnes();
1751
Jay Foad7a874dd2010-12-01 08:53:58 +00001752 KnownOne2.clearAllBits();
1753 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001754 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001755 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1756 if (RHSUnknownLeadingOnes != BitWidth)
1757 LeadZ = std::min(BitWidth,
1758 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001759
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001760 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001761 return;
1762 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001763 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001764 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1765 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001766 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1767 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1768
Dan Gohmanea859be2007-06-22 14:59:07 +00001769 // Only known if known in both the LHS and RHS.
1770 KnownOne &= KnownOne2;
1771 KnownZero &= KnownZero2;
1772 return;
1773 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001774 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1775 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001776 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1777 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1778
Dan Gohmanea859be2007-06-22 14:59:07 +00001779 // Only known if known in both the LHS and RHS.
1780 KnownOne &= KnownOne2;
1781 KnownZero &= KnownZero2;
1782 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001783 case ISD::SADDO:
1784 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001785 case ISD::SSUBO:
1786 case ISD::USUBO:
1787 case ISD::SMULO:
1788 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001789 if (Op.getResNo() != 1)
1790 return;
Duncan Sands03228082008-11-23 15:47:28 +00001791 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001792 case ISD::SETCC:
1793 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands28b77e92011-09-06 19:07:46 +00001794 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
1795 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001796 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001797 return;
1798 case ISD::SHL:
1799 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1800 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001801 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001802
1803 // If the shift count is an invalid immediate, don't do anything.
1804 if (ShAmt >= BitWidth)
1805 return;
1806
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001807 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001808 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001809 KnownZero <<= ShAmt;
1810 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001811 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001812 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001813 }
1814 return;
1815 case ISD::SRL:
1816 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1817 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001818 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001819
Dan Gohmand4cf9922008-02-26 18:50:50 +00001820 // If the shift count is an invalid immediate, don't do anything.
1821 if (ShAmt >= BitWidth)
1822 return;
1823
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001824 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001825 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001826 KnownZero = KnownZero.lshr(ShAmt);
1827 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001828
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001829 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001830 KnownZero |= HighBits; // High bits known zero.
1831 }
1832 return;
1833 case ISD::SRA:
1834 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001835 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001836
Dan Gohmand4cf9922008-02-26 18:50:50 +00001837 // If the shift count is an invalid immediate, don't do anything.
1838 if (ShAmt >= BitWidth)
1839 return;
1840
Dan Gohmanea859be2007-06-22 14:59:07 +00001841 // If any of the demanded bits are produced by the sign extension, we also
1842 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001843 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001844
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001845 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001846 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001847 KnownZero = KnownZero.lshr(ShAmt);
1848 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
Dan Gohmanea859be2007-06-22 14:59:07 +00001850 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001851 APInt SignBit = APInt::getSignBit(BitWidth);
1852 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001853
Dan Gohmanca93a432008-02-20 16:30:17 +00001854 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001855 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001856 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001857 KnownOne |= HighBits; // New bits are known one.
1858 }
1859 }
1860 return;
1861 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001862 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001863 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001864
1865 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001866 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001867 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001868
Dan Gohman977a76f2008-02-13 22:28:48 +00001869 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001870 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001871
Dan Gohmanea859be2007-06-22 14:59:07 +00001872 // If the sign extended bits are demanded, we know that the sign
1873 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001874 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001875 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001876 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001877
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001878 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1879 KnownOne &= InputDemandedBits;
1880 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001881 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1882
Dan Gohmanea859be2007-06-22 14:59:07 +00001883 // If the sign bit of the input is known set or clear, then we know the
1884 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001885 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001886 KnownZero |= NewBits;
1887 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001888 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001889 KnownOne |= NewBits;
1890 KnownZero &= ~NewBits;
1891 } else { // Input sign bit unknown
1892 KnownZero &= ~NewBits;
1893 KnownOne &= ~NewBits;
1894 }
1895 return;
1896 }
1897 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001898 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001899 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001900 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001902 unsigned LowBits = Log2_32(BitWidth)+1;
1903 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001904 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001905 return;
1906 }
1907 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001908 LoadSDNode *LD = cast<LoadSDNode>(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00001909 if (ISD::isZEXTLoad(Op.getNode())) {
Owen Andersone50ed302009-08-10 22:56:29 +00001910 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001911 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001912 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00001913 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001914 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001915 }
1916 return;
1917 }
1918 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001919 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001920 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001921 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00001922 KnownZero = KnownZero.trunc(InBits);
1923 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001924 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001925 KnownZero = KnownZero.zext(BitWidth);
1926 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001927 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001928 return;
1929 }
1930 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001931 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001932 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001933 APInt InSignBit = APInt::getSignBit(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001934 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001935
Jay Foad40f8f622010-12-07 08:25:19 +00001936 KnownZero = KnownZero.trunc(InBits);
1937 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001938 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001939
1940 // Note if the sign bit is known to be zero or one.
1941 bool SignBitKnownZero = KnownZero.isNegative();
1942 bool SignBitKnownOne = KnownOne.isNegative();
1943 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1944 "Sign bit can't be known to be both zero and one!");
1945
Jay Foad40f8f622010-12-07 08:25:19 +00001946 KnownZero = KnownZero.zext(BitWidth);
1947 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001948
Dan Gohman977a76f2008-02-13 22:28:48 +00001949 // If the sign bit is known zero or one, the top bits match.
1950 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001951 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001952 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001953 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001954 return;
1955 }
1956 case ISD::ANY_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001957 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001958 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001959 KnownZero = KnownZero.trunc(InBits);
1960 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001961 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001962 KnownZero = KnownZero.zext(BitWidth);
1963 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001964 return;
1965 }
1966 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001967 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001968 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001969 KnownZero = KnownZero.zext(InBits);
1970 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001971 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001972 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001973 KnownZero = KnownZero.trunc(BitWidth);
1974 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001975 break;
1976 }
1977 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001978 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001979 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001980 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1981 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00001982 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001983 return;
1984 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001985 case ISD::FGETSIGN:
1986 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001987 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001988 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001989
Dan Gohman23e8b712008-04-28 17:02:21 +00001990 case ISD::SUB: {
1991 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1992 // We know that the top bits of C-X are clear if X contains less bits
1993 // than C (i.e. no wrap-around can happen). For example, 20-X is
1994 // positive if we can prove that X is >= 0 and < 16.
1995 if (CLHS->getAPIntValue().isNonNegative()) {
1996 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1997 // NLZ can't be BitWidth with no sign bit
1998 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001999 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002000
2001 // If all of the MaskV bits are known to be zero, then we know the
2002 // output top bits are zero, because we now know that the output is
2003 // from [0-C].
2004 if ((KnownZero2 & MaskV) == MaskV) {
2005 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2006 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002007 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002008 }
2009 }
2010 }
2011 }
2012 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002013 case ISD::ADD:
2014 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002015 // Output known-0 bits are known if clear or set in both the low clear bits
2016 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2017 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002018 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002019 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002020 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2021
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002022 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002023 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002024 KnownZeroOut = std::min(KnownZeroOut,
2025 KnownZero2.countTrailingOnes());
2026
Chris Lattnerda605882010-12-19 20:38:28 +00002027 if (Op.getOpcode() == ISD::ADD) {
2028 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2029 return;
2030 }
2031
2032 // With ADDE, a carry bit may be added in, so we can only use this
2033 // information if we know (at least) that the low two bits are clear. We
2034 // then return to the caller that the low bit is unknown but that other bits
2035 // are known zero.
2036 if (KnownZeroOut >= 2) // ADDE
2037 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002038 return;
2039 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002040 case ISD::SREM:
2041 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002042 const APInt &RA = Rem->getAPIntValue().abs();
2043 if (RA.isPowerOf2()) {
2044 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002045 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002046 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002047
Duncan Sands5c2873a2010-01-29 09:45:26 +00002048 // The low bits of the first operand are unchanged by the srem.
2049 KnownZero = KnownZero2 & LowBits;
2050 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002051
Duncan Sands5c2873a2010-01-29 09:45:26 +00002052 // If the first operand is non-negative or has all low bits zero, then
2053 // the upper bits are all zero.
2054 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2055 KnownZero |= ~LowBits;
2056
2057 // If the first operand is negative and not all low bits are zero, then
2058 // the upper bits are all one.
2059 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2060 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002061 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002062 }
2063 }
2064 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002065 case ISD::UREM: {
2066 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002067 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002068 if (RA.isPowerOf2()) {
2069 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002070 KnownZero |= ~LowBits;
2071 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002072 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2073 break;
2074 }
2075 }
2076
2077 // Since the result is less than or equal to either operand, any leading
2078 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002079 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2080 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002081
2082 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2083 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002084 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002085 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002086 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002087 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002088 case ISD::FrameIndex:
2089 case ISD::TargetFrameIndex:
2090 if (unsigned Align = InferPtrAlignment(Op)) {
2091 // The low bits are known zero if the pointer is aligned.
2092 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2093 return;
2094 }
2095 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002096
Dan Gohmanea859be2007-06-22 14:59:07 +00002097 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002098 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2099 break;
2100 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002101 case ISD::INTRINSIC_WO_CHAIN:
2102 case ISD::INTRINSIC_W_CHAIN:
2103 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002104 // Allow the target to implement this method for its nodes.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002105 TLI.computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002106 return;
2107 }
2108}
2109
2110/// ComputeNumSignBits - Return the number of times the sign bit of the
2111/// register is replicated into the other bits. We know that at least 1 bit
2112/// is always equal to the sign bit (itself), but other cases can give us
2113/// information. For example, immediately after an "SRA X, 2", we know that
2114/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002115unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002116 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002117 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002118 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002119 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002120 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Dan Gohmanea859be2007-06-22 14:59:07 +00002122 if (Depth == 6)
2123 return 1; // Limit search depth.
2124
2125 switch (Op.getOpcode()) {
2126 default: break;
2127 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002128 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002129 return VTBits-Tmp+1;
2130 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002131 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002132 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Dan Gohmanea859be2007-06-22 14:59:07 +00002134 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002135 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002136 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002137 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002138
Dan Gohmanea859be2007-06-22 14:59:07 +00002139 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002140 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002142
Dan Gohmanea859be2007-06-22 14:59:07 +00002143 case ISD::SIGN_EXTEND_INREG:
2144 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002145 Tmp =
2146 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002147 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002148
Dan Gohmanea859be2007-06-22 14:59:07 +00002149 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2150 return std::max(Tmp, Tmp2);
2151
2152 case ISD::SRA:
2153 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2154 // SRA X, C -> adds C sign bits.
2155 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002156 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002157 if (Tmp > VTBits) Tmp = VTBits;
2158 }
2159 return Tmp;
2160 case ISD::SHL:
2161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2162 // shl destroys sign bits.
2163 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002164 if (C->getZExtValue() >= VTBits || // Bad shift.
2165 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2166 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002167 }
2168 break;
2169 case ISD::AND:
2170 case ISD::OR:
2171 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002172 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002173 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002174 if (Tmp != 1) {
2175 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2176 FirstAnswer = std::min(Tmp, Tmp2);
2177 // We computed what we know about the sign bits as our first
2178 // answer. Now proceed to the generic code that uses
2179 // ComputeMaskedBits, and pick whichever answer is better.
2180 }
2181 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002182
2183 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002184 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002185 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002186 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002187 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002188
2189 case ISD::SADDO:
2190 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002191 case ISD::SSUBO:
2192 case ISD::USUBO:
2193 case ISD::SMULO:
2194 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002195 if (Op.getResNo() != 1)
2196 break;
Duncan Sands03228082008-11-23 15:47:28 +00002197 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002198 case ISD::SETCC:
2199 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002200 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002201 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002202 return VTBits;
2203 break;
2204 case ISD::ROTL:
2205 case ISD::ROTR:
2206 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002207 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002208
Dan Gohmanea859be2007-06-22 14:59:07 +00002209 // Handle rotate right by N like a rotate left by 32-N.
2210 if (Op.getOpcode() == ISD::ROTR)
2211 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2212
2213 // If we aren't rotating out all of the known-in sign bits, return the
2214 // number that are left. This handles rotl(sext(x), 1) for example.
2215 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2216 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2217 }
2218 break;
2219 case ISD::ADD:
2220 // Add can have at most one carry bit. Thus we know that the output
2221 // is, at worst, one more bit than the inputs.
2222 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2223 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002224
Dan Gohmanea859be2007-06-22 14:59:07 +00002225 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002226 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002227 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002228 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002229 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002230
Dan Gohmanea859be2007-06-22 14:59:07 +00002231 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2232 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002233 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002234 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002235
Dan Gohmanea859be2007-06-22 14:59:07 +00002236 // If we are subtracting one from a positive number, there is no carry
2237 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002238 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002239 return Tmp;
2240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002241
Dan Gohmanea859be2007-06-22 14:59:07 +00002242 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2243 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002244 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002245
Dan Gohmanea859be2007-06-22 14:59:07 +00002246 case ISD::SUB:
2247 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2248 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002249
Dan Gohmanea859be2007-06-22 14:59:07 +00002250 // Handle NEG.
2251 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002252 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002253 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002254 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002255 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2256 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002257 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002259
Dan Gohmanea859be2007-06-22 14:59:07 +00002260 // If the input is known to be positive (the sign bit is known clear),
2261 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002262 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002263 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002264
Dan Gohmanea859be2007-06-22 14:59:07 +00002265 // Otherwise, we treat this like a SUB.
2266 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002267
Dan Gohmanea859be2007-06-22 14:59:07 +00002268 // Sub can have at most one carry bit. Thus we know that the output
2269 // is, at worst, one more bit than the inputs.
2270 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2271 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002272 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002273 case ISD::TRUNCATE:
2274 // FIXME: it's tricky to do anything useful for this, but it is an important
2275 // case for targets like X86.
2276 break;
2277 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002278
Dan Gohmanea859be2007-06-22 14:59:07 +00002279 // Handle LOADX separately here. EXTLOAD case will fallthrough.
Jakub Staszakce00b442012-05-01 23:06:00 +00002280 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002281 unsigned ExtType = LD->getExtensionType();
2282 switch (ExtType) {
2283 default: break;
2284 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002285 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002286 return VTBits-Tmp+1;
2287 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002288 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002289 return VTBits-Tmp;
2290 }
2291 }
2292
2293 // Allow the target to implement this method for its nodes.
2294 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002295 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002296 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2297 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2298 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002299 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002301
Dan Gohmanea859be2007-06-22 14:59:07 +00002302 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2303 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002304 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002305 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002306
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002307 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002308 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002309 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002310 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002311 Mask = KnownOne;
2312 } else {
2313 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002314 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002315 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002316
Dan Gohmanea859be2007-06-22 14:59:07 +00002317 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2318 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002319 Mask = ~Mask;
2320 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002321 // Return # leading zeros. We use 'min' here in case Val was zero before
2322 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002323 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002324}
2325
Chris Lattner0a9481f2011-02-13 22:25:43 +00002326/// isBaseWithConstantOffset - Return true if the specified operand is an
2327/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2328/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2329/// semantics as an ADD. This handles the equivalence:
Chris Lattner463b3c22011-02-14 06:14:42 +00002330/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002331bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2332 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2333 !isa<ConstantSDNode>(Op.getOperand(1)))
2334 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002335
2336 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002337 !MaskedValueIsZero(Op.getOperand(0),
2338 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2339 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002340
Chris Lattner0a9481f2011-02-13 22:25:43 +00002341 return true;
2342}
2343
2344
Dan Gohman8d44b282009-09-03 20:34:31 +00002345bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2346 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002347 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002348 return true;
2349
2350 // If the value is a constant, we can obviously see if it is a NaN or not.
2351 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2352 return !C->getValueAPF().isNaN();
2353
2354 // TODO: Recognize more cases here.
2355
2356 return false;
2357}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002358
Dan Gohmane8326932010-02-24 06:52:40 +00002359bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2360 // If the value is a constant, we can obviously see if it is a zero or not.
2361 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2362 return !C->isZero();
2363
2364 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002365 switch (Op.getOpcode()) {
2366 default: break;
2367 case ISD::OR:
2368 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2369 return !C->isNullValue();
2370 break;
2371 }
Dan Gohmane8326932010-02-24 06:52:40 +00002372
2373 return false;
2374}
2375
2376bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2377 // Check the obvious case.
2378 if (A == B) return true;
2379
2380 // For for negative and positive zero.
2381 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2382 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2383 if (CA->isZero() && CB->isZero()) return true;
2384
2385 // Otherwise they may not be equal.
2386 return false;
2387}
2388
Chris Lattnerc3aae252005-01-07 07:46:32 +00002389/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002390///
Owen Andersone50ed302009-08-10 22:56:29 +00002391SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002392 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002393 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002394 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002396 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002397
Dan Gohman95531882010-03-18 18:49:47 +00002398 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002399 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Chris Lattner4a283e92006-08-11 18:38:11 +00002401 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002402#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002403 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002404#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002405 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002406}
2407
Bill Wendling7ade28c2009-01-28 22:17:52 +00002408SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002409 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002410 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002411 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002412 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002413 switch (Opcode) {
2414 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002415 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002416 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002417 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002418 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002419 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002420 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002421 case ISD::UINT_TO_FP:
2422 case ISD::SINT_TO_FP: {
Chris Lattner7fb5c2d2010-03-15 16:05:15 +00002423 // No compile time operations on ppcf128.
2424 if (VT == MVT::ppcf128) break;
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002425 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002426 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002427 Opcode==ISD::SINT_TO_FP,
2428 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002429 return getConstantFP(apf, VT);
2430 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002431 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002432 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002433 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002434 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002435 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002436 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002437 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002438 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002439 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002440 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002441 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002442 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002443 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002444 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002445 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002446 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002447 }
2448 }
2449
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002450 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002451 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002452 APFloat V = C->getValueAPF(); // make copy
Owen Anderson825b72b2009-08-11 20:47:22 +00002453 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002454 switch (Opcode) {
2455 case ISD::FNEG:
2456 V.changeSign();
2457 return getConstantFP(V, VT);
2458 case ISD::FABS:
2459 V.clearSign();
2460 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002461 case ISD::FP_EXTEND: {
2462 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002463 // This can return overflow, underflow, or inexact; we don't care.
2464 // FIXME need to be more flexible about rounding mode.
Owen Andersone50ed302009-08-10 22:56:29 +00002465 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002466 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002467 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002468 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002469 case ISD::FP_TO_SINT:
2470 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002471 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002472 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002473 assert(integerPartWidth >= 64);
2474 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002475 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002476 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002477 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002478 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2479 break;
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00002480 APInt api(VT.getSizeInBits(), x);
Dale Johannesen2f91f302009-04-24 21:34:13 +00002481 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002482 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002483 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002484 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002485 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002487 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002488 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002489 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002490 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002491 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002492
Gabor Greifba36cb52008-08-28 21:40:38 +00002493 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002494 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002495 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002496 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002497 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002498 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002499 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002500 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002501 assert(VT.isFloatingPoint() &&
2502 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002503 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002504 assert((!VT.isVector() ||
2505 VT.getVectorNumElements() ==
2506 Operand.getValueType().getVectorNumElements()) &&
2507 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002508 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002509 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002510 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002511 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002512 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002513 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002514 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002515 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2516 "Invalid sext node, dst < src!");
2517 assert((!VT.isVector() ||
2518 VT.getVectorNumElements() ==
2519 Operand.getValueType().getVectorNumElements()) &&
2520 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002521 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002522 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002523 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002524 // sext(undef) = 0, because the top bits will all be the same.
2525 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002526 break;
2527 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002528 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002529 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002530 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002531 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2532 "Invalid zext node, dst < src!");
2533 assert((!VT.isVector() ||
2534 VT.getVectorNumElements() ==
2535 Operand.getValueType().getVectorNumElements()) &&
2536 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002537 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002538 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002539 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002540 else if (OpOpcode == ISD::UNDEF)
2541 // zext(undef) = 0, because the top bits will be zero.
2542 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002543 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002544 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002545 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002546 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002547 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002548 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2549 "Invalid anyext node, dst < src!");
2550 assert((!VT.isVector() ||
2551 VT.getVectorNumElements() ==
2552 Operand.getValueType().getVectorNumElements()) &&
2553 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002554
Dan Gohman9e86a732010-06-18 00:08:30 +00002555 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2556 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002557 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002558 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002559 else if (OpOpcode == ISD::UNDEF)
2560 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002561
2562 // (ext (trunx x)) -> x
2563 if (OpOpcode == ISD::TRUNCATE) {
2564 SDValue OpOp = Operand.getNode()->getOperand(0);
2565 if (OpOp.getValueType() == VT)
2566 return OpOp;
2567 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002568 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002569 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002570 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002571 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002572 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002573 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2574 "Invalid truncate node, src < dst!");
2575 assert((!VT.isVector() ||
2576 VT.getVectorNumElements() ==
2577 Operand.getValueType().getVectorNumElements()) &&
2578 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002579 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002580 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002581 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2582 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002583 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002584 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2585 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002586 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002587 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002588 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002589 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002590 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002591 if (OpOpcode == ISD::UNDEF)
2592 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002593 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002594 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002595 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002596 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002597 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002598 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002599 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2600 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002601 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002602 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002603 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002604 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002605 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002606 (VT.getVectorElementType() == Operand.getValueType() ||
2607 (VT.getVectorElementType().isInteger() &&
2608 Operand.getValueType().isInteger() &&
2609 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002610 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002611 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002612 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002613 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2614 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2615 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2616 Operand.getConstantOperandVal(1) == 0 &&
2617 Operand.getOperand(0).getValueType() == VT)
2618 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002619 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002620 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002621 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002622 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002623 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002624 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002625 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002626 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002627 break;
2628 case ISD::FABS:
2629 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002630 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002631 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002632 }
2633
Chris Lattner43247a12005-08-25 19:12:10 +00002634 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002635 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002636 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002637 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002638 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002639 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002640 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002641 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002642 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002643
Dan Gohman95531882010-03-18 18:49:47 +00002644 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002645 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002646 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002647 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002648 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002649
Chris Lattnerc3aae252005-01-07 07:46:32 +00002650 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002651#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002652 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002653#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002654 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002655}
2656
Bill Wendling688d1c42008-09-24 10:16:24 +00002657SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002658 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002659 ConstantSDNode *Cst1,
2660 ConstantSDNode *Cst2) {
2661 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2662
2663 switch (Opcode) {
2664 case ISD::ADD: return getConstant(C1 + C2, VT);
2665 case ISD::SUB: return getConstant(C1 - C2, VT);
2666 case ISD::MUL: return getConstant(C1 * C2, VT);
2667 case ISD::UDIV:
2668 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2669 break;
2670 case ISD::UREM:
2671 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2672 break;
2673 case ISD::SDIV:
2674 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2675 break;
2676 case ISD::SREM:
2677 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2678 break;
2679 case ISD::AND: return getConstant(C1 & C2, VT);
2680 case ISD::OR: return getConstant(C1 | C2, VT);
2681 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2682 case ISD::SHL: return getConstant(C1 << C2, VT);
2683 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2684 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2685 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2686 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2687 default: break;
2688 }
2689
2690 return SDValue();
2691}
2692
Owen Andersone50ed302009-08-10 22:56:29 +00002693SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002694 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002695 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2696 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002697 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002698 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002699 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002700 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2701 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002702 // Fold trivial token factors.
2703 if (N1.getOpcode() == ISD::EntryToken) return N2;
2704 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002705 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002706 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002707 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002708 // Concat of UNDEFs is UNDEF.
2709 if (N1.getOpcode() == ISD::UNDEF &&
2710 N2.getOpcode() == ISD::UNDEF)
2711 return getUNDEF(VT);
2712
Dan Gohman7f8613e2008-08-14 20:04:46 +00002713 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2714 // one big BUILD_VECTOR.
2715 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2716 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002717 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2718 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002719 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002720 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002721 }
2722 break;
Chris Lattner76365122005-01-16 02:23:22 +00002723 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002724 assert(VT.isInteger() && "This operator does not apply to FP types!");
2725 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002726 N1.getValueType() == VT && "Binary operator types must match!");
2727 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2728 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002729 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002730 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002731 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2732 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002733 break;
Chris Lattner76365122005-01-16 02:23:22 +00002734 case ISD::OR:
2735 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002736 case ISD::ADD:
2737 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002738 assert(VT.isInteger() && "This operator does not apply to FP types!");
2739 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002740 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002741 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2742 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002743 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002744 return N1;
2745 break;
Chris Lattner76365122005-01-16 02:23:22 +00002746 case ISD::UDIV:
2747 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002748 case ISD::MULHU:
2749 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002750 case ISD::MUL:
2751 case ISD::SDIV:
2752 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002753 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002754 assert(N1.getValueType() == N2.getValueType() &&
2755 N1.getValueType() == VT && "Binary operator types must match!");
2756 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002757 case ISD::FADD:
2758 case ISD::FSUB:
2759 case ISD::FMUL:
2760 case ISD::FDIV:
2761 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002762 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002763 if (Opcode == ISD::FADD) {
2764 // 0+x --> x
2765 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2766 if (CFP->getValueAPF().isZero())
2767 return N2;
2768 // x+0 --> x
2769 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2770 if (CFP->getValueAPF().isZero())
2771 return N1;
2772 } else if (Opcode == ISD::FSUB) {
2773 // x-0 --> x
2774 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2775 if (CFP->getValueAPF().isZero())
2776 return N1;
2777 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002778 }
Dale Johannesen78995512010-05-15 18:38:02 +00002779 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002780 assert(N1.getValueType() == N2.getValueType() &&
2781 N1.getValueType() == VT && "Binary operator types must match!");
2782 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002783 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2784 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002785 N1.getValueType().isFloatingPoint() &&
2786 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002787 "Invalid FCOPYSIGN!");
2788 break;
Chris Lattner76365122005-01-16 02:23:22 +00002789 case ISD::SHL:
2790 case ISD::SRA:
2791 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002792 case ISD::ROTL:
2793 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002794 assert(VT == N1.getValueType() &&
2795 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002796 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002797 "Shifts only work on integers");
Chris Lattnere0751182011-02-13 19:09:16 +00002798 // Verify that the shift amount VT is bit enough to hold valid shift
2799 // amounts. This catches things like trying to shift an i1024 value by an
2800 // i8, which is easy to fall into in generic code that uses
2801 // TLI.getShiftAmount().
2802 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002803 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002804 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002805
2806 // Always fold shifts of i1 values so the code generator doesn't need to
2807 // handle them. Since we know the size of the shift has to be less than the
2808 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002809 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002810 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002811 if (N2C && N2C->isNullValue())
2812 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002813 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002814 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002815 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002816 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002817 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002818 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002819 assert(EVT.isVector() == VT.isVector() &&
2820 "FP_ROUND_INREG type should be vector iff the operand "
2821 "type is vector!");
2822 assert((!EVT.isVector() ||
2823 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2824 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002825 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002826 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002827 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002828 break;
2829 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002830 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002831 assert(VT.isFloatingPoint() &&
2832 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002833 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002834 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002835 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002836 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002837 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002838 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002839 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002840 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002841 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002842 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002843 assert(!EVT.isVector() &&
2844 "AssertSExt/AssertZExt type should be the vector element type "
2845 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002846 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002847 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002848 break;
2849 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002850 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002851 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002852 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002853 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002854 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002855 assert(EVT.isVector() == VT.isVector() &&
2856 "SIGN_EXTEND_INREG type should be vector iff the operand "
2857 "type is vector!");
2858 assert((!EVT.isVector() ||
2859 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2860 "Vector element counts must match in SIGN_EXTEND_INREG");
2861 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002862 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002863
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002864 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002865 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002866 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002867 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002868 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002869 return getConstant(Val, VT);
2870 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002871 break;
2872 }
2873 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002874 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2875 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002876 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002877
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002878 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2879 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002880 if (N2C &&
2881 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002882 N1.getNumOperands() > 0) {
2883 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002884 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002885 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002886 N1.getOperand(N2C->getZExtValue() / Factor),
2887 getConstant(N2C->getZExtValue() % Factor,
2888 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002889 }
2890
2891 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2892 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002893 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2894 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersone50ed302009-08-10 22:56:29 +00002895 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanc680ac92009-07-09 22:01:03 +00002896 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002897 // If the vector element type is not legal, the BUILD_VECTOR operands
2898 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002899 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2900 }
2901 if (VT != VEltTy) {
2902 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2903 // result is implicitly extended.
2904 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002905 }
2906 return Elt;
2907 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002908
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002909 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2910 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002911 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00002912 // If the indices are the same, return the inserted element else
2913 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00002914 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00002915 SDValue N1Op2 = N1.getOperand(2);
2916 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
2917
2918 if (N1Op2C && N2C) {
2919 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
2920 if (VT == N1.getOperand(1).getValueType())
2921 return N1.getOperand(1);
2922 else
2923 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2924 }
2925
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002926 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00002927 }
Dan Gohman6ab64222008-08-13 21:51:37 +00002928 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002929 break;
2930 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002931 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002932 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2933 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00002934 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00002935 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002936
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002937 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2938 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002939 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002940 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002941 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002942
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002943 // EXTRACT_ELEMENT of a constant int is also very common.
2944 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002945 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002946 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002947 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2948 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002949 }
2950 break;
David Greene91585092011-01-26 15:38:49 +00002951 case ISD::EXTRACT_SUBVECTOR: {
2952 SDValue Index = N2;
2953 if (VT.isSimple() && N1.getValueType().isSimple()) {
2954 assert(VT.isVector() && N1.getValueType().isVector() &&
2955 "Extract subvector VTs must be a vectors!");
2956 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
2957 "Extract subvector VTs must have the same element type!");
2958 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
2959 "Extract subvector must be from larger vector to smaller vector!");
2960
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00002961 if (isa<ConstantSDNode>(Index.getNode())) {
2962 assert((VT.getVectorNumElements() +
2963 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00002964 <= N1.getValueType().getVectorNumElements())
2965 && "Extract subvector overflow!");
2966 }
2967
2968 // Trivial extraction.
2969 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
2970 return N1;
2971 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002972 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002973 }
David Greene91585092011-01-26 15:38:49 +00002974 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002975
2976 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002977 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002978 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2979 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002980 } else { // Cannonicalize constant to RHS if commutative
2981 if (isCommutativeBinOp(Opcode)) {
2982 std::swap(N1C, N2C);
2983 std::swap(N1, N2);
2984 }
2985 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002986 }
2987
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002988 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002989 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2990 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002991 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002992 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2993 // Cannonicalize constant to RHS if commutative
2994 std::swap(N1CFP, N2CFP);
2995 std::swap(N1, N2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002996 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002997 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2998 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002999 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003000 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003001 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003002 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003003 return getConstantFP(V1, VT);
3004 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003005 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003006 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3007 if (s!=APFloat::opInvalidOp)
3008 return getConstantFP(V1, VT);
3009 break;
3010 case ISD::FMUL:
3011 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3012 if (s!=APFloat::opInvalidOp)
3013 return getConstantFP(V1, VT);
3014 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003015 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003016 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3017 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3018 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003019 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003020 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003021 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3022 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3023 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003024 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003025 case ISD::FCOPYSIGN:
3026 V1.copySign(V2);
3027 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003028 default: break;
3029 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003030 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003031
3032 if (Opcode == ISD::FP_ROUND) {
3033 APFloat V = N1CFP->getValueAPF(); // make copy
3034 bool ignored;
3035 // This can return overflow, underflow, or inexact; we don't care.
3036 // FIXME need to be more flexible about rounding mode.
3037 (void)V.convert(*EVTToAPFloatSemantics(VT),
3038 APFloat::rmNearestTiesToEven, &ignored);
3039 return getConstantFP(V, VT);
3040 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003041 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003042
Chris Lattner62b57722006-04-20 05:39:12 +00003043 // Canonicalize an UNDEF to the RHS, even over a constant.
3044 if (N1.getOpcode() == ISD::UNDEF) {
3045 if (isCommutativeBinOp(Opcode)) {
3046 std::swap(N1, N2);
3047 } else {
3048 switch (Opcode) {
3049 case ISD::FP_ROUND_INREG:
3050 case ISD::SIGN_EXTEND_INREG:
3051 case ISD::SUB:
3052 case ISD::FSUB:
3053 case ISD::FDIV:
3054 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003055 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003056 return N1; // fold op(undef, arg2) -> undef
3057 case ISD::UDIV:
3058 case ISD::SDIV:
3059 case ISD::UREM:
3060 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003061 case ISD::SRL:
3062 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003063 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003064 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3065 // For vectors, we can't easily build an all zero vector, just return
3066 // the LHS.
3067 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003068 }
3069 }
3070 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003071
3072 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003073 if (N2.getOpcode() == ISD::UNDEF) {
3074 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003075 case ISD::XOR:
3076 if (N1.getOpcode() == ISD::UNDEF)
3077 // Handle undef ^ undef -> 0 special case. This is a common
3078 // idiom (misuse).
3079 return getConstant(0, VT);
3080 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003081 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003082 case ISD::ADDC:
3083 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003084 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003085 case ISD::UDIV:
3086 case ISD::SDIV:
3087 case ISD::UREM:
3088 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003089 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003090 case ISD::FADD:
3091 case ISD::FSUB:
3092 case ISD::FMUL:
3093 case ISD::FDIV:
3094 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003095 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003096 return N2;
3097 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003098 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003099 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003100 case ISD::SRL:
3101 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003102 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003103 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3104 // For vectors, we can't easily build an all zero vector, just return
3105 // the LHS.
3106 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003107 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003108 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003109 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003110 // For vectors, we can't easily build an all one vector, just return
3111 // the LHS.
3112 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003113 case ISD::SRA:
3114 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003115 }
3116 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003117
Chris Lattner27e9b412005-05-11 18:57:39 +00003118 // Memoize this node if possible.
3119 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003120 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003121 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003122 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003123 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003124 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003125 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003126 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003127 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003128
Dan Gohman95531882010-03-18 18:49:47 +00003129 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003130 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003131 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003132 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003133 }
3134
Chris Lattnerc3aae252005-01-07 07:46:32 +00003135 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003136#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003137 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003138#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003139 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003140}
3141
Owen Andersone50ed302009-08-10 22:56:29 +00003142SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003143 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003144 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003145 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003146 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003147 case ISD::CONCAT_VECTORS:
3148 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3149 // one big BUILD_VECTOR.
3150 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3151 N2.getOpcode() == ISD::BUILD_VECTOR &&
3152 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003153 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3154 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003155 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3156 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003157 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003158 }
3159 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003160 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003161 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003162 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003163 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003164 break;
3165 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003166 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003167 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003168 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003169 return N2; // select true, X, Y -> X
3170 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003171 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003172
3173 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003174 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003175 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003176 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003177 case ISD::INSERT_SUBVECTOR: {
3178 SDValue Index = N3;
3179 if (VT.isSimple() && N1.getValueType().isSimple()
3180 && N2.getValueType().isSimple()) {
3181 assert(VT.isVector() && N1.getValueType().isVector() &&
3182 N2.getValueType().isVector() &&
3183 "Insert subvector VTs must be a vectors");
3184 assert(VT == N1.getValueType() &&
3185 "Dest and insert subvector source types must match!");
3186 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3187 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003188 if (isa<ConstantSDNode>(Index.getNode())) {
3189 assert((N2.getValueType().getVectorNumElements() +
3190 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003191 <= VT.getVectorNumElements())
3192 && "Insert subvector overflow!");
3193 }
3194
3195 // Trivial insertion.
3196 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3197 return N2;
3198 }
3199 break;
3200 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003201 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003202 // Fold bit_convert nodes from a type to themselves.
3203 if (N1.getValueType() == VT)
3204 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003205 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003206 }
3207
Chris Lattner43247a12005-08-25 19:12:10 +00003208 // Memoize node if it doesn't produce a flag.
3209 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003210 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003211 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003212 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003213 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003214 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003215 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003216 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003217 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003218
Dan Gohman95531882010-03-18 18:49:47 +00003219 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003220 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003221 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003222 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003223 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003224
Chris Lattnerc3aae252005-01-07 07:46:32 +00003225 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003226#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003227 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003228#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003229 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003230}
3231
Owen Andersone50ed302009-08-10 22:56:29 +00003232SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003233 SDValue N1, SDValue N2, SDValue N3,
3234 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003235 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003236 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003237}
3238
Owen Andersone50ed302009-08-10 22:56:29 +00003239SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003240 SDValue N1, SDValue N2, SDValue N3,
3241 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003243 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003244}
3245
Dan Gohman98ca4f22009-08-05 01:29:28 +00003246/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3247/// the incoming stack arguments to be loaded from the stack.
3248SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3249 SmallVector<SDValue, 8> ArgChains;
3250
3251 // Include the original chain at the beginning of the list. When this is
3252 // used by target LowerCall hooks, this helps legalize find the
3253 // CALLSEQ_BEGIN node.
3254 ArgChains.push_back(Chain);
3255
3256 // Add a chain value for each stack argument.
3257 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3258 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3259 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3260 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3261 if (FI->getIndex() < 0)
3262 ArgChains.push_back(SDValue(L, 1));
3263
3264 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003266 &ArgChains[0], ArgChains.size());
3267}
3268
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003269/// SplatByte - Distribute ByteVal over NumBits bits.
3270static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3271 APInt Val = APInt(NumBits, ByteVal);
3272 unsigned Shift = 8;
3273 for (unsigned i = NumBits; i > 8; i >>= 1) {
3274 Val = (Val << Shift) | Val;
3275 Shift <<= 1;
3276 }
3277 return Val;
3278}
3279
Dan Gohman707e0182008-04-12 04:36:06 +00003280/// getMemsetValue - Vectorized representation of the memset value
3281/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003282static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003283 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003284 assert(Value.getOpcode() != ISD::UNDEF);
3285
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003286 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003287 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003288 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003289 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003290 return DAG.getConstant(Val, VT);
3291 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003292 }
Evan Chengf0df0312008-05-15 08:39:06 +00003293
Dale Johannesen0f502f62009-02-03 22:26:09 +00003294 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003295 if (NumBits > 8) {
3296 // Use a multiplication with 0x010101... to extend the input to the
3297 // required length.
3298 APInt Magic = SplatByte(NumBits, 0x01);
3299 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003300 }
3301
3302 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003303}
3304
Dan Gohman707e0182008-04-12 04:36:06 +00003305/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3306/// used when a memcpy is turned into a memset when the source is a constant
3307/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003308static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003309 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003310 // Handle vector with all elements zero.
3311 if (Str.empty()) {
3312 if (VT.isInteger())
3313 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003314 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003315 return DAG.getConstantFP(0.0, VT);
3316 else if (VT.isVector()) {
3317 unsigned NumElts = VT.getVectorNumElements();
3318 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003319 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003320 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3321 EltVT, NumElts)));
3322 } else
3323 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003324 }
3325
Duncan Sands83ec4b62008-06-06 12:08:01 +00003326 assert(!VT.isVector() && "Can't handle vector type here!");
Chris Lattner18c7f802012-02-05 02:29:43 +00003327 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3328 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3329
Dan Gohman707e0182008-04-12 04:36:06 +00003330 uint64_t Val = 0;
Chris Lattner18c7f802012-02-05 02:29:43 +00003331 if (TLI.isLittleEndian()) {
3332 for (unsigned i = 0; i != NumBytes; ++i)
3333 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3334 } else {
3335 for (unsigned i = 0; i != NumBytes; ++i)
3336 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003337 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003338
Dan Gohman707e0182008-04-12 04:36:06 +00003339 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003340}
3341
Scott Michelfdc40a02009-02-17 22:15:04 +00003342/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003343///
Dan Gohman475871a2008-07-27 21:46:04 +00003344static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003345 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003346 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003347 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003348 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003349}
3350
Evan Chengf0df0312008-05-15 08:39:06 +00003351/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3352///
Chris Lattner18c7f802012-02-05 02:29:43 +00003353static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003354 unsigned SrcDelta = 0;
3355 GlobalAddressSDNode *G = NULL;
3356 if (Src.getOpcode() == ISD::GlobalAddress)
3357 G = cast<GlobalAddressSDNode>(Src);
3358 else if (Src.getOpcode() == ISD::ADD &&
3359 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3360 Src.getOperand(1).getOpcode() == ISD::Constant) {
3361 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003362 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003363 }
3364 if (!G)
3365 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003366
Chris Lattner18c7f802012-02-05 02:29:43 +00003367 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003368}
Dan Gohman707e0182008-04-12 04:36:06 +00003369
Evan Cheng255f20f2010-04-01 06:04:33 +00003370/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3371/// to replace the memset / memcpy. Return true if the number of memory ops
3372/// is below the threshold. It returns the types of the sequence of
3373/// memory ops to perform memset / memcpy by reference.
3374static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003375 unsigned Limit, uint64_t Size,
3376 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003377 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00003378 bool MemcpyStrSrc,
Evan Cheng255f20f2010-04-01 06:04:33 +00003379 SelectionDAG &DAG,
3380 const TargetLowering &TLI) {
3381 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3382 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003383 // If 'SrcAlign' is zero, that means the memory operation does not need to
3384 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3385 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3386 // is the specified alignment of the memory operation. If it is zero, that
3387 // means it's possible to change the alignment of the destination.
3388 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3389 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003390 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003391 IsZeroVal, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003392 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003393
Chris Lattneracee6472010-03-07 07:45:08 +00003394 if (VT == MVT::Other) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003395 if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003396 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003397 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003398 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003399 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 case 0: VT = MVT::i64; break;
3401 case 4: VT = MVT::i32; break;
3402 case 2: VT = MVT::i16; break;
3403 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003404 }
3405 }
3406
Owen Anderson766b5ef2009-08-11 21:59:30 +00003407 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003408 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003409 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003410 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003411
Duncan Sands8e4eb092008-06-08 20:54:56 +00003412 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003413 VT = LVT;
3414 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003415
Dan Gohman707e0182008-04-12 04:36:06 +00003416 unsigned NumMemOps = 0;
3417 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003418 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003419 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003420 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng255f20f2010-04-01 06:04:33 +00003421 if (VT.isVector() || VT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003423 while (!TLI.isTypeLegal(VT))
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003425 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003426 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003427 // This can result in a type that is not legal on the target, e.g.
3428 // 1 or 2 bytes on PPC.
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003430 VTSize >>= 1;
3431 }
Dan Gohman707e0182008-04-12 04:36:06 +00003432 }
Dan Gohman707e0182008-04-12 04:36:06 +00003433
3434 if (++NumMemOps > Limit)
3435 return false;
3436 MemOps.push_back(VT);
3437 Size -= VTSize;
3438 }
3439
3440 return true;
3441}
3442
Dale Johannesen0f502f62009-02-03 22:26:09 +00003443static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003444 SDValue Chain, SDValue Dst,
3445 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003446 unsigned Align, bool isVol,
3447 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003448 MachinePointerInfo DstPtrInfo,
3449 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003450 // Turn a memcpy of undef to nop.
3451 if (Src.getOpcode() == ISD::UNDEF)
3452 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003453
Dan Gohman21323f32008-05-29 19:42:22 +00003454 // Expand memcpy to a series of load and store ops if the size operand falls
3455 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003456 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3457 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003458 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003459 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003460 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003461 MachineFunction &MF = DAG.getMachineFunction();
3462 MachineFrameInfo *MFI = MF.getFrameInfo();
3463 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003464 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3465 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3466 DstAlignCanChange = true;
3467 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3468 if (Align > SrcAlign)
3469 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003470 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003471 bool CopyFromStr = isMemSrcFromString(Src, Str);
3472 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003473 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003474
Evan Cheng94107ba2010-04-01 18:19:11 +00003475 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003476 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003477 (isZeroStr ? 0 : SrcAlign),
3478 true, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003479 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003480
Evan Cheng255f20f2010-04-01 06:04:33 +00003481 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003482 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003483 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3484 if (NewAlign > Align) {
3485 // Give the stack frame object a larger alignment if needed.
3486 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3487 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3488 Align = NewAlign;
3489 }
3490 }
Dan Gohman707e0182008-04-12 04:36:06 +00003491
Dan Gohman475871a2008-07-27 21:46:04 +00003492 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003493 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003494 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003495 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003496 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003498 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003499
Evan Cheng255f20f2010-04-01 06:04:33 +00003500 if (CopyFromStr &&
3501 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003502 // It's unlikely a store of a vector immediate can be done in a single
3503 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003504 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003505 // FIXME: Handle other cases where store of vector immediate is done in
3506 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003507 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Dale Johannesen0f502f62009-02-03 22:26:09 +00003508 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003509 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003510 DstPtrInfo.getWithOffset(DstOff), isVol,
3511 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003512 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003513 // The type might not be legal for the target. This should only happen
3514 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003515 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3516 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003517 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003518 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003519 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003520 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003521 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003522 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003523 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003524 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003525 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003526 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3527 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003528 }
3529 OutChains.push_back(Store);
3530 SrcOff += VTSize;
3531 DstOff += VTSize;
3532 }
3533
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003535 &OutChains[0], OutChains.size());
3536}
3537
Dale Johannesen0f502f62009-02-03 22:26:09 +00003538static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003539 SDValue Chain, SDValue Dst,
3540 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003541 unsigned Align, bool isVol,
3542 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003543 MachinePointerInfo DstPtrInfo,
3544 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003545 // Turn a memmove of undef to nop.
3546 if (Src.getOpcode() == ISD::UNDEF)
3547 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003548
3549 // Expand memmove to a series of load and store ops if the size operand falls
3550 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003551 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003552 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003553 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003554 MachineFunction &MF = DAG.getMachineFunction();
3555 MachineFrameInfo *MFI = MF.getFrameInfo();
3556 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003557 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3558 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3559 DstAlignCanChange = true;
3560 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3561 if (Align > SrcAlign)
3562 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003563 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003564
Evan Cheng94107ba2010-04-01 18:19:11 +00003565 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003566 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003567 SrcAlign, true, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003568 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003569
Evan Cheng255f20f2010-04-01 06:04:33 +00003570 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003571 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003572 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3573 if (NewAlign > Align) {
3574 // Give the stack frame object a larger alignment if needed.
3575 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3576 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3577 Align = NewAlign;
3578 }
3579 }
Dan Gohman21323f32008-05-29 19:42:22 +00003580
Evan Cheng255f20f2010-04-01 06:04:33 +00003581 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003582 SmallVector<SDValue, 8> LoadValues;
3583 SmallVector<SDValue, 8> LoadChains;
3584 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003585 unsigned NumMemOps = MemOps.size();
3586 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003587 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003588 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003589 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003590
Dale Johannesen0f502f62009-02-03 22:26:09 +00003591 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003592 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003593 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003594 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003595 LoadValues.push_back(Value);
3596 LoadChains.push_back(Value.getValue(1));
3597 SrcOff += VTSize;
3598 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003600 &LoadChains[0], LoadChains.size());
3601 OutChains.clear();
3602 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003603 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003604 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003605 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003606
Dale Johannesen0f502f62009-02-03 22:26:09 +00003607 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003608 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003609 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003610 OutChains.push_back(Store);
3611 DstOff += VTSize;
3612 }
3613
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003615 &OutChains[0], OutChains.size());
3616}
3617
Dale Johannesen0f502f62009-02-03 22:26:09 +00003618static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003619 SDValue Chain, SDValue Dst,
3620 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003621 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003622 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003623 // Turn a memset of undef to nop.
3624 if (Src.getOpcode() == ISD::UNDEF)
3625 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003626
3627 // Expand memset to a series of load/store ops if the size operand
3628 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003629 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003630 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003631 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003632 MachineFunction &MF = DAG.getMachineFunction();
3633 MachineFrameInfo *MFI = MF.getFrameInfo();
3634 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003635 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3636 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3637 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003638 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003639 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003640 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003641 Size, (DstAlignCanChange ? 0 : Align), 0,
Lang Hames15701f82011-10-26 23:50:43 +00003642 IsZeroVal, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003643 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003644
Evan Cheng255f20f2010-04-01 06:04:33 +00003645 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003646 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003647 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3648 if (NewAlign > Align) {
3649 // Give the stack frame object a larger alignment if needed.
3650 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3651 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3652 Align = NewAlign;
3653 }
3654 }
3655
Dan Gohman475871a2008-07-27 21:46:04 +00003656 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003657 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003658 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003659
3660 // Find the largest store and generate the bit pattern for it.
3661 EVT LargestVT = MemOps[0];
3662 for (unsigned i = 1; i < NumMemOps; i++)
3663 if (MemOps[i].bitsGT(LargestVT))
3664 LargestVT = MemOps[i];
3665 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3666
Dan Gohman707e0182008-04-12 04:36:06 +00003667 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003668 EVT VT = MemOps[i];
Benjamin Kramer80220362011-01-02 19:57:05 +00003669
3670 // If this store is smaller than the largest store see whether we can get
3671 // the smaller value for free with a truncate.
3672 SDValue Value = MemSetValue;
3673 if (VT.bitsLT(LargestVT)) {
3674 if (!LargestVT.isVector() && !VT.isVector() &&
3675 TLI.isTruncateFree(LargestVT, VT))
3676 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3677 else
3678 Value = getMemsetValue(Src, VT, DAG, dl);
3679 }
3680 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003681 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003682 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003683 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003684 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003685 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003686 DstOff += VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003687 }
3688
Owen Anderson825b72b2009-08-11 20:47:22 +00003689 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003690 &OutChains[0], OutChains.size());
3691}
3692
Dale Johannesen0f502f62009-02-03 22:26:09 +00003693SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003694 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003695 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003696 MachinePointerInfo DstPtrInfo,
3697 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003698
3699 // Check to see if we should lower the memcpy to loads and stores first.
3700 // For cases within the target-specified limits, this is the best choice.
3701 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3702 if (ConstantSize) {
3703 // Memcpy with size zero? Just return the original chain.
3704 if (ConstantSize->isNullValue())
3705 return Chain;
3706
Evan Cheng255f20f2010-04-01 06:04:33 +00003707 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3708 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003709 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003710 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003711 return Result;
3712 }
3713
3714 // Then check to see if we should lower the memcpy with target-specific
3715 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003716 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003717 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003718 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003719 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003720 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003721 return Result;
3722
3723 // If we really need inline code and the target declined to provide it,
3724 // use a (potentially long) sequence of loads and stores.
3725 if (AlwaysInline) {
3726 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003727 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003728 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003729 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003730 }
3731
Dan Gohman7b55d362010-04-05 20:24:08 +00003732 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3733 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3734 // respect volatile, so they may do things like read or write memory
3735 // beyond the given memory regions. But fixing this isn't easy, and most
3736 // people don't care.
3737
Dan Gohman707e0182008-04-12 04:36:06 +00003738 // Emit a library call.
3739 TargetLowering::ArgListTy Args;
3740 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003741 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003742 Entry.Node = Dst; Args.push_back(Entry);
3743 Entry.Node = Src; Args.push_back(Entry);
3744 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003745 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003746 TargetLowering::
3747 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003748 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003749 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3750 /*isTailCall=*/false,
3751 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003752 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003753 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003754 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003755 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3756
Dan Gohman707e0182008-04-12 04:36:06 +00003757 return CallResult.second;
3758}
3759
Dale Johannesen0f502f62009-02-03 22:26:09 +00003760SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003761 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003762 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003763 MachinePointerInfo DstPtrInfo,
3764 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003765
Dan Gohman21323f32008-05-29 19:42:22 +00003766 // Check to see if we should lower the memmove to loads and stores first.
3767 // For cases within the target-specified limits, this is the best choice.
3768 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3769 if (ConstantSize) {
3770 // Memmove with size zero? Just return the original chain.
3771 if (ConstantSize->isNullValue())
3772 return Chain;
3773
Dan Gohman475871a2008-07-27 21:46:04 +00003774 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003775 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003776 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003777 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003778 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003779 return Result;
3780 }
Dan Gohman707e0182008-04-12 04:36:06 +00003781
3782 // Then check to see if we should lower the memmove with target-specific
3783 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003784 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003785 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003786 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003787 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003788 return Result;
3789
Mon P Wang01f0e852010-04-06 08:27:51 +00003790 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3791 // not be safe. See memcpy above for more details.
3792
Dan Gohman707e0182008-04-12 04:36:06 +00003793 // Emit a library call.
3794 TargetLowering::ArgListTy Args;
3795 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003796 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003797 Entry.Node = Dst; Args.push_back(Entry);
3798 Entry.Node = Src; Args.push_back(Entry);
3799 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003800 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003801 TargetLowering::
3802 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003803 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003804 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3805 /*isTailCall=*/false,
3806 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003807 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003808 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003809 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003810 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3811
Dan Gohman707e0182008-04-12 04:36:06 +00003812 return CallResult.second;
3813}
3814
Dale Johannesen0f502f62009-02-03 22:26:09 +00003815SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003816 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003817 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003818 MachinePointerInfo DstPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003819
3820 // Check to see if we should lower the memset to stores first.
3821 // For cases within the target-specified limits, this is the best choice.
3822 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3823 if (ConstantSize) {
3824 // Memset with size zero? Just return the original chain.
3825 if (ConstantSize->isNullValue())
3826 return Chain;
3827
Mon P Wang20adc9d2010-04-04 03:10:48 +00003828 SDValue Result =
3829 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003830 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00003831
Gabor Greifba36cb52008-08-28 21:40:38 +00003832 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003833 return Result;
3834 }
3835
3836 // Then check to see if we should lower the memset with target-specific
3837 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003838 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003839 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003840 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003841 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003842 return Result;
3843
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003844 // Emit a library call.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003845 Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003846 TargetLowering::ArgListTy Args;
3847 TargetLowering::ArgListEntry Entry;
3848 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3849 Args.push_back(Entry);
3850 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 if (Src.getValueType().bitsGT(MVT::i32))
3852 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003853 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003854 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003855 Entry.Node = Src;
3856 Entry.Ty = Type::getInt32Ty(*getContext());
3857 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003858 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003859 Entry.Node = Size;
3860 Entry.Ty = IntPtrTy;
3861 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003862 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003863 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003864 TargetLowering::
3865 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003866 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003867 TLI.getLibcallCallingConv(RTLIB::MEMSET),
3868 /*isTailCall=*/false,
3869 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003870 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003871 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003872 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003873 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3874
Dan Gohman707e0182008-04-12 04:36:06 +00003875 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003876}
3877
Owen Andersone50ed302009-08-10 22:56:29 +00003878SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00003879 SDValue Chain, SDValue Ptr, SDValue Cmp,
3880 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00003881 unsigned Alignment,
3882 AtomicOrdering Ordering,
3883 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003884 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3885 Alignment = getEVTAlignment(MemVT);
3886
Evan Chengff89dcb2009-10-18 18:16:27 +00003887 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003888 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3889
3890 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003891 // FIXME: Volatile isn't really correct; we should keep track of atomic
3892 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003893 Flags |= MachineMemOperand::MOVolatile;
3894
3895 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00003896 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003897
Eli Friedman55ba8162011-07-29 03:05:32 +00003898 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
3899 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003900}
3901
3902SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3903 SDValue Chain,
3904 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00003905 SDValue Swp, MachineMemOperand *MMO,
3906 AtomicOrdering Ordering,
3907 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003908 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3909 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3910
Owen Andersone50ed302009-08-10 22:56:29 +00003911 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003912
Owen Anderson825b72b2009-08-11 20:47:22 +00003913 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003914 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003915 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003916 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3917 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Pete Cooper32ecfb42012-07-30 20:23:19 +00003918 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00003919 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003920 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3921 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003922 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003923 }
Dan Gohman95531882010-03-18 18:49:47 +00003924 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003925 Ptr, Cmp, Swp, MMO, Ordering,
3926 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003927 CSEMap.InsertNode(N, IP);
3928 AllNodes.push_back(N);
3929 return SDValue(N, 0);
3930}
3931
Owen Andersone50ed302009-08-10 22:56:29 +00003932SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003933 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003934 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003935 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00003936 unsigned Alignment,
3937 AtomicOrdering Ordering,
3938 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003939 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3940 Alignment = getEVTAlignment(MemVT);
3941
Evan Chengff89dcb2009-10-18 18:16:27 +00003942 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00003943 // A monotonic store does not load; a release store "loads" in the sense
3944 // that other stores cannot be sunk past it.
3945 // (An atomicrmw obviously both loads and stores.)
3946 unsigned Flags = MachineMemOperand::MOStore;
3947 if (Opcode != ISD::ATOMIC_STORE || Ordering > Monotonic)
3948 Flags |= MachineMemOperand::MOLoad;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003949
3950 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003951 // FIXME: Volatile isn't really correct; we should keep track of atomic
3952 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003953 Flags |= MachineMemOperand::MOVolatile;
3954
3955 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00003956 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00003957 MemVT.getStoreSize(), Alignment);
3958
Eli Friedman55ba8162011-07-29 03:05:32 +00003959 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
3960 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003961}
3962
3963SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3964 SDValue Chain,
3965 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00003966 MachineMemOperand *MMO,
3967 AtomicOrdering Ordering,
3968 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003969 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3970 Opcode == ISD::ATOMIC_LOAD_SUB ||
3971 Opcode == ISD::ATOMIC_LOAD_AND ||
3972 Opcode == ISD::ATOMIC_LOAD_OR ||
3973 Opcode == ISD::ATOMIC_LOAD_XOR ||
3974 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003975 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003976 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003977 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003978 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00003979 Opcode == ISD::ATOMIC_SWAP ||
3980 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00003981 "Invalid Atomic Op");
3982
Owen Andersone50ed302009-08-10 22:56:29 +00003983 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003984
Eli Friedman327236c2011-08-24 20:50:09 +00003985 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
3986 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003987 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003988 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003989 SDValue Ops[] = {Chain, Ptr, Val};
3990 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Pete Cooper32ecfb42012-07-30 20:23:19 +00003991 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00003992 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003993 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3994 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003995 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003996 }
Dan Gohman95531882010-03-18 18:49:47 +00003997 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003998 Ptr, Val, MMO,
3999 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004000 CSEMap.InsertNode(N, IP);
4001 AllNodes.push_back(N);
4002 return SDValue(N, 0);
4003}
4004
Eli Friedman327236c2011-08-24 20:50:09 +00004005SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4006 EVT VT, SDValue Chain,
4007 SDValue Ptr,
4008 const Value* PtrVal,
4009 unsigned Alignment,
4010 AtomicOrdering Ordering,
4011 SynchronizationScope SynchScope) {
4012 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4013 Alignment = getEVTAlignment(MemVT);
4014
4015 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00004016 // A monotonic load does not store; an acquire load "stores" in the sense
4017 // that other loads cannot be hoisted past it.
4018 unsigned Flags = MachineMemOperand::MOLoad;
4019 if (Ordering > Monotonic)
4020 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004021
4022 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004023 // FIXME: Volatile isn't really correct; we should keep track of atomic
4024 // orderings in the memoperand.
Eli Friedman327236c2011-08-24 20:50:09 +00004025 Flags |= MachineMemOperand::MOVolatile;
4026
4027 MachineMemOperand *MMO =
4028 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4029 MemVT.getStoreSize(), Alignment);
4030
4031 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4032 Ordering, SynchScope);
4033}
4034
4035SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4036 EVT VT, SDValue Chain,
4037 SDValue Ptr,
4038 MachineMemOperand *MMO,
4039 AtomicOrdering Ordering,
4040 SynchronizationScope SynchScope) {
4041 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4042
4043 SDVTList VTs = getVTList(VT, MVT::Other);
4044 FoldingSetNodeID ID;
4045 ID.AddInteger(MemVT.getRawBits());
4046 SDValue Ops[] = {Chain, Ptr};
4047 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004048 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Eli Friedman327236c2011-08-24 20:50:09 +00004049 void* IP = 0;
4050 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4051 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4052 return SDValue(E, 0);
4053 }
4054 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4055 Ptr, MMO, Ordering, SynchScope);
4056 CSEMap.InsertNode(N, IP);
4057 AllNodes.push_back(N);
4058 return SDValue(N, 0);
4059}
4060
Duncan Sands4bdcb612008-07-02 17:40:58 +00004061/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004062SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4063 DebugLoc dl) {
4064 if (NumOps == 1)
4065 return Ops[0];
4066
Owen Andersone50ed302009-08-10 22:56:29 +00004067 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004068 VTs.reserve(NumOps);
4069 for (unsigned i = 0; i < NumOps; ++i)
4070 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004071 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004072 Ops, NumOps);
4073}
4074
Dan Gohman475871a2008-07-27 21:46:04 +00004075SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004076SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004077 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004078 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004079 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004080 unsigned Align, bool Vol,
4081 bool ReadMem, bool WriteMem) {
4082 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004083 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004084 ReadMem, WriteMem);
4085}
4086
4087SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004088SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4089 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004090 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004091 unsigned Align, bool Vol,
4092 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004093 if (Align == 0) // Ensure that codegen never sees alignment 0
4094 Align = getEVTAlignment(MemVT);
4095
4096 MachineFunction &MF = getMachineFunction();
4097 unsigned Flags = 0;
4098 if (WriteMem)
4099 Flags |= MachineMemOperand::MOStore;
4100 if (ReadMem)
4101 Flags |= MachineMemOperand::MOLoad;
4102 if (Vol)
4103 Flags |= MachineMemOperand::MOVolatile;
4104 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004105 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004106
4107 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4108}
4109
4110SDValue
4111SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4112 const SDValue *Ops, unsigned NumOps,
4113 EVT MemVT, MachineMemOperand *MMO) {
4114 assert((Opcode == ISD::INTRINSIC_VOID ||
4115 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004116 Opcode == ISD::PREFETCH ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004117 (Opcode <= INT_MAX &&
4118 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4119 "Opcode is not a memory-accessing opcode!");
4120
Dale Johannesene8c17332009-01-29 00:47:48 +00004121 // Memoize the node unless it returns a flag.
4122 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004123 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004124 FoldingSetNodeID ID;
4125 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004126 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004127 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004128 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4129 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004130 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004132
Dan Gohman95531882010-03-18 18:49:47 +00004133 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4134 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004135 CSEMap.InsertNode(N, IP);
4136 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004137 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4138 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004139 }
4140 AllNodes.push_back(N);
4141 return SDValue(N, 0);
4142}
4143
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004144/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4145/// MachinePointerInfo record from it. This is particularly useful because the
4146/// code generator has many cases where it doesn't bother passing in a
4147/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4148static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4149 // If this is FI+Offset, we can model it.
4150 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4151 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4152
4153 // If this is (FI+Offset1)+Offset2, we can model it.
4154 if (Ptr.getOpcode() != ISD::ADD ||
4155 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4156 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4157 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004158
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004159 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4160 return MachinePointerInfo::getFixedStack(FI, Offset+
4161 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4162}
4163
4164/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4165/// MachinePointerInfo record from it. This is particularly useful because the
4166/// code generator has many cases where it doesn't bother passing in a
4167/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4168static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4169 // If the 'Offset' value isn't a constant, we can't handle this.
4170 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4171 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4172 if (OffsetOp.getOpcode() == ISD::UNDEF)
4173 return InferPointerInfo(Ptr);
4174 return MachinePointerInfo();
4175}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004176
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004177
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004178SDValue
4179SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4180 EVT VT, DebugLoc dl, SDValue Chain,
4181 SDValue Ptr, SDValue Offset,
4182 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004183 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004184 unsigned Alignment, const MDNode *TBAAInfo,
4185 const MDNode *Ranges) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004186 assert(Chain.getValueType() == MVT::Other &&
4187 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004188 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4189 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004190
Dan Gohmanc76909a2009-09-25 20:36:54 +00004191 unsigned Flags = MachineMemOperand::MOLoad;
4192 if (isVolatile)
4193 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004194 if (isNonTemporal)
4195 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004196 if (isInvariant)
4197 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004198
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004199 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4200 // clients.
4201 if (PtrInfo.V == 0)
4202 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004203
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004204 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004205 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004206 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004207 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004208 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004209}
4210
4211SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004212SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004213 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004214 SDValue Ptr, SDValue Offset, EVT MemVT,
4215 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004216 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004217 ExtType = ISD::NON_EXTLOAD;
4218 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004219 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004220 } else {
4221 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004222 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4223 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004224 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004225 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004226 assert(VT.isVector() == MemVT.isVector() &&
4227 "Cannot use trunc store to convert to or from a vector!");
4228 assert((!VT.isVector() ||
4229 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4230 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004231 }
4232
4233 bool Indexed = AM != ISD::UNINDEXED;
4234 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4235 "Unindexed load with an offset!");
4236
4237 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004239 SDValue Ops[] = { Chain, Ptr, Offset };
4240 FoldingSetNodeID ID;
4241 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004242 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004243 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004244 MMO->isNonTemporal(),
4245 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004246 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004247 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004248 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4249 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004250 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004251 }
Dan Gohman95531882010-03-18 18:49:47 +00004252 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4253 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004254 CSEMap.InsertNode(N, IP);
4255 AllNodes.push_back(N);
4256 return SDValue(N, 0);
4257}
4258
Owen Andersone50ed302009-08-10 22:56:29 +00004259SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004260 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004261 MachinePointerInfo PtrInfo,
4262 bool isVolatile, bool isNonTemporal,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004263 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004264 const MDNode *TBAAInfo,
4265 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004266 SDValue Undef = getUNDEF(Ptr.getValueType());
4267 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004268 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4269 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004270}
Dale Johannesene8c17332009-01-29 00:47:48 +00004271
Stuart Hastingsa9011292011-02-16 16:23:55 +00004272SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004273 SDValue Chain, SDValue Ptr,
4274 MachinePointerInfo PtrInfo, EVT MemVT,
4275 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004276 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004277 SDValue Undef = getUNDEF(Ptr.getValueType());
4278 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004279 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004280 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004281}
4282
4283
Dan Gohman475871a2008-07-27 21:46:04 +00004284SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004285SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4286 SDValue Offset, ISD::MemIndexedMode AM) {
4287 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4288 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4289 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004290 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004291 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004292 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
4293 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004294}
4295
Dale Johannesene8c17332009-01-29 00:47:48 +00004296SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004297 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004298 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004299 unsigned Alignment, const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004300 assert(Chain.getValueType() == MVT::Other &&
4301 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004302 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004303 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004304
Dan Gohmanc76909a2009-09-25 20:36:54 +00004305 unsigned Flags = MachineMemOperand::MOStore;
4306 if (isVolatile)
4307 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004308 if (isNonTemporal)
4309 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004310
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004311 if (PtrInfo.V == 0)
4312 PtrInfo = InferPointerInfo(Ptr);
4313
4314 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004315 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004316 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004317 Val.getValueType().getStoreSize(), Alignment,
4318 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004319
4320 return getStore(Chain, dl, Val, Ptr, MMO);
4321}
4322
4323SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4324 SDValue Ptr, MachineMemOperand *MMO) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004325 assert(Chain.getValueType() == MVT::Other &&
4326 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004327 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004328 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004329 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004330 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4331 FoldingSetNodeID ID;
4332 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004333 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004334 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004335 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004336 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004337 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004338 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4339 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004340 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004341 }
Dan Gohman95531882010-03-18 18:49:47 +00004342 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4343 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004344 CSEMap.InsertNode(N, IP);
4345 AllNodes.push_back(N);
4346 return SDValue(N, 0);
4347}
4348
Dale Johannesene8c17332009-01-29 00:47:48 +00004349SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004350 SDValue Ptr, MachinePointerInfo PtrInfo,
4351 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004352 unsigned Alignment,
4353 const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004354 assert(Chain.getValueType() == MVT::Other &&
4355 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004356 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4357 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004358
Dan Gohmanc76909a2009-09-25 20:36:54 +00004359 unsigned Flags = MachineMemOperand::MOStore;
4360 if (isVolatile)
4361 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004362 if (isNonTemporal)
4363 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004364
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004365 if (PtrInfo.V == 0)
4366 PtrInfo = InferPointerInfo(Ptr);
4367
4368 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004369 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004370 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4371 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004372
4373 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4374}
4375
4376SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4377 SDValue Ptr, EVT SVT,
4378 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004379 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004380
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004381 assert(Chain.getValueType() == MVT::Other &&
4382 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004383 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004384 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004385
Dan Gohman2e141d72009-12-14 23:40:38 +00004386 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4387 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004388 assert(VT.isInteger() == SVT.isInteger() &&
4389 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004390 assert(VT.isVector() == SVT.isVector() &&
4391 "Cannot use trunc store to convert to or from a vector!");
4392 assert((!VT.isVector() ||
4393 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4394 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004395
Owen Anderson825b72b2009-08-11 20:47:22 +00004396 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004397 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004398 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4399 FoldingSetNodeID ID;
4400 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004401 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004402 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004403 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004404 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004405 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004406 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4407 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004408 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004409 }
Dan Gohman95531882010-03-18 18:49:47 +00004410 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4411 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004412 CSEMap.InsertNode(N, IP);
4413 AllNodes.push_back(N);
4414 return SDValue(N, 0);
4415}
4416
Dan Gohman475871a2008-07-27 21:46:04 +00004417SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004418SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4419 SDValue Offset, ISD::MemIndexedMode AM) {
4420 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4421 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4422 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004423 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004424 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4425 FoldingSetNodeID ID;
4426 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004427 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004428 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004429 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004430 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004431 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004432 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004433
Dan Gohman95531882010-03-18 18:49:47 +00004434 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4435 ST->isTruncatingStore(),
4436 ST->getMemoryVT(),
4437 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004438 CSEMap.InsertNode(N, IP);
4439 AllNodes.push_back(N);
4440 return SDValue(N, 0);
4441}
4442
Owen Andersone50ed302009-08-10 22:56:29 +00004443SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004444 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004445 SDValue SV,
4446 unsigned Align) {
4447 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4448 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004449}
4450
Owen Andersone50ed302009-08-10 22:56:29 +00004451SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004452 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004453 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004454 case 0: return getNode(Opcode, DL, VT);
4455 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4456 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4457 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004458 default: break;
4459 }
4460
Dan Gohman475871a2008-07-27 21:46:04 +00004461 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004462 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004463 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004464 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004465}
4466
Owen Andersone50ed302009-08-10 22:56:29 +00004467SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004468 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004469 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004470 case 0: return getNode(Opcode, DL, VT);
4471 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4472 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4473 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004474 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004476
Chris Lattneref847df2005-04-09 03:27:28 +00004477 switch (Opcode) {
4478 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004479 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004480 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004481 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4482 "LHS and RHS of condition must have same type!");
4483 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4484 "True and False arms of SelectCC must have same type!");
4485 assert(Ops[2].getValueType() == VT &&
4486 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004487 break;
4488 }
4489 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004490 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004491 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4492 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004493 break;
4494 }
Chris Lattneref847df2005-04-09 03:27:28 +00004495 }
4496
Chris Lattner385328c2005-05-14 07:42:29 +00004497 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004498 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004499 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004500
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004501 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004502 FoldingSetNodeID ID;
4503 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004504 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004505
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004506 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004507 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004508
Dan Gohman95531882010-03-18 18:49:47 +00004509 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004510 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004511 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004512 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004513 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004514
Chris Lattneref847df2005-04-09 03:27:28 +00004515 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004516#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004517 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004518#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004519 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004520}
4521
Bill Wendling7ade28c2009-01-28 22:17:52 +00004522SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004523 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004524 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004525 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004526 Ops, NumOps);
4527}
4528
Bill Wendling7ade28c2009-01-28 22:17:52 +00004529SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004530 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004531 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004532 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004533 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4534 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004535}
4536
Bill Wendling7ade28c2009-01-28 22:17:52 +00004537SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4538 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004539 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004540 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004541
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004542#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004543 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004544 // FIXME: figure out how to safely handle things like
4545 // int foo(int x) { return 1 << (x & 255); }
4546 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004547 case ISD::SRA_PARTS:
4548 case ISD::SRL_PARTS:
4549 case ISD::SHL_PARTS:
4550 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004551 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004552 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004553 else if (N3.getOpcode() == ISD::AND)
4554 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4555 // If the and is only masking out bits that cannot effect the shift,
4556 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004557 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004558 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004559 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004560 }
4561 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004562 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004563#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004564
Chris Lattner43247a12005-08-25 19:12:10 +00004565 // Memoize the node unless it returns a flag.
4566 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004567 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004568 FoldingSetNodeID ID;
4569 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004570 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004571 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004572 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004573
Dan Gohman0e5f1302008-07-07 23:02:41 +00004574 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004575 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004576 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004577 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004578 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004579 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4580 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004581 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004582 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004583 }
Chris Lattnera5682852006-08-07 23:03:03 +00004584 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004585 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004586 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004587 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004588 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004589 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004590 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004591 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4592 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004593 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004594 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004595 }
Chris Lattner43247a12005-08-25 19:12:10 +00004596 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004597 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004598#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004599 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004600#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004601 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004602}
4603
Bill Wendling7ade28c2009-01-28 22:17:52 +00004604SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4605 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004606}
4607
Bill Wendling7ade28c2009-01-28 22:17:52 +00004608SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4609 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004610 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004611 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004612}
4613
Bill Wendling7ade28c2009-01-28 22:17:52 +00004614SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4615 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004616 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004617 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004618}
4619
Bill Wendling7ade28c2009-01-28 22:17:52 +00004620SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4621 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004622 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004623 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004624}
4625
Bill Wendling7ade28c2009-01-28 22:17:52 +00004626SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4627 SDValue N1, SDValue N2, SDValue N3,
4628 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004629 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004630 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004631}
4632
Bill Wendling7ade28c2009-01-28 22:17:52 +00004633SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4634 SDValue N1, SDValue N2, SDValue N3,
4635 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004636 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004637 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004638}
4639
Owen Andersone50ed302009-08-10 22:56:29 +00004640SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004641 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004642}
4643
Owen Andersone50ed302009-08-10 22:56:29 +00004644SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004645 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4646 E = VTList.rend(); I != E; ++I)
4647 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4648 return *I;
4649
Owen Andersone50ed302009-08-10 22:56:29 +00004650 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004651 Array[0] = VT1;
4652 Array[1] = VT2;
4653 SDVTList Result = makeVTList(Array, 2);
4654 VTList.push_back(Result);
4655 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004656}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004657
Owen Andersone50ed302009-08-10 22:56:29 +00004658SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004659 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4660 E = VTList.rend(); I != E; ++I)
4661 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4662 I->VTs[2] == VT3)
4663 return *I;
4664
Owen Andersone50ed302009-08-10 22:56:29 +00004665 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004666 Array[0] = VT1;
4667 Array[1] = VT2;
4668 Array[2] = VT3;
4669 SDVTList Result = makeVTList(Array, 3);
4670 VTList.push_back(Result);
4671 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004672}
4673
Owen Andersone50ed302009-08-10 22:56:29 +00004674SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004675 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4676 E = VTList.rend(); I != E; ++I)
4677 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4678 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4679 return *I;
4680
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004681 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004682 Array[0] = VT1;
4683 Array[1] = VT2;
4684 Array[2] = VT3;
4685 Array[3] = VT4;
4686 SDVTList Result = makeVTList(Array, 4);
4687 VTList.push_back(Result);
4688 return Result;
4689}
4690
Owen Andersone50ed302009-08-10 22:56:29 +00004691SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004692 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004693 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004694 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004695 case 2: return getVTList(VTs[0], VTs[1]);
4696 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004697 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004698 default: break;
4699 }
4700
Dan Gohmane8be6c62008-07-17 19:10:17 +00004701 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4702 E = VTList.rend(); I != E; ++I) {
4703 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4704 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004705
Benjamin Kramerb26e2912012-07-19 10:46:05 +00004706 if (std::equal(&VTs[2], &VTs[NumVTs], &I->VTs[2]))
Dan Gohmane8be6c62008-07-17 19:10:17 +00004707 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004708 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004709
Owen Andersone50ed302009-08-10 22:56:29 +00004710 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004711 std::copy(VTs, VTs+NumVTs, Array);
4712 SDVTList Result = makeVTList(Array, NumVTs);
4713 VTList.push_back(Result);
4714 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004715}
4716
4717
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004718/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4719/// specified operands. If the resultant node already exists in the DAG,
4720/// this does not modify the specified node, instead it returns the node that
4721/// already exists. If the resultant node does not exist in the DAG, the
4722/// input node is returned. As a degenerate case, if you specify the same
4723/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004724SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004725 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004726
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004727 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004728 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004729
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004730 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004731 void *InsertPos = 0;
4732 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004733 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004734
Dan Gohman79acd2b2008-07-21 22:38:59 +00004735 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004736 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004737 if (!RemoveNodeFromCSEMaps(N))
4738 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004739
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004740 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004741 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004742
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004743 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004744 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004745 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004746}
4747
Dan Gohman027657d2010-06-18 15:30:29 +00004748SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004749 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004750
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004751 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004752 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004753 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004754
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004755 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004756 void *InsertPos = 0;
4757 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004758 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004759
Dan Gohman79acd2b2008-07-21 22:38:59 +00004760 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004761 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004762 if (!RemoveNodeFromCSEMaps(N))
4763 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004764
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004765 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004766 if (N->OperandList[0] != Op1)
4767 N->OperandList[0].set(Op1);
4768 if (N->OperandList[1] != Op2)
4769 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004770
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004771 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004772 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004773 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004774}
4775
Dan Gohman027657d2010-06-18 15:30:29 +00004776SDNode *SelectionDAG::
4777UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004778 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004779 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004780}
4781
Dan Gohman027657d2010-06-18 15:30:29 +00004782SDNode *SelectionDAG::
4783UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004784 SDValue Op3, SDValue Op4) {
4785 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004786 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004787}
4788
Dan Gohman027657d2010-06-18 15:30:29 +00004789SDNode *SelectionDAG::
4790UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004791 SDValue Op3, SDValue Op4, SDValue Op5) {
4792 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004793 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004794}
4795
Dan Gohman027657d2010-06-18 15:30:29 +00004796SDNode *SelectionDAG::
4797UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004798 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004799 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004800
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004801 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004802 bool AnyChange = false;
4803 for (unsigned i = 0; i != NumOps; ++i) {
4804 if (Ops[i] != N->getOperand(i)) {
4805 AnyChange = true;
4806 break;
4807 }
4808 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004809
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004810 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004811 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004812
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004813 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004814 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004815 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004816 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004817
Dan Gohman7ceda162008-05-02 00:05:03 +00004818 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004819 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004820 if (!RemoveNodeFromCSEMaps(N))
4821 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004822
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004823 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004824 for (unsigned i = 0; i != NumOps; ++i)
4825 if (N->OperandList[i] != Ops[i])
4826 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004827
4828 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004829 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004830 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004831}
4832
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004833/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004834/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004835void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004836 // Unlike the code in MorphNodeTo that does this, we don't need to
4837 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004838 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4839 SDUse &Use = *I++;
4840 Use.set(SDValue());
4841 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004842}
Chris Lattner149c58c2005-08-16 18:17:10 +00004843
Dan Gohmane8be6c62008-07-17 19:10:17 +00004844/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4845/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004846///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004847SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004848 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004849 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004850 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004851}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004852
Dan Gohmane8be6c62008-07-17 19:10:17 +00004853SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004854 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004855 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004856 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004857 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004858}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004859
Dan Gohmane8be6c62008-07-17 19:10:17 +00004860SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004861 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004862 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004863 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004864 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004865 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004866}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004867
Dan Gohmane8be6c62008-07-17 19:10:17 +00004868SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004869 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004870 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004871 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004872 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004873 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004874}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004875
Dan Gohmane8be6c62008-07-17 19:10:17 +00004876SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004877 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004878 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004879 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004880 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004881}
4882
Dan Gohmane8be6c62008-07-17 19:10:17 +00004883SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004884 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004885 unsigned NumOps) {
4886 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004887 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004888}
4889
Dan Gohmane8be6c62008-07-17 19:10:17 +00004890SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004891 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004892 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004893 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004894}
4895
Dan Gohmane8be6c62008-07-17 19:10:17 +00004896SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004897 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004898 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004899 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004900 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004901}
4902
Bill Wendling13d6d442008-12-01 23:28:22 +00004903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004904 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00004905 const SDValue *Ops, unsigned NumOps) {
4906 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4907 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4908}
4909
Scott Michelfdc40a02009-02-17 22:15:04 +00004910SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004911 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004912 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004913 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004914 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004915 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004916}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004917
Scott Michelfdc40a02009-02-17 22:15:04 +00004918SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004919 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004920 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004921 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004922 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004923 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004924}
4925
Dan Gohmane8be6c62008-07-17 19:10:17 +00004926SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004927 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004928 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004929 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004930 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004931 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004932 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004933}
4934
Dan Gohmane8be6c62008-07-17 19:10:17 +00004935SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004936 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004937 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004938 SDValue Op3) {
4939 SDVTList VTs = getVTList(VT1, VT2, VT3);
4940 SDValue Ops[] = { Op1, Op2, Op3 };
4941 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4942}
4943
4944SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004945 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004946 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00004947 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4948 // Reset the NodeID to -1.
4949 N->setNodeId(-1);
4950 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004951}
4952
Devang Patel0508d042011-12-15 18:21:18 +00004953/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
4954/// the line number information on the merged node since it is not possible to
4955/// preserve the information that operation is associated with multiple lines.
4956/// This will make the debugger working better at -O0, were there is a higher
4957/// probability having other instructions associated with that line.
4958///
4959SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
4960 DebugLoc NLoc = N->getDebugLoc();
4961 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
4962 N->setDebugLoc(DebugLoc());
4963 }
4964 return N;
4965}
4966
Chris Lattner21221e32010-02-28 21:36:14 +00004967/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00004968/// return type, opcode, and operands.
4969///
4970/// Note that MorphNodeTo returns the resultant node. If there is already a
4971/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004972/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004973///
4974/// Using MorphNodeTo is faster than creating a new node and swapping it in
4975/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004976/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004977/// the node's users.
4978///
4979SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004980 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004981 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004982 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004983 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004984 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004985 FoldingSetNodeID ID;
4986 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004987 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00004988 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004989 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004990
Dan Gohman095cc292008-09-13 01:54:27 +00004991 if (!RemoveNodeFromCSEMaps(N))
4992 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004993
Dan Gohmane8be6c62008-07-17 19:10:17 +00004994 // Start the morphing.
4995 N->NodeType = Opc;
4996 N->ValueList = VTs.VTs;
4997 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00004998
Dan Gohmane8be6c62008-07-17 19:10:17 +00004999 // Clear the operands list, updating used nodes to remove this from their
5000 // use list. Keep track of any operands that become dead as a result.
5001 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005002 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5003 SDUse &Use = *I++;
5004 SDNode *Used = Use.getNode();
5005 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005006 if (Used->use_empty())
5007 DeadNodeSet.insert(Used);
5008 }
5009
Dan Gohmanc76909a2009-09-25 20:36:54 +00005010 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5011 // Initialize the memory references information.
5012 MN->setMemRefs(0, 0);
5013 // If NumOps is larger than the # of operands we can have in a
5014 // MachineSDNode, reallocate the operand list.
5015 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5016 if (MN->OperandsNeedDelete)
5017 delete[] MN->OperandList;
5018 if (NumOps > array_lengthof(MN->LocalOperands))
5019 // We're creating a final node that will live unmorphed for the
5020 // remainder of the current SelectionDAG iteration, so we can allocate
5021 // the operands directly out of a pool with no recycling metadata.
5022 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005023 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005024 else
5025 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5026 MN->OperandsNeedDelete = false;
5027 } else
5028 MN->InitOperands(MN->OperandList, Ops, NumOps);
5029 } else {
5030 // If NumOps is larger than the # of operands we currently have, reallocate
5031 // the operand list.
5032 if (NumOps > N->NumOperands) {
5033 if (N->OperandsNeedDelete)
5034 delete[] N->OperandList;
5035 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005036 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005037 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005038 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005039 }
5040
5041 // Delete any nodes that are still dead after adding the uses for the
5042 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005043 if (!DeadNodeSet.empty()) {
5044 SmallVector<SDNode *, 16> DeadNodes;
5045 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5046 E = DeadNodeSet.end(); I != E; ++I)
5047 if ((*I)->use_empty())
5048 DeadNodes.push_back(*I);
5049 RemoveDeadNodes(DeadNodes);
5050 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005051
Dan Gohmane8be6c62008-07-17 19:10:17 +00005052 if (IP)
5053 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005054 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005055}
5056
Chris Lattner0fb094f2005-11-19 01:44:53 +00005057
Dan Gohman602b0c82009-09-25 18:54:59 +00005058/// getMachineNode - These are used for target selectors to create a new node
5059/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005060///
Dan Gohman602b0c82009-09-25 18:54:59 +00005061/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005062/// node of the specified opcode and operands, it returns that node instead of
5063/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005064MachineSDNode *
5065SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005066 SDVTList VTs = getVTList(VT);
5067 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005068}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005069
Dan Gohmanc81b7832009-10-10 01:29:16 +00005070MachineSDNode *
5071SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005072 SDVTList VTs = getVTList(VT);
5073 SDValue Ops[] = { Op1 };
5074 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005075}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005076
Dan Gohmanc81b7832009-10-10 01:29:16 +00005077MachineSDNode *
5078SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5079 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005080 SDVTList VTs = getVTList(VT);
5081 SDValue Ops[] = { Op1, Op2 };
5082 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005083}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005084
Dan Gohmanc81b7832009-10-10 01:29:16 +00005085MachineSDNode *
5086SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5087 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005088 SDVTList VTs = getVTList(VT);
5089 SDValue Ops[] = { Op1, Op2, Op3 };
5090 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005091}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005092
Dan Gohmanc81b7832009-10-10 01:29:16 +00005093MachineSDNode *
5094SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5095 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005096 SDVTList VTs = getVTList(VT);
5097 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005098}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005099
Dan Gohmanc81b7832009-10-10 01:29:16 +00005100MachineSDNode *
5101SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005102 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005103 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005104}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005105
Dan Gohmanc81b7832009-10-10 01:29:16 +00005106MachineSDNode *
5107SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5108 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005109 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005110 SDValue Ops[] = { Op1 };
5111 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005112}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005113
Dan Gohmanc81b7832009-10-10 01:29:16 +00005114MachineSDNode *
5115SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5116 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005117 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005118 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005119 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005120}
5121
Dan Gohmanc81b7832009-10-10 01:29:16 +00005122MachineSDNode *
5123SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5124 EVT VT1, EVT VT2, SDValue Op1,
5125 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005126 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005127 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005128 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005129}
5130
Dan Gohmanc81b7832009-10-10 01:29:16 +00005131MachineSDNode *
5132SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5133 EVT VT1, EVT VT2,
5134 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005135 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005136 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005137}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005138
Dan Gohmanc81b7832009-10-10 01:29:16 +00005139MachineSDNode *
5140SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5141 EVT VT1, EVT VT2, EVT VT3,
5142 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005143 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005144 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005145 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005146}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005147
Dan Gohmanc81b7832009-10-10 01:29:16 +00005148MachineSDNode *
5149SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5150 EVT VT1, EVT VT2, EVT VT3,
5151 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005152 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005153 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005154 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00005155}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005156
Dan Gohmanc81b7832009-10-10 01:29:16 +00005157MachineSDNode *
5158SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5159 EVT VT1, EVT VT2, EVT VT3,
5160 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005161 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005162 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005163}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005164
Dan Gohmanc81b7832009-10-10 01:29:16 +00005165MachineSDNode *
5166SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5167 EVT VT2, EVT VT3, EVT VT4,
5168 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005169 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005170 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005171}
5172
Dan Gohmanc81b7832009-10-10 01:29:16 +00005173MachineSDNode *
5174SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5175 const std::vector<EVT> &ResultTys,
5176 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005177 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
5178 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
5179}
5180
Dan Gohmanc81b7832009-10-10 01:29:16 +00005181MachineSDNode *
5182SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
5183 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005184 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005185 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005186 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005187
5188 if (DoCSE) {
5189 FoldingSetNodeID ID;
5190 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5191 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005192 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5193 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5194 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005195 }
5196
5197 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005198 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005199
5200 // Initialize the operands list.
5201 if (NumOps > array_lengthof(N->LocalOperands))
5202 // We're creating a final node that will live unmorphed for the
5203 // remainder of the current SelectionDAG iteration, so we can allocate
5204 // the operands directly out of a pool with no recycling metadata.
5205 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5206 Ops, NumOps);
5207 else
5208 N->InitOperands(N->LocalOperands, Ops, NumOps);
5209 N->OperandsNeedDelete = false;
5210
5211 if (DoCSE)
5212 CSEMap.InsertNode(N, IP);
5213
5214 AllNodes.push_back(N);
5215#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005216 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005217#endif
5218 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005219}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005220
Dan Gohman6a402dc2009-08-19 18:16:17 +00005221/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005222/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005223SDValue
5224SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5225 SDValue Operand) {
5226 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005227 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005228 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005229 return SDValue(Subreg, 0);
5230}
5231
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005232/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005233/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005234SDValue
5235SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5236 SDValue Operand, SDValue Subreg) {
5237 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005238 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005239 VT, Operand, Subreg, SRIdxVal);
5240 return SDValue(Result, 0);
5241}
5242
Evan Cheng08b11732008-03-22 01:55:50 +00005243/// getNodeIfExists - Get the specified node if it's already available, or
5244/// else return NULL.
5245SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005246 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005247 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005248 FoldingSetNodeID ID;
5249 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5250 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005251 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005252 return E;
5253 }
5254 return NULL;
5255}
5256
Evan Cheng31441b72010-03-29 20:48:30 +00005257/// getDbgValue - Creates a SDDbgValue node.
5258///
5259SDDbgValue *
5260SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5261 DebugLoc DL, unsigned O) {
5262 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5263}
5264
5265SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005266SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005267 DebugLoc DL, unsigned O) {
5268 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5269}
5270
5271SDDbgValue *
5272SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5273 DebugLoc DL, unsigned O) {
5274 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5275}
5276
Dan Gohmana72d2a22010-03-03 21:33:37 +00005277namespace {
5278
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005279/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005280/// pointed to by a use iterator is deleted, increment the use iterator
5281/// so that it doesn't dangle.
5282///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005283class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005284 SDNode::use_iterator &UI;
5285 SDNode::use_iterator &UE;
5286
5287 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5288 // Increment the iterator as needed.
5289 while (UI != UE && N == *UI)
5290 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005291 }
5292
5293public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005294 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005295 SDNode::use_iterator &ui,
5296 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005297 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005298};
5299
5300}
5301
Evan Cheng99157a02006-08-07 22:13:29 +00005302/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005303/// This can cause recursive merging of nodes in the DAG.
5304///
Chris Lattner11d049c2008-02-03 03:35:22 +00005305/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005306///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005307void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005308 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005309 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005310 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005311 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005312
Dan Gohman39946102009-01-25 16:29:12 +00005313 // Iterate over all the existing uses of From. New uses will be added
5314 // to the beginning of the use list, which we avoid visiting.
5315 // This specifically avoids visiting uses of From that arise while the
5316 // replacement is happening, because any such uses would be the result
5317 // of CSE: If an existing node looks like From after one of its operands
5318 // is replaced by To, we don't want to replace of all its users with To
5319 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005320 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005321 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005322 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005323 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005324
Chris Lattner8b8749f2005-08-17 19:00:20 +00005325 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005326 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005327
Dan Gohman39946102009-01-25 16:29:12 +00005328 // A user can appear in a use list multiple times, and when this
5329 // happens the uses are usually next to each other in the list.
5330 // To help reduce the number of CSE recomputations, process all
5331 // the uses of this user that we can find this way.
5332 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005333 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005334 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005335 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005336 } while (UI != UE && *UI == User);
5337
5338 // Now that we have modified User, add it back to the CSE maps. If it
5339 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005340 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005341 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005342
5343 // If we just RAUW'd the root, take note.
5344 if (FromN == getRoot())
5345 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005346}
5347
5348/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5349/// This can cause recursive merging of nodes in the DAG.
5350///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005351/// This version assumes that for each value of From, there is a
5352/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005353///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005354void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005355#ifndef NDEBUG
5356 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5357 assert((!From->hasAnyUseOfValue(i) ||
5358 From->getValueType(i) == To->getValueType(i)) &&
5359 "Cannot use this version of ReplaceAllUsesWith!");
5360#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005361
5362 // Handle the trivial case.
5363 if (From == To)
5364 return;
5365
Dan Gohmandbe664a2009-01-19 21:44:21 +00005366 // Iterate over just the existing users of From. See the comments in
5367 // the ReplaceAllUsesWith above.
5368 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005369 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005370 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005371 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005372
Chris Lattner8b52f212005-08-26 18:36:28 +00005373 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005374 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005375
Dan Gohman39946102009-01-25 16:29:12 +00005376 // A user can appear in a use list multiple times, and when this
5377 // happens the uses are usually next to each other in the list.
5378 // To help reduce the number of CSE recomputations, process all
5379 // the uses of this user that we can find this way.
5380 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005381 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005382 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005383 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005384 } while (UI != UE && *UI == User);
5385
5386 // Now that we have modified User, add it back to the CSE maps. If it
5387 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005388 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005389 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005390
5391 // If we just RAUW'd the root, take note.
5392 if (From == getRoot().getNode())
5393 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005394}
5395
Chris Lattner8b52f212005-08-26 18:36:28 +00005396/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5397/// This can cause recursive merging of nodes in the DAG.
5398///
5399/// This version can replace From with any result values. To must match the
5400/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005401void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005402 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005403 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005404
Dan Gohmandbe664a2009-01-19 21:44:21 +00005405 // Iterate over just the existing users of From. See the comments in
5406 // the ReplaceAllUsesWith above.
5407 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005408 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005409 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005410 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005411
Chris Lattner7b2880c2005-08-24 22:44:39 +00005412 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005413 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005414
Dan Gohman39946102009-01-25 16:29:12 +00005415 // A user can appear in a use list multiple times, and when this
5416 // happens the uses are usually next to each other in the list.
5417 // To help reduce the number of CSE recomputations, process all
5418 // the uses of this user that we can find this way.
5419 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005420 SDUse &Use = UI.getUse();
5421 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005422 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005423 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005424 } while (UI != UE && *UI == User);
5425
5426 // Now that we have modified User, add it back to the CSE maps. If it
5427 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005428 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005429 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005430
5431 // If we just RAUW'd the root, take note.
5432 if (From == getRoot().getNode())
5433 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005434}
5435
Chris Lattner012f2412006-02-17 21:58:01 +00005436/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005437/// uses of other values produced by From.getNode() alone. The Deleted
5438/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005439void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005440 // Handle the really simple, really trivial case efficiently.
5441 if (From == To) return;
5442
Chris Lattner012f2412006-02-17 21:58:01 +00005443 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005444 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005445 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005446 return;
5447 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005448
Dan Gohman39946102009-01-25 16:29:12 +00005449 // Iterate over just the existing users of From. See the comments in
5450 // the ReplaceAllUsesWith above.
5451 SDNode::use_iterator UI = From.getNode()->use_begin(),
5452 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005453 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005454 while (UI != UE) {
5455 SDNode *User = *UI;
5456 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005457
Dan Gohman39946102009-01-25 16:29:12 +00005458 // A user can appear in a use list multiple times, and when this
5459 // happens the uses are usually next to each other in the list.
5460 // To help reduce the number of CSE recomputations, process all
5461 // the uses of this user that we can find this way.
5462 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005463 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005464
5465 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005466 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005467 ++UI;
5468 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005469 }
Dan Gohman39946102009-01-25 16:29:12 +00005470
5471 // If this node hasn't been modified yet, it's still in the CSE maps,
5472 // so remove its old self from the CSE maps.
5473 if (!UserRemovedFromCSEMaps) {
5474 RemoveNodeFromCSEMaps(User);
5475 UserRemovedFromCSEMaps = true;
5476 }
5477
5478 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005479 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005480 } while (UI != UE && *UI == User);
5481
5482 // We are iterating over all uses of the From node, so if a use
5483 // doesn't use the specific value, no changes are made.
5484 if (!UserRemovedFromCSEMaps)
5485 continue;
5486
Chris Lattner01d029b2007-10-15 06:10:22 +00005487 // Now that we have modified User, add it back to the CSE maps. If it
5488 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005489 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005490 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005491
5492 // If we just RAUW'd the root, take note.
5493 if (From == getRoot())
5494 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005495}
5496
Dan Gohman39946102009-01-25 16:29:12 +00005497namespace {
5498 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5499 /// to record information about a use.
5500 struct UseMemo {
5501 SDNode *User;
5502 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005503 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005504 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005505
5506 /// operator< - Sort Memos by User.
5507 bool operator<(const UseMemo &L, const UseMemo &R) {
5508 return (intptr_t)L.User < (intptr_t)R.User;
5509 }
Dan Gohman39946102009-01-25 16:29:12 +00005510}
5511
Dan Gohmane8be6c62008-07-17 19:10:17 +00005512/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005513/// uses of other values produced by From.getNode() alone. The same value
5514/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005515/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005516void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5517 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005518 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005519 // Handle the simple, trivial case efficiently.
5520 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005521 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005522
Dan Gohman39946102009-01-25 16:29:12 +00005523 // Read up all the uses and make records of them. This helps
5524 // processing new uses that are introduced during the
5525 // replacement process.
5526 SmallVector<UseMemo, 4> Uses;
5527 for (unsigned i = 0; i != Num; ++i) {
5528 unsigned FromResNo = From[i].getResNo();
5529 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005530 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005531 E = FromNode->use_end(); UI != E; ++UI) {
5532 SDUse &Use = UI.getUse();
5533 if (Use.getResNo() == FromResNo) {
5534 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005535 Uses.push_back(Memo);
5536 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005537 }
Dan Gohman39946102009-01-25 16:29:12 +00005538 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005539
Dan Gohmane7852d02009-01-26 04:35:06 +00005540 // Sort the uses, so that all the uses from a given User are together.
5541 std::sort(Uses.begin(), Uses.end());
5542
Dan Gohman39946102009-01-25 16:29:12 +00005543 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5544 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005545 // We know that this user uses some value of From. If it is the right
5546 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005547 SDNode *User = Uses[UseIndex].User;
5548
5549 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005550 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005551
Dan Gohmane7852d02009-01-26 04:35:06 +00005552 // The Uses array is sorted, so all the uses for a given User
5553 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005554 // To help reduce the number of CSE recomputations, process all
5555 // the uses of this user that we can find this way.
5556 do {
5557 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005558 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005559 ++UseIndex;
5560
Dan Gohmane7852d02009-01-26 04:35:06 +00005561 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005562 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5563
Dan Gohmane8be6c62008-07-17 19:10:17 +00005564 // Now that we have modified User, add it back to the CSE maps. If it
5565 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005566 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005567 }
5568}
5569
Evan Chenge6f35d82006-08-01 08:20:41 +00005570/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005571/// based on their topological order. It returns the maximum id and a vector
5572/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005573unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005574
Dan Gohmanf06c8352008-09-30 18:30:35 +00005575 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005576
Dan Gohmanf06c8352008-09-30 18:30:35 +00005577 // SortedPos tracks the progress of the algorithm. Nodes before it are
5578 // sorted, nodes after it are unsorted. When the algorithm completes
5579 // it is at the end of the list.
5580 allnodes_iterator SortedPos = allnodes_begin();
5581
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005582 // Visit all the nodes. Move nodes with no operands to the front of
5583 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005584 // operand count. Before we do this, the Node Id fields of the nodes
5585 // may contain arbitrary values. After, the Node Id fields for nodes
5586 // before SortedPos will contain the topological sort index, and the
5587 // Node Id fields for nodes At SortedPos and after will contain the
5588 // count of outstanding operands.
5589 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5590 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005591 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005592 unsigned Degree = N->getNumOperands();
5593 if (Degree == 0) {
5594 // A node with no uses, add it to the result array immediately.
5595 N->setNodeId(DAGSize++);
5596 allnodes_iterator Q = N;
5597 if (Q != SortedPos)
5598 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005599 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005600 ++SortedPos;
5601 } else {
5602 // Temporarily use the Node Id as scratch space for the degree count.
5603 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005604 }
5605 }
5606
Chad Rosierf496dea2012-05-21 17:13:41 +00005607 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005608 // such that by the time the end is reached all nodes will be sorted.
5609 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5610 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005611 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005612 // N is in sorted position, so all its uses have one less operand
5613 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005614 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5615 UI != UE; ++UI) {
5616 SDNode *P = *UI;
5617 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005618 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005619 --Degree;
5620 if (Degree == 0) {
5621 // All of P's operands are sorted, so P may sorted now.
5622 P->setNodeId(DAGSize++);
5623 if (P != SortedPos)
5624 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005625 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005626 ++SortedPos;
5627 } else {
5628 // Update P's outstanding operand count.
5629 P->setNodeId(Degree);
5630 }
5631 }
David Greene221925e2010-01-20 00:59:23 +00005632 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005633#ifndef NDEBUG
5634 SDNode *S = ++I;
5635 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005636 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005637#endif
5638 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005639 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005640 }
5641
5642 assert(SortedPos == AllNodes.end() &&
5643 "Topological sort incomplete!");
5644 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5645 "First node in topological sort is not the entry token!");
5646 assert(AllNodes.front().getNodeId() == 0 &&
5647 "First node in topological sort has non-zero id!");
5648 assert(AllNodes.front().getNumOperands() == 0 &&
5649 "First node in topological sort has operands!");
5650 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5651 "Last node in topologic sort has unexpected id!");
5652 assert(AllNodes.back().use_empty() &&
5653 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005654 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005655 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005656}
5657
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005658/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005659void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005660 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005661 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005662}
Evan Chenge6f35d82006-08-01 08:20:41 +00005663
Bill Wendling0777e922009-12-21 21:59:52 +00005664/// GetOrdering - Get the order for the SDNode.
5665unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5666 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005667 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005668}
5669
Evan Chengbfcb3052010-03-25 01:38:16 +00005670/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5671/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005672void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5673 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005674 if (SD)
5675 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005676}
Evan Cheng091cba12006-07-27 06:39:06 +00005677
Devang Patela2e868d2011-01-25 23:27:42 +00005678/// TransferDbgValues - Transfer SDDbgValues.
5679void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5680 if (From == To || !From.getNode()->getHasDebugValue())
5681 return;
5682 SDNode *FromNode = From.getNode();
5683 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005684 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005685 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005686 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005687 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005688 SDDbgValue *Dbg = *I;
5689 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5690 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5691 Dbg->getOffset(), Dbg->getDebugLoc(),
5692 Dbg->getOrder());
5693 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005694 }
5695 }
Devang Patela778f5c2011-02-18 22:43:42 +00005696 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5697 E = ClonedDVs.end(); I != E; ++I)
5698 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005699}
5700
Jim Laskey58b968b2005-08-17 20:08:02 +00005701//===----------------------------------------------------------------------===//
5702// SDNode Class
5703//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005704
Chris Lattner48b85922007-02-04 02:41:42 +00005705HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005706 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005707}
5708
Devang Patel0d881da2010-07-06 22:08:15 +00005709GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5710 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005711 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005712 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005713 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005714}
Chris Lattner48b85922007-02-04 02:41:42 +00005715
Owen Andersone50ed302009-08-10 22:56:29 +00005716MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005717 MachineMemOperand *mmo)
5718 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005719 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005720 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005721 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005722 assert(isNonTemporal() == MMO->isNonTemporal() &&
5723 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005724 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005725}
5726
Scott Michelfdc40a02009-02-17 22:15:04 +00005727MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005728 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005729 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005730 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005731 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005732 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005733 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005734 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5735 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005736}
5737
Jim Laskey583bd472006-10-27 23:46:08 +00005738/// Profile - Gather unique data for the node.
5739///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005740void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005741 AddNodeIDNode(ID, this);
5742}
5743
Owen Andersond8110fb2009-08-25 22:27:22 +00005744namespace {
5745 struct EVTArray {
5746 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005747
Owen Andersond8110fb2009-08-25 22:27:22 +00005748 EVTArray() {
5749 VTs.reserve(MVT::LAST_VALUETYPE);
5750 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5751 VTs.push_back(MVT((MVT::SimpleValueType)i));
5752 }
5753 };
5754}
5755
Owen Andersone50ed302009-08-10 22:56:29 +00005756static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005757static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005758static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005759
Chris Lattnera3255112005-11-08 23:30:28 +00005760/// getValueTypeList - Return a pointer to the specified value type.
5761///
Owen Andersone50ed302009-08-10 22:56:29 +00005762const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005763 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005764 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005765 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005766 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005767 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005768 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005769 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005770 }
Chris Lattnera3255112005-11-08 23:30:28 +00005771}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005772
Chris Lattner5c884562005-01-12 18:37:47 +00005773/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5774/// indicated value. This method ignores uses of other values defined by this
5775/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005776bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005777 assert(Value < getNumValues() && "Bad value!");
5778
Roman Levensteindc1adac2008-04-07 10:06:32 +00005779 // TODO: Only iterate over uses of a given value of the node
5780 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005781 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005782 if (NUses == 0)
5783 return false;
5784 --NUses;
5785 }
Chris Lattner5c884562005-01-12 18:37:47 +00005786 }
5787
5788 // Found exactly the right number of uses?
5789 return NUses == 0;
5790}
5791
5792
Evan Cheng33d55952007-08-02 05:29:38 +00005793/// hasAnyUseOfValue - Return true if there are any use of the indicated
5794/// value. This method ignores uses of other values defined by this operation.
5795bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5796 assert(Value < getNumValues() && "Bad value!");
5797
Dan Gohman1373c1c2008-07-09 22:39:01 +00005798 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005799 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005800 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005801
5802 return false;
5803}
5804
5805
Dan Gohman2a629952008-07-27 18:06:42 +00005806/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005807///
Dan Gohman2a629952008-07-27 18:06:42 +00005808bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005809 bool Seen = false;
5810 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005811 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005812 if (User == this)
5813 Seen = true;
5814 else
5815 return false;
5816 }
5817
5818 return Seen;
5819}
5820
Evan Chenge6e97e62006-11-03 07:31:32 +00005821/// isOperand - Return true if this node is an operand of N.
5822///
Dan Gohman475871a2008-07-27 21:46:04 +00005823bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005824 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5825 if (*this == N->getOperand(i))
5826 return true;
5827 return false;
5828}
5829
Evan Cheng917be682008-03-04 00:41:45 +00005830bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005831 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005832 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005833 return true;
5834 return false;
5835}
Evan Cheng4ee62112006-02-05 06:29:23 +00005836
Chris Lattner572dee72008-01-16 05:49:24 +00005837/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005838/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005839/// side-effecting instructions on any chain path. In practice, this looks
5840/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005841/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005842bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005843 unsigned Depth) const {
5844 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005845
Chris Lattner572dee72008-01-16 05:49:24 +00005846 // Don't search too deeply, we just want to be able to see through
5847 // TokenFactor's etc.
5848 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005849
Chris Lattner572dee72008-01-16 05:49:24 +00005850 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005851 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00005852 if (getOpcode() == ISD::TokenFactor) {
5853 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005854 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5855 return false;
5856 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00005857 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005858
Chris Lattner572dee72008-01-16 05:49:24 +00005859 // Loads don't have side effects, look through them.
5860 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5861 if (!Ld->isVolatile())
5862 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5863 }
5864 return false;
5865}
5866
Lang Hames944520f2011-07-07 04:31:51 +00005867/// hasPredecessor - Return true if N is a predecessor of this node.
5868/// N is either an operand of this node, or can be reached by recursively
5869/// traversing up the operands.
5870/// NOTE: This is an expensive method. Use it carefully.
5871bool SDNode::hasPredecessor(const SDNode *N) const {
5872 SmallPtrSet<const SDNode *, 32> Visited;
5873 SmallVector<const SDNode *, 16> Worklist;
5874 return hasPredecessorHelper(N, Visited, Worklist);
5875}
Dan Gohman6688d612009-10-28 03:44:30 +00005876
Lang Hames944520f2011-07-07 04:31:51 +00005877bool SDNode::hasPredecessorHelper(const SDNode *N,
5878 SmallPtrSet<const SDNode *, 32> &Visited,
5879 SmallVector<const SDNode *, 16> &Worklist) const {
5880 if (Visited.empty()) {
5881 Worklist.push_back(this);
5882 } else {
5883 // Take a look in the visited set. If we've already encountered this node
5884 // we needn't search further.
5885 if (Visited.count(N))
5886 return true;
5887 }
5888
5889 // Haven't visited N yet. Continue the search.
5890 while (!Worklist.empty()) {
5891 const SDNode *M = Worklist.pop_back_val();
5892 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
5893 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00005894 if (Visited.insert(Op))
5895 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00005896 if (Op == N)
5897 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00005898 }
Lang Hames944520f2011-07-07 04:31:51 +00005899 }
Dan Gohman6688d612009-10-28 03:44:30 +00005900
5901 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005902}
5903
Evan Chengc5484282006-10-04 00:56:09 +00005904uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5905 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005906 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005907}
5908
Mon P Wangcd6e7252009-11-30 02:42:02 +00005909SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
5910 assert(N->getNumValues() == 1 &&
5911 "Can't unroll a vector with multiple results!");
5912
5913 EVT VT = N->getValueType(0);
5914 unsigned NE = VT.getVectorNumElements();
5915 EVT EltVT = VT.getVectorElementType();
5916 DebugLoc dl = N->getDebugLoc();
5917
5918 SmallVector<SDValue, 8> Scalars;
5919 SmallVector<SDValue, 4> Operands(N->getNumOperands());
5920
5921 // If ResNE is 0, fully unroll the vector op.
5922 if (ResNE == 0)
5923 ResNE = NE;
5924 else if (NE > ResNE)
5925 NE = ResNE;
5926
5927 unsigned i;
5928 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00005929 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00005930 SDValue Operand = N->getOperand(j);
5931 EVT OperandVT = Operand.getValueType();
5932 if (OperandVT.isVector()) {
5933 // A vector operand; extract a single element.
5934 EVT OperandEltVT = OperandVT.getVectorElementType();
5935 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5936 OperandEltVT,
5937 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00005938 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005939 } else {
5940 // A scalar operand; just use it as is.
5941 Operands[j] = Operand;
5942 }
5943 }
5944
5945 switch (N->getOpcode()) {
5946 default:
5947 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5948 &Operands[0], Operands.size()));
5949 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00005950 case ISD::VSELECT:
5951 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
5952 &Operands[0], Operands.size()));
5953 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00005954 case ISD::SHL:
5955 case ISD::SRA:
5956 case ISD::SRL:
5957 case ISD::ROTL:
5958 case ISD::ROTR:
5959 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00005960 getShiftAmountOperand(Operands[0].getValueType(),
5961 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005962 break;
Dan Gohmand1996362010-01-09 02:13:55 +00005963 case ISD::SIGN_EXTEND_INREG:
5964 case ISD::FP_ROUND_INREG: {
5965 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
5966 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5967 Operands[0],
5968 getValueType(ExtVT)));
5969 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00005970 }
5971 }
5972
5973 for (; i < ResNE; ++i)
5974 Scalars.push_back(getUNDEF(EltVT));
5975
5976 return getNode(ISD::BUILD_VECTOR, dl,
5977 EVT::getVectorVT(*getContext(), EltVT, ResNE),
5978 &Scalars[0], Scalars.size());
5979}
5980
Evan Cheng64fa4a92009-12-09 01:36:00 +00005981
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005982/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
5983/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00005984/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005985bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00005986 unsigned Bytes, int Dist) const {
5987 if (LD->getChain() != Base->getChain())
5988 return false;
5989 EVT VT = LD->getValueType(0);
5990 if (VT.getSizeInBits() / 8 != Bytes)
5991 return false;
5992
5993 SDValue Loc = LD->getOperand(1);
5994 SDValue BaseLoc = Base->getOperand(1);
5995 if (Loc.getOpcode() == ISD::FrameIndex) {
5996 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5997 return false;
5998 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
5999 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6000 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6001 int FS = MFI->getObjectSize(FI);
6002 int BFS = MFI->getObjectSize(BFI);
6003 if (FS != BFS || FS != (int)Bytes) return false;
6004 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6005 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006006
6007 // Handle X+C
6008 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6009 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6010 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006011
Dan Gohman46510a72010-04-15 01:51:59 +00006012 const GlobalValue *GV1 = NULL;
6013 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006014 int64_t Offset1 = 0;
6015 int64_t Offset2 = 0;
6016 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6017 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6018 if (isGA1 && isGA2 && GV1 == GV2)
6019 return Offset1 == (Offset2 + Dist*Bytes);
6020 return false;
6021}
6022
6023
Evan Chengf2dc5c72009-12-09 01:04:59 +00006024/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6025/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006026unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006027 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006028 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006029 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00006030 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006031 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006032 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006033 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6034 TLI.getTargetData());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006035 unsigned AlignBits = KnownZero.countTrailingOnes();
6036 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6037 if (Align)
6038 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006039 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006040
Evan Chengf2dc5c72009-12-09 01:04:59 +00006041 // If this is a direct reference to a stack slot, use information about the
6042 // stack slot's alignment.
6043 int FrameIdx = 1 << 31;
6044 int64_t FrameOffset = 0;
6045 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6046 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006047 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006048 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006049 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006050 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6051 FrameOffset = Ptr.getConstantOperandVal(1);
6052 }
6053
6054 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006055 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006056 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6057 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006058 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006059 }
6060
6061 return 0;
6062}
6063
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006064// getAddressSpace - Return the address space this GlobalAddress belongs to.
6065unsigned GlobalAddressSDNode::getAddressSpace() const {
6066 return getGlobal()->getType()->getAddressSpace();
6067}
6068
6069
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006070Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006071 if (isMachineConstantPoolEntry())
6072 return Val.MachineCPVal->getType();
6073 return Val.ConstVal->getType();
6074}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006075
Bob Wilson24e338e2009-03-02 23:24:16 +00006076bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6077 APInt &SplatUndef,
6078 unsigned &SplatBitSize,
6079 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006080 unsigned MinSplatBits,
6081 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006082 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006083 assert(VT.isVector() && "Expected a vector type");
6084 unsigned sz = VT.getSizeInBits();
6085 if (MinSplatBits > sz)
6086 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006087
Bob Wilson24e338e2009-03-02 23:24:16 +00006088 SplatValue = APInt(sz, 0);
6089 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006090
Bob Wilson24e338e2009-03-02 23:24:16 +00006091 // Get the bits. Bits with undefined values (when the corresponding element
6092 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6093 // in SplatValue. If any of the values are not constant, give up and return
6094 // false.
6095 unsigned int nOps = getNumOperands();
6096 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6097 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006098
6099 for (unsigned j = 0; j < nOps; ++j) {
6100 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006101 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006102 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006103
Bob Wilson24e338e2009-03-02 23:24:16 +00006104 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006105 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006106 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006107 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006108 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006109 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006110 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006111 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006112 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006113 }
6114
Bob Wilson24e338e2009-03-02 23:24:16 +00006115 // The build_vector is all constants or undefs. Find the smallest element
6116 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006117
Bob Wilson24e338e2009-03-02 23:24:16 +00006118 HasAnyUndefs = (SplatUndef != 0);
6119 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006120
Bob Wilson24e338e2009-03-02 23:24:16 +00006121 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006122 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6123 APInt LowValue = SplatValue.trunc(HalfSize);
6124 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6125 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006126
Bob Wilson24e338e2009-03-02 23:24:16 +00006127 // If the two halves do not match (ignoring undef bits), stop here.
6128 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6129 MinSplatBits > HalfSize)
6130 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006131
Bob Wilson24e338e2009-03-02 23:24:16 +00006132 SplatValue = HighValue | LowValue;
6133 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006134
Bob Wilson24e338e2009-03-02 23:24:16 +00006135 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006136 }
6137
Bob Wilson24e338e2009-03-02 23:24:16 +00006138 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006139 return true;
6140}
Nate Begeman9008ca62009-04-27 18:41:29 +00006141
Owen Andersone50ed302009-08-10 22:56:29 +00006142bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006143 // Find the first non-undef value in the shuffle mask.
6144 unsigned i, e;
6145 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6146 /* search */;
6147
Nate Begemana6415752009-04-29 18:13:31 +00006148 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006149
Nate Begeman5a5ca152009-04-29 05:20:52 +00006150 // Make sure all remaining elements are either undef or the same as the first
6151 // non-undef value.
6152 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 if (Mask[i] >= 0 && Mask[i] != Idx)
6154 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006155 return true;
6156}
David Greenecf495bc2010-01-20 20:13:31 +00006157
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006158#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006159static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006160 SmallPtrSet<const SDNode*, 32> &Visited,
6161 SmallPtrSet<const SDNode*, 32> &Checked) {
6162 // If this node has already been checked, don't check it again.
6163 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006164 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006165
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006166 // If a node has already been visited on this depth-first walk, reject it as
6167 // a cycle.
6168 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006169 dbgs() << "Offending node:\n";
6170 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006171 errs() << "Detected cycle in SelectionDAG\n";
6172 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006173 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006174
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006175 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6176 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006177
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006178 Checked.insert(N);
6179 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006180}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006181#endif
David Greenecf495bc2010-01-20 20:13:31 +00006182
6183void llvm::checkForCycles(const llvm::SDNode *N) {
6184#ifdef XDEBUG
6185 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006186 SmallPtrSet<const SDNode*, 32> visited;
6187 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006188 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006189#endif
6190}
6191
6192void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6193 checkForCycles(DAG->getRoot().getNode());
6194}