blob: f4fe8927f69669544e90e53b5ae4d3c869eda1c2 [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 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000442 case ISD::TargetIndex: {
443 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
444 ID.AddInteger(TI->getIndex());
445 ID.AddInteger(TI->getOffset());
446 ID.AddInteger(TI->getTargetFlags());
447 break;
448 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000449 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000450 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000451 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000452 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000453 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000454 break;
455 }
456 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000457 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000458 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000459 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000460 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000461 break;
462 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000463 case ISD::ATOMIC_CMP_SWAP:
464 case ISD::ATOMIC_SWAP:
465 case ISD::ATOMIC_LOAD_ADD:
466 case ISD::ATOMIC_LOAD_SUB:
467 case ISD::ATOMIC_LOAD_AND:
468 case ISD::ATOMIC_LOAD_OR:
469 case ISD::ATOMIC_LOAD_XOR:
470 case ISD::ATOMIC_LOAD_NAND:
471 case ISD::ATOMIC_LOAD_MIN:
472 case ISD::ATOMIC_LOAD_MAX:
473 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000474 case ISD::ATOMIC_LOAD_UMAX:
475 case ISD::ATOMIC_LOAD:
476 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000477 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000478 ID.AddInteger(AT->getMemoryVT().getRawBits());
479 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000480 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
481 break;
482 }
483 case ISD::PREFETCH: {
484 const MemSDNode *PF = cast<MemSDNode>(N);
485 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000486 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000487 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000488 case ISD::VECTOR_SHUFFLE: {
489 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000490 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000491 i != e; ++i)
492 ID.AddInteger(SVN->getMaskElt(i));
493 break;
494 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000495 case ISD::TargetBlockAddress:
496 case ISD::BlockAddress: {
Dan Gohman29cbade2009-11-20 23:18:13 +0000497 ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
498 ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000499 break;
500 }
Mon P Wang28873102008-06-25 08:15:39 +0000501 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000502
503 // Target specific memory nodes could also have address spaces to check.
504 if (N->isTargetMemoryOpcode())
505 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000506}
507
Duncan Sands0dc40452008-10-27 15:30:53 +0000508/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
509/// data.
510static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
511 AddNodeIDOpcode(ID, N->getOpcode());
512 // Add the return value info.
513 AddNodeIDValueTypes(ID, N->getVTList());
514 // Add the operand info.
515 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
516
517 // Handle SDNode leafs with special info.
518 AddNodeIDCustom(ID, N);
519}
520
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000521/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000522/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000523/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000524///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000525static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000526encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000527 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000528 assert((ConvType & 3) == ConvType &&
529 "ConvType may not require more than 2 bits!");
530 assert((AM & 7) == AM &&
531 "AM may not require more than 3 bits!");
532 return ConvType |
533 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000534 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000535 (isNonTemporal << 6) |
536 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000537}
538
Jim Laskey583bd472006-10-27 23:46:08 +0000539//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000540// SelectionDAG Class
541//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000542
Duncan Sands0dc40452008-10-27 15:30:53 +0000543/// doNotCSE - Return true if CSE should not be performed for this node.
544static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000545 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000546 return true; // Never CSE anything that produces a flag.
547
548 switch (N->getOpcode()) {
549 default: break;
550 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000551 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000552 return true; // Never CSE these nodes.
553 }
554
555 // Check that remaining values produced are not flags.
556 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000557 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000558 return true; // Never CSE anything that produces a flag.
559
560 return false;
561}
562
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000563/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000564/// SelectionDAG.
565void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000566 // Create a dummy node (which is not added to allnodes), that adds a reference
567 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000568 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000569
Chris Lattner190a4182006-08-04 17:45:20 +0000570 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000571
Chris Lattner190a4182006-08-04 17:45:20 +0000572 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000573 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000574 if (I->use_empty())
575 DeadNodes.push_back(I);
576
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000577 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000578
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000579 // If the root changed (e.g. it was a dead load, update the root).
580 setRoot(Dummy.getValue());
581}
582
583/// RemoveDeadNodes - This method deletes the unreachable nodes in the
584/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000585void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000586
Chris Lattner190a4182006-08-04 17:45:20 +0000587 // Process the worklist, deleting the nodes and adding their uses to the
588 // worklist.
589 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000590 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000591
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000592 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
593 DUL->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000594
Chris Lattner190a4182006-08-04 17:45:20 +0000595 // Take the node out of the appropriate CSE map.
596 RemoveNodeFromCSEMaps(N);
597
598 // Next, brutally remove the operand list. This is safe to do, as there are
599 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000600 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
601 SDUse &Use = *I++;
602 SDNode *Operand = Use.getNode();
603 Use.set(SDValue());
604
Chris Lattner190a4182006-08-04 17:45:20 +0000605 // Now that we removed this operand, see if there are no uses of it left.
606 if (Operand->use_empty())
607 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000608 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000609
Dan Gohmanc5336122009-01-19 22:39:36 +0000610 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000611 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000612}
613
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000614void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000615 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000616
617 // Create a dummy node that adds a reference to the root node, preventing
618 // it from being deleted. (This matters if the root is an operand of the
619 // dead node.)
620 HandleSDNode Dummy(getRoot());
621
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000622 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000623}
624
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000625void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000626 // First take this out of the appropriate CSE map.
627 RemoveNodeFromCSEMaps(N);
628
Scott Michelfdc40a02009-02-17 22:15:04 +0000629 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000630 // AllNodes list, and delete the node.
631 DeleteNodeNotInCSEMaps(N);
632}
633
634void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000635 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
636 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000637
Dan Gohmanf06c8352008-09-30 18:30:35 +0000638 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000639 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000640
Dan Gohmanc5336122009-01-19 22:39:36 +0000641 DeallocateNode(N);
642}
643
644void SelectionDAG::DeallocateNode(SDNode *N) {
645 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000646 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000647
Dan Gohmanc5336122009-01-19 22:39:36 +0000648 // Set the opcode to DELETED_NODE to help catch bugs when node
649 // memory is reallocated.
650 N->NodeType = ISD::DELETED_NODE;
651
Dan Gohman11467282008-08-26 01:44:34 +0000652 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000653
654 // Remove the ordering of this node.
Bill Wendling187361b2010-01-23 10:26:57 +0000655 Ordering->remove(N);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000656
Evan Chengbfcb3052010-03-25 01:38:16 +0000657 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000658 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000659 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
660 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000661}
662
Chris Lattner149c58c2005-08-16 18:17:10 +0000663/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
664/// correspond to it. This is useful when we're about to delete or repurpose
665/// the node. We don't want future request for structurally identical nodes
666/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000667bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000668 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000669 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000670 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000671 case ISD::CONDCODE:
672 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
673 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000674 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000675 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
676 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000677 case ISD::ExternalSymbol:
678 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000679 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000680 case ISD::TargetExternalSymbol: {
681 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
682 Erased = TargetExternalSymbols.erase(
683 std::pair<std::string,unsigned char>(ESN->getSymbol(),
684 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000685 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000686 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000687 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000688 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000689 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000690 Erased = ExtendedValueTypeNodes.erase(VT);
691 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
693 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000694 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000695 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000696 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000697 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000698 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000699 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
700 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000701 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000702 break;
703 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000704#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000705 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000706 // flag result (which cannot be CSE'd) or is one of the special cases that are
707 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000708 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000709 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000710 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000711 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000712 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000713 }
714#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000715 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000716}
717
Dan Gohman39946102009-01-25 16:29:12 +0000718/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
719/// maps and modified in place. Add it back to the CSE maps, unless an identical
720/// node already exists, in which case transfer all its users to the existing
721/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000722///
Dan Gohman39946102009-01-25 16:29:12 +0000723void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000724SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000725 // For node types that aren't CSE'd, just act as if no identical node
726 // already exists.
727 if (!doNotCSE(N)) {
728 SDNode *Existing = CSEMap.GetOrInsertNode(N);
729 if (Existing != N) {
730 // If there was already an existing matching node, use ReplaceAllUsesWith
731 // to replace the dead one with the existing one. This can cause
732 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000733 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000734
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000735 // N is now dead. Inform the listeners and delete it.
736 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
737 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000738 DeleteNodeNotInCSEMaps(N);
739 return;
740 }
741 }
Evan Cheng71e86852008-07-08 20:06:39 +0000742
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000743 // If the node doesn't already exist, we updated it. Inform listeners.
744 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
745 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000746}
747
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000748/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000749/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000750/// return null, otherwise return a pointer to the slot it would take. If a
751/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000752SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000753 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000754 if (doNotCSE(N))
755 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000756
Dan Gohman475871a2008-07-27 21:46:04 +0000757 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000758 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000759 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000760 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000761 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000762 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000763}
764
765/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000766/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000767/// return null, otherwise return a pointer to the slot it would take. If a
768/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000769SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000770 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000771 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000772 if (doNotCSE(N))
773 return 0;
774
Dan Gohman475871a2008-07-27 21:46:04 +0000775 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000776 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000777 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000778 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000779 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000780 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000781}
782
783
784/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000785/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000786/// return null, otherwise return a pointer to the slot it would take. If a
787/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000788SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000789 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000790 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000791 if (doNotCSE(N))
792 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000793
Jim Laskey583bd472006-10-27 23:46:08 +0000794 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000795 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000796 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000797 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000798 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000799}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000800
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000801#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000802/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
803static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000804 switch (N->getOpcode()) {
805 default:
806 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000807 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000808 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000809 assert(N->getNumValues() == 1 && "Too many results!");
810 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
811 "Wrong return type!");
812 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
813 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
814 "Mismatched operand types!");
815 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
816 "Wrong operand type!");
817 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
818 "Wrong return type size");
819 break;
820 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000821 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000822 assert(N->getNumValues() == 1 && "Too many results!");
823 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000824 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000825 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000826 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000827 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000828 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000829 (EltVT.isInteger() && I->getValueType().isInteger() &&
830 EltVT.bitsLE(I->getValueType()))) &&
831 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000832 assert(I->getValueType() == N->getOperand(0).getValueType() &&
833 "Operands must all have the same type");
834 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000835 break;
836 }
837 }
838}
839
Duncan Sands59d2dad2010-11-20 11:25:00 +0000840/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
841static void VerifySDNode(SDNode *N) {
842 // The SDNode allocators cannot be used to allocate nodes with fields that are
843 // not present in an SDNode!
844 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
845 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
846 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
847 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
848 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
849 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
850 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
851 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
852 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
853 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
854 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
855 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
856 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
857 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
858 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
859 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
860 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
861 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
862 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
863
864 VerifyNodeCommon(N);
865}
866
867/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
868/// invalid.
869static void VerifyMachineNode(SDNode *N) {
870 // The MachineNode allocators cannot be used to allocate nodes with fields
871 // that are not present in a MachineNode!
872 // Currently there are no such nodes.
873
874 VerifyNodeCommon(N);
875}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000876#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000877
Owen Andersone50ed302009-08-10 22:56:29 +0000878/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000879/// given type.
880///
Owen Andersone50ed302009-08-10 22:56:29 +0000881unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000882 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000883 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000884 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000885
886 return TLI.getTargetData()->getABITypeAlignment(Ty);
887}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000888
Dale Johannesen92570c42009-02-07 02:15:05 +0000889// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000890SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Dan Gohmanff7a5622010-05-11 17:31:57 +0000891 : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()),
Devang Patel0508d042011-12-15 18:21:18 +0000892 OptLevel(OL), EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000893 Root(getEntryNode()), Ordering(0), UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000894 AllNodes.push_back(&EntryNode);
Bill Wendling187361b2010-01-23 10:26:57 +0000895 Ordering = new SDNodeOrdering();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000896 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000897}
898
Chris Lattnerde6e7832010-04-05 06:10:13 +0000899void SelectionDAG::init(MachineFunction &mf) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000900 MF = &mf;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000901 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000902}
903
Chris Lattner78ec3112003-08-11 14:57:33 +0000904SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000905 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000906 allnodes_clear();
Daniel Dunbar819309e2009-12-16 20:10:05 +0000907 delete Ordering;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000908 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000909}
910
911void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000912 assert(&*AllNodes.begin() == &EntryNode);
913 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000914 while (!AllNodes.empty())
915 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000916}
917
Dan Gohman7c3234c2008-08-27 23:52:12 +0000918void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000919 allnodes_clear();
920 OperandAllocator.Reset();
921 CSEMap.clear();
922
923 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000924 ExternalSymbols.clear();
925 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000926 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
927 static_cast<CondCodeSDNode*>(0));
928 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
929 static_cast<SDNode*>(0));
930
Dan Gohmane7852d02009-01-26 04:35:06 +0000931 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000932 AllNodes.push_back(&EntryNode);
933 Root = getEntryNode();
Dan Gohman27445f02010-06-18 15:40:58 +0000934 Ordering->clear();
Evan Cheng31441b72010-03-29 20:48:30 +0000935 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000936}
937
Nadav Rotema3c42f32011-09-27 11:16:47 +0000938SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
939 return VT.bitsGT(Op.getValueType()) ?
940 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
941 getNode(ISD::TRUNCATE, DL, VT, Op);
942}
943
Duncan Sands3a66a682009-10-13 21:04:12 +0000944SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
945 return VT.bitsGT(Op.getValueType()) ?
946 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
947 getNode(ISD::TRUNCATE, DL, VT, Op);
948}
949
950SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
951 return VT.bitsGT(Op.getValueType()) ?
952 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
953 getNode(ISD::TRUNCATE, DL, VT, Op);
954}
955
Owen Andersone50ed302009-08-10 22:56:29 +0000956SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000957 assert(!VT.isVector() &&
958 "getZeroExtendInReg should use the vector element type instead of "
959 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000960 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000961 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
962 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000963 VT.getSizeInBits());
964 return getNode(ISD::AND, DL, Op.getValueType(), Op,
965 getConstant(Imm, Op.getValueType()));
966}
967
Bob Wilson4c245462009-01-22 17:39:32 +0000968/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
969///
Owen Andersone50ed302009-08-10 22:56:29 +0000970SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000971 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000972 SDValue NegOne =
973 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000974 return getNode(ISD::XOR, DL, VT, Val, NegOne);
975}
976
Owen Andersone50ed302009-08-10 22:56:29 +0000977SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000978 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000979 assert((EltVT.getSizeInBits() >= 64 ||
980 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
981 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000982 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000983}
984
Owen Andersone50ed302009-08-10 22:56:29 +0000985SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000986 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000987}
988
Owen Andersone50ed302009-08-10 22:56:29 +0000989SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000990 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000991
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000992 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000993 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000994
Duncan Sands28b77e92011-09-06 19:07:46 +0000995 // In some cases the vector type is legal but the element type is illegal and
996 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
997 // inserted value (the type does not need to match the vector element type).
998 // Any extra bits introduced will be truncated away.
999 if (VT.isVector() && TLI.getTypeAction(*getContext(), EltVT) ==
1000 TargetLowering::TypePromoteInteger) {
1001 EltVT = TLI.getTypeToTransformTo(*getContext(), EltVT);
1002 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1003 Elt = ConstantInt::get(*getContext(), NewVal);
1004 }
1005
1006 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1007 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001008 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001009 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001010 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +00001011 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +00001012 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +00001013 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001014 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001015 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001016 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001017
Dan Gohman89081322007-12-12 22:21:26 +00001018 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001019 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001020 CSEMap.InsertNode(N, IP);
1021 AllNodes.push_back(N);
1022 }
1023
Dan Gohman475871a2008-07-27 21:46:04 +00001024 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001025 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001026 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001027 Ops.assign(VT.getVectorNumElements(), Result);
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001028 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001029 }
1030 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001031}
1032
Dan Gohman475871a2008-07-27 21:46:04 +00001033SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +00001034 return getConstant(Val, TLI.getPointerTy(), isTarget);
1035}
1036
1037
Owen Andersone50ed302009-08-10 22:56:29 +00001038SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001039 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001040}
1041
Owen Andersone50ed302009-08-10 22:56:29 +00001042SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001043 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001044
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001045 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001046
Chris Lattnerd8658612005-02-17 20:17:32 +00001047 // Do the map lookup using the actual bit pattern for the floating point
1048 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1049 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001050 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001051 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001052 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001053 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001054 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001055 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001056 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001057 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001058 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001059
Evan Chengc908dcd2007-06-29 21:36:04 +00001060 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001061 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001062 CSEMap.InsertNode(N, IP);
1063 AllNodes.push_back(N);
1064 }
1065
Dan Gohman475871a2008-07-27 21:46:04 +00001066 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001067 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001068 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001069 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +00001070 // FIXME DebugLoc info might be appropriate here
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001071 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001072 }
1073 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074}
1075
Owen Andersone50ed302009-08-10 22:56:29 +00001076SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001077 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001079 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001080 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001081 return getConstantFP(APFloat(Val), VT, isTarget);
Owen Andersond7484e52012-04-05 18:50:32 +00001082 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001083 bool ignored;
1084 APFloat apf = APFloat(Val);
1085 apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1086 &ignored);
1087 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001088 } else
1089 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001090}
1091
Devang Patel0d881da2010-07-06 22:08:15 +00001092SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001093 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001094 bool isTargetGA,
1095 unsigned char TargetFlags) {
1096 assert((TargetFlags == 0 || isTargetGA) &&
1097 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001098
Dan Gohman6520e202008-10-18 02:06:02 +00001099 // Truncate (with sign-extension) the offset value to the pointer size.
Owen Andersone50ed302009-08-10 22:56:29 +00001100 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001101 unsigned BitWidth = PTy.getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +00001102 if (BitWidth < 64)
1103 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
1104
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001105 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1106 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001107 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001108 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001109 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001110 }
1111
Chris Lattner2a4ed822009-06-25 21:21:14 +00001112 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001113 if (GVar && GVar->isThreadLocal())
1114 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1115 else
1116 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001117
Jim Laskey583bd472006-10-27 23:46:08 +00001118 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001119 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001120 ID.AddPointer(GV);
1121 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001122 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001123 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001124 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001125 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001126 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001127
Devang Patel0d881da2010-07-06 22:08:15 +00001128 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001129 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001130 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001132 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133}
1134
Owen Andersone50ed302009-08-10 22:56:29 +00001135SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001136 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001137 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001138 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001139 ID.AddInteger(FI);
1140 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001141 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001142 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001143
Dan Gohman95531882010-03-18 18:49:47 +00001144 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001145 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001146 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001147 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001148}
1149
Owen Andersone50ed302009-08-10 22:56:29 +00001150SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001151 unsigned char TargetFlags) {
1152 assert((TargetFlags == 0 || isTarget) &&
1153 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001154 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001155 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001156 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001157 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001158 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001159 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001160 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001161 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001162
Dan Gohman95531882010-03-18 18:49:47 +00001163 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1164 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001165 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001166 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001167 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001168}
1169
Dan Gohman46510a72010-04-15 01:51:59 +00001170SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001171 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001172 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001173 unsigned char TargetFlags) {
1174 assert((TargetFlags == 0 || isTarget) &&
1175 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001176 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001177 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001178 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001179 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001180 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001181 ID.AddInteger(Alignment);
1182 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001183 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001184 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001185 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001186 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001187 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001188
Dan Gohman95531882010-03-18 18:49:47 +00001189 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1190 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001191 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001192 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001193 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001194}
1195
Chris Lattnerc3aae252005-01-07 07:46:32 +00001196
Owen Andersone50ed302009-08-10 22:56:29 +00001197SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001198 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001199 bool isTarget,
1200 unsigned char TargetFlags) {
1201 assert((TargetFlags == 0 || isTarget) &&
1202 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001203 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001204 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001205 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001206 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001207 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001208 ID.AddInteger(Alignment);
1209 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001210 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001211 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001212 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001213 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001214 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001215
Dan Gohman95531882010-03-18 18:49:47 +00001216 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1217 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001218 CSEMap.InsertNode(N, IP);
1219 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001220 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001221}
1222
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001223SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1224 unsigned char TargetFlags) {
1225 FoldingSetNodeID ID;
1226 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1227 ID.AddInteger(Index);
1228 ID.AddInteger(Offset);
1229 ID.AddInteger(TargetFlags);
1230 void *IP = 0;
1231 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1232 return SDValue(E, 0);
1233
1234 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1235 TargetFlags);
1236 CSEMap.InsertNode(N, IP);
1237 AllNodes.push_back(N);
1238 return SDValue(N, 0);
1239}
1240
Dan Gohman475871a2008-07-27 21:46:04 +00001241SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001242 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001244 ID.AddPointer(MBB);
1245 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001246 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001247 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001248
Dan Gohman95531882010-03-18 18:49:47 +00001249 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001250 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001251 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001252 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001253}
1254
Owen Andersone50ed302009-08-10 22:56:29 +00001255SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1257 ValueTypeNodes.size())
1258 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001259
Duncan Sands83ec4b62008-06-06 12:08:01 +00001260 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001262
Dan Gohman475871a2008-07-27 21:46:04 +00001263 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001264 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001265 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001266 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001267}
1268
Owen Andersone50ed302009-08-10 22:56:29 +00001269SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001270 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001271 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001272 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001273 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001274 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001275}
1276
Owen Andersone50ed302009-08-10 22:56:29 +00001277SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001278 unsigned char TargetFlags) {
1279 SDNode *&N =
1280 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1281 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001282 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001283 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001284 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001285 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001286}
1287
Dan Gohman475871a2008-07-27 21:46:04 +00001288SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001289 if ((unsigned)Cond >= CondCodeNodes.size())
1290 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001291
Chris Lattner079a27a2005-08-09 20:40:02 +00001292 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001293 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001294 CondCodeNodes[Cond] = N;
1295 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001296 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001297
Dan Gohman475871a2008-07-27 21:46:04 +00001298 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001299}
1300
Nate Begeman5a5ca152009-04-29 05:20:52 +00001301// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1302// the shuffle mask M that point at N1 to point at N2, and indices that point
1303// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001304static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1305 std::swap(N1, N2);
1306 int NElts = M.size();
1307 for (int i = 0; i != NElts; ++i) {
1308 if (M[i] >= NElts)
1309 M[i] -= NElts;
1310 else if (M[i] >= 0)
1311 M[i] += NElts;
1312 }
1313}
1314
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001315SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001316 SDValue N2, const int *Mask) {
1317 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001318 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001319 "Vector Shuffle VTs must be a vectors");
1320 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1321 && "Vector Shuffle VTs must have same element type");
1322
1323 // Canonicalize shuffle undef, undef -> undef
1324 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001325 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001326
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001327 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001328 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001329 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001330 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001331 for (unsigned i = 0; i != NElts; ++i) {
1332 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001333 MaskVec.push_back(Mask[i]);
1334 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001335
Nate Begeman9008ca62009-04-27 18:41:29 +00001336 // Canonicalize shuffle v, v -> v, undef
1337 if (N1 == N2) {
1338 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001339 for (unsigned i = 0; i != NElts; ++i)
1340 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001341 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001342
Nate Begeman9008ca62009-04-27 18:41:29 +00001343 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1344 if (N1.getOpcode() == ISD::UNDEF)
1345 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001346
Nate Begeman9008ca62009-04-27 18:41:29 +00001347 // Canonicalize all index into lhs, -> shuffle lhs, undef
1348 // Canonicalize all index into rhs, -> shuffle rhs, undef
1349 bool AllLHS = true, AllRHS = true;
1350 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001351 for (unsigned i = 0; i != NElts; ++i) {
1352 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001353 if (N2Undef)
1354 MaskVec[i] = -1;
1355 else
1356 AllLHS = false;
1357 } else if (MaskVec[i] >= 0) {
1358 AllRHS = false;
1359 }
1360 }
1361 if (AllLHS && AllRHS)
1362 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001363 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001364 N2 = getUNDEF(VT);
1365 if (AllRHS) {
1366 N1 = getUNDEF(VT);
1367 commuteShuffle(N1, N2, MaskVec);
1368 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001369
Nate Begeman9008ca62009-04-27 18:41:29 +00001370 // If Identity shuffle, or all shuffle in to undef, return that node.
1371 bool AllUndef = true;
1372 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001373 for (unsigned i = 0; i != NElts; ++i) {
1374 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001375 if (MaskVec[i] >= 0) AllUndef = false;
1376 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001377 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001378 return N1;
1379 if (AllUndef)
1380 return getUNDEF(VT);
1381
1382 FoldingSetNodeID ID;
1383 SDValue Ops[2] = { N1, N2 };
1384 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001385 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001386 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001387
Nate Begeman9008ca62009-04-27 18:41:29 +00001388 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001390 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001391
Nate Begeman9008ca62009-04-27 18:41:29 +00001392 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1393 // SDNode doesn't have access to it. This memory will be "leaked" when
1394 // the node is deallocated, but recovered when the NodeAllocator is released.
1395 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1396 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001397
Dan Gohman95531882010-03-18 18:49:47 +00001398 ShuffleVectorSDNode *N =
1399 new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001400 CSEMap.InsertNode(N, IP);
1401 AllNodes.push_back(N);
1402 return SDValue(N, 0);
1403}
1404
Owen Andersone50ed302009-08-10 22:56:29 +00001405SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001406 SDValue Val, SDValue DTy,
1407 SDValue STy, SDValue Rnd, SDValue Sat,
1408 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001409 // If the src and dest types are the same and the conversion is between
1410 // integer types of the same sign or two floats, no conversion is necessary.
1411 if (DTy == STy &&
1412 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001413 return Val;
1414
1415 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001416 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1417 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001418 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001419 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001420 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001421
Dan Gohman95531882010-03-18 18:49:47 +00001422 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
1423 Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001424 CSEMap.InsertNode(N, IP);
1425 AllNodes.push_back(N);
1426 return SDValue(N, 0);
1427}
1428
Owen Andersone50ed302009-08-10 22:56:29 +00001429SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001430 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001431 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001432 ID.AddInteger(RegNo);
1433 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001435 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001436
Dan Gohman95531882010-03-18 18:49:47 +00001437 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001438 CSEMap.InsertNode(N, IP);
1439 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001440 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001441}
1442
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001443SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1444 FoldingSetNodeID ID;
1445 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1446 ID.AddPointer(RegMask);
1447 void *IP = 0;
1448 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1449 return SDValue(E, 0);
1450
1451 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1452 CSEMap.InsertNode(N, IP);
1453 AllNodes.push_back(N);
1454 return SDValue(N, 0);
1455}
1456
Chris Lattner7561d482010-03-14 02:33:54 +00001457SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001458 FoldingSetNodeID ID;
1459 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001460 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1461 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001462 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001463 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001464 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001465
Dan Gohman95531882010-03-18 18:49:47 +00001466 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001467 CSEMap.InsertNode(N, IP);
1468 AllNodes.push_back(N);
1469 return SDValue(N, 0);
1470}
1471
Chris Lattner7561d482010-03-14 02:33:54 +00001472
Dan Gohman46510a72010-04-15 01:51:59 +00001473SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Dan Gohman29cbade2009-11-20 23:18:13 +00001474 bool isTarget,
1475 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001476 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1477
1478 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001479 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001480 ID.AddPointer(BA);
Dan Gohman29cbade2009-11-20 23:18:13 +00001481 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001482 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001483 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001484 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001485
Dan Gohman95531882010-03-18 18:49:47 +00001486 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001487 CSEMap.InsertNode(N, IP);
1488 AllNodes.push_back(N);
1489 return SDValue(N, 0);
1490}
1491
Dan Gohman475871a2008-07-27 21:46:04 +00001492SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001493 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001494 "SrcValue is not a pointer?");
1495
Jim Laskey583bd472006-10-27 23:46:08 +00001496 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001497 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001498 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001499
Chris Lattner61b09412006-08-11 21:01:22 +00001500 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001501 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001502 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001503
Dan Gohman95531882010-03-18 18:49:47 +00001504 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001505 CSEMap.InsertNode(N, IP);
1506 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001507 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001508}
1509
Chris Lattnerdecc2672010-04-07 05:20:54 +00001510/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1511SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1512 FoldingSetNodeID ID;
1513 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1514 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001515
Chris Lattnerdecc2672010-04-07 05:20:54 +00001516 void *IP = 0;
1517 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1518 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001519
Chris Lattnerdecc2672010-04-07 05:20:54 +00001520 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1521 CSEMap.InsertNode(N, IP);
1522 AllNodes.push_back(N);
1523 return SDValue(N, 0);
1524}
1525
1526
Duncan Sands92abc622009-01-31 15:50:11 +00001527/// getShiftAmountOperand - Return the specified value casted to
1528/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001529SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001530 EVT OpTy = Op.getValueType();
Owen Anderson6154f6c2011-03-07 18:29:47 +00001531 MVT ShTy = TLI.getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001532 if (OpTy == ShTy || OpTy.isVector()) return Op;
1533
1534 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001535 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001536}
1537
Chris Lattner37ce9df2007-10-15 17:47:20 +00001538/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1539/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001540SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001541 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001542 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001543 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001544 unsigned StackAlign =
1545 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001546
David Greene3f2bf852009-11-12 20:49:22 +00001547 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner37ce9df2007-10-15 17:47:20 +00001548 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1549}
1550
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001551/// CreateStackTemporary - Create a stack temporary suitable for holding
1552/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001553SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001554 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1555 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001556 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1557 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001558 const TargetData *TD = TLI.getTargetData();
1559 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1560 TD->getPrefTypeAlignment(Ty2));
1561
1562 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001563 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001564 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1565}
1566
Owen Andersone50ed302009-08-10 22:56:29 +00001567SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001568 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001569 // These setcc operations always fold.
1570 switch (Cond) {
1571 default: break;
1572 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001573 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001574 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001575 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Chris Lattnera83385f2006-04-27 05:01:07 +00001577 case ISD::SETOEQ:
1578 case ISD::SETOGT:
1579 case ISD::SETOGE:
1580 case ISD::SETOLT:
1581 case ISD::SETOLE:
1582 case ISD::SETONE:
1583 case ISD::SETO:
1584 case ISD::SETUO:
1585 case ISD::SETUEQ:
1586 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001587 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001588 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001590
Gabor Greifba36cb52008-08-28 21:40:38 +00001591 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001592 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001593 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001594 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001595
Chris Lattnerc3aae252005-01-07 07:46:32 +00001596 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001597 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001598 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1599 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001600 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1601 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1602 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1603 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1604 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1605 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1606 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1607 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001608 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001609 }
Chris Lattner67255a12005-04-07 18:14:58 +00001610 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001611 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1612 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001613 // No compile time operations on this type yet.
Owen Anderson825b72b2009-08-11 20:47:22 +00001614 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001615 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001616
1617 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001618 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001619 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001620 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001621 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001622 // fall through
1623 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001624 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001625 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001626 // fall through
1627 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001628 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001629 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001630 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001631 // fall through
1632 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001633 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001634 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001635 // fall through
1636 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001637 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001638 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001639 // fall through
1640 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001641 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001642 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001643 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001644 // fall through
1645 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001646 R==APFloat::cmpEqual, VT);
1647 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1648 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1649 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1650 R==APFloat::cmpEqual, VT);
1651 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1652 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1653 R==APFloat::cmpLessThan, VT);
1654 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1655 R==APFloat::cmpUnordered, VT);
1656 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1657 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001658 }
1659 } else {
1660 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001661 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001662 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001663 }
1664
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001665 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001666 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001667}
1668
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001669/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1670/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001671bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001672 // This predicate is not safe for vector operations.
1673 if (Op.getValueType().isVector())
1674 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001675
Dan Gohman87862e72009-12-11 21:31:27 +00001676 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001677 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1678}
1679
Dan Gohmanea859be2007-06-22 14:59:07 +00001680/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1681/// this predicate to simplify operations downstream. Mask is known to be zero
1682/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001683bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001684 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001685 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001686 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001687 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001688 return (KnownZero & Mask) == Mask;
1689}
1690
1691/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1692/// known to be either zero or one and return them in the KnownZero/KnownOne
1693/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1694/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001695void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1696 APInt &KnownOne, unsigned Depth) const {
1697 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001698
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001699 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001700 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001701 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001702
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001703 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001704
1705 switch (Op.getOpcode()) {
1706 case ISD::Constant:
1707 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001708 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1709 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001710 return;
1711 case ISD::AND:
1712 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001713 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1714 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001715 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1716 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001717
1718 // Output known-1 bits are only known if set in both the LHS & RHS.
1719 KnownOne &= KnownOne2;
1720 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1721 KnownZero |= KnownZero2;
1722 return;
1723 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001724 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1725 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001726 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1727 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1728
Dan Gohmanea859be2007-06-22 14:59:07 +00001729 // Output known-0 bits are only known if clear in both the LHS & RHS.
1730 KnownZero &= KnownZero2;
1731 // Output known-1 are known to be set if set in either the LHS | RHS.
1732 KnownOne |= KnownOne2;
1733 return;
1734 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001735 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1736 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001737 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1738 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1739
Dan Gohmanea859be2007-06-22 14:59:07 +00001740 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001741 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001742 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1743 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1744 KnownZero = KnownZeroOut;
1745 return;
1746 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001747 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001748 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1749 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001750 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1751 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1752
1753 // If low bits are zero in either operand, output low known-0 bits.
1754 // Also compute a conserative estimate for high known-0 bits.
1755 // More trickiness is possible, but this is sufficient for the
1756 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001757 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001758 unsigned TrailZ = KnownZero.countTrailingOnes() +
1759 KnownZero2.countTrailingOnes();
1760 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001761 KnownZero2.countLeadingOnes(),
1762 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001763
1764 TrailZ = std::min(TrailZ, BitWidth);
1765 LeadZ = std::min(LeadZ, BitWidth);
1766 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1767 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001768 return;
1769 }
1770 case ISD::UDIV: {
1771 // For the purposes of computing leading zeros we can conservatively
1772 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001773 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001774 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001775 unsigned LeadZ = KnownZero2.countLeadingOnes();
1776
Jay Foad7a874dd2010-12-01 08:53:58 +00001777 KnownOne2.clearAllBits();
1778 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001779 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001780 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1781 if (RHSUnknownLeadingOnes != BitWidth)
1782 LeadZ = std::min(BitWidth,
1783 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001784
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001785 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001786 return;
1787 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001788 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001789 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1790 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001791 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1792 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1793
Dan Gohmanea859be2007-06-22 14:59:07 +00001794 // Only known if known in both the LHS and RHS.
1795 KnownOne &= KnownOne2;
1796 KnownZero &= KnownZero2;
1797 return;
1798 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001799 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1800 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001801 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1802 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1803
Dan Gohmanea859be2007-06-22 14:59:07 +00001804 // Only known if known in both the LHS and RHS.
1805 KnownOne &= KnownOne2;
1806 KnownZero &= KnownZero2;
1807 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001808 case ISD::SADDO:
1809 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001810 case ISD::SSUBO:
1811 case ISD::USUBO:
1812 case ISD::SMULO:
1813 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001814 if (Op.getResNo() != 1)
1815 return;
Duncan Sands03228082008-11-23 15:47:28 +00001816 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001817 case ISD::SETCC:
1818 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands28b77e92011-09-06 19:07:46 +00001819 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
1820 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001821 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001822 return;
1823 case ISD::SHL:
1824 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1825 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001826 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001827
1828 // If the shift count is an invalid immediate, don't do anything.
1829 if (ShAmt >= BitWidth)
1830 return;
1831
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001832 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001833 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001834 KnownZero <<= ShAmt;
1835 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001836 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001837 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001838 }
1839 return;
1840 case ISD::SRL:
1841 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1842 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001843 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001844
Dan Gohmand4cf9922008-02-26 18:50:50 +00001845 // If the shift count is an invalid immediate, don't do anything.
1846 if (ShAmt >= BitWidth)
1847 return;
1848
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001849 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001850 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001851 KnownZero = KnownZero.lshr(ShAmt);
1852 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001853
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001854 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001855 KnownZero |= HighBits; // High bits known zero.
1856 }
1857 return;
1858 case ISD::SRA:
1859 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001860 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001861
Dan Gohmand4cf9922008-02-26 18:50:50 +00001862 // If the shift count is an invalid immediate, don't do anything.
1863 if (ShAmt >= BitWidth)
1864 return;
1865
Dan Gohmanea859be2007-06-22 14:59:07 +00001866 // If any of the demanded bits are produced by the sign extension, we also
1867 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001868 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001869
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001870 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001871 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001872 KnownZero = KnownZero.lshr(ShAmt);
1873 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001874
Dan Gohmanea859be2007-06-22 14:59:07 +00001875 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001876 APInt SignBit = APInt::getSignBit(BitWidth);
1877 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001878
Dan Gohmanca93a432008-02-20 16:30:17 +00001879 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001880 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001881 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001882 KnownOne |= HighBits; // New bits are known one.
1883 }
1884 }
1885 return;
1886 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001887 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001888 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001889
1890 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001891 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001892 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001893
Dan Gohman977a76f2008-02-13 22:28:48 +00001894 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001895 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001896
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 // If the sign extended bits are demanded, we know that the sign
1898 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001899 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001900 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001902
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001903 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1904 KnownOne &= InputDemandedBits;
1905 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001906 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1907
Dan Gohmanea859be2007-06-22 14:59:07 +00001908 // If the sign bit of the input is known set or clear, then we know the
1909 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001910 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 KnownZero |= NewBits;
1912 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001913 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 KnownOne |= NewBits;
1915 KnownZero &= ~NewBits;
1916 } else { // Input sign bit unknown
1917 KnownZero &= ~NewBits;
1918 KnownOne &= ~NewBits;
1919 }
1920 return;
1921 }
1922 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001923 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001924 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001925 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001926 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001927 unsigned LowBits = Log2_32(BitWidth)+1;
1928 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001929 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001930 return;
1931 }
1932 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001933 LoadSDNode *LD = cast<LoadSDNode>(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00001934 if (ISD::isZEXTLoad(Op.getNode())) {
Owen Andersone50ed302009-08-10 22:56:29 +00001935 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001936 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001937 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00001938 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001939 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001940 }
1941 return;
1942 }
1943 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001944 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001945 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001946 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00001947 KnownZero = KnownZero.trunc(InBits);
1948 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001949 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001950 KnownZero = KnownZero.zext(BitWidth);
1951 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001952 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001953 return;
1954 }
1955 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001956 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001957 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001958 APInt InSignBit = APInt::getSignBit(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001959 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001960
Jay Foad40f8f622010-12-07 08:25:19 +00001961 KnownZero = KnownZero.trunc(InBits);
1962 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001963 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001964
1965 // Note if the sign bit is known to be zero or one.
1966 bool SignBitKnownZero = KnownZero.isNegative();
1967 bool SignBitKnownOne = KnownOne.isNegative();
1968 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1969 "Sign bit can't be known to be both zero and one!");
1970
Jay Foad40f8f622010-12-07 08:25:19 +00001971 KnownZero = KnownZero.zext(BitWidth);
1972 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001973
Dan Gohman977a76f2008-02-13 22:28:48 +00001974 // If the sign bit is known zero or one, the top bits match.
1975 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001976 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001977 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001978 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001979 return;
1980 }
1981 case ISD::ANY_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001982 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001983 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001984 KnownZero = KnownZero.trunc(InBits);
1985 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001986 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001987 KnownZero = KnownZero.zext(BitWidth);
1988 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001989 return;
1990 }
1991 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001992 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001993 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001994 KnownZero = KnownZero.zext(InBits);
1995 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001996 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001997 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001998 KnownZero = KnownZero.trunc(BitWidth);
1999 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002000 break;
2001 }
2002 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002003 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002004 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002005 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2006 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002007 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 return;
2009 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002010 case ISD::FGETSIGN:
2011 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002012 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00002013 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00002014
Dan Gohman23e8b712008-04-28 17:02:21 +00002015 case ISD::SUB: {
2016 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2017 // We know that the top bits of C-X are clear if X contains less bits
2018 // than C (i.e. no wrap-around can happen). For example, 20-X is
2019 // positive if we can prove that X is >= 0 and < 16.
2020 if (CLHS->getAPIntValue().isNonNegative()) {
2021 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2022 // NLZ can't be BitWidth with no sign bit
2023 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002024 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002025
2026 // If all of the MaskV bits are known to be zero, then we know the
2027 // output top bits are zero, because we now know that the output is
2028 // from [0-C].
2029 if ((KnownZero2 & MaskV) == MaskV) {
2030 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2031 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002032 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002033 }
2034 }
2035 }
2036 }
2037 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002038 case ISD::ADD:
2039 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002040 // Output known-0 bits are known if clear or set in both the low clear bits
2041 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2042 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002043 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002044 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002045 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2046
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002047 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002048 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002049 KnownZeroOut = std::min(KnownZeroOut,
2050 KnownZero2.countTrailingOnes());
2051
Chris Lattnerda605882010-12-19 20:38:28 +00002052 if (Op.getOpcode() == ISD::ADD) {
2053 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2054 return;
2055 }
2056
2057 // With ADDE, a carry bit may be added in, so we can only use this
2058 // information if we know (at least) that the low two bits are clear. We
2059 // then return to the caller that the low bit is unknown but that other bits
2060 // are known zero.
2061 if (KnownZeroOut >= 2) // ADDE
2062 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002063 return;
2064 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002065 case ISD::SREM:
2066 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002067 const APInt &RA = Rem->getAPIntValue().abs();
2068 if (RA.isPowerOf2()) {
2069 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002070 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002071 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002072
Duncan Sands5c2873a2010-01-29 09:45:26 +00002073 // The low bits of the first operand are unchanged by the srem.
2074 KnownZero = KnownZero2 & LowBits;
2075 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002076
Duncan Sands5c2873a2010-01-29 09:45:26 +00002077 // If the first operand is non-negative or has all low bits zero, then
2078 // the upper bits are all zero.
2079 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2080 KnownZero |= ~LowBits;
2081
2082 // If the first operand is negative and not all low bits are zero, then
2083 // the upper bits are all one.
2084 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2085 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002086 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002087 }
2088 }
2089 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002090 case ISD::UREM: {
2091 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002092 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002093 if (RA.isPowerOf2()) {
2094 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002095 KnownZero |= ~LowBits;
2096 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002097 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2098 break;
2099 }
2100 }
2101
2102 // Since the result is less than or equal to either operand, any leading
2103 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002104 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2105 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002106
2107 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2108 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002109 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002110 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002111 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002112 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002113 case ISD::FrameIndex:
2114 case ISD::TargetFrameIndex:
2115 if (unsigned Align = InferPtrAlignment(Op)) {
2116 // The low bits are known zero if the pointer is aligned.
2117 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2118 return;
2119 }
2120 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002121
Dan Gohmanea859be2007-06-22 14:59:07 +00002122 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002123 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2124 break;
2125 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002126 case ISD::INTRINSIC_WO_CHAIN:
2127 case ISD::INTRINSIC_W_CHAIN:
2128 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002129 // Allow the target to implement this method for its nodes.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002130 TLI.computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002131 return;
2132 }
2133}
2134
2135/// ComputeNumSignBits - Return the number of times the sign bit of the
2136/// register is replicated into the other bits. We know that at least 1 bit
2137/// is always equal to the sign bit (itself), but other cases can give us
2138/// information. For example, immediately after an "SRA X, 2", we know that
2139/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002140unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002141 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002142 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002143 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002144 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002145 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002146
Dan Gohmanea859be2007-06-22 14:59:07 +00002147 if (Depth == 6)
2148 return 1; // Limit search depth.
2149
2150 switch (Op.getOpcode()) {
2151 default: break;
2152 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002153 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002154 return VTBits-Tmp+1;
2155 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002156 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002157 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002158
Dan Gohmanea859be2007-06-22 14:59:07 +00002159 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002160 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002161 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002162 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002163
Dan Gohmanea859be2007-06-22 14:59:07 +00002164 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002165 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002166 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002167
Dan Gohmanea859be2007-06-22 14:59:07 +00002168 case ISD::SIGN_EXTEND_INREG:
2169 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002170 Tmp =
2171 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002172 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002173
Dan Gohmanea859be2007-06-22 14:59:07 +00002174 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2175 return std::max(Tmp, Tmp2);
2176
2177 case ISD::SRA:
2178 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2179 // SRA X, C -> adds C sign bits.
2180 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002181 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002182 if (Tmp > VTBits) Tmp = VTBits;
2183 }
2184 return Tmp;
2185 case ISD::SHL:
2186 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2187 // shl destroys sign bits.
2188 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002189 if (C->getZExtValue() >= VTBits || // Bad shift.
2190 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2191 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002192 }
2193 break;
2194 case ISD::AND:
2195 case ISD::OR:
2196 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002197 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002198 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002199 if (Tmp != 1) {
2200 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2201 FirstAnswer = std::min(Tmp, Tmp2);
2202 // We computed what we know about the sign bits as our first
2203 // answer. Now proceed to the generic code that uses
2204 // ComputeMaskedBits, and pick whichever answer is better.
2205 }
2206 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002207
2208 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002209 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002210 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002211 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002212 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002213
2214 case ISD::SADDO:
2215 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002216 case ISD::SSUBO:
2217 case ISD::USUBO:
2218 case ISD::SMULO:
2219 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002220 if (Op.getResNo() != 1)
2221 break;
Duncan Sands03228082008-11-23 15:47:28 +00002222 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002223 case ISD::SETCC:
2224 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002225 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002226 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002227 return VTBits;
2228 break;
2229 case ISD::ROTL:
2230 case ISD::ROTR:
2231 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002232 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002233
Dan Gohmanea859be2007-06-22 14:59:07 +00002234 // Handle rotate right by N like a rotate left by 32-N.
2235 if (Op.getOpcode() == ISD::ROTR)
2236 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2237
2238 // If we aren't rotating out all of the known-in sign bits, return the
2239 // number that are left. This handles rotl(sext(x), 1) for example.
2240 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2241 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2242 }
2243 break;
2244 case ISD::ADD:
2245 // Add can have at most one carry bit. Thus we know that the output
2246 // is, at worst, one more bit than the inputs.
2247 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2248 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002249
Dan Gohmanea859be2007-06-22 14:59:07 +00002250 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002251 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002252 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002253 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002254 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002255
Dan Gohmanea859be2007-06-22 14:59:07 +00002256 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2257 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002258 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002259 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002260
Dan Gohmanea859be2007-06-22 14:59:07 +00002261 // If we are subtracting one from a positive number, there is no carry
2262 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002263 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002264 return Tmp;
2265 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002266
Dan Gohmanea859be2007-06-22 14:59:07 +00002267 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2268 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002269 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002270
Dan Gohmanea859be2007-06-22 14:59:07 +00002271 case ISD::SUB:
2272 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2273 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002274
Dan Gohmanea859be2007-06-22 14:59:07 +00002275 // Handle NEG.
2276 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002277 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002278 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002279 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002280 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2281 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002282 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002283 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002284
Dan Gohmanea859be2007-06-22 14:59:07 +00002285 // If the input is known to be positive (the sign bit is known clear),
2286 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002287 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002288 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002289
Dan Gohmanea859be2007-06-22 14:59:07 +00002290 // Otherwise, we treat this like a SUB.
2291 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002292
Dan Gohmanea859be2007-06-22 14:59:07 +00002293 // Sub can have at most one carry bit. Thus we know that the output
2294 // is, at worst, one more bit than the inputs.
2295 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2296 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002297 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002298 case ISD::TRUNCATE:
2299 // FIXME: it's tricky to do anything useful for this, but it is an important
2300 // case for targets like X86.
2301 break;
2302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002303
Dan Gohmanea859be2007-06-22 14:59:07 +00002304 // Handle LOADX separately here. EXTLOAD case will fallthrough.
Jakub Staszakce00b442012-05-01 23:06:00 +00002305 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002306 unsigned ExtType = LD->getExtensionType();
2307 switch (ExtType) {
2308 default: break;
2309 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002310 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002311 return VTBits-Tmp+1;
2312 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002313 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002314 return VTBits-Tmp;
2315 }
2316 }
2317
2318 // Allow the target to implement this method for its nodes.
2319 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002320 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002321 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2322 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2323 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002324 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002325 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002326
Dan Gohmanea859be2007-06-22 14:59:07 +00002327 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2328 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002329 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002330 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002331
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002332 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002333 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002334 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002335 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002336 Mask = KnownOne;
2337 } else {
2338 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002339 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002341
Dan Gohmanea859be2007-06-22 14:59:07 +00002342 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2343 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002344 Mask = ~Mask;
2345 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002346 // Return # leading zeros. We use 'min' here in case Val was zero before
2347 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002348 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002349}
2350
Chris Lattner0a9481f2011-02-13 22:25:43 +00002351/// isBaseWithConstantOffset - Return true if the specified operand is an
2352/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2353/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2354/// semantics as an ADD. This handles the equivalence:
Chris Lattner463b3c22011-02-14 06:14:42 +00002355/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002356bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2357 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2358 !isa<ConstantSDNode>(Op.getOperand(1)))
2359 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002360
2361 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002362 !MaskedValueIsZero(Op.getOperand(0),
2363 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2364 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002365
Chris Lattner0a9481f2011-02-13 22:25:43 +00002366 return true;
2367}
2368
2369
Dan Gohman8d44b282009-09-03 20:34:31 +00002370bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2371 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002372 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002373 return true;
2374
2375 // If the value is a constant, we can obviously see if it is a NaN or not.
2376 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2377 return !C->getValueAPF().isNaN();
2378
2379 // TODO: Recognize more cases here.
2380
2381 return false;
2382}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002383
Dan Gohmane8326932010-02-24 06:52:40 +00002384bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2385 // If the value is a constant, we can obviously see if it is a zero or not.
2386 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2387 return !C->isZero();
2388
2389 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002390 switch (Op.getOpcode()) {
2391 default: break;
2392 case ISD::OR:
2393 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2394 return !C->isNullValue();
2395 break;
2396 }
Dan Gohmane8326932010-02-24 06:52:40 +00002397
2398 return false;
2399}
2400
2401bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2402 // Check the obvious case.
2403 if (A == B) return true;
2404
2405 // For for negative and positive zero.
2406 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2407 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2408 if (CA->isZero() && CB->isZero()) return true;
2409
2410 // Otherwise they may not be equal.
2411 return false;
2412}
2413
Chris Lattnerc3aae252005-01-07 07:46:32 +00002414/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002415///
Owen Andersone50ed302009-08-10 22:56:29 +00002416SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002417 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002418 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002419 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002420 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002421 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002422
Dan Gohman95531882010-03-18 18:49:47 +00002423 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002424 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002425
Chris Lattner4a283e92006-08-11 18:38:11 +00002426 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002427#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002428 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002429#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002430 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002431}
2432
Bill Wendling7ade28c2009-01-28 22:17:52 +00002433SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002434 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002435 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002436 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002437 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002438 switch (Opcode) {
2439 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002440 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002441 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002442 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002443 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002444 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002445 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002446 case ISD::UINT_TO_FP:
2447 case ISD::SINT_TO_FP: {
Chris Lattner7fb5c2d2010-03-15 16:05:15 +00002448 // No compile time operations on ppcf128.
2449 if (VT == MVT::ppcf128) break;
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002450 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002451 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002452 Opcode==ISD::SINT_TO_FP,
2453 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002454 return getConstantFP(apf, VT);
2455 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002456 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002457 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002458 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002459 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002460 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002461 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002462 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002463 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002464 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002465 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002466 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002467 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002468 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002469 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002470 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002471 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002472 }
2473 }
2474
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002475 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002477 APFloat V = C->getValueAPF(); // make copy
Owen Anderson825b72b2009-08-11 20:47:22 +00002478 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002479 switch (Opcode) {
2480 case ISD::FNEG:
2481 V.changeSign();
2482 return getConstantFP(V, VT);
2483 case ISD::FABS:
2484 V.clearSign();
2485 return getConstantFP(V, VT);
Owen Anderson7c626d32012-08-13 23:32:49 +00002486 case ISD::FCEIL: {
2487 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2488 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2489 return getConstantFP(V, VT);
2490 break;
2491 }
2492 case ISD::FTRUNC: {
2493 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2494 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2495 return getConstantFP(V, VT);
2496 break;
2497 }
2498 case ISD::FFLOOR: {
2499 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2500 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2501 return getConstantFP(V, VT);
2502 break;
2503 }
Dale Johannesen23a98552008-10-09 23:00:39 +00002504 case ISD::FP_EXTEND: {
2505 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002506 // This can return overflow, underflow, or inexact; we don't care.
2507 // FIXME need to be more flexible about rounding mode.
Owen Andersone50ed302009-08-10 22:56:29 +00002508 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002509 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002510 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002511 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002512 case ISD::FP_TO_SINT:
2513 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002514 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002515 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002516 assert(integerPartWidth >= 64);
2517 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002518 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002519 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002520 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002521 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2522 break;
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00002523 APInt api(VT.getSizeInBits(), x);
Dale Johannesen2f91f302009-04-24 21:34:13 +00002524 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002525 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002526 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002527 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002528 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002529 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002530 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002531 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002532 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002533 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002534 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002535
Gabor Greifba36cb52008-08-28 21:40:38 +00002536 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002537 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002538 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002539 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002540 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002541 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002542 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002543 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 assert(VT.isFloatingPoint() &&
2545 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002546 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002547 assert((!VT.isVector() ||
2548 VT.getVectorNumElements() ==
2549 Operand.getValueType().getVectorNumElements()) &&
2550 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002551 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002552 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002553 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002554 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002555 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002556 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002557 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002558 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2559 "Invalid sext node, dst < src!");
2560 assert((!VT.isVector() ||
2561 VT.getVectorNumElements() ==
2562 Operand.getValueType().getVectorNumElements()) &&
2563 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002564 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002565 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002566 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002567 // sext(undef) = 0, because the top bits will all be the same.
2568 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002569 break;
2570 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002571 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002572 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002573 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002574 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2575 "Invalid zext node, dst < src!");
2576 assert((!VT.isVector() ||
2577 VT.getVectorNumElements() ==
2578 Operand.getValueType().getVectorNumElements()) &&
2579 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002580 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002581 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002582 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002583 else if (OpOpcode == ISD::UNDEF)
2584 // zext(undef) = 0, because the top bits will be zero.
2585 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002586 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002587 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002588 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002589 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002590 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002591 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2592 "Invalid anyext node, dst < src!");
2593 assert((!VT.isVector() ||
2594 VT.getVectorNumElements() ==
2595 Operand.getValueType().getVectorNumElements()) &&
2596 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002597
Dan Gohman9e86a732010-06-18 00:08:30 +00002598 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2599 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002600 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002601 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002602 else if (OpOpcode == ISD::UNDEF)
2603 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002604
2605 // (ext (trunx x)) -> x
2606 if (OpOpcode == ISD::TRUNCATE) {
2607 SDValue OpOp = Operand.getNode()->getOperand(0);
2608 if (OpOp.getValueType() == VT)
2609 return OpOp;
2610 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002611 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002612 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002613 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002614 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002615 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002616 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2617 "Invalid truncate node, src < dst!");
2618 assert((!VT.isVector() ||
2619 VT.getVectorNumElements() ==
2620 Operand.getValueType().getVectorNumElements()) &&
2621 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002622 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002623 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002624 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2625 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002626 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002627 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2628 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002629 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002630 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002631 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002632 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002633 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002634 if (OpOpcode == ISD::UNDEF)
2635 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002636 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002637 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002638 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002639 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002640 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002641 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002642 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2643 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002644 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002645 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002646 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002647 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002648 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002649 (VT.getVectorElementType() == Operand.getValueType() ||
2650 (VT.getVectorElementType().isInteger() &&
2651 Operand.getValueType().isInteger() &&
2652 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002653 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002654 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002655 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002656 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2657 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2658 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2659 Operand.getConstantOperandVal(1) == 0 &&
2660 Operand.getOperand(0).getValueType() == VT)
2661 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002662 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002663 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002664 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002665 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002666 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002667 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002668 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002669 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002670 break;
2671 case ISD::FABS:
2672 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002673 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002674 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002675 }
2676
Chris Lattner43247a12005-08-25 19:12:10 +00002677 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002678 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002679 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002680 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002681 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002682 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002683 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002684 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002685 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002686
Dan Gohman95531882010-03-18 18:49:47 +00002687 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002688 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002689 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002690 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002691 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002692
Chris Lattnerc3aae252005-01-07 07:46:32 +00002693 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002694#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002695 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002696#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002697 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002698}
2699
Bill Wendling688d1c42008-09-24 10:16:24 +00002700SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002701 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002702 ConstantSDNode *Cst1,
2703 ConstantSDNode *Cst2) {
2704 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2705
2706 switch (Opcode) {
2707 case ISD::ADD: return getConstant(C1 + C2, VT);
2708 case ISD::SUB: return getConstant(C1 - C2, VT);
2709 case ISD::MUL: return getConstant(C1 * C2, VT);
2710 case ISD::UDIV:
2711 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2712 break;
2713 case ISD::UREM:
2714 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2715 break;
2716 case ISD::SDIV:
2717 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2718 break;
2719 case ISD::SREM:
2720 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2721 break;
2722 case ISD::AND: return getConstant(C1 & C2, VT);
2723 case ISD::OR: return getConstant(C1 | C2, VT);
2724 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2725 case ISD::SHL: return getConstant(C1 << C2, VT);
2726 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2727 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2728 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2729 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2730 default: break;
2731 }
2732
2733 return SDValue();
2734}
2735
Owen Andersone50ed302009-08-10 22:56:29 +00002736SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002737 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002738 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2739 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002740 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002741 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002742 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002743 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2744 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002745 // Fold trivial token factors.
2746 if (N1.getOpcode() == ISD::EntryToken) return N2;
2747 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002748 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002749 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002750 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002751 // Concat of UNDEFs is UNDEF.
2752 if (N1.getOpcode() == ISD::UNDEF &&
2753 N2.getOpcode() == ISD::UNDEF)
2754 return getUNDEF(VT);
2755
Dan Gohman7f8613e2008-08-14 20:04:46 +00002756 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2757 // one big BUILD_VECTOR.
2758 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2759 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002760 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2761 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002762 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002763 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002764 }
2765 break;
Chris Lattner76365122005-01-16 02:23:22 +00002766 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002767 assert(VT.isInteger() && "This operator does not apply to FP types!");
2768 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002769 N1.getValueType() == VT && "Binary operator types must match!");
2770 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2771 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002772 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002773 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002774 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2775 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002776 break;
Chris Lattner76365122005-01-16 02:23:22 +00002777 case ISD::OR:
2778 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002779 case ISD::ADD:
2780 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002781 assert(VT.isInteger() && "This operator does not apply to FP types!");
2782 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002783 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002784 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2785 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002786 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002787 return N1;
2788 break;
Chris Lattner76365122005-01-16 02:23:22 +00002789 case ISD::UDIV:
2790 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002791 case ISD::MULHU:
2792 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002793 case ISD::MUL:
2794 case ISD::SDIV:
2795 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002796 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002797 assert(N1.getValueType() == N2.getValueType() &&
2798 N1.getValueType() == VT && "Binary operator types must match!");
2799 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002800 case ISD::FADD:
2801 case ISD::FSUB:
2802 case ISD::FMUL:
2803 case ISD::FDIV:
2804 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002805 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002806 if (Opcode == ISD::FADD) {
2807 // 0+x --> x
2808 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2809 if (CFP->getValueAPF().isZero())
2810 return N2;
2811 // x+0 --> x
2812 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2813 if (CFP->getValueAPF().isZero())
2814 return N1;
2815 } else if (Opcode == ISD::FSUB) {
2816 // x-0 --> x
2817 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2818 if (CFP->getValueAPF().isZero())
2819 return N1;
2820 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002821 }
Dale Johannesen78995512010-05-15 18:38:02 +00002822 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002823 assert(N1.getValueType() == N2.getValueType() &&
2824 N1.getValueType() == VT && "Binary operator types must match!");
2825 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002826 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2827 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002828 N1.getValueType().isFloatingPoint() &&
2829 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002830 "Invalid FCOPYSIGN!");
2831 break;
Chris Lattner76365122005-01-16 02:23:22 +00002832 case ISD::SHL:
2833 case ISD::SRA:
2834 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002835 case ISD::ROTL:
2836 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002837 assert(VT == N1.getValueType() &&
2838 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002839 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002840 "Shifts only work on integers");
Chris Lattnere0751182011-02-13 19:09:16 +00002841 // Verify that the shift amount VT is bit enough to hold valid shift
2842 // amounts. This catches things like trying to shift an i1024 value by an
2843 // i8, which is easy to fall into in generic code that uses
2844 // TLI.getShiftAmount().
2845 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002846 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002847 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002848
2849 // Always fold shifts of i1 values so the code generator doesn't need to
2850 // handle them. Since we know the size of the shift has to be less than the
2851 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002852 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002853 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002854 if (N2C && N2C->isNullValue())
2855 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002856 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002857 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002858 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002859 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002860 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002861 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002862 assert(EVT.isVector() == VT.isVector() &&
2863 "FP_ROUND_INREG type should be vector iff the operand "
2864 "type is vector!");
2865 assert((!EVT.isVector() ||
2866 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2867 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002868 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002869 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002870 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002871 break;
2872 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002873 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002874 assert(VT.isFloatingPoint() &&
2875 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002876 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002877 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002878 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002879 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002880 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002881 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002882 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002883 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002884 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002885 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002886 assert(!EVT.isVector() &&
2887 "AssertSExt/AssertZExt type should be the vector element type "
2888 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002889 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002890 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002891 break;
2892 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002893 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002894 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002895 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002896 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002897 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002898 assert(EVT.isVector() == VT.isVector() &&
2899 "SIGN_EXTEND_INREG type should be vector iff the operand "
2900 "type is vector!");
2901 assert((!EVT.isVector() ||
2902 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2903 "Vector element counts must match in SIGN_EXTEND_INREG");
2904 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002905 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002906
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002907 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002908 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002909 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002910 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002911 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002912 return getConstant(Val, VT);
2913 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002914 break;
2915 }
2916 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002917 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2918 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002919 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002920
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002921 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2922 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002923 if (N2C &&
2924 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002925 N1.getNumOperands() > 0) {
2926 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002927 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002928 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002929 N1.getOperand(N2C->getZExtValue() / Factor),
2930 getConstant(N2C->getZExtValue() % Factor,
2931 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002932 }
2933
2934 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2935 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002936 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2937 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersone50ed302009-08-10 22:56:29 +00002938 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanc680ac92009-07-09 22:01:03 +00002939 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002940 // If the vector element type is not legal, the BUILD_VECTOR operands
2941 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002942 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2943 }
2944 if (VT != VEltTy) {
2945 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2946 // result is implicitly extended.
2947 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002948 }
2949 return Elt;
2950 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002951
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002952 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2953 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002954 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00002955 // If the indices are the same, return the inserted element else
2956 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00002957 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00002958 SDValue N1Op2 = N1.getOperand(2);
2959 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
2960
2961 if (N1Op2C && N2C) {
2962 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
2963 if (VT == N1.getOperand(1).getValueType())
2964 return N1.getOperand(1);
2965 else
2966 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2967 }
2968
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002969 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00002970 }
Dan Gohman6ab64222008-08-13 21:51:37 +00002971 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002972 break;
2973 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002974 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002975 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2976 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00002977 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00002978 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002979
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002980 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2981 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002982 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002983 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002984 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002985
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002986 // EXTRACT_ELEMENT of a constant int is also very common.
2987 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002988 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002989 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002990 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2991 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002992 }
2993 break;
David Greene91585092011-01-26 15:38:49 +00002994 case ISD::EXTRACT_SUBVECTOR: {
2995 SDValue Index = N2;
2996 if (VT.isSimple() && N1.getValueType().isSimple()) {
2997 assert(VT.isVector() && N1.getValueType().isVector() &&
2998 "Extract subvector VTs must be a vectors!");
2999 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
3000 "Extract subvector VTs must have the same element type!");
3001 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3002 "Extract subvector must be from larger vector to smaller vector!");
3003
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003004 if (isa<ConstantSDNode>(Index.getNode())) {
3005 assert((VT.getVectorNumElements() +
3006 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003007 <= N1.getValueType().getVectorNumElements())
3008 && "Extract subvector overflow!");
3009 }
3010
3011 // Trivial extraction.
3012 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
3013 return N1;
3014 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003015 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003016 }
David Greene91585092011-01-26 15:38:49 +00003017 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003018
3019 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003020 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00003021 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
3022 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003023 } else { // Cannonicalize constant to RHS if commutative
3024 if (isCommutativeBinOp(Opcode)) {
3025 std::swap(N1C, N2C);
3026 std::swap(N1, N2);
3027 }
3028 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003029 }
3030
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003031 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003032 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3033 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003034 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003035 if (!N2CFP && isCommutativeBinOp(Opcode)) {
3036 // Cannonicalize constant to RHS if commutative
3037 std::swap(N1CFP, N2CFP);
3038 std::swap(N1, N2);
Owen Anderson825b72b2009-08-11 20:47:22 +00003039 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003040 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3041 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003042 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003043 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003044 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003045 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003046 return getConstantFP(V1, VT);
3047 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003048 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003049 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3050 if (s!=APFloat::opInvalidOp)
3051 return getConstantFP(V1, VT);
3052 break;
3053 case ISD::FMUL:
3054 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3055 if (s!=APFloat::opInvalidOp)
3056 return getConstantFP(V1, VT);
3057 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003058 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003059 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3060 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3061 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003062 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003063 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003064 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3065 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3066 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003067 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003068 case ISD::FCOPYSIGN:
3069 V1.copySign(V2);
3070 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003071 default: break;
3072 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003073 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003074
3075 if (Opcode == ISD::FP_ROUND) {
3076 APFloat V = N1CFP->getValueAPF(); // make copy
3077 bool ignored;
3078 // This can return overflow, underflow, or inexact; we don't care.
3079 // FIXME need to be more flexible about rounding mode.
3080 (void)V.convert(*EVTToAPFloatSemantics(VT),
3081 APFloat::rmNearestTiesToEven, &ignored);
3082 return getConstantFP(V, VT);
3083 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003085
Chris Lattner62b57722006-04-20 05:39:12 +00003086 // Canonicalize an UNDEF to the RHS, even over a constant.
3087 if (N1.getOpcode() == ISD::UNDEF) {
3088 if (isCommutativeBinOp(Opcode)) {
3089 std::swap(N1, N2);
3090 } else {
3091 switch (Opcode) {
3092 case ISD::FP_ROUND_INREG:
3093 case ISD::SIGN_EXTEND_INREG:
3094 case ISD::SUB:
3095 case ISD::FSUB:
3096 case ISD::FDIV:
3097 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003098 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003099 return N1; // fold op(undef, arg2) -> undef
3100 case ISD::UDIV:
3101 case ISD::SDIV:
3102 case ISD::UREM:
3103 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003104 case ISD::SRL:
3105 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003106 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003107 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3108 // For vectors, we can't easily build an all zero vector, just return
3109 // the LHS.
3110 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003111 }
3112 }
3113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003114
3115 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003116 if (N2.getOpcode() == ISD::UNDEF) {
3117 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003118 case ISD::XOR:
3119 if (N1.getOpcode() == ISD::UNDEF)
3120 // Handle undef ^ undef -> 0 special case. This is a common
3121 // idiom (misuse).
3122 return getConstant(0, VT);
3123 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003124 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003125 case ISD::ADDC:
3126 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003127 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003128 case ISD::UDIV:
3129 case ISD::SDIV:
3130 case ISD::UREM:
3131 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003132 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003133 case ISD::FADD:
3134 case ISD::FSUB:
3135 case ISD::FMUL:
3136 case ISD::FDIV:
3137 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003138 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003139 return N2;
3140 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003141 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003142 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003143 case ISD::SRL:
3144 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003145 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003146 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3147 // For vectors, we can't easily build an all zero vector, just return
3148 // the LHS.
3149 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003150 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003152 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003153 // For vectors, we can't easily build an all one vector, just return
3154 // the LHS.
3155 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003156 case ISD::SRA:
3157 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003158 }
3159 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003160
Chris Lattner27e9b412005-05-11 18:57:39 +00003161 // Memoize this node if possible.
3162 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003163 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003164 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003165 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003166 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003167 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003168 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003169 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003170 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003171
Dan Gohman95531882010-03-18 18:49:47 +00003172 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003173 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003174 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003175 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003176 }
3177
Chris Lattnerc3aae252005-01-07 07:46:32 +00003178 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003179#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003180 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003181#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003182 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003183}
3184
Owen Andersone50ed302009-08-10 22:56:29 +00003185SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003186 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003187 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003188 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003189 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003190 case ISD::CONCAT_VECTORS:
3191 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3192 // one big BUILD_VECTOR.
3193 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3194 N2.getOpcode() == ISD::BUILD_VECTOR &&
3195 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003196 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3197 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003198 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3199 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003200 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003201 }
3202 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003203 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003204 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003205 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003206 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003207 break;
3208 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003209 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003210 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003211 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003212 return N2; // select true, X, Y -> X
3213 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003214 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003215
3216 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003217 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003218 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003219 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003220 case ISD::INSERT_SUBVECTOR: {
3221 SDValue Index = N3;
3222 if (VT.isSimple() && N1.getValueType().isSimple()
3223 && N2.getValueType().isSimple()) {
3224 assert(VT.isVector() && N1.getValueType().isVector() &&
3225 N2.getValueType().isVector() &&
3226 "Insert subvector VTs must be a vectors");
3227 assert(VT == N1.getValueType() &&
3228 "Dest and insert subvector source types must match!");
3229 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3230 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003231 if (isa<ConstantSDNode>(Index.getNode())) {
3232 assert((N2.getValueType().getVectorNumElements() +
3233 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003234 <= VT.getVectorNumElements())
3235 && "Insert subvector overflow!");
3236 }
3237
3238 // Trivial insertion.
3239 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3240 return N2;
3241 }
3242 break;
3243 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003244 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003245 // Fold bit_convert nodes from a type to themselves.
3246 if (N1.getValueType() == VT)
3247 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003248 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003249 }
3250
Chris Lattner43247a12005-08-25 19:12:10 +00003251 // Memoize node if it doesn't produce a flag.
3252 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003253 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003254 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003255 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003256 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003257 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003258 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003259 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003260 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003261
Dan Gohman95531882010-03-18 18:49:47 +00003262 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003263 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003264 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003265 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003266 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003267
Chris Lattnerc3aae252005-01-07 07:46:32 +00003268 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003269#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003270 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003271#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003272 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003273}
3274
Owen Andersone50ed302009-08-10 22:56:29 +00003275SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003276 SDValue N1, SDValue N2, SDValue N3,
3277 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003278 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003279 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003280}
3281
Owen Andersone50ed302009-08-10 22:56:29 +00003282SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003283 SDValue N1, SDValue N2, SDValue N3,
3284 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003285 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003286 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003287}
3288
Dan Gohman98ca4f22009-08-05 01:29:28 +00003289/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3290/// the incoming stack arguments to be loaded from the stack.
3291SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3292 SmallVector<SDValue, 8> ArgChains;
3293
3294 // Include the original chain at the beginning of the list. When this is
3295 // used by target LowerCall hooks, this helps legalize find the
3296 // CALLSEQ_BEGIN node.
3297 ArgChains.push_back(Chain);
3298
3299 // Add a chain value for each stack argument.
3300 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3301 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3302 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3303 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3304 if (FI->getIndex() < 0)
3305 ArgChains.push_back(SDValue(L, 1));
3306
3307 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003308 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003309 &ArgChains[0], ArgChains.size());
3310}
3311
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003312/// SplatByte - Distribute ByteVal over NumBits bits.
3313static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3314 APInt Val = APInt(NumBits, ByteVal);
3315 unsigned Shift = 8;
3316 for (unsigned i = NumBits; i > 8; i >>= 1) {
3317 Val = (Val << Shift) | Val;
3318 Shift <<= 1;
3319 }
3320 return Val;
3321}
3322
Dan Gohman707e0182008-04-12 04:36:06 +00003323/// getMemsetValue - Vectorized representation of the memset value
3324/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003325static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003326 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003327 assert(Value.getOpcode() != ISD::UNDEF);
3328
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003329 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003330 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003331 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003332 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003333 return DAG.getConstant(Val, VT);
3334 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003335 }
Evan Chengf0df0312008-05-15 08:39:06 +00003336
Dale Johannesen0f502f62009-02-03 22:26:09 +00003337 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003338 if (NumBits > 8) {
3339 // Use a multiplication with 0x010101... to extend the input to the
3340 // required length.
3341 APInt Magic = SplatByte(NumBits, 0x01);
3342 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003343 }
3344
3345 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003346}
3347
Dan Gohman707e0182008-04-12 04:36:06 +00003348/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3349/// used when a memcpy is turned into a memset when the source is a constant
3350/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003351static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003352 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003353 // Handle vector with all elements zero.
3354 if (Str.empty()) {
3355 if (VT.isInteger())
3356 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003357 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003358 return DAG.getConstantFP(0.0, VT);
3359 else if (VT.isVector()) {
3360 unsigned NumElts = VT.getVectorNumElements();
3361 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003362 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003363 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3364 EltVT, NumElts)));
3365 } else
3366 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003367 }
3368
Duncan Sands83ec4b62008-06-06 12:08:01 +00003369 assert(!VT.isVector() && "Can't handle vector type here!");
Chris Lattner18c7f802012-02-05 02:29:43 +00003370 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3371 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3372
Dan Gohman707e0182008-04-12 04:36:06 +00003373 uint64_t Val = 0;
Chris Lattner18c7f802012-02-05 02:29:43 +00003374 if (TLI.isLittleEndian()) {
3375 for (unsigned i = 0; i != NumBytes; ++i)
3376 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3377 } else {
3378 for (unsigned i = 0; i != NumBytes; ++i)
3379 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003380 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003381
Dan Gohman707e0182008-04-12 04:36:06 +00003382 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003383}
3384
Scott Michelfdc40a02009-02-17 22:15:04 +00003385/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003386///
Dan Gohman475871a2008-07-27 21:46:04 +00003387static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003388 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003389 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003390 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003391 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003392}
3393
Evan Chengf0df0312008-05-15 08:39:06 +00003394/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3395///
Chris Lattner18c7f802012-02-05 02:29:43 +00003396static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003397 unsigned SrcDelta = 0;
3398 GlobalAddressSDNode *G = NULL;
3399 if (Src.getOpcode() == ISD::GlobalAddress)
3400 G = cast<GlobalAddressSDNode>(Src);
3401 else if (Src.getOpcode() == ISD::ADD &&
3402 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3403 Src.getOperand(1).getOpcode() == ISD::Constant) {
3404 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003405 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003406 }
3407 if (!G)
3408 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003409
Chris Lattner18c7f802012-02-05 02:29:43 +00003410 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003411}
Dan Gohman707e0182008-04-12 04:36:06 +00003412
Evan Cheng255f20f2010-04-01 06:04:33 +00003413/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3414/// to replace the memset / memcpy. Return true if the number of memory ops
3415/// is below the threshold. It returns the types of the sequence of
3416/// memory ops to perform memset / memcpy by reference.
3417static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003418 unsigned Limit, uint64_t Size,
3419 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003420 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00003421 bool MemcpyStrSrc,
Evan Cheng255f20f2010-04-01 06:04:33 +00003422 SelectionDAG &DAG,
3423 const TargetLowering &TLI) {
3424 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3425 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003426 // If 'SrcAlign' is zero, that means the memory operation does not need to
3427 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3428 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3429 // is the specified alignment of the memory operation. If it is zero, that
3430 // means it's possible to change the alignment of the destination.
3431 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3432 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003433 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003434 IsZeroVal, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003435 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003436
Chris Lattneracee6472010-03-07 07:45:08 +00003437 if (VT == MVT::Other) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003438 if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003439 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003440 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003441 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003442 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003443 case 0: VT = MVT::i64; break;
3444 case 4: VT = MVT::i32; break;
3445 case 2: VT = MVT::i16; break;
3446 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003447 }
3448 }
3449
Owen Anderson766b5ef2009-08-11 21:59:30 +00003450 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003451 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003452 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003453 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003454
Duncan Sands8e4eb092008-06-08 20:54:56 +00003455 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003456 VT = LVT;
3457 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003458
Dan Gohman707e0182008-04-12 04:36:06 +00003459 unsigned NumMemOps = 0;
3460 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003461 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003462 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003463 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng255f20f2010-04-01 06:04:33 +00003464 if (VT.isVector() || VT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003466 while (!TLI.isTypeLegal(VT))
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003468 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003469 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003470 // This can result in a type that is not legal on the target, e.g.
3471 // 1 or 2 bytes on PPC.
Owen Anderson825b72b2009-08-11 20:47:22 +00003472 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003473 VTSize >>= 1;
3474 }
Dan Gohman707e0182008-04-12 04:36:06 +00003475 }
Dan Gohman707e0182008-04-12 04:36:06 +00003476
3477 if (++NumMemOps > Limit)
3478 return false;
3479 MemOps.push_back(VT);
3480 Size -= VTSize;
3481 }
3482
3483 return true;
3484}
3485
Dale Johannesen0f502f62009-02-03 22:26:09 +00003486static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003487 SDValue Chain, SDValue Dst,
3488 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003489 unsigned Align, bool isVol,
3490 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003491 MachinePointerInfo DstPtrInfo,
3492 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003493 // Turn a memcpy of undef to nop.
3494 if (Src.getOpcode() == ISD::UNDEF)
3495 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003496
Dan Gohman21323f32008-05-29 19:42:22 +00003497 // Expand memcpy to a series of load and store ops if the size operand falls
3498 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003499 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3500 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003501 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003502 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003503 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003504 MachineFunction &MF = DAG.getMachineFunction();
3505 MachineFrameInfo *MFI = MF.getFrameInfo();
3506 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003507 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3508 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3509 DstAlignCanChange = true;
3510 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3511 if (Align > SrcAlign)
3512 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003513 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003514 bool CopyFromStr = isMemSrcFromString(Src, Str);
3515 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003516 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003517
Evan Cheng94107ba2010-04-01 18:19:11 +00003518 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003519 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003520 (isZeroStr ? 0 : SrcAlign),
3521 true, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003522 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003523
Evan Cheng255f20f2010-04-01 06:04:33 +00003524 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003525 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003526 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3527 if (NewAlign > Align) {
3528 // Give the stack frame object a larger alignment if needed.
3529 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3530 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3531 Align = NewAlign;
3532 }
3533 }
Dan Gohman707e0182008-04-12 04:36:06 +00003534
Dan Gohman475871a2008-07-27 21:46:04 +00003535 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003536 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003537 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003538 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003539 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003541 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003542
Evan Cheng255f20f2010-04-01 06:04:33 +00003543 if (CopyFromStr &&
3544 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003545 // It's unlikely a store of a vector immediate can be done in a single
3546 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003547 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003548 // FIXME: Handle other cases where store of vector immediate is done in
3549 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003550 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Dale Johannesen0f502f62009-02-03 22:26:09 +00003551 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003552 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003553 DstPtrInfo.getWithOffset(DstOff), isVol,
3554 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003555 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003556 // The type might not be legal for the target. This should only happen
3557 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003558 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3559 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003560 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003561 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003562 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003563 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003564 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003565 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003566 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003567 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003568 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003569 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3570 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003571 }
3572 OutChains.push_back(Store);
3573 SrcOff += VTSize;
3574 DstOff += VTSize;
3575 }
3576
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003578 &OutChains[0], OutChains.size());
3579}
3580
Dale Johannesen0f502f62009-02-03 22:26:09 +00003581static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003582 SDValue Chain, SDValue Dst,
3583 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003584 unsigned Align, bool isVol,
3585 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003586 MachinePointerInfo DstPtrInfo,
3587 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003588 // Turn a memmove of undef to nop.
3589 if (Src.getOpcode() == ISD::UNDEF)
3590 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003591
3592 // Expand memmove to a series of load and store ops if the size operand falls
3593 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003594 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003595 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003596 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003597 MachineFunction &MF = DAG.getMachineFunction();
3598 MachineFrameInfo *MFI = MF.getFrameInfo();
3599 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003600 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3601 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3602 DstAlignCanChange = true;
3603 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3604 if (Align > SrcAlign)
3605 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003606 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003607
Evan Cheng94107ba2010-04-01 18:19:11 +00003608 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003609 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003610 SrcAlign, true, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003611 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003612
Evan Cheng255f20f2010-04-01 06:04:33 +00003613 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003614 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003615 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3616 if (NewAlign > Align) {
3617 // Give the stack frame object a larger alignment if needed.
3618 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3619 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3620 Align = NewAlign;
3621 }
3622 }
Dan Gohman21323f32008-05-29 19:42:22 +00003623
Evan Cheng255f20f2010-04-01 06:04:33 +00003624 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003625 SmallVector<SDValue, 8> LoadValues;
3626 SmallVector<SDValue, 8> LoadChains;
3627 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003628 unsigned NumMemOps = MemOps.size();
3629 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003630 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003631 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003632 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003633
Dale Johannesen0f502f62009-02-03 22:26:09 +00003634 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003635 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003636 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003637 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003638 LoadValues.push_back(Value);
3639 LoadChains.push_back(Value.getValue(1));
3640 SrcOff += VTSize;
3641 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003642 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003643 &LoadChains[0], LoadChains.size());
3644 OutChains.clear();
3645 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003646 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003647 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003648 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003649
Dale Johannesen0f502f62009-02-03 22:26:09 +00003650 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003651 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003652 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003653 OutChains.push_back(Store);
3654 DstOff += VTSize;
3655 }
3656
Owen Anderson825b72b2009-08-11 20:47:22 +00003657 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003658 &OutChains[0], OutChains.size());
3659}
3660
Dale Johannesen0f502f62009-02-03 22:26:09 +00003661static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003662 SDValue Chain, SDValue Dst,
3663 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003664 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003665 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003666 // Turn a memset of undef to nop.
3667 if (Src.getOpcode() == ISD::UNDEF)
3668 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003669
3670 // Expand memset to a series of load/store ops if the size operand
3671 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003672 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003673 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003674 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003675 MachineFunction &MF = DAG.getMachineFunction();
3676 MachineFrameInfo *MFI = MF.getFrameInfo();
3677 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003678 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3679 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3680 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003681 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003682 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003683 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003684 Size, (DstAlignCanChange ? 0 : Align), 0,
Lang Hames15701f82011-10-26 23:50:43 +00003685 IsZeroVal, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003686 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003687
Evan Cheng255f20f2010-04-01 06:04:33 +00003688 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003689 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003690 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3691 if (NewAlign > Align) {
3692 // Give the stack frame object a larger alignment if needed.
3693 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3694 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3695 Align = NewAlign;
3696 }
3697 }
3698
Dan Gohman475871a2008-07-27 21:46:04 +00003699 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003700 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003701 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003702
3703 // Find the largest store and generate the bit pattern for it.
3704 EVT LargestVT = MemOps[0];
3705 for (unsigned i = 1; i < NumMemOps; i++)
3706 if (MemOps[i].bitsGT(LargestVT))
3707 LargestVT = MemOps[i];
3708 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3709
Dan Gohman707e0182008-04-12 04:36:06 +00003710 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003711 EVT VT = MemOps[i];
Benjamin Kramer80220362011-01-02 19:57:05 +00003712
3713 // If this store is smaller than the largest store see whether we can get
3714 // the smaller value for free with a truncate.
3715 SDValue Value = MemSetValue;
3716 if (VT.bitsLT(LargestVT)) {
3717 if (!LargestVT.isVector() && !VT.isVector() &&
3718 TLI.isTruncateFree(LargestVT, VT))
3719 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3720 else
3721 Value = getMemsetValue(Src, VT, DAG, dl);
3722 }
3723 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003724 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003725 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003726 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003727 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003728 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003729 DstOff += VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003730 }
3731
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003733 &OutChains[0], OutChains.size());
3734}
3735
Dale Johannesen0f502f62009-02-03 22:26:09 +00003736SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003737 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003738 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003739 MachinePointerInfo DstPtrInfo,
3740 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003741
3742 // Check to see if we should lower the memcpy to loads and stores first.
3743 // For cases within the target-specified limits, this is the best choice.
3744 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3745 if (ConstantSize) {
3746 // Memcpy with size zero? Just return the original chain.
3747 if (ConstantSize->isNullValue())
3748 return Chain;
3749
Evan Cheng255f20f2010-04-01 06:04:33 +00003750 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3751 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003752 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003753 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003754 return Result;
3755 }
3756
3757 // Then check to see if we should lower the memcpy with target-specific
3758 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003759 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003760 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003761 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003762 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003763 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003764 return Result;
3765
3766 // If we really need inline code and the target declined to provide it,
3767 // use a (potentially long) sequence of loads and stores.
3768 if (AlwaysInline) {
3769 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003770 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003771 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003772 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003773 }
3774
Dan Gohman7b55d362010-04-05 20:24:08 +00003775 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3776 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3777 // respect volatile, so they may do things like read or write memory
3778 // beyond the given memory regions. But fixing this isn't easy, and most
3779 // people don't care.
3780
Dan Gohman707e0182008-04-12 04:36:06 +00003781 // Emit a library call.
3782 TargetLowering::ArgListTy Args;
3783 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003784 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003785 Entry.Node = Dst; Args.push_back(Entry);
3786 Entry.Node = Src; Args.push_back(Entry);
3787 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003788 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003789 TargetLowering::
3790 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003791 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003792 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3793 /*isTailCall=*/false,
3794 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003795 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003796 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003797 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003798 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3799
Dan Gohman707e0182008-04-12 04:36:06 +00003800 return CallResult.second;
3801}
3802
Dale Johannesen0f502f62009-02-03 22:26:09 +00003803SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003804 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003805 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003806 MachinePointerInfo DstPtrInfo,
3807 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003808
Dan Gohman21323f32008-05-29 19:42:22 +00003809 // Check to see if we should lower the memmove to loads and stores first.
3810 // For cases within the target-specified limits, this is the best choice.
3811 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3812 if (ConstantSize) {
3813 // Memmove with size zero? Just return the original chain.
3814 if (ConstantSize->isNullValue())
3815 return Chain;
3816
Dan Gohman475871a2008-07-27 21:46:04 +00003817 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003818 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003819 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003820 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003821 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003822 return Result;
3823 }
Dan Gohman707e0182008-04-12 04:36:06 +00003824
3825 // Then check to see if we should lower the memmove with target-specific
3826 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003827 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003828 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003829 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003830 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003831 return Result;
3832
Mon P Wang01f0e852010-04-06 08:27:51 +00003833 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3834 // not be safe. See memcpy above for more details.
3835
Dan Gohman707e0182008-04-12 04:36:06 +00003836 // Emit a library call.
3837 TargetLowering::ArgListTy Args;
3838 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003839 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003840 Entry.Node = Dst; Args.push_back(Entry);
3841 Entry.Node = Src; Args.push_back(Entry);
3842 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003843 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003844 TargetLowering::
3845 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003846 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003847 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3848 /*isTailCall=*/false,
3849 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003850 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003851 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003852 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003853 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3854
Dan Gohman707e0182008-04-12 04:36:06 +00003855 return CallResult.second;
3856}
3857
Dale Johannesen0f502f62009-02-03 22:26:09 +00003858SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003859 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003860 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003861 MachinePointerInfo DstPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003862
3863 // Check to see if we should lower the memset to stores first.
3864 // For cases within the target-specified limits, this is the best choice.
3865 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3866 if (ConstantSize) {
3867 // Memset with size zero? Just return the original chain.
3868 if (ConstantSize->isNullValue())
3869 return Chain;
3870
Mon P Wang20adc9d2010-04-04 03:10:48 +00003871 SDValue Result =
3872 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003873 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00003874
Gabor Greifba36cb52008-08-28 21:40:38 +00003875 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003876 return Result;
3877 }
3878
3879 // Then check to see if we should lower the memset with target-specific
3880 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003881 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003882 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003883 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003884 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003885 return Result;
3886
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003887 // Emit a library call.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003888 Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003889 TargetLowering::ArgListTy Args;
3890 TargetLowering::ArgListEntry Entry;
3891 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3892 Args.push_back(Entry);
3893 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003894 if (Src.getValueType().bitsGT(MVT::i32))
3895 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003896 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003897 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003898 Entry.Node = Src;
3899 Entry.Ty = Type::getInt32Ty(*getContext());
3900 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003901 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003902 Entry.Node = Size;
3903 Entry.Ty = IntPtrTy;
3904 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003905 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003906 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003907 TargetLowering::
3908 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003909 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003910 TLI.getLibcallCallingConv(RTLIB::MEMSET),
3911 /*isTailCall=*/false,
3912 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003913 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003914 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003915 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003916 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3917
Dan Gohman707e0182008-04-12 04:36:06 +00003918 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003919}
3920
Owen Andersone50ed302009-08-10 22:56:29 +00003921SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00003922 SDValue Chain, SDValue Ptr, SDValue Cmp,
3923 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00003924 unsigned Alignment,
3925 AtomicOrdering Ordering,
3926 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003927 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3928 Alignment = getEVTAlignment(MemVT);
3929
Evan Chengff89dcb2009-10-18 18:16:27 +00003930 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003931 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3932
3933 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003934 // FIXME: Volatile isn't really correct; we should keep track of atomic
3935 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003936 Flags |= MachineMemOperand::MOVolatile;
3937
3938 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00003939 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003940
Eli Friedman55ba8162011-07-29 03:05:32 +00003941 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
3942 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003943}
3944
3945SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3946 SDValue Chain,
3947 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00003948 SDValue Swp, MachineMemOperand *MMO,
3949 AtomicOrdering Ordering,
3950 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003951 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3952 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3953
Owen Andersone50ed302009-08-10 22:56:29 +00003954 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003955
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003957 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003958 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003959 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3960 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Pete Cooper32ecfb42012-07-30 20:23:19 +00003961 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00003962 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003963 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3964 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003965 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003966 }
Dan Gohman95531882010-03-18 18:49:47 +00003967 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003968 Ptr, Cmp, Swp, MMO, Ordering,
3969 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003970 CSEMap.InsertNode(N, IP);
3971 AllNodes.push_back(N);
3972 return SDValue(N, 0);
3973}
3974
Owen Andersone50ed302009-08-10 22:56:29 +00003975SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003976 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003977 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003978 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00003979 unsigned Alignment,
3980 AtomicOrdering Ordering,
3981 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003982 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3983 Alignment = getEVTAlignment(MemVT);
3984
Evan Chengff89dcb2009-10-18 18:16:27 +00003985 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00003986 // A monotonic store does not load; a release store "loads" in the sense
3987 // that other stores cannot be sunk past it.
3988 // (An atomicrmw obviously both loads and stores.)
3989 unsigned Flags = MachineMemOperand::MOStore;
3990 if (Opcode != ISD::ATOMIC_STORE || Ordering > Monotonic)
3991 Flags |= MachineMemOperand::MOLoad;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003992
3993 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003994 // FIXME: Volatile isn't really correct; we should keep track of atomic
3995 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003996 Flags |= MachineMemOperand::MOVolatile;
3997
3998 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00003999 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004000 MemVT.getStoreSize(), Alignment);
4001
Eli Friedman55ba8162011-07-29 03:05:32 +00004002 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4003 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004004}
4005
4006SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4007 SDValue Chain,
4008 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004009 MachineMemOperand *MMO,
4010 AtomicOrdering Ordering,
4011 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004012 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4013 Opcode == ISD::ATOMIC_LOAD_SUB ||
4014 Opcode == ISD::ATOMIC_LOAD_AND ||
4015 Opcode == ISD::ATOMIC_LOAD_OR ||
4016 Opcode == ISD::ATOMIC_LOAD_XOR ||
4017 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004018 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004019 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004020 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004021 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004022 Opcode == ISD::ATOMIC_SWAP ||
4023 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004024 "Invalid Atomic Op");
4025
Owen Andersone50ed302009-08-10 22:56:29 +00004026 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004027
Eli Friedman327236c2011-08-24 20:50:09 +00004028 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4029 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004030 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00004031 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00004032 SDValue Ops[] = {Chain, Ptr, Val};
4033 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004034 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004035 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004036 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4037 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004038 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004039 }
Dan Gohman95531882010-03-18 18:49:47 +00004040 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00004041 Ptr, Val, MMO,
4042 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004043 CSEMap.InsertNode(N, IP);
4044 AllNodes.push_back(N);
4045 return SDValue(N, 0);
4046}
4047
Eli Friedman327236c2011-08-24 20:50:09 +00004048SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4049 EVT VT, SDValue Chain,
4050 SDValue Ptr,
4051 const Value* PtrVal,
4052 unsigned Alignment,
4053 AtomicOrdering Ordering,
4054 SynchronizationScope SynchScope) {
4055 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4056 Alignment = getEVTAlignment(MemVT);
4057
4058 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00004059 // A monotonic load does not store; an acquire load "stores" in the sense
4060 // that other loads cannot be hoisted past it.
4061 unsigned Flags = MachineMemOperand::MOLoad;
4062 if (Ordering > Monotonic)
4063 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004064
4065 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004066 // FIXME: Volatile isn't really correct; we should keep track of atomic
4067 // orderings in the memoperand.
Eli Friedman327236c2011-08-24 20:50:09 +00004068 Flags |= MachineMemOperand::MOVolatile;
4069
4070 MachineMemOperand *MMO =
4071 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4072 MemVT.getStoreSize(), Alignment);
4073
4074 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4075 Ordering, SynchScope);
4076}
4077
4078SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4079 EVT VT, SDValue Chain,
4080 SDValue Ptr,
4081 MachineMemOperand *MMO,
4082 AtomicOrdering Ordering,
4083 SynchronizationScope SynchScope) {
4084 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4085
4086 SDVTList VTs = getVTList(VT, MVT::Other);
4087 FoldingSetNodeID ID;
4088 ID.AddInteger(MemVT.getRawBits());
4089 SDValue Ops[] = {Chain, Ptr};
4090 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004091 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Eli Friedman327236c2011-08-24 20:50:09 +00004092 void* IP = 0;
4093 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4094 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4095 return SDValue(E, 0);
4096 }
4097 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4098 Ptr, MMO, Ordering, SynchScope);
4099 CSEMap.InsertNode(N, IP);
4100 AllNodes.push_back(N);
4101 return SDValue(N, 0);
4102}
4103
Duncan Sands4bdcb612008-07-02 17:40:58 +00004104/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004105SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4106 DebugLoc dl) {
4107 if (NumOps == 1)
4108 return Ops[0];
4109
Owen Andersone50ed302009-08-10 22:56:29 +00004110 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004111 VTs.reserve(NumOps);
4112 for (unsigned i = 0; i < NumOps; ++i)
4113 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004114 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004115 Ops, NumOps);
4116}
4117
Dan Gohman475871a2008-07-27 21:46:04 +00004118SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004119SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004120 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004121 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004122 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004123 unsigned Align, bool Vol,
4124 bool ReadMem, bool WriteMem) {
4125 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004126 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004127 ReadMem, WriteMem);
4128}
4129
4130SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004131SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4132 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004133 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004134 unsigned Align, bool Vol,
4135 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004136 if (Align == 0) // Ensure that codegen never sees alignment 0
4137 Align = getEVTAlignment(MemVT);
4138
4139 MachineFunction &MF = getMachineFunction();
4140 unsigned Flags = 0;
4141 if (WriteMem)
4142 Flags |= MachineMemOperand::MOStore;
4143 if (ReadMem)
4144 Flags |= MachineMemOperand::MOLoad;
4145 if (Vol)
4146 Flags |= MachineMemOperand::MOVolatile;
4147 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004148 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004149
4150 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4151}
4152
4153SDValue
4154SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4155 const SDValue *Ops, unsigned NumOps,
4156 EVT MemVT, MachineMemOperand *MMO) {
4157 assert((Opcode == ISD::INTRINSIC_VOID ||
4158 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004159 Opcode == ISD::PREFETCH ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004160 (Opcode <= INT_MAX &&
4161 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4162 "Opcode is not a memory-accessing opcode!");
4163
Dale Johannesene8c17332009-01-29 00:47:48 +00004164 // Memoize the node unless it returns a flag.
4165 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004166 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004167 FoldingSetNodeID ID;
4168 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004169 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004170 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004171 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4172 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004173 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004174 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004175
Dan Gohman95531882010-03-18 18:49:47 +00004176 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4177 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004178 CSEMap.InsertNode(N, IP);
4179 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004180 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4181 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004182 }
4183 AllNodes.push_back(N);
4184 return SDValue(N, 0);
4185}
4186
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004187/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4188/// MachinePointerInfo record from it. This is particularly useful because the
4189/// code generator has many cases where it doesn't bother passing in a
4190/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4191static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4192 // If this is FI+Offset, we can model it.
4193 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4194 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4195
4196 // If this is (FI+Offset1)+Offset2, we can model it.
4197 if (Ptr.getOpcode() != ISD::ADD ||
4198 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4199 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4200 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004201
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004202 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4203 return MachinePointerInfo::getFixedStack(FI, Offset+
4204 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4205}
4206
4207/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4208/// MachinePointerInfo record from it. This is particularly useful because the
4209/// code generator has many cases where it doesn't bother passing in a
4210/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4211static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4212 // If the 'Offset' value isn't a constant, we can't handle this.
4213 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4214 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4215 if (OffsetOp.getOpcode() == ISD::UNDEF)
4216 return InferPointerInfo(Ptr);
4217 return MachinePointerInfo();
4218}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004219
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004220
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004221SDValue
4222SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4223 EVT VT, DebugLoc dl, SDValue Chain,
4224 SDValue Ptr, SDValue Offset,
4225 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004226 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004227 unsigned Alignment, const MDNode *TBAAInfo,
4228 const MDNode *Ranges) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004229 assert(Chain.getValueType() == MVT::Other &&
4230 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004231 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4232 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004233
Dan Gohmanc76909a2009-09-25 20:36:54 +00004234 unsigned Flags = MachineMemOperand::MOLoad;
4235 if (isVolatile)
4236 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004237 if (isNonTemporal)
4238 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004239 if (isInvariant)
4240 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004241
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004242 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4243 // clients.
4244 if (PtrInfo.V == 0)
4245 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004246
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004247 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004248 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004249 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004250 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004251 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004252}
4253
4254SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004255SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004256 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004257 SDValue Ptr, SDValue Offset, EVT MemVT,
4258 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004259 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004260 ExtType = ISD::NON_EXTLOAD;
4261 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004262 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004263 } else {
4264 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004265 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4266 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004267 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004268 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004269 assert(VT.isVector() == MemVT.isVector() &&
4270 "Cannot use trunc store to convert to or from a vector!");
4271 assert((!VT.isVector() ||
4272 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4273 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004274 }
4275
4276 bool Indexed = AM != ISD::UNINDEXED;
4277 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4278 "Unindexed load with an offset!");
4279
4280 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004281 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004282 SDValue Ops[] = { Chain, Ptr, Offset };
4283 FoldingSetNodeID ID;
4284 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004285 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004286 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004287 MMO->isNonTemporal(),
4288 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004289 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004290 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004291 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4292 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004293 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004294 }
Dan Gohman95531882010-03-18 18:49:47 +00004295 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4296 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004297 CSEMap.InsertNode(N, IP);
4298 AllNodes.push_back(N);
4299 return SDValue(N, 0);
4300}
4301
Owen Andersone50ed302009-08-10 22:56:29 +00004302SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004303 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004304 MachinePointerInfo PtrInfo,
4305 bool isVolatile, bool isNonTemporal,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004306 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004307 const MDNode *TBAAInfo,
4308 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004309 SDValue Undef = getUNDEF(Ptr.getValueType());
4310 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004311 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4312 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004313}
Dale Johannesene8c17332009-01-29 00:47:48 +00004314
Stuart Hastingsa9011292011-02-16 16:23:55 +00004315SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004316 SDValue Chain, SDValue Ptr,
4317 MachinePointerInfo PtrInfo, EVT MemVT,
4318 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004319 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004320 SDValue Undef = getUNDEF(Ptr.getValueType());
4321 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004322 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004323 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004324}
4325
4326
Dan Gohman475871a2008-07-27 21:46:04 +00004327SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004328SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4329 SDValue Offset, ISD::MemIndexedMode AM) {
4330 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4331 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4332 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004333 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004334 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004335 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
4336 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004337}
4338
Dale Johannesene8c17332009-01-29 00:47:48 +00004339SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004340 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004341 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004342 unsigned Alignment, const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004343 assert(Chain.getValueType() == MVT::Other &&
4344 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004345 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004346 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004347
Dan Gohmanc76909a2009-09-25 20:36:54 +00004348 unsigned Flags = MachineMemOperand::MOStore;
4349 if (isVolatile)
4350 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004351 if (isNonTemporal)
4352 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004353
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004354 if (PtrInfo.V == 0)
4355 PtrInfo = InferPointerInfo(Ptr);
4356
4357 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004358 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004359 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004360 Val.getValueType().getStoreSize(), Alignment,
4361 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004362
4363 return getStore(Chain, dl, Val, Ptr, MMO);
4364}
4365
4366SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4367 SDValue Ptr, MachineMemOperand *MMO) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004368 assert(Chain.getValueType() == MVT::Other &&
4369 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004370 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004371 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004372 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004373 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4374 FoldingSetNodeID ID;
4375 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004376 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004377 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004378 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004379 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004380 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004381 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4382 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004383 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004384 }
Dan Gohman95531882010-03-18 18:49:47 +00004385 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4386 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004387 CSEMap.InsertNode(N, IP);
4388 AllNodes.push_back(N);
4389 return SDValue(N, 0);
4390}
4391
Dale Johannesene8c17332009-01-29 00:47:48 +00004392SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004393 SDValue Ptr, MachinePointerInfo PtrInfo,
4394 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004395 unsigned Alignment,
4396 const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004397 assert(Chain.getValueType() == MVT::Other &&
4398 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004399 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4400 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004401
Dan Gohmanc76909a2009-09-25 20:36:54 +00004402 unsigned Flags = MachineMemOperand::MOStore;
4403 if (isVolatile)
4404 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004405 if (isNonTemporal)
4406 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004407
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004408 if (PtrInfo.V == 0)
4409 PtrInfo = InferPointerInfo(Ptr);
4410
4411 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004412 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004413 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4414 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004415
4416 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4417}
4418
4419SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4420 SDValue Ptr, EVT SVT,
4421 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004422 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004423
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004424 assert(Chain.getValueType() == MVT::Other &&
4425 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004426 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004427 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004428
Dan Gohman2e141d72009-12-14 23:40:38 +00004429 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4430 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004431 assert(VT.isInteger() == SVT.isInteger() &&
4432 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004433 assert(VT.isVector() == SVT.isVector() &&
4434 "Cannot use trunc store to convert to or from a vector!");
4435 assert((!VT.isVector() ||
4436 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4437 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004438
Owen Anderson825b72b2009-08-11 20:47:22 +00004439 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004440 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004441 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4442 FoldingSetNodeID ID;
4443 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004444 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004445 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004446 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004447 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004448 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004449 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4450 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004451 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004452 }
Dan Gohman95531882010-03-18 18:49:47 +00004453 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4454 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004455 CSEMap.InsertNode(N, IP);
4456 AllNodes.push_back(N);
4457 return SDValue(N, 0);
4458}
4459
Dan Gohman475871a2008-07-27 21:46:04 +00004460SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004461SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4462 SDValue Offset, ISD::MemIndexedMode AM) {
4463 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4464 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4465 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004466 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004467 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4468 FoldingSetNodeID ID;
4469 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004470 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004471 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004472 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004473 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004474 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004475 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004476
Dan Gohman95531882010-03-18 18:49:47 +00004477 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4478 ST->isTruncatingStore(),
4479 ST->getMemoryVT(),
4480 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004481 CSEMap.InsertNode(N, IP);
4482 AllNodes.push_back(N);
4483 return SDValue(N, 0);
4484}
4485
Owen Andersone50ed302009-08-10 22:56:29 +00004486SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004487 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004488 SDValue SV,
4489 unsigned Align) {
4490 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4491 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004492}
4493
Owen Andersone50ed302009-08-10 22:56:29 +00004494SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004495 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004496 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004497 case 0: return getNode(Opcode, DL, VT);
4498 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4499 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4500 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004501 default: break;
4502 }
4503
Dan Gohman475871a2008-07-27 21:46:04 +00004504 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004505 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004506 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004507 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004508}
4509
Owen Andersone50ed302009-08-10 22:56:29 +00004510SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004511 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004512 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004513 case 0: return getNode(Opcode, DL, VT);
4514 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4515 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4516 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004517 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004519
Chris Lattneref847df2005-04-09 03:27:28 +00004520 switch (Opcode) {
4521 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004522 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004523 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004524 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4525 "LHS and RHS of condition must have same type!");
4526 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4527 "True and False arms of SelectCC must have same type!");
4528 assert(Ops[2].getValueType() == VT &&
4529 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004530 break;
4531 }
4532 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004533 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004534 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4535 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004536 break;
4537 }
Chris Lattneref847df2005-04-09 03:27:28 +00004538 }
4539
Chris Lattner385328c2005-05-14 07:42:29 +00004540 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004541 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004542 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004543
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004544 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004545 FoldingSetNodeID ID;
4546 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004547 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004548
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004549 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004550 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004551
Dan Gohman95531882010-03-18 18:49:47 +00004552 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004553 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004554 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004555 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004556 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004557
Chris Lattneref847df2005-04-09 03:27:28 +00004558 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004559#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004560 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004561#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004562 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004563}
4564
Bill Wendling7ade28c2009-01-28 22:17:52 +00004565SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004566 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004567 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004568 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004569 Ops, NumOps);
4570}
4571
Bill Wendling7ade28c2009-01-28 22:17:52 +00004572SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004573 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004574 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004575 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004576 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4577 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004578}
4579
Bill Wendling7ade28c2009-01-28 22:17:52 +00004580SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4581 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004582 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004583 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004584
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004585#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004586 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004587 // FIXME: figure out how to safely handle things like
4588 // int foo(int x) { return 1 << (x & 255); }
4589 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004590 case ISD::SRA_PARTS:
4591 case ISD::SRL_PARTS:
4592 case ISD::SHL_PARTS:
4593 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004594 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004595 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004596 else if (N3.getOpcode() == ISD::AND)
4597 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4598 // If the and is only masking out bits that cannot effect the shift,
4599 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004600 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004601 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004602 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004603 }
4604 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004605 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004606#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004607
Chris Lattner43247a12005-08-25 19:12:10 +00004608 // Memoize the node unless it returns a flag.
4609 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004610 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004611 FoldingSetNodeID ID;
4612 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004613 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004614 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004615 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004616
Dan Gohman0e5f1302008-07-07 23:02:41 +00004617 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004618 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004619 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004620 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004621 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004622 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4623 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004624 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004625 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004626 }
Chris Lattnera5682852006-08-07 23:03:03 +00004627 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004628 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004629 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004630 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004631 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004632 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004633 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004634 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4635 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004636 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004637 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004638 }
Chris Lattner43247a12005-08-25 19:12:10 +00004639 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004640 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004641#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004642 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004643#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004644 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004645}
4646
Bill Wendling7ade28c2009-01-28 22:17:52 +00004647SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4648 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004649}
4650
Bill Wendling7ade28c2009-01-28 22:17:52 +00004651SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4652 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004653 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004654 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004655}
4656
Bill Wendling7ade28c2009-01-28 22:17:52 +00004657SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4658 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004659 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004660 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004661}
4662
Bill Wendling7ade28c2009-01-28 22:17:52 +00004663SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4664 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004665 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004666 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004667}
4668
Bill Wendling7ade28c2009-01-28 22:17:52 +00004669SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4670 SDValue N1, SDValue N2, SDValue N3,
4671 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004672 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004673 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004674}
4675
Bill Wendling7ade28c2009-01-28 22:17:52 +00004676SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4677 SDValue N1, SDValue N2, SDValue N3,
4678 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004679 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004680 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004681}
4682
Owen Andersone50ed302009-08-10 22:56:29 +00004683SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004684 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004685}
4686
Owen Andersone50ed302009-08-10 22:56:29 +00004687SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004688 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4689 E = VTList.rend(); I != E; ++I)
4690 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4691 return *I;
4692
Owen Andersone50ed302009-08-10 22:56:29 +00004693 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004694 Array[0] = VT1;
4695 Array[1] = VT2;
4696 SDVTList Result = makeVTList(Array, 2);
4697 VTList.push_back(Result);
4698 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004699}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004700
Owen Andersone50ed302009-08-10 22:56:29 +00004701SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004702 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4703 E = VTList.rend(); I != E; ++I)
4704 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4705 I->VTs[2] == VT3)
4706 return *I;
4707
Owen Andersone50ed302009-08-10 22:56:29 +00004708 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004709 Array[0] = VT1;
4710 Array[1] = VT2;
4711 Array[2] = VT3;
4712 SDVTList Result = makeVTList(Array, 3);
4713 VTList.push_back(Result);
4714 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004715}
4716
Owen Andersone50ed302009-08-10 22:56:29 +00004717SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004718 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4719 E = VTList.rend(); I != E; ++I)
4720 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4721 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4722 return *I;
4723
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004724 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004725 Array[0] = VT1;
4726 Array[1] = VT2;
4727 Array[2] = VT3;
4728 Array[3] = VT4;
4729 SDVTList Result = makeVTList(Array, 4);
4730 VTList.push_back(Result);
4731 return Result;
4732}
4733
Owen Andersone50ed302009-08-10 22:56:29 +00004734SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004735 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004736 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004737 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004738 case 2: return getVTList(VTs[0], VTs[1]);
4739 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004740 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004741 default: break;
4742 }
4743
Dan Gohmane8be6c62008-07-17 19:10:17 +00004744 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4745 E = VTList.rend(); I != E; ++I) {
4746 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4747 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004748
Benjamin Kramerb26e2912012-07-19 10:46:05 +00004749 if (std::equal(&VTs[2], &VTs[NumVTs], &I->VTs[2]))
Dan Gohmane8be6c62008-07-17 19:10:17 +00004750 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004752
Owen Andersone50ed302009-08-10 22:56:29 +00004753 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004754 std::copy(VTs, VTs+NumVTs, Array);
4755 SDVTList Result = makeVTList(Array, NumVTs);
4756 VTList.push_back(Result);
4757 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004758}
4759
4760
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004761/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4762/// specified operands. If the resultant node already exists in the DAG,
4763/// this does not modify the specified node, instead it returns the node that
4764/// already exists. If the resultant node does not exist in the DAG, the
4765/// input node is returned. As a degenerate case, if you specify the same
4766/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004767SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004768 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004769
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004770 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004771 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004772
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004773 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004774 void *InsertPos = 0;
4775 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004776 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004777
Dan Gohman79acd2b2008-07-21 22:38:59 +00004778 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004779 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004780 if (!RemoveNodeFromCSEMaps(N))
4781 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004782
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004783 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004784 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004785
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004786 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004787 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004788 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004789}
4790
Dan Gohman027657d2010-06-18 15:30:29 +00004791SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004792 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004793
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004794 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004795 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004796 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004797
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004798 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004799 void *InsertPos = 0;
4800 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004801 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004802
Dan Gohman79acd2b2008-07-21 22:38:59 +00004803 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004804 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004805 if (!RemoveNodeFromCSEMaps(N))
4806 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004807
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004808 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004809 if (N->OperandList[0] != Op1)
4810 N->OperandList[0].set(Op1);
4811 if (N->OperandList[1] != Op2)
4812 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004813
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004814 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004815 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004816 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004817}
4818
Dan Gohman027657d2010-06-18 15:30:29 +00004819SDNode *SelectionDAG::
4820UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004821 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004822 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004823}
4824
Dan Gohman027657d2010-06-18 15:30:29 +00004825SDNode *SelectionDAG::
4826UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004827 SDValue Op3, SDValue Op4) {
4828 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004829 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004830}
4831
Dan Gohman027657d2010-06-18 15:30:29 +00004832SDNode *SelectionDAG::
4833UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004834 SDValue Op3, SDValue Op4, SDValue Op5) {
4835 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004836 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004837}
4838
Dan Gohman027657d2010-06-18 15:30:29 +00004839SDNode *SelectionDAG::
4840UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004841 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004842 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004843
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004844 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004845 bool AnyChange = false;
4846 for (unsigned i = 0; i != NumOps; ++i) {
4847 if (Ops[i] != N->getOperand(i)) {
4848 AnyChange = true;
4849 break;
4850 }
4851 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004852
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004853 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004854 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004855
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004856 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004857 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004858 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004859 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004860
Dan Gohman7ceda162008-05-02 00:05:03 +00004861 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004862 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004863 if (!RemoveNodeFromCSEMaps(N))
4864 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004865
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004866 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004867 for (unsigned i = 0; i != NumOps; ++i)
4868 if (N->OperandList[i] != Ops[i])
4869 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004870
4871 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004872 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004873 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004874}
4875
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004876/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004877/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004878void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004879 // Unlike the code in MorphNodeTo that does this, we don't need to
4880 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004881 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4882 SDUse &Use = *I++;
4883 Use.set(SDValue());
4884 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004885}
Chris Lattner149c58c2005-08-16 18:17:10 +00004886
Dan Gohmane8be6c62008-07-17 19:10:17 +00004887/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4888/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004889///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004890SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004891 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004892 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004893 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004894}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004895
Dan Gohmane8be6c62008-07-17 19:10:17 +00004896SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004897 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004898 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004899 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004900 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004901}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004902
Dan Gohmane8be6c62008-07-17 19:10:17 +00004903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004904 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004905 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004906 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004907 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004908 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004909}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004910
Dan Gohmane8be6c62008-07-17 19:10:17 +00004911SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004912 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004913 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004914 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004915 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004916 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004917}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004918
Dan Gohmane8be6c62008-07-17 19:10:17 +00004919SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004920 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004921 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004922 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004923 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +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, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004928 unsigned NumOps) {
4929 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004930 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004931}
4932
Dan Gohmane8be6c62008-07-17 19:10:17 +00004933SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004934 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004935 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004936 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004937}
4938
Dan Gohmane8be6c62008-07-17 19:10:17 +00004939SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004940 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004941 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004942 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004943 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004944}
4945
Bill Wendling13d6d442008-12-01 23:28:22 +00004946SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004947 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00004948 const SDValue *Ops, unsigned NumOps) {
4949 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4950 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4951}
4952
Scott Michelfdc40a02009-02-17 22:15:04 +00004953SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004954 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004955 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004956 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004957 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004958 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004959}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004960
Scott Michelfdc40a02009-02-17 22:15:04 +00004961SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004962 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004963 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004964 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004965 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004966 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004967}
4968
Dan Gohmane8be6c62008-07-17 19:10:17 +00004969SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004970 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004971 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004972 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004973 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004974 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004975 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004976}
4977
Dan Gohmane8be6c62008-07-17 19:10:17 +00004978SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004979 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004980 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004981 SDValue Op3) {
4982 SDVTList VTs = getVTList(VT1, VT2, VT3);
4983 SDValue Ops[] = { Op1, Op2, Op3 };
4984 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4985}
4986
4987SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004988 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004989 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00004990 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4991 // Reset the NodeID to -1.
4992 N->setNodeId(-1);
4993 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004994}
4995
Devang Patel0508d042011-12-15 18:21:18 +00004996/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
4997/// the line number information on the merged node since it is not possible to
4998/// preserve the information that operation is associated with multiple lines.
4999/// This will make the debugger working better at -O0, were there is a higher
5000/// probability having other instructions associated with that line.
5001///
5002SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
5003 DebugLoc NLoc = N->getDebugLoc();
5004 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
5005 N->setDebugLoc(DebugLoc());
5006 }
5007 return N;
5008}
5009
Chris Lattner21221e32010-02-28 21:36:14 +00005010/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005011/// return type, opcode, and operands.
5012///
5013/// Note that MorphNodeTo returns the resultant node. If there is already a
5014/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00005015/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005016///
5017/// Using MorphNodeTo is faster than creating a new node and swapping it in
5018/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005019/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005020/// the node's users.
5021///
5022SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00005023 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005024 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005025 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00005026 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005027 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005028 FoldingSetNodeID ID;
5029 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005030 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00005031 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005032 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005033
Dan Gohman095cc292008-09-13 01:54:27 +00005034 if (!RemoveNodeFromCSEMaps(N))
5035 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00005036
Dan Gohmane8be6c62008-07-17 19:10:17 +00005037 // Start the morphing.
5038 N->NodeType = Opc;
5039 N->ValueList = VTs.VTs;
5040 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005041
Dan Gohmane8be6c62008-07-17 19:10:17 +00005042 // Clear the operands list, updating used nodes to remove this from their
5043 // use list. Keep track of any operands that become dead as a result.
5044 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005045 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5046 SDUse &Use = *I++;
5047 SDNode *Used = Use.getNode();
5048 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005049 if (Used->use_empty())
5050 DeadNodeSet.insert(Used);
5051 }
5052
Dan Gohmanc76909a2009-09-25 20:36:54 +00005053 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5054 // Initialize the memory references information.
5055 MN->setMemRefs(0, 0);
5056 // If NumOps is larger than the # of operands we can have in a
5057 // MachineSDNode, reallocate the operand list.
5058 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5059 if (MN->OperandsNeedDelete)
5060 delete[] MN->OperandList;
5061 if (NumOps > array_lengthof(MN->LocalOperands))
5062 // We're creating a final node that will live unmorphed for the
5063 // remainder of the current SelectionDAG iteration, so we can allocate
5064 // the operands directly out of a pool with no recycling metadata.
5065 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005066 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005067 else
5068 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5069 MN->OperandsNeedDelete = false;
5070 } else
5071 MN->InitOperands(MN->OperandList, Ops, NumOps);
5072 } else {
5073 // If NumOps is larger than the # of operands we currently have, reallocate
5074 // the operand list.
5075 if (NumOps > N->NumOperands) {
5076 if (N->OperandsNeedDelete)
5077 delete[] N->OperandList;
5078 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005079 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005080 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005081 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005082 }
5083
5084 // Delete any nodes that are still dead after adding the uses for the
5085 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005086 if (!DeadNodeSet.empty()) {
5087 SmallVector<SDNode *, 16> DeadNodes;
5088 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5089 E = DeadNodeSet.end(); I != E; ++I)
5090 if ((*I)->use_empty())
5091 DeadNodes.push_back(*I);
5092 RemoveDeadNodes(DeadNodes);
5093 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005094
Dan Gohmane8be6c62008-07-17 19:10:17 +00005095 if (IP)
5096 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005097 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005098}
5099
Chris Lattner0fb094f2005-11-19 01:44:53 +00005100
Dan Gohman602b0c82009-09-25 18:54:59 +00005101/// getMachineNode - These are used for target selectors to create a new node
5102/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005103///
Dan Gohman602b0c82009-09-25 18:54:59 +00005104/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005105/// node of the specified opcode and operands, it returns that node instead of
5106/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005107MachineSDNode *
5108SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005109 SDVTList VTs = getVTList(VT);
5110 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005111}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005112
Dan Gohmanc81b7832009-10-10 01:29:16 +00005113MachineSDNode *
5114SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005115 SDVTList VTs = getVTList(VT);
5116 SDValue Ops[] = { Op1 };
5117 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005118}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005119
Dan Gohmanc81b7832009-10-10 01:29:16 +00005120MachineSDNode *
5121SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5122 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005123 SDVTList VTs = getVTList(VT);
5124 SDValue Ops[] = { Op1, Op2 };
5125 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005126}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005127
Dan Gohmanc81b7832009-10-10 01:29:16 +00005128MachineSDNode *
5129SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5130 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005131 SDVTList VTs = getVTList(VT);
5132 SDValue Ops[] = { Op1, Op2, Op3 };
5133 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005134}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005135
Dan Gohmanc81b7832009-10-10 01:29:16 +00005136MachineSDNode *
5137SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5138 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005139 SDVTList VTs = getVTList(VT);
5140 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005141}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005142
Dan Gohmanc81b7832009-10-10 01:29:16 +00005143MachineSDNode *
5144SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005145 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005146 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005147}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005148
Dan Gohmanc81b7832009-10-10 01:29:16 +00005149MachineSDNode *
5150SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5151 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005152 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005153 SDValue Ops[] = { Op1 };
5154 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +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, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005160 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005161 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005162 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005163}
5164
Dan Gohmanc81b7832009-10-10 01:29:16 +00005165MachineSDNode *
5166SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5167 EVT VT1, EVT VT2, SDValue Op1,
5168 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005169 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005170 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005171 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005172}
5173
Dan Gohmanc81b7832009-10-10 01:29:16 +00005174MachineSDNode *
5175SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5176 EVT VT1, EVT VT2,
5177 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005178 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005179 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005180}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005181
Dan Gohmanc81b7832009-10-10 01:29:16 +00005182MachineSDNode *
5183SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5184 EVT VT1, EVT VT2, EVT VT3,
5185 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005186 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005187 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005188 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005189}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005190
Dan Gohmanc81b7832009-10-10 01:29:16 +00005191MachineSDNode *
5192SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5193 EVT VT1, EVT VT2, EVT VT3,
5194 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005195 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005196 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005197 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00005198}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005199
Dan Gohmanc81b7832009-10-10 01:29:16 +00005200MachineSDNode *
5201SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5202 EVT VT1, EVT VT2, EVT VT3,
5203 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005204 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005205 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005206}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005207
Dan Gohmanc81b7832009-10-10 01:29:16 +00005208MachineSDNode *
5209SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5210 EVT VT2, EVT VT3, EVT VT4,
5211 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005212 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005213 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005214}
5215
Dan Gohmanc81b7832009-10-10 01:29:16 +00005216MachineSDNode *
5217SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5218 const std::vector<EVT> &ResultTys,
5219 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005220 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
5221 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
5222}
5223
Dan Gohmanc81b7832009-10-10 01:29:16 +00005224MachineSDNode *
5225SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
5226 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005227 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005228 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005229 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005230
5231 if (DoCSE) {
5232 FoldingSetNodeID ID;
5233 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5234 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005235 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5236 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5237 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005238 }
5239
5240 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005241 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005242
5243 // Initialize the operands list.
5244 if (NumOps > array_lengthof(N->LocalOperands))
5245 // We're creating a final node that will live unmorphed for the
5246 // remainder of the current SelectionDAG iteration, so we can allocate
5247 // the operands directly out of a pool with no recycling metadata.
5248 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5249 Ops, NumOps);
5250 else
5251 N->InitOperands(N->LocalOperands, Ops, NumOps);
5252 N->OperandsNeedDelete = false;
5253
5254 if (DoCSE)
5255 CSEMap.InsertNode(N, IP);
5256
5257 AllNodes.push_back(N);
5258#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005259 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005260#endif
5261 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005262}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005263
Dan Gohman6a402dc2009-08-19 18:16:17 +00005264/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005265/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005266SDValue
5267SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5268 SDValue Operand) {
5269 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005270 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005271 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005272 return SDValue(Subreg, 0);
5273}
5274
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005275/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005276/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005277SDValue
5278SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5279 SDValue Operand, SDValue Subreg) {
5280 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005281 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005282 VT, Operand, Subreg, SRIdxVal);
5283 return SDValue(Result, 0);
5284}
5285
Evan Cheng08b11732008-03-22 01:55:50 +00005286/// getNodeIfExists - Get the specified node if it's already available, or
5287/// else return NULL.
5288SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005289 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005290 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005291 FoldingSetNodeID ID;
5292 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5293 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005294 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005295 return E;
5296 }
5297 return NULL;
5298}
5299
Evan Cheng31441b72010-03-29 20:48:30 +00005300/// getDbgValue - Creates a SDDbgValue node.
5301///
5302SDDbgValue *
5303SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5304 DebugLoc DL, unsigned O) {
5305 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5306}
5307
5308SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005309SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005310 DebugLoc DL, unsigned O) {
5311 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5312}
5313
5314SDDbgValue *
5315SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5316 DebugLoc DL, unsigned O) {
5317 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5318}
5319
Dan Gohmana72d2a22010-03-03 21:33:37 +00005320namespace {
5321
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005322/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005323/// pointed to by a use iterator is deleted, increment the use iterator
5324/// so that it doesn't dangle.
5325///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005326class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005327 SDNode::use_iterator &UI;
5328 SDNode::use_iterator &UE;
5329
5330 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5331 // Increment the iterator as needed.
5332 while (UI != UE && N == *UI)
5333 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005334 }
5335
5336public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005337 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005338 SDNode::use_iterator &ui,
5339 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005340 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005341};
5342
5343}
5344
Evan Cheng99157a02006-08-07 22:13:29 +00005345/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005346/// This can cause recursive merging of nodes in the DAG.
5347///
Chris Lattner11d049c2008-02-03 03:35:22 +00005348/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005349///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005350void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005351 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005352 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005353 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005354 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005355
Dan Gohman39946102009-01-25 16:29:12 +00005356 // Iterate over all the existing uses of From. New uses will be added
5357 // to the beginning of the use list, which we avoid visiting.
5358 // This specifically avoids visiting uses of From that arise while the
5359 // replacement is happening, because any such uses would be the result
5360 // of CSE: If an existing node looks like From after one of its operands
5361 // is replaced by To, we don't want to replace of all its users with To
5362 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005363 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005364 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005365 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005366 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005367
Chris Lattner8b8749f2005-08-17 19:00:20 +00005368 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005369 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005370
Dan Gohman39946102009-01-25 16:29:12 +00005371 // A user can appear in a use list multiple times, and when this
5372 // happens the uses are usually next to each other in the list.
5373 // To help reduce the number of CSE recomputations, process all
5374 // the uses of this user that we can find this way.
5375 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005376 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005377 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005378 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005379 } while (UI != UE && *UI == User);
5380
5381 // Now that we have modified User, add it back to the CSE maps. If it
5382 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005383 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005384 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005385
5386 // If we just RAUW'd the root, take note.
5387 if (FromN == getRoot())
5388 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005389}
5390
5391/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5392/// This can cause recursive merging of nodes in the DAG.
5393///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005394/// This version assumes that for each value of From, there is a
5395/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005396///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005397void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005398#ifndef NDEBUG
5399 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5400 assert((!From->hasAnyUseOfValue(i) ||
5401 From->getValueType(i) == To->getValueType(i)) &&
5402 "Cannot use this version of ReplaceAllUsesWith!");
5403#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005404
5405 // Handle the trivial case.
5406 if (From == To)
5407 return;
5408
Dan Gohmandbe664a2009-01-19 21:44:21 +00005409 // Iterate over just the existing users of From. See the comments in
5410 // the ReplaceAllUsesWith above.
5411 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005412 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005413 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005414 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005415
Chris Lattner8b52f212005-08-26 18:36:28 +00005416 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005417 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005418
Dan Gohman39946102009-01-25 16:29:12 +00005419 // A user can appear in a use list multiple times, and when this
5420 // happens the uses are usually next to each other in the list.
5421 // To help reduce the number of CSE recomputations, process all
5422 // the uses of this user that we can find this way.
5423 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005424 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005425 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005426 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005427 } while (UI != UE && *UI == User);
5428
5429 // Now that we have modified User, add it back to the CSE maps. If it
5430 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005431 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005432 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005433
5434 // If we just RAUW'd the root, take note.
5435 if (From == getRoot().getNode())
5436 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005437}
5438
Chris Lattner8b52f212005-08-26 18:36:28 +00005439/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5440/// This can cause recursive merging of nodes in the DAG.
5441///
5442/// This version can replace From with any result values. To must match the
5443/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005444void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005445 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005446 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005447
Dan Gohmandbe664a2009-01-19 21:44:21 +00005448 // Iterate over just the existing users of From. See the comments in
5449 // the ReplaceAllUsesWith above.
5450 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005451 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005452 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005453 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005454
Chris Lattner7b2880c2005-08-24 22:44:39 +00005455 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005456 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +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();
5464 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005465 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005466 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005467 } while (UI != UE && *UI == User);
5468
5469 // Now that we have modified User, add it back to the CSE maps. If it
5470 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005471 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005472 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005473
5474 // If we just RAUW'd the root, take note.
5475 if (From == getRoot().getNode())
5476 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005477}
5478
Chris Lattner012f2412006-02-17 21:58:01 +00005479/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005480/// uses of other values produced by From.getNode() alone. The Deleted
5481/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005482void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005483 // Handle the really simple, really trivial case efficiently.
5484 if (From == To) return;
5485
Chris Lattner012f2412006-02-17 21:58:01 +00005486 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005487 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005488 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005489 return;
5490 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005491
Dan Gohman39946102009-01-25 16:29:12 +00005492 // Iterate over just the existing users of From. See the comments in
5493 // the ReplaceAllUsesWith above.
5494 SDNode::use_iterator UI = From.getNode()->use_begin(),
5495 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005496 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005497 while (UI != UE) {
5498 SDNode *User = *UI;
5499 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005500
Dan Gohman39946102009-01-25 16:29:12 +00005501 // A user can appear in a use list multiple times, and when this
5502 // happens the uses are usually next to each other in the list.
5503 // To help reduce the number of CSE recomputations, process all
5504 // the uses of this user that we can find this way.
5505 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005506 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005507
5508 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005509 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005510 ++UI;
5511 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005512 }
Dan Gohman39946102009-01-25 16:29:12 +00005513
5514 // If this node hasn't been modified yet, it's still in the CSE maps,
5515 // so remove its old self from the CSE maps.
5516 if (!UserRemovedFromCSEMaps) {
5517 RemoveNodeFromCSEMaps(User);
5518 UserRemovedFromCSEMaps = true;
5519 }
5520
5521 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005522 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005523 } while (UI != UE && *UI == User);
5524
5525 // We are iterating over all uses of the From node, so if a use
5526 // doesn't use the specific value, no changes are made.
5527 if (!UserRemovedFromCSEMaps)
5528 continue;
5529
Chris Lattner01d029b2007-10-15 06:10:22 +00005530 // Now that we have modified User, add it back to the CSE maps. If it
5531 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005532 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005533 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005534
5535 // If we just RAUW'd the root, take note.
5536 if (From == getRoot())
5537 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005538}
5539
Dan Gohman39946102009-01-25 16:29:12 +00005540namespace {
5541 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5542 /// to record information about a use.
5543 struct UseMemo {
5544 SDNode *User;
5545 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005546 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005547 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005548
5549 /// operator< - Sort Memos by User.
5550 bool operator<(const UseMemo &L, const UseMemo &R) {
5551 return (intptr_t)L.User < (intptr_t)R.User;
5552 }
Dan Gohman39946102009-01-25 16:29:12 +00005553}
5554
Dan Gohmane8be6c62008-07-17 19:10:17 +00005555/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005556/// uses of other values produced by From.getNode() alone. The same value
5557/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005558/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005559void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5560 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005561 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005562 // Handle the simple, trivial case efficiently.
5563 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005564 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005565
Dan Gohman39946102009-01-25 16:29:12 +00005566 // Read up all the uses and make records of them. This helps
5567 // processing new uses that are introduced during the
5568 // replacement process.
5569 SmallVector<UseMemo, 4> Uses;
5570 for (unsigned i = 0; i != Num; ++i) {
5571 unsigned FromResNo = From[i].getResNo();
5572 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005573 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005574 E = FromNode->use_end(); UI != E; ++UI) {
5575 SDUse &Use = UI.getUse();
5576 if (Use.getResNo() == FromResNo) {
5577 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005578 Uses.push_back(Memo);
5579 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005580 }
Dan Gohman39946102009-01-25 16:29:12 +00005581 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005582
Dan Gohmane7852d02009-01-26 04:35:06 +00005583 // Sort the uses, so that all the uses from a given User are together.
5584 std::sort(Uses.begin(), Uses.end());
5585
Dan Gohman39946102009-01-25 16:29:12 +00005586 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5587 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005588 // We know that this user uses some value of From. If it is the right
5589 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005590 SDNode *User = Uses[UseIndex].User;
5591
5592 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005593 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005594
Dan Gohmane7852d02009-01-26 04:35:06 +00005595 // The Uses array is sorted, so all the uses for a given User
5596 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005597 // To help reduce the number of CSE recomputations, process all
5598 // the uses of this user that we can find this way.
5599 do {
5600 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005601 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005602 ++UseIndex;
5603
Dan Gohmane7852d02009-01-26 04:35:06 +00005604 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005605 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5606
Dan Gohmane8be6c62008-07-17 19:10:17 +00005607 // Now that we have modified User, add it back to the CSE maps. If it
5608 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005609 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005610 }
5611}
5612
Evan Chenge6f35d82006-08-01 08:20:41 +00005613/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005614/// based on their topological order. It returns the maximum id and a vector
5615/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005616unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005617
Dan Gohmanf06c8352008-09-30 18:30:35 +00005618 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005619
Dan Gohmanf06c8352008-09-30 18:30:35 +00005620 // SortedPos tracks the progress of the algorithm. Nodes before it are
5621 // sorted, nodes after it are unsorted. When the algorithm completes
5622 // it is at the end of the list.
5623 allnodes_iterator SortedPos = allnodes_begin();
5624
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005625 // Visit all the nodes. Move nodes with no operands to the front of
5626 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005627 // operand count. Before we do this, the Node Id fields of the nodes
5628 // may contain arbitrary values. After, the Node Id fields for nodes
5629 // before SortedPos will contain the topological sort index, and the
5630 // Node Id fields for nodes At SortedPos and after will contain the
5631 // count of outstanding operands.
5632 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5633 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005634 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005635 unsigned Degree = N->getNumOperands();
5636 if (Degree == 0) {
5637 // A node with no uses, add it to the result array immediately.
5638 N->setNodeId(DAGSize++);
5639 allnodes_iterator Q = N;
5640 if (Q != SortedPos)
5641 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005642 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005643 ++SortedPos;
5644 } else {
5645 // Temporarily use the Node Id as scratch space for the degree count.
5646 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005647 }
5648 }
5649
Chad Rosierf496dea2012-05-21 17:13:41 +00005650 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005651 // such that by the time the end is reached all nodes will be sorted.
5652 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5653 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005654 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005655 // N is in sorted position, so all its uses have one less operand
5656 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005657 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5658 UI != UE; ++UI) {
5659 SDNode *P = *UI;
5660 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005661 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005662 --Degree;
5663 if (Degree == 0) {
5664 // All of P's operands are sorted, so P may sorted now.
5665 P->setNodeId(DAGSize++);
5666 if (P != SortedPos)
5667 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005668 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005669 ++SortedPos;
5670 } else {
5671 // Update P's outstanding operand count.
5672 P->setNodeId(Degree);
5673 }
5674 }
David Greene221925e2010-01-20 00:59:23 +00005675 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005676#ifndef NDEBUG
5677 SDNode *S = ++I;
5678 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005679 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005680#endif
5681 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005682 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005683 }
5684
5685 assert(SortedPos == AllNodes.end() &&
5686 "Topological sort incomplete!");
5687 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5688 "First node in topological sort is not the entry token!");
5689 assert(AllNodes.front().getNodeId() == 0 &&
5690 "First node in topological sort has non-zero id!");
5691 assert(AllNodes.front().getNumOperands() == 0 &&
5692 "First node in topological sort has operands!");
5693 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5694 "Last node in topologic sort has unexpected id!");
5695 assert(AllNodes.back().use_empty() &&
5696 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005697 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005698 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005699}
5700
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005701/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005702void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005703 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005704 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005705}
Evan Chenge6f35d82006-08-01 08:20:41 +00005706
Bill Wendling0777e922009-12-21 21:59:52 +00005707/// GetOrdering - Get the order for the SDNode.
5708unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5709 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005710 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005711}
5712
Evan Chengbfcb3052010-03-25 01:38:16 +00005713/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5714/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005715void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5716 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005717 if (SD)
5718 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005719}
Evan Cheng091cba12006-07-27 06:39:06 +00005720
Devang Patela2e868d2011-01-25 23:27:42 +00005721/// TransferDbgValues - Transfer SDDbgValues.
5722void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5723 if (From == To || !From.getNode()->getHasDebugValue())
5724 return;
5725 SDNode *FromNode = From.getNode();
5726 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005727 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005728 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005729 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005730 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005731 SDDbgValue *Dbg = *I;
5732 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5733 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5734 Dbg->getOffset(), Dbg->getDebugLoc(),
5735 Dbg->getOrder());
5736 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005737 }
5738 }
Devang Patela778f5c2011-02-18 22:43:42 +00005739 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5740 E = ClonedDVs.end(); I != E; ++I)
5741 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005742}
5743
Jim Laskey58b968b2005-08-17 20:08:02 +00005744//===----------------------------------------------------------------------===//
5745// SDNode Class
5746//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005747
Chris Lattner48b85922007-02-04 02:41:42 +00005748HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005749 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005750}
5751
Devang Patel0d881da2010-07-06 22:08:15 +00005752GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5753 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005754 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005755 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005756 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005757}
Chris Lattner48b85922007-02-04 02:41:42 +00005758
Owen Andersone50ed302009-08-10 22:56:29 +00005759MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005760 MachineMemOperand *mmo)
5761 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005762 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005763 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005764 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005765 assert(isNonTemporal() == MMO->isNonTemporal() &&
5766 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005767 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005768}
5769
Scott Michelfdc40a02009-02-17 22:15:04 +00005770MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005771 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005772 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005773 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005774 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005775 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005776 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005777 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5778 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005779}
5780
Jim Laskey583bd472006-10-27 23:46:08 +00005781/// Profile - Gather unique data for the node.
5782///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005783void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005784 AddNodeIDNode(ID, this);
5785}
5786
Owen Andersond8110fb2009-08-25 22:27:22 +00005787namespace {
5788 struct EVTArray {
5789 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005790
Owen Andersond8110fb2009-08-25 22:27:22 +00005791 EVTArray() {
5792 VTs.reserve(MVT::LAST_VALUETYPE);
5793 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5794 VTs.push_back(MVT((MVT::SimpleValueType)i));
5795 }
5796 };
5797}
5798
Owen Andersone50ed302009-08-10 22:56:29 +00005799static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005800static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005801static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005802
Chris Lattnera3255112005-11-08 23:30:28 +00005803/// getValueTypeList - Return a pointer to the specified value type.
5804///
Owen Andersone50ed302009-08-10 22:56:29 +00005805const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005806 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005807 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005808 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005809 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005810 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005811 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005812 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005813 }
Chris Lattnera3255112005-11-08 23:30:28 +00005814}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005815
Chris Lattner5c884562005-01-12 18:37:47 +00005816/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5817/// indicated value. This method ignores uses of other values defined by this
5818/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005819bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005820 assert(Value < getNumValues() && "Bad value!");
5821
Roman Levensteindc1adac2008-04-07 10:06:32 +00005822 // TODO: Only iterate over uses of a given value of the node
5823 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005824 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005825 if (NUses == 0)
5826 return false;
5827 --NUses;
5828 }
Chris Lattner5c884562005-01-12 18:37:47 +00005829 }
5830
5831 // Found exactly the right number of uses?
5832 return NUses == 0;
5833}
5834
5835
Evan Cheng33d55952007-08-02 05:29:38 +00005836/// hasAnyUseOfValue - Return true if there are any use of the indicated
5837/// value. This method ignores uses of other values defined by this operation.
5838bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5839 assert(Value < getNumValues() && "Bad value!");
5840
Dan Gohman1373c1c2008-07-09 22:39:01 +00005841 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005842 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005843 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005844
5845 return false;
5846}
5847
5848
Dan Gohman2a629952008-07-27 18:06:42 +00005849/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005850///
Dan Gohman2a629952008-07-27 18:06:42 +00005851bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005852 bool Seen = false;
5853 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005854 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005855 if (User == this)
5856 Seen = true;
5857 else
5858 return false;
5859 }
5860
5861 return Seen;
5862}
5863
Evan Chenge6e97e62006-11-03 07:31:32 +00005864/// isOperand - Return true if this node is an operand of N.
5865///
Dan Gohman475871a2008-07-27 21:46:04 +00005866bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005867 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5868 if (*this == N->getOperand(i))
5869 return true;
5870 return false;
5871}
5872
Evan Cheng917be682008-03-04 00:41:45 +00005873bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005874 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005875 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005876 return true;
5877 return false;
5878}
Evan Cheng4ee62112006-02-05 06:29:23 +00005879
Chris Lattner572dee72008-01-16 05:49:24 +00005880/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005881/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005882/// side-effecting instructions on any chain path. In practice, this looks
5883/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005884/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005885bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005886 unsigned Depth) const {
5887 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005888
Chris Lattner572dee72008-01-16 05:49:24 +00005889 // Don't search too deeply, we just want to be able to see through
5890 // TokenFactor's etc.
5891 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005892
Chris Lattner572dee72008-01-16 05:49:24 +00005893 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005894 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00005895 if (getOpcode() == ISD::TokenFactor) {
5896 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005897 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5898 return false;
5899 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00005900 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005901
Chris Lattner572dee72008-01-16 05:49:24 +00005902 // Loads don't have side effects, look through them.
5903 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5904 if (!Ld->isVolatile())
5905 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5906 }
5907 return false;
5908}
5909
Lang Hames944520f2011-07-07 04:31:51 +00005910/// hasPredecessor - Return true if N is a predecessor of this node.
5911/// N is either an operand of this node, or can be reached by recursively
5912/// traversing up the operands.
5913/// NOTE: This is an expensive method. Use it carefully.
5914bool SDNode::hasPredecessor(const SDNode *N) const {
5915 SmallPtrSet<const SDNode *, 32> Visited;
5916 SmallVector<const SDNode *, 16> Worklist;
5917 return hasPredecessorHelper(N, Visited, Worklist);
5918}
Dan Gohman6688d612009-10-28 03:44:30 +00005919
Lang Hames944520f2011-07-07 04:31:51 +00005920bool SDNode::hasPredecessorHelper(const SDNode *N,
5921 SmallPtrSet<const SDNode *, 32> &Visited,
5922 SmallVector<const SDNode *, 16> &Worklist) const {
5923 if (Visited.empty()) {
5924 Worklist.push_back(this);
5925 } else {
5926 // Take a look in the visited set. If we've already encountered this node
5927 // we needn't search further.
5928 if (Visited.count(N))
5929 return true;
5930 }
5931
5932 // Haven't visited N yet. Continue the search.
5933 while (!Worklist.empty()) {
5934 const SDNode *M = Worklist.pop_back_val();
5935 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
5936 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00005937 if (Visited.insert(Op))
5938 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00005939 if (Op == N)
5940 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00005941 }
Lang Hames944520f2011-07-07 04:31:51 +00005942 }
Dan Gohman6688d612009-10-28 03:44:30 +00005943
5944 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005945}
5946
Evan Chengc5484282006-10-04 00:56:09 +00005947uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5948 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005949 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005950}
5951
Mon P Wangcd6e7252009-11-30 02:42:02 +00005952SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
5953 assert(N->getNumValues() == 1 &&
5954 "Can't unroll a vector with multiple results!");
5955
5956 EVT VT = N->getValueType(0);
5957 unsigned NE = VT.getVectorNumElements();
5958 EVT EltVT = VT.getVectorElementType();
5959 DebugLoc dl = N->getDebugLoc();
5960
5961 SmallVector<SDValue, 8> Scalars;
5962 SmallVector<SDValue, 4> Operands(N->getNumOperands());
5963
5964 // If ResNE is 0, fully unroll the vector op.
5965 if (ResNE == 0)
5966 ResNE = NE;
5967 else if (NE > ResNE)
5968 NE = ResNE;
5969
5970 unsigned i;
5971 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00005972 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00005973 SDValue Operand = N->getOperand(j);
5974 EVT OperandVT = Operand.getValueType();
5975 if (OperandVT.isVector()) {
5976 // A vector operand; extract a single element.
5977 EVT OperandEltVT = OperandVT.getVectorElementType();
5978 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5979 OperandEltVT,
5980 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00005981 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005982 } else {
5983 // A scalar operand; just use it as is.
5984 Operands[j] = Operand;
5985 }
5986 }
5987
5988 switch (N->getOpcode()) {
5989 default:
5990 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5991 &Operands[0], Operands.size()));
5992 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00005993 case ISD::VSELECT:
5994 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
5995 &Operands[0], Operands.size()));
5996 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00005997 case ISD::SHL:
5998 case ISD::SRA:
5999 case ISD::SRL:
6000 case ISD::ROTL:
6001 case ISD::ROTR:
6002 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00006003 getShiftAmountOperand(Operands[0].getValueType(),
6004 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006005 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006006 case ISD::SIGN_EXTEND_INREG:
6007 case ISD::FP_ROUND_INREG: {
6008 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6009 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6010 Operands[0],
6011 getValueType(ExtVT)));
6012 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006013 }
6014 }
6015
6016 for (; i < ResNE; ++i)
6017 Scalars.push_back(getUNDEF(EltVT));
6018
6019 return getNode(ISD::BUILD_VECTOR, dl,
6020 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6021 &Scalars[0], Scalars.size());
6022}
6023
Evan Cheng64fa4a92009-12-09 01:36:00 +00006024
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006025/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6026/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006027/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006028bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006029 unsigned Bytes, int Dist) const {
6030 if (LD->getChain() != Base->getChain())
6031 return false;
6032 EVT VT = LD->getValueType(0);
6033 if (VT.getSizeInBits() / 8 != Bytes)
6034 return false;
6035
6036 SDValue Loc = LD->getOperand(1);
6037 SDValue BaseLoc = Base->getOperand(1);
6038 if (Loc.getOpcode() == ISD::FrameIndex) {
6039 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6040 return false;
6041 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6042 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6043 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6044 int FS = MFI->getObjectSize(FI);
6045 int BFS = MFI->getObjectSize(BFI);
6046 if (FS != BFS || FS != (int)Bytes) return false;
6047 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6048 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006049
6050 // Handle X+C
6051 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6052 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6053 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006054
Dan Gohman46510a72010-04-15 01:51:59 +00006055 const GlobalValue *GV1 = NULL;
6056 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006057 int64_t Offset1 = 0;
6058 int64_t Offset2 = 0;
6059 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6060 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6061 if (isGA1 && isGA2 && GV1 == GV2)
6062 return Offset1 == (Offset2 + Dist*Bytes);
6063 return false;
6064}
6065
6066
Evan Chengf2dc5c72009-12-09 01:04:59 +00006067/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6068/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006069unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006070 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006071 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006072 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00006073 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006074 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006075 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006076 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6077 TLI.getTargetData());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006078 unsigned AlignBits = KnownZero.countTrailingOnes();
6079 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6080 if (Align)
6081 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006082 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006083
Evan Chengf2dc5c72009-12-09 01:04:59 +00006084 // If this is a direct reference to a stack slot, use information about the
6085 // stack slot's alignment.
6086 int FrameIdx = 1 << 31;
6087 int64_t FrameOffset = 0;
6088 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6089 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006090 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006091 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006092 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006093 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6094 FrameOffset = Ptr.getConstantOperandVal(1);
6095 }
6096
6097 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006098 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006099 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6100 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006101 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006102 }
6103
6104 return 0;
6105}
6106
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006107// getAddressSpace - Return the address space this GlobalAddress belongs to.
6108unsigned GlobalAddressSDNode::getAddressSpace() const {
6109 return getGlobal()->getType()->getAddressSpace();
6110}
6111
6112
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006113Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006114 if (isMachineConstantPoolEntry())
6115 return Val.MachineCPVal->getType();
6116 return Val.ConstVal->getType();
6117}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006118
Bob Wilson24e338e2009-03-02 23:24:16 +00006119bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6120 APInt &SplatUndef,
6121 unsigned &SplatBitSize,
6122 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006123 unsigned MinSplatBits,
6124 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006125 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006126 assert(VT.isVector() && "Expected a vector type");
6127 unsigned sz = VT.getSizeInBits();
6128 if (MinSplatBits > sz)
6129 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006130
Bob Wilson24e338e2009-03-02 23:24:16 +00006131 SplatValue = APInt(sz, 0);
6132 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006133
Bob Wilson24e338e2009-03-02 23:24:16 +00006134 // Get the bits. Bits with undefined values (when the corresponding element
6135 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6136 // in SplatValue. If any of the values are not constant, give up and return
6137 // false.
6138 unsigned int nOps = getNumOperands();
6139 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6140 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006141
6142 for (unsigned j = 0; j < nOps; ++j) {
6143 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006144 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006145 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006146
Bob Wilson24e338e2009-03-02 23:24:16 +00006147 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006148 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006149 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006150 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006151 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006152 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006153 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006154 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006155 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006156 }
6157
Bob Wilson24e338e2009-03-02 23:24:16 +00006158 // The build_vector is all constants or undefs. Find the smallest element
6159 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006160
Bob Wilson24e338e2009-03-02 23:24:16 +00006161 HasAnyUndefs = (SplatUndef != 0);
6162 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006163
Bob Wilson24e338e2009-03-02 23:24:16 +00006164 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006165 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6166 APInt LowValue = SplatValue.trunc(HalfSize);
6167 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6168 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006169
Bob Wilson24e338e2009-03-02 23:24:16 +00006170 // If the two halves do not match (ignoring undef bits), stop here.
6171 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6172 MinSplatBits > HalfSize)
6173 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006174
Bob Wilson24e338e2009-03-02 23:24:16 +00006175 SplatValue = HighValue | LowValue;
6176 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006177
Bob Wilson24e338e2009-03-02 23:24:16 +00006178 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006179 }
6180
Bob Wilson24e338e2009-03-02 23:24:16 +00006181 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006182 return true;
6183}
Nate Begeman9008ca62009-04-27 18:41:29 +00006184
Owen Andersone50ed302009-08-10 22:56:29 +00006185bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006186 // Find the first non-undef value in the shuffle mask.
6187 unsigned i, e;
6188 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6189 /* search */;
6190
Nate Begemana6415752009-04-29 18:13:31 +00006191 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006192
Nate Begeman5a5ca152009-04-29 05:20:52 +00006193 // Make sure all remaining elements are either undef or the same as the first
6194 // non-undef value.
6195 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006196 if (Mask[i] >= 0 && Mask[i] != Idx)
6197 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006198 return true;
6199}
David Greenecf495bc2010-01-20 20:13:31 +00006200
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006201#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006202static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006203 SmallPtrSet<const SDNode*, 32> &Visited,
6204 SmallPtrSet<const SDNode*, 32> &Checked) {
6205 // If this node has already been checked, don't check it again.
6206 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006207 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006208
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006209 // If a node has already been visited on this depth-first walk, reject it as
6210 // a cycle.
6211 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006212 dbgs() << "Offending node:\n";
6213 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006214 errs() << "Detected cycle in SelectionDAG\n";
6215 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006216 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006217
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006218 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6219 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006220
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006221 Checked.insert(N);
6222 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006223}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006224#endif
David Greenecf495bc2010-01-20 20:13:31 +00006225
6226void llvm::checkForCycles(const llvm::SDNode *N) {
6227#ifdef XDEBUG
6228 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006229 SmallPtrSet<const SDNode*, 32> visited;
6230 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006231 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006232#endif
6233}
6234
6235void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6236 checkForCycles(DAG->getRoot().getNode());
6237}